X Tutup
Skip to content

Commit e43cb2b

Browse files
committed
Port more legacy stubs to the new ask stubber
1 parent a33b5a5 commit e43cb2b

File tree

14 files changed

+81
-148
lines changed

14 files changed

+81
-148
lines changed

pkg/cmd/auth/login/login_test.go

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -548,8 +548,7 @@ func Test_loginRun_Survey(t *testing.T) {
548548
hostsBuf := bytes.Buffer{}
549549
defer config.StubWriteConfig(&mainBuf, &hostsBuf)()
550550

551-
as, teardown := prompt.InitAskStubber()
552-
defer teardown()
551+
as := prompt.NewAskStubber(t)
553552
if tt.askStubs != nil {
554553
tt.askStubs(as)
555554
}

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/issue/create/create_test.go

Lines changed: 11 additions & 54 deletions
Original file line numberDiff line numberDiff line change
@@ -401,27 +401,11 @@ func TestIssueCreate_recover(t *testing.T) {
401401
assert.Equal(t, []interface{}{"BUGID", "TODOID"}, inputs["labelIds"])
402402
}))
403403

404-
as, teardown := prompt.InitAskStubber()
405-
defer teardown()
404+
as := prompt.NewAskStubber(t)
406405

407-
as.Stub([]*prompt.QuestionStub{
408-
{
409-
Name: "Title",
410-
Default: true,
411-
},
412-
})
413-
as.Stub([]*prompt.QuestionStub{
414-
{
415-
Name: "Body",
416-
Default: true,
417-
},
418-
})
419-
as.Stub([]*prompt.QuestionStub{
420-
{
421-
Name: "confirmation",
422-
Value: 0,
423-
},
424-
})
406+
as.StubPrompt("Title").AnswerDefault()
407+
as.StubPrompt("Body").AnswerDefault()
408+
as.StubPrompt("What's next?").AnswerWith("Submit")
425409

426410
tmpfile, err := ioutil.TempFile(t.TempDir(), "testrecover*")
427411
assert.NoError(t, err)
@@ -484,25 +468,11 @@ func TestIssueCreate_nonLegacyTemplate(t *testing.T) {
484468
}),
485469
)
486470

487-
as, teardown := prompt.InitAskStubber()
488-
defer teardown()
471+
as := prompt.NewAskStubber(t)
489472

490-
// template
491-
as.StubOne(1)
492-
// body
493-
as.Stub([]*prompt.QuestionStub{
494-
{
495-
Name: "Body",
496-
Default: true,
497-
},
498-
}) // body
499-
// confirm
500-
as.Stub([]*prompt.QuestionStub{
501-
{
502-
Name: "confirmation",
503-
Value: 0,
504-
},
505-
})
473+
as.StubPrompt("Choose a template").AnswerWith("Submit a request")
474+
as.StubPrompt("Body").AnswerDefault()
475+
as.StubPrompt("What's next?").AnswerWith("Submit")
506476

507477
output, err := runCommandWithRootDirOverridden(http, true, `-t hello`, "./fixtures/repoWithNonLegacyIssueTemplates")
508478
if err != nil {
@@ -526,23 +496,10 @@ func TestIssueCreate_continueInBrowser(t *testing.T) {
526496
} } }`),
527497
)
528498

529-
as, teardown := prompt.InitAskStubber()
530-
defer teardown()
499+
as := prompt.NewAskStubber(t)
531500

532-
// title
533-
as.Stub([]*prompt.QuestionStub{
534-
{
535-
Name: "Title",
536-
Value: "hello",
537-
},
538-
})
539-
// confirm
540-
as.Stub([]*prompt.QuestionStub{
541-
{
542-
Name: "confirmation",
543-
Value: 1,
544-
},
545-
})
501+
as.StubPrompt("Title").AnswerWith("hello")
502+
as.StubPrompt("What's next?").AnswerWith("Continue in browser")
546503

547504
_, cmdTeardown := run.Stub()
548505
defer cmdTeardown(t)

pkg/cmd/issue/delete/delete_test.go

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -75,9 +75,9 @@ func TestIssueDelete(t *testing.T) {
7575
assert.Equal(t, inputs["issueId"], "THE-ID")
7676
}),
7777
)
78-
as, teardown := prompt.InitAskStubber()
79-
defer teardown()
80-
as.StubOne("13")
78+
79+
as := prompt.NewAskStubber(t)
80+
as.StubPrompt("You're going to delete issue #13. This action cannot be reversed. To confirm, type the issue number:").AnswerWith("13")
8181

8282
output, err := runCommand(httpRegistry, true, "13")
8383
if err != nil {
@@ -103,9 +103,9 @@ func TestIssueDelete_cancel(t *testing.T) {
103103
"issue": { "id": "THE-ID", "number": 13, "title": "The title of the issue"}
104104
} } }`),
105105
)
106-
as, teardown := prompt.InitAskStubber()
107-
defer teardown()
108-
as.StubOne("14")
106+
107+
as := prompt.NewAskStubber(t)
108+
as.StubPrompt("You're going to delete issue #13. This action cannot be reversed. To confirm, type the issue number:").AnswerWith("14")
109109

110110
output, err := runCommand(httpRegistry, true, "13")
111111
if err != nil {

pkg/cmd/run/watch/watch_test.go

Lines changed: 17 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -234,7 +234,9 @@ func TestWatchRun(t *testing.T) {
234234
},
235235
httpStubs: successfulRunStubs,
236236
askStubs: func(as *prompt.AskStubber) {
237-
as.StubOne(1)
237+
as.StubPrompt("Select a workflow run").
238+
AssertOptions([]string{"* cool commit, run (trunk) Feb 23, 2021", "* cool commit, more runs (trunk) Feb 23, 2021"}).
239+
AnswerWith("* cool commit, more runs (trunk) Feb 23, 2021")
238240
},
239241
wantOut: "\x1b[2J\x1b[0;0H\x1b[JRefreshing run status every 0 seconds. Press Ctrl+C to quit.\n\n* trunk more runs · 2\nTriggered via push about 59 minutes ago\n\nJOBS\n✓ cool job in 4m34s (ID 10)\n ✓ fob the barz\n ✓ barz the fob\n\x1b[0;0H\x1b[JRefreshing run status every 0 seconds. Press Ctrl+C to quit.\n\n✓ trunk more runs · 2\nTriggered via push about 59 minutes ago\n\nJOBS\n✓ cool job in 4m34s (ID 10)\n ✓ fob the barz\n ✓ barz the fob\n\n✓ Run more runs (2) completed with 'success'\n",
240242
},
@@ -247,7 +249,9 @@ func TestWatchRun(t *testing.T) {
247249
},
248250
httpStubs: successfulRunStubs,
249251
askStubs: func(as *prompt.AskStubber) {
250-
as.StubOne(1)
252+
as.StubPrompt("Select a workflow run").
253+
AssertOptions([]string{"* cool commit, run (trunk) Feb 23, 2021", "* cool commit, more runs (trunk) Feb 23, 2021"}).
254+
AnswerWith("* cool commit, more runs (trunk) Feb 23, 2021")
251255
},
252256
wantOut: "\x1b[2J\x1b[2JRefreshing run status every 0 seconds. Press Ctrl+C to quit.\n\n* trunk more runs · 2\nTriggered via push about 59 minutes ago\n\nJOBS\n✓ cool job in 4m34s (ID 10)\n ✓ fob the barz\n ✓ barz the fob\n\x1b[2JRefreshing run status every 0 seconds. Press Ctrl+C to quit.\n\n✓ trunk more runs · 2\nTriggered via push about 59 minutes ago\n\nJOBS\n✓ cool job in 4m34s (ID 10)\n ✓ fob the barz\n ✓ barz the fob\n",
253257
},
@@ -262,7 +266,9 @@ func TestWatchRun(t *testing.T) {
262266
},
263267
httpStubs: failedRunStubs,
264268
askStubs: func(as *prompt.AskStubber) {
265-
as.StubOne(1)
269+
as.StubPrompt("Select a workflow run").
270+
AssertOptions([]string{"* cool commit, run (trunk) Feb 23, 2021", "* cool commit, more runs (trunk) Feb 23, 2021"}).
271+
AnswerWith("* cool commit, more runs (trunk) Feb 23, 2021")
266272
},
267273
wantOut: "\x1b[2J\x1b[0;0H\x1b[JRefreshing run status every 0 seconds. Press Ctrl+C to quit.\n\n* trunk more runs · 2\nTriggered via push about 59 minutes ago\n\n\x1b[0;0H\x1b[JRefreshing run status every 0 seconds. Press Ctrl+C to quit.\n\nX trunk more runs · 2\nTriggered via push about 59 minutes ago\n\nJOBS\nX sad job in 4m34s (ID 20)\n ✓ barf the quux\n X quux the barf\n\nANNOTATIONS\nX the job is sad\nsad job: blaze.py#420\n\n\nX Run more runs (2) completed with 'failure'\n",
268274
wantErr: true,
@@ -278,7 +284,9 @@ func TestWatchRun(t *testing.T) {
278284
},
279285
httpStubs: failedRunStubs,
280286
askStubs: func(as *prompt.AskStubber) {
281-
as.StubOne(1)
287+
as.StubPrompt("Select a workflow run").
288+
AssertOptions([]string{"* cool commit, run (trunk) Feb 23, 2021", "* cool commit, more runs (trunk) Feb 23, 2021"}).
289+
AnswerWith("* cool commit, more runs (trunk) Feb 23, 2021")
282290
},
283291
wantOut: "\x1b[2J\x1b[2JRefreshing run status every 0 seconds. Press Ctrl+C to quit.\n\n* trunk more runs · 2\nTriggered via push about 59 minutes ago\n\n\x1b[2JRefreshing run status every 0 seconds. Press Ctrl+C to quit.\n\nX trunk more runs · 2\nTriggered via push about 59 minutes ago\n\nJOBS\nX sad job in 4m34s (ID 20)\n ✓ barf the quux\n X quux the barf\n\nANNOTATIONS\nX the job is sad\nsad job: blaze.py#420\n\n",
284292
wantErr: true,
@@ -313,13 +321,12 @@ func TestWatchRun(t *testing.T) {
313321
return ghrepo.FromFullName("OWNER/REPO")
314322
}
315323

316-
as, teardown := prompt.InitAskStubber()
317-
defer teardown()
318-
if tt.askStubs != nil {
319-
tt.askStubs(as)
320-
}
321-
322324
t.Run(tt.name, func(t *testing.T) {
325+
as := prompt.NewAskStubber(t)
326+
if tt.askStubs != nil {
327+
tt.askStubs(as)
328+
}
329+
323330
err := watchRun(tt.opts)
324331
if tt.wantErr {
325332
assert.EqualError(t, err, tt.errMsg)

0 commit comments

Comments
 (0)
X Tutup