55 "net/http"
66 "os"
77 "regexp"
8+ "runtime"
89 "testing"
910
1011 "github.com/MakeNowJust/heredoc"
@@ -18,6 +19,21 @@ import (
1819 "github.com/stretchr/testify/assert"
1920)
2021
22+ func stubHomeDir (t * testing.T , dir string ) {
23+ homeEnv := "HOME"
24+ switch runtime .GOOS {
25+ case "windows" :
26+ homeEnv = "USERPROFILE"
27+ case "plan9" :
28+ homeEnv = "home"
29+ }
30+ oldHomeDir := os .Getenv (homeEnv )
31+ os .Setenv (homeEnv , dir )
32+ t .Cleanup (func () {
33+ os .Setenv (homeEnv , oldHomeDir )
34+ })
35+ }
36+
2137func Test_NewCmdLogin (t * testing.T ) {
2238 tests := []struct {
2339 name string
@@ -352,6 +368,8 @@ func Test_loginRun_nontty(t *testing.T) {
352368}
353369
354370func Test_loginRun_Survey (t * testing.T ) {
371+ stubHomeDir (t , t .TempDir ())
372+
355373 tests := []struct {
356374 name string
357375 opts * LoginOptions
@@ -377,8 +395,8 @@ func Test_loginRun_Survey(t *testing.T) {
377395 // httpmock.StringResponse(`{"data":{"viewer":{"login":"jillv"}}}`))
378396 },
379397 askStubs : func (as * prompt.AskStubber ) {
380- as .StubOne ( 0 ) // host type github. com
381- as .StubOne ( false ) // do not continue
398+ as .StubPrompt ( "What account do you want to log into?" ). AnswerWith ( "GitHub. com" )
399+ as .StubPrompt ( "You're already logged into github.com. Do you want to re-authenticate?" ). AnswerWith ( false )
382400 },
383401 wantHosts : "" , // nothing should have been written to hosts
384402 wantErrOut : nil ,
@@ -396,10 +414,10 @@ func Test_loginRun_Survey(t *testing.T) {
396414 git_protocol: https
397415 ` ),
398416 askStubs : func (as * prompt.AskStubber ) {
399- as .StubOne ( "HTTPS" ) // git_protocol
400- as .StubOne ( false ) // cache credentials
401- as .StubOne ( 1 ) // auth mode: token
402- as .StubOne ( "def456" ) // auth token
417+ as .StubPrompt ( "What is your preferred protocol for Git operations?" ). AnswerWith ( "HTTPS" )
418+ as .StubPrompt ( "Authenticate Git with your GitHub credentials?" ). AnswerWith ( false )
419+ as .StubPrompt ( "How would you like to authenticate GitHub CLI?" ). AnswerWith ( "Paste an authentication token" )
420+ as .StubPrompt ( "Paste your authentication token:" ). AnswerWith ( "def456" )
403421 },
404422 runStubs : func (rs * run.CommandStubber ) {
405423 rs .Register (`git config credential\.https:/` , 1 , "" )
@@ -425,12 +443,12 @@ func Test_loginRun_Survey(t *testing.T) {
425443 Interactive : true ,
426444 },
427445 askStubs : func (as * prompt.AskStubber ) {
428- as .StubOne ( 1 ) // host type enterprise
429- as .StubOne ( " brad.vickers" ) // hostname
430- as .StubOne ( "HTTPS" ) // git_protocol
431- as .StubOne ( false ) // cache credentials
432- as .StubOne ( 1 ) // auth mode: token
433- as .StubOne ( "def456" ) // auth token
446+ as .StubPrompt ( "What account do you want to log into?" ). AnswerWith ( "GitHub Enterprise Server" )
447+ as .StubPrompt ( "GHE hostname:" ). AnswerWith ( " brad.vickers" )
448+ as .StubPrompt ( "What is your preferred protocol for Git operations?" ). AnswerWith ( "HTTPS" )
449+ as .StubPrompt ( "Authenticate Git with your GitHub credentials?" ). AnswerWith ( false )
450+ as .StubPrompt ( "How would you like to authenticate GitHub CLI?" ). AnswerWith ( "Paste an authentication token" )
451+ as .StubPrompt ( "Paste your authentication token:" ). AnswerWith ( "def456" )
434452 },
435453 runStubs : func (rs * run.CommandStubber ) {
436454 rs .Register (`git config credential\.https:/` , 1 , "" )
@@ -456,11 +474,11 @@ func Test_loginRun_Survey(t *testing.T) {
456474 Interactive : true ,
457475 },
458476 askStubs : func (as * prompt.AskStubber ) {
459- as .StubOne ( 0 ) // host type github. com
460- as .StubOne ( "HTTPS" ) // git_protocol
461- as .StubOne ( false ) // cache credentials
462- as .StubOne ( 1 ) // auth mode: token
463- as .StubOne ( "def456" ) // auth token
477+ as .StubPrompt ( "What account do you want to log into?" ). AnswerWith ( "GitHub. com" )
478+ as .StubPrompt ( "What is your preferred protocol for Git operations?" ). AnswerWith ( "HTTPS" )
479+ as .StubPrompt ( "Authenticate Git with your GitHub credentials?" ). AnswerWith ( false )
480+ as .StubPrompt ( "How would you like to authenticate GitHub CLI?" ). AnswerWith ( "Paste an authentication token" )
481+ as .StubPrompt ( "Paste your authentication token:" ). AnswerWith ( "def456" )
464482 },
465483 runStubs : func (rs * run.CommandStubber ) {
466484 rs .Register (`git config credential\.https:/` , 1 , "" )
@@ -480,11 +498,11 @@ func Test_loginRun_Survey(t *testing.T) {
480498 Interactive : true ,
481499 },
482500 askStubs : func (as * prompt.AskStubber ) {
483- as .StubOne ( 0 ) // host type github. com
484- as .StubOne ( "SSH" ) // git_protocol
485- as .StubOne ( 10 ) // TODO: SSH key selection
486- as .StubOne ( 1 ) // auth mode: token
487- as .StubOne ( "def456" ) // auth token
501+ as .StubPrompt ( "What account do you want to log into?" ). AnswerWith ( "GitHub. com" )
502+ as .StubPrompt ( "What is your preferred protocol for Git operations?" ). AnswerWith ( "SSH" )
503+ as .StubPrompt ( "Generate a new SSH key to add to your GitHub account?" ). AnswerWith ( false )
504+ as .StubPrompt ( "How would you like to authenticate GitHub CLI?" ). AnswerWith ( "Paste an authentication token" )
505+ as .StubPrompt ( "Paste your authentication token:" ). AnswerWith ( "def456" )
488506 },
489507 wantErrOut : regexp .MustCompile ("Tip: you can generate a Personal Access Token here https://github.com/settings/tokens" ),
490508 },
@@ -530,8 +548,7 @@ func Test_loginRun_Survey(t *testing.T) {
530548 hostsBuf := bytes.Buffer {}
531549 defer config .StubWriteConfig (& mainBuf , & hostsBuf )()
532550
533- as , teardown := prompt .InitAskStubber ()
534- defer teardown ()
551+ as := prompt .NewAskStubber (t )
535552 if tt .askStubs != nil {
536553 tt .askStubs (as )
537554 }
0 commit comments