Skip to content

Commit a3f0940

Browse files
authored
Merge pull request cli#5032 from cli/ask-stubber
Improve Survey stubber for tests
2 parents 583af3e + 44775f8 commit a3f0940

File tree

31 files changed

+486
-393
lines changed

31 files changed

+486
-393
lines changed

pkg/cmd/auth/login/login_test.go

Lines changed: 41 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@ import (
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+
2137
func Test_NewCmdLogin(t *testing.T) {
2238
tests := []struct {
2339
name string
@@ -352,6 +368,8 @@ func Test_loginRun_nontty(t *testing.T) {
352368
}
353369

354370
func 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
}

pkg/cmd/auth/logout/logout_test.go

Lines changed: 5 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -106,8 +106,8 @@ func Test_logoutRun_tty(t *testing.T) {
106106
cfgHosts: []string{"cheryl.mason", "github.com"},
107107
wantHosts: "cheryl.mason:\n oauth_token: abc123\n",
108108
askStubs: func(as *prompt.AskStubber) {
109-
as.StubOne("github.com")
110-
as.StubOne(true)
109+
as.StubPrompt("What account do you want to log out of?").AnswerWith("github.com")
110+
as.StubPrompt("Are you sure you want to log out of github.com account 'cybilb'?").AnswerWith(true)
111111
},
112112
wantErrOut: regexp.MustCompile(`Logged out of github.com account 'cybilb'`),
113113
},
@@ -116,7 +116,7 @@ func Test_logoutRun_tty(t *testing.T) {
116116
opts: &LogoutOptions{},
117117
cfgHosts: []string{"github.com"},
118118
askStubs: func(as *prompt.AskStubber) {
119-
as.StubOne(true)
119+
as.StubPrompt("Are you sure you want to log out of github.com account 'cybilb'?").AnswerWith(true)
120120
},
121121
wantErrOut: regexp.MustCompile(`Logged out of github.com account 'cybilb'`),
122122
},
@@ -133,7 +133,7 @@ func Test_logoutRun_tty(t *testing.T) {
133133
cfgHosts: []string{"cheryl.mason", "github.com"},
134134
wantHosts: "github.com:\n oauth_token: abc123\n",
135135
askStubs: func(as *prompt.AskStubber) {
136-
as.StubOne(true)
136+
as.StubPrompt("Are you sure you want to log out of cheryl.mason account 'cybilb'?").AnswerWith(true)
137137
},
138138
wantErrOut: regexp.MustCompile(`Logged out of cheryl.mason account 'cybilb'`),
139139
},
@@ -169,8 +169,7 @@ func Test_logoutRun_tty(t *testing.T) {
169169
hostsBuf := bytes.Buffer{}
170170
defer config.StubWriteConfig(&mainBuf, &hostsBuf)()
171171

172-
as, teardown := prompt.InitAskStubber()
173-
defer teardown()
172+
as := prompt.NewAskStubber(t)
174173
if tt.askStubs != nil {
175174
tt.askStubs(as)
176175
}

pkg/cmd/auth/refresh/refresh_test.go

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -194,7 +194,7 @@ func Test_refreshRun(t *testing.T) {
194194
Hostname: "",
195195
},
196196
askStubs: func(as *prompt.AskStubber) {
197-
as.StubOne("github.com")
197+
as.StubPrompt("What account do you want to refresh auth for?").AnswerWith("github.com")
198198
},
199199
wantAuthArgs: authArgs{
200200
hostname: "github.com",
@@ -276,8 +276,7 @@ func Test_refreshRun(t *testing.T) {
276276
hostsBuf := bytes.Buffer{}
277277
defer config.StubWriteConfig(&mainBuf, &hostsBuf)()
278278

279-
as, teardown := prompt.InitAskStubber()
280-
defer teardown()
279+
as := prompt.NewAskStubber(t)
281280
if tt.askStubs != nil {
282281
tt.askStubs(as)
283282
}

pkg/cmd/auth/shared/login_flow_test.go

Lines changed: 7 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -47,14 +47,13 @@ func TestLogin_ssh(t *testing.T) {
4747
httpmock.REST("POST", "api/v3/user/keys"),
4848
httpmock.StringResponse(`{}`))
4949

50-
ask, askRestore := prompt.InitAskStubber()
51-
defer askRestore()
52-
53-
ask.StubOne("SSH") // preferred protocol
54-
ask.StubOne(true) // generate a new key
55-
ask.StubOne("monkey") // enter a passphrase
56-
ask.StubOne(1) // paste a token
57-
ask.StubOne("ATOKEN") // token
50+
ask := prompt.NewAskStubber(t)
51+
52+
ask.StubPrompt("What is your preferred protocol for Git operations?").AnswerWith("SSH")
53+
ask.StubPrompt("Generate a new SSH key to add to your GitHub account?").AnswerWith(true)
54+
ask.StubPrompt("Enter a passphrase for your new SSH key (Optional)").AnswerWith("monkey")
55+
ask.StubPrompt("How would you like to authenticate GitHub CLI?").AnswerWith("Paste an authentication token")
56+
ask.StubPrompt("Paste your authentication token:").AnswerWith("ATOKEN")
5857

5958
rs, runRestore := run.Stub()
6059
defer runRestore(t)

pkg/cmd/extension/command_test.go

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -316,8 +316,10 @@ func TestNewCmdExtension(t *testing.T) {
316316
},
317317
isTTY: true,
318318
askStubs: func(as *prompt.AskStubber) {
319-
as.StubOne("test")
320-
as.StubOne(0)
319+
as.StubPrompt("Extension name:").AnswerWith("test")
320+
as.StubPrompt("What kind of extension?").
321+
AssertOptions([]string{"Script (Bash, Ruby, Python, etc)", "Go", "Other Precompiled (C++, Rust, etc)"}).
322+
AnswerDefault()
321323
},
322324
wantStdout: heredoc.Doc(`
323325
✓ Created directory gh-test
@@ -456,8 +458,7 @@ func TestNewCmdExtension(t *testing.T) {
456458
assertFunc = tt.managerStubs(em)
457459
}
458460

459-
as, teardown := prompt.InitAskStubber()
460-
defer teardown()
461+
as := prompt.NewAskStubber(t)
461462
if tt.askStubs != nil {
462463
tt.askStubs(as)
463464
}

pkg/cmd/gist/delete/delete_test.go

Lines changed: 0 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,6 @@ import (
1010
"github.com/cli/cli/v2/pkg/cmdutil"
1111
"github.com/cli/cli/v2/pkg/httpmock"
1212
"github.com/cli/cli/v2/pkg/iostreams"
13-
"github.com/cli/cli/v2/pkg/prompt"
1413
"github.com/google/shlex"
1514
"github.com/stretchr/testify/assert"
1615
)
@@ -61,7 +60,6 @@ func Test_deleteRun(t *testing.T) {
6160
opts *DeleteOptions
6261
gist *shared.Gist
6362
httpStubs func(*httpmock.Registry)
64-
askStubs func(*prompt.AskStubber)
6563
nontty bool
6664
wantErr bool
6765
wantStderr string
@@ -122,12 +120,6 @@ func Test_deleteRun(t *testing.T) {
122120
tt.httpStubs(reg)
123121
}
124122

125-
as, teardown := prompt.InitAskStubber()
126-
defer teardown()
127-
if tt.askStubs != nil {
128-
tt.askStubs(as)
129-
}
130-
131123
if tt.opts == nil {
132124
tt.opts = &DeleteOptions{}
133125
}

pkg/cmd/gist/edit/edit_test.go

Lines changed: 9 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -146,8 +146,8 @@ func Test_editRun(t *testing.T) {
146146
{
147147
name: "multiple files, submit",
148148
askStubs: func(as *prompt.AskStubber) {
149-
as.StubOne("unix.md")
150-
as.StubOne("Submit")
149+
as.StubPrompt("Edit which file?").AnswerWith("unix.md")
150+
as.StubPrompt("What next?").AnswerWith("Submit")
151151
},
152152
gist: &shared.Gist{
153153
ID: "1234",
@@ -191,8 +191,8 @@ func Test_editRun(t *testing.T) {
191191
{
192192
name: "multiple files, cancel",
193193
askStubs: func(as *prompt.AskStubber) {
194-
as.StubOne("unix.md")
195-
as.StubOne("Cancel")
194+
as.StubPrompt("Edit which file?").AnswerWith("unix.md")
195+
as.StubPrompt("What next?").AnswerWith("Cancel")
196196
},
197197
wantErr: "CancelError",
198198
gist: &shared.Gist{
@@ -280,12 +280,6 @@ func Test_editRun(t *testing.T) {
280280
tt.httpStubs(reg)
281281
}
282282

283-
as, teardown := prompt.InitAskStubber()
284-
defer teardown()
285-
if tt.askStubs != nil {
286-
tt.askStubs(as)
287-
}
288-
289283
if tt.opts == nil {
290284
tt.opts = &EditOptions{}
291285
}
@@ -308,6 +302,11 @@ func Test_editRun(t *testing.T) {
308302
}
309303

310304
t.Run(tt.name, func(t *testing.T) {
305+
as := prompt.NewAskStubber(t)
306+
if tt.askStubs != nil {
307+
tt.askStubs(as)
308+
}
309+
311310
err := editRun(tt.opts)
312311
reg.Verify(t)
313312
if tt.wantErr != "" {

pkg/cmd/gist/view/view_test.go

Lines changed: 21 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -355,9 +355,8 @@ func Test_viewRun(t *testing.T) {
355355
)),
356356
)
357357

358-
as, surveyteardown := prompt.InitAskStubber()
359-
defer surveyteardown()
360-
as.StubOne(0)
358+
as := prompt.NewAskStubber(t)
359+
as.StubPrompt("Select a gist").AnswerDefault()
361360
}
362361

363362
if tt.opts == nil {
@@ -392,16 +391,18 @@ func Test_viewRun(t *testing.T) {
392391

393392
func Test_promptGists(t *testing.T) {
394393
tests := []struct {
395-
name string
396-
gistIndex int
397-
response string
398-
wantOut string
399-
gist *shared.Gist
400-
wantErr bool
394+
name string
395+
askStubs func(as *prompt.AskStubber)
396+
response string
397+
wantOut string
398+
gist *shared.Gist
399+
wantErr bool
401400
}{
402401
{
403-
name: "multiple files, select first gist",
404-
gistIndex: 0,
402+
name: "multiple files, select first gist",
403+
askStubs: func(as *prompt.AskStubber) {
404+
as.StubPrompt("Select a gist").AnswerWith("cool.txt about 6 hours ago")
405+
},
405406
response: `{ "data": { "viewer": { "gists": { "nodes": [
406407
{
407408
"name": "gistid1",
@@ -421,8 +422,10 @@ func Test_promptGists(t *testing.T) {
421422
wantOut: "gistid1",
422423
},
423424
{
424-
name: "multiple files, select second gist",
425-
gistIndex: 1,
425+
name: "multiple files, select second gist",
426+
askStubs: func(as *prompt.AskStubber) {
427+
as.StubPrompt("Select a gist").AnswerWith("gistfile0.txt about 6 hours ago")
428+
},
426429
response: `{ "data": { "viewer": { "gists": { "nodes": [
427430
{
428431
"name": "gistid1",
@@ -465,11 +468,12 @@ func Test_promptGists(t *testing.T) {
465468
)
466469
client := &http.Client{Transport: reg}
467470

468-
as, surveyteardown := prompt.InitAskStubber()
469-
defer surveyteardown()
470-
as.StubOne(tt.gistIndex)
471-
472471
t.Run(tt.name, func(t *testing.T) {
472+
as := prompt.NewAskStubber(t)
473+
if tt.askStubs != nil {
474+
tt.askStubs(as)
475+
}
476+
473477
gistID, err := promptGists(client, "github.com", io.ColorScheme())
474478
assert.NoError(t, err)
475479
assert.Equal(t, tt.wantOut, gistID)

0 commit comments

Comments
 (0)