From e43cb2b880df8c8d0419ce2dd232e04efcab5dbc Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Mislav=20Marohni=C4=87?= Date: Fri, 14 Jan 2022 19:34:15 +0100 Subject: [PATCH] Port more legacy stubs to the new ask stubber --- pkg/cmd/auth/login/login_test.go | 3 +- pkg/cmd/auth/logout/logout_test.go | 11 ++-- pkg/cmd/auth/refresh/refresh_test.go | 5 +- pkg/cmd/auth/shared/login_flow_test.go | 13 +++-- pkg/cmd/extension/command_test.go | 9 ++-- pkg/cmd/gist/delete/delete_test.go | 8 --- pkg/cmd/gist/edit/edit_test.go | 19 ++++--- pkg/cmd/issue/create/create_test.go | 65 ++++-------------------- pkg/cmd/issue/delete/delete_test.go | 12 ++--- pkg/cmd/run/watch/watch_test.go | 27 ++++++---- pkg/cmd/secret/set/set_test.go | 6 +-- pkg/cmd/workflow/disable/disable_test.go | 15 +++--- pkg/cmd/workflow/run/run_test.go | 26 ++++------ pkg/cmd/workflow/view/view_test.go | 8 --- 14 files changed, 80 insertions(+), 147 deletions(-) diff --git a/pkg/cmd/auth/login/login_test.go b/pkg/cmd/auth/login/login_test.go index dc4bab91e..b7c8438cb 100644 --- a/pkg/cmd/auth/login/login_test.go +++ b/pkg/cmd/auth/login/login_test.go @@ -548,8 +548,7 @@ func Test_loginRun_Survey(t *testing.T) { hostsBuf := bytes.Buffer{} defer config.StubWriteConfig(&mainBuf, &hostsBuf)() - as, teardown := prompt.InitAskStubber() - defer teardown() + as := prompt.NewAskStubber(t) if tt.askStubs != nil { tt.askStubs(as) } diff --git a/pkg/cmd/auth/logout/logout_test.go b/pkg/cmd/auth/logout/logout_test.go index a5d46ef09..4d74caaf5 100644 --- a/pkg/cmd/auth/logout/logout_test.go +++ b/pkg/cmd/auth/logout/logout_test.go @@ -106,8 +106,8 @@ func Test_logoutRun_tty(t *testing.T) { cfgHosts: []string{"cheryl.mason", "github.com"}, wantHosts: "cheryl.mason:\n oauth_token: abc123\n", askStubs: func(as *prompt.AskStubber) { - as.StubOne("github.com") - as.StubOne(true) + as.StubPrompt("What account do you want to log out of?").AnswerWith("github.com") + as.StubPrompt("Are you sure you want to log out of github.com account 'cybilb'?").AnswerWith(true) }, wantErrOut: regexp.MustCompile(`Logged out of github.com account 'cybilb'`), }, @@ -116,7 +116,7 @@ func Test_logoutRun_tty(t *testing.T) { opts: &LogoutOptions{}, cfgHosts: []string{"github.com"}, askStubs: func(as *prompt.AskStubber) { - as.StubOne(true) + as.StubPrompt("Are you sure you want to log out of github.com account 'cybilb'?").AnswerWith(true) }, wantErrOut: regexp.MustCompile(`Logged out of github.com account 'cybilb'`), }, @@ -133,7 +133,7 @@ func Test_logoutRun_tty(t *testing.T) { cfgHosts: []string{"cheryl.mason", "github.com"}, wantHosts: "github.com:\n oauth_token: abc123\n", askStubs: func(as *prompt.AskStubber) { - as.StubOne(true) + as.StubPrompt("Are you sure you want to log out of cheryl.mason account 'cybilb'?").AnswerWith(true) }, wantErrOut: regexp.MustCompile(`Logged out of cheryl.mason account 'cybilb'`), }, @@ -169,8 +169,7 @@ func Test_logoutRun_tty(t *testing.T) { hostsBuf := bytes.Buffer{} defer config.StubWriteConfig(&mainBuf, &hostsBuf)() - as, teardown := prompt.InitAskStubber() - defer teardown() + as := prompt.NewAskStubber(t) if tt.askStubs != nil { tt.askStubs(as) } diff --git a/pkg/cmd/auth/refresh/refresh_test.go b/pkg/cmd/auth/refresh/refresh_test.go index 800913b1a..1bee8435d 100644 --- a/pkg/cmd/auth/refresh/refresh_test.go +++ b/pkg/cmd/auth/refresh/refresh_test.go @@ -194,7 +194,7 @@ func Test_refreshRun(t *testing.T) { Hostname: "", }, askStubs: func(as *prompt.AskStubber) { - as.StubOne("github.com") + as.StubPrompt("What account do you want to refresh auth for?").AnswerWith("github.com") }, wantAuthArgs: authArgs{ hostname: "github.com", @@ -276,8 +276,7 @@ func Test_refreshRun(t *testing.T) { hostsBuf := bytes.Buffer{} defer config.StubWriteConfig(&mainBuf, &hostsBuf)() - as, teardown := prompt.InitAskStubber() - defer teardown() + as := prompt.NewAskStubber(t) if tt.askStubs != nil { tt.askStubs(as) } diff --git a/pkg/cmd/auth/shared/login_flow_test.go b/pkg/cmd/auth/shared/login_flow_test.go index 530e34045..6f1b35ebe 100644 --- a/pkg/cmd/auth/shared/login_flow_test.go +++ b/pkg/cmd/auth/shared/login_flow_test.go @@ -47,14 +47,13 @@ func TestLogin_ssh(t *testing.T) { httpmock.REST("POST", "api/v3/user/keys"), httpmock.StringResponse(`{}`)) - ask, askRestore := prompt.InitAskStubber() - defer askRestore() + ask := prompt.NewAskStubber(t) - ask.StubOne("SSH") // preferred protocol - ask.StubOne(true) // generate a new key - ask.StubOne("monkey") // enter a passphrase - ask.StubOne(1) // paste a token - ask.StubOne("ATOKEN") // token + ask.StubPrompt("What is your preferred protocol for Git operations?").AnswerWith("SSH") + ask.StubPrompt("Generate a new SSH key to add to your GitHub account?").AnswerWith(true) + ask.StubPrompt("Enter a passphrase for your new SSH key (Optional)").AnswerWith("monkey") + ask.StubPrompt("How would you like to authenticate GitHub CLI?").AnswerWith("Paste an authentication token") + ask.StubPrompt("Paste your authentication token:").AnswerWith("ATOKEN") rs, runRestore := run.Stub() defer runRestore(t) diff --git a/pkg/cmd/extension/command_test.go b/pkg/cmd/extension/command_test.go index 5a46592f9..8f896eab0 100644 --- a/pkg/cmd/extension/command_test.go +++ b/pkg/cmd/extension/command_test.go @@ -316,8 +316,10 @@ func TestNewCmdExtension(t *testing.T) { }, isTTY: true, askStubs: func(as *prompt.AskStubber) { - as.StubOne("test") - as.StubOne(0) + as.StubPrompt("Extension name:").AnswerWith("test") + as.StubPrompt("What kind of extension?"). + AssertOptions([]string{"Script (Bash, Ruby, Python, etc)", "Go", "Other Precompiled (C++, Rust, etc)"}). + AnswerDefault() }, wantStdout: heredoc.Doc(` ✓ Created directory gh-test @@ -456,8 +458,7 @@ func TestNewCmdExtension(t *testing.T) { assertFunc = tt.managerStubs(em) } - as, teardown := prompt.InitAskStubber() - defer teardown() + as := prompt.NewAskStubber(t) if tt.askStubs != nil { tt.askStubs(as) } diff --git a/pkg/cmd/gist/delete/delete_test.go b/pkg/cmd/gist/delete/delete_test.go index 0d98a44c2..9d6574911 100644 --- a/pkg/cmd/gist/delete/delete_test.go +++ b/pkg/cmd/gist/delete/delete_test.go @@ -10,7 +10,6 @@ import ( "github.com/cli/cli/v2/pkg/cmdutil" "github.com/cli/cli/v2/pkg/httpmock" "github.com/cli/cli/v2/pkg/iostreams" - "github.com/cli/cli/v2/pkg/prompt" "github.com/google/shlex" "github.com/stretchr/testify/assert" ) @@ -61,7 +60,6 @@ func Test_deleteRun(t *testing.T) { opts *DeleteOptions gist *shared.Gist httpStubs func(*httpmock.Registry) - askStubs func(*prompt.AskStubber) nontty bool wantErr bool wantStderr string @@ -122,12 +120,6 @@ func Test_deleteRun(t *testing.T) { tt.httpStubs(reg) } - as, teardown := prompt.InitAskStubber() - defer teardown() - if tt.askStubs != nil { - tt.askStubs(as) - } - if tt.opts == nil { tt.opts = &DeleteOptions{} } diff --git a/pkg/cmd/gist/edit/edit_test.go b/pkg/cmd/gist/edit/edit_test.go index cac99b923..74c4f3502 100644 --- a/pkg/cmd/gist/edit/edit_test.go +++ b/pkg/cmd/gist/edit/edit_test.go @@ -146,8 +146,8 @@ func Test_editRun(t *testing.T) { { name: "multiple files, submit", askStubs: func(as *prompt.AskStubber) { - as.StubOne("unix.md") - as.StubOne("Submit") + as.StubPrompt("Edit which file?").AnswerWith("unix.md") + as.StubPrompt("What next?").AnswerWith("Submit") }, gist: &shared.Gist{ ID: "1234", @@ -191,8 +191,8 @@ func Test_editRun(t *testing.T) { { name: "multiple files, cancel", askStubs: func(as *prompt.AskStubber) { - as.StubOne("unix.md") - as.StubOne("Cancel") + as.StubPrompt("Edit which file?").AnswerWith("unix.md") + as.StubPrompt("What next?").AnswerWith("Cancel") }, wantErr: "CancelError", gist: &shared.Gist{ @@ -280,12 +280,6 @@ func Test_editRun(t *testing.T) { tt.httpStubs(reg) } - as, teardown := prompt.InitAskStubber() - defer teardown() - if tt.askStubs != nil { - tt.askStubs(as) - } - if tt.opts == nil { tt.opts = &EditOptions{} } @@ -308,6 +302,11 @@ func Test_editRun(t *testing.T) { } t.Run(tt.name, func(t *testing.T) { + as := prompt.NewAskStubber(t) + if tt.askStubs != nil { + tt.askStubs(as) + } + err := editRun(tt.opts) reg.Verify(t) if tt.wantErr != "" { diff --git a/pkg/cmd/issue/create/create_test.go b/pkg/cmd/issue/create/create_test.go index f8d948999..4eaea333a 100644 --- a/pkg/cmd/issue/create/create_test.go +++ b/pkg/cmd/issue/create/create_test.go @@ -401,27 +401,11 @@ func TestIssueCreate_recover(t *testing.T) { assert.Equal(t, []interface{}{"BUGID", "TODOID"}, inputs["labelIds"]) })) - as, teardown := prompt.InitAskStubber() - defer teardown() + as := prompt.NewAskStubber(t) - as.Stub([]*prompt.QuestionStub{ - { - Name: "Title", - Default: true, - }, - }) - as.Stub([]*prompt.QuestionStub{ - { - Name: "Body", - Default: true, - }, - }) - as.Stub([]*prompt.QuestionStub{ - { - Name: "confirmation", - Value: 0, - }, - }) + as.StubPrompt("Title").AnswerDefault() + as.StubPrompt("Body").AnswerDefault() + as.StubPrompt("What's next?").AnswerWith("Submit") tmpfile, err := ioutil.TempFile(t.TempDir(), "testrecover*") assert.NoError(t, err) @@ -484,25 +468,11 @@ func TestIssueCreate_nonLegacyTemplate(t *testing.T) { }), ) - as, teardown := prompt.InitAskStubber() - defer teardown() + as := prompt.NewAskStubber(t) - // template - as.StubOne(1) - // body - as.Stub([]*prompt.QuestionStub{ - { - Name: "Body", - Default: true, - }, - }) // body - // confirm - as.Stub([]*prompt.QuestionStub{ - { - Name: "confirmation", - Value: 0, - }, - }) + as.StubPrompt("Choose a template").AnswerWith("Submit a request") + as.StubPrompt("Body").AnswerDefault() + as.StubPrompt("What's next?").AnswerWith("Submit") output, err := runCommandWithRootDirOverridden(http, true, `-t hello`, "./fixtures/repoWithNonLegacyIssueTemplates") if err != nil { @@ -526,23 +496,10 @@ func TestIssueCreate_continueInBrowser(t *testing.T) { } } }`), ) - as, teardown := prompt.InitAskStubber() - defer teardown() + as := prompt.NewAskStubber(t) - // title - as.Stub([]*prompt.QuestionStub{ - { - Name: "Title", - Value: "hello", - }, - }) - // confirm - as.Stub([]*prompt.QuestionStub{ - { - Name: "confirmation", - Value: 1, - }, - }) + as.StubPrompt("Title").AnswerWith("hello") + as.StubPrompt("What's next?").AnswerWith("Continue in browser") _, cmdTeardown := run.Stub() defer cmdTeardown(t) diff --git a/pkg/cmd/issue/delete/delete_test.go b/pkg/cmd/issue/delete/delete_test.go index 3d5fd1761..d9cf089c3 100644 --- a/pkg/cmd/issue/delete/delete_test.go +++ b/pkg/cmd/issue/delete/delete_test.go @@ -75,9 +75,9 @@ func TestIssueDelete(t *testing.T) { assert.Equal(t, inputs["issueId"], "THE-ID") }), ) - as, teardown := prompt.InitAskStubber() - defer teardown() - as.StubOne("13") + + as := prompt.NewAskStubber(t) + as.StubPrompt("You're going to delete issue #13. This action cannot be reversed. To confirm, type the issue number:").AnswerWith("13") output, err := runCommand(httpRegistry, true, "13") if err != nil { @@ -103,9 +103,9 @@ func TestIssueDelete_cancel(t *testing.T) { "issue": { "id": "THE-ID", "number": 13, "title": "The title of the issue"} } } }`), ) - as, teardown := prompt.InitAskStubber() - defer teardown() - as.StubOne("14") + + as := prompt.NewAskStubber(t) + as.StubPrompt("You're going to delete issue #13. This action cannot be reversed. To confirm, type the issue number:").AnswerWith("14") output, err := runCommand(httpRegistry, true, "13") if err != nil { diff --git a/pkg/cmd/run/watch/watch_test.go b/pkg/cmd/run/watch/watch_test.go index a96f826f4..a21df2217 100644 --- a/pkg/cmd/run/watch/watch_test.go +++ b/pkg/cmd/run/watch/watch_test.go @@ -234,7 +234,9 @@ func TestWatchRun(t *testing.T) { }, httpStubs: successfulRunStubs, askStubs: func(as *prompt.AskStubber) { - as.StubOne(1) + as.StubPrompt("Select a workflow run"). + AssertOptions([]string{"* cool commit, run (trunk) Feb 23, 2021", "* cool commit, more runs (trunk) Feb 23, 2021"}). + AnswerWith("* cool commit, more runs (trunk) Feb 23, 2021") }, 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", }, @@ -247,7 +249,9 @@ func TestWatchRun(t *testing.T) { }, httpStubs: successfulRunStubs, askStubs: func(as *prompt.AskStubber) { - as.StubOne(1) + as.StubPrompt("Select a workflow run"). + AssertOptions([]string{"* cool commit, run (trunk) Feb 23, 2021", "* cool commit, more runs (trunk) Feb 23, 2021"}). + AnswerWith("* cool commit, more runs (trunk) Feb 23, 2021") }, 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", }, @@ -262,7 +266,9 @@ func TestWatchRun(t *testing.T) { }, httpStubs: failedRunStubs, askStubs: func(as *prompt.AskStubber) { - as.StubOne(1) + as.StubPrompt("Select a workflow run"). + AssertOptions([]string{"* cool commit, run (trunk) Feb 23, 2021", "* cool commit, more runs (trunk) Feb 23, 2021"}). + AnswerWith("* cool commit, more runs (trunk) Feb 23, 2021") }, 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", wantErr: true, @@ -278,7 +284,9 @@ func TestWatchRun(t *testing.T) { }, httpStubs: failedRunStubs, askStubs: func(as *prompt.AskStubber) { - as.StubOne(1) + as.StubPrompt("Select a workflow run"). + AssertOptions([]string{"* cool commit, run (trunk) Feb 23, 2021", "* cool commit, more runs (trunk) Feb 23, 2021"}). + AnswerWith("* cool commit, more runs (trunk) Feb 23, 2021") }, 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", wantErr: true, @@ -313,13 +321,12 @@ func TestWatchRun(t *testing.T) { return ghrepo.FromFullName("OWNER/REPO") } - as, teardown := prompt.InitAskStubber() - defer teardown() - if tt.askStubs != nil { - tt.askStubs(as) - } - t.Run(tt.name, func(t *testing.T) { + as := prompt.NewAskStubber(t) + if tt.askStubs != nil { + tt.askStubs(as) + } + err := watchRun(tt.opts) if tt.wantErr { assert.EqualError(t, err, tt.errMsg) diff --git a/pkg/cmd/secret/set/set_test.go b/pkg/cmd/secret/set/set_test.go index d65ed1cae..29a2c4758 100644 --- a/pkg/cmd/secret/set/set_test.go +++ b/pkg/cmd/secret/set/set_test.go @@ -487,10 +487,8 @@ func Test_getBodyPrompt(t *testing.T) { io.SetStdinTTY(true) io.SetStdoutTTY(true) - as, teardown := prompt.InitAskStubber() - defer teardown() - - as.StubOne("cool secret") + as := prompt.NewAskStubber(t) + as.StubPrompt("Paste your secret").AnswerWith("cool secret") body, err := getBody(&SetOptions{ IO: io, diff --git a/pkg/cmd/workflow/disable/disable_test.go b/pkg/cmd/workflow/disable/disable_test.go index 1057da6a9..a1adbdd45 100644 --- a/pkg/cmd/workflow/disable/disable_test.go +++ b/pkg/cmd/workflow/disable/disable_test.go @@ -121,7 +121,7 @@ func TestDisableRun(t *testing.T) { httpmock.StatusStringResponse(204, "{}")) }, askStubs: func(as *prompt.AskStubber) { - as.StubOne(1) + as.StubPrompt("Select a workflow").AnswerWith("another workflow (another.yml)") }, wantOut: "✓ Disabled another workflow\n", }, @@ -176,7 +176,7 @@ func TestDisableRun(t *testing.T) { httpmock.StatusStringResponse(204, "{}")) }, askStubs: func(as *prompt.AskStubber) { - as.StubOne(1) + as.StubPrompt("Which workflow do you mean?").AnswerWith("another workflow (yetanother.yml)") }, wantOut: "✓ Disabled another workflow\n", }, @@ -277,13 +277,12 @@ func TestDisableRun(t *testing.T) { return ghrepo.FromFullName("OWNER/REPO") } - as, teardown := prompt.InitAskStubber() - defer teardown() - if tt.askStubs != nil { - tt.askStubs(as) - } - t.Run(tt.name, func(t *testing.T) { + as := prompt.NewAskStubber(t) + if tt.askStubs != nil { + tt.askStubs(as) + } + err := runDisable(tt.opts) if tt.wantErr { assert.Error(t, err) diff --git a/pkg/cmd/workflow/run/run_test.go b/pkg/cmd/workflow/run/run_test.go index 196f1850d..1f42164ff 100644 --- a/pkg/cmd/workflow/run/run_test.go +++ b/pkg/cmd/workflow/run/run_test.go @@ -557,7 +557,7 @@ jobs: httpmock.StatusStringResponse(204, "cool")) }, askStubs: func(as *prompt.AskStubber) { - as.StubOne(0) + as.StubPrompt("Select a workflow").AnswerDefault() }, wantBody: map[string]interface{}{ "inputs": map[string]interface{}{}, @@ -594,17 +594,9 @@ jobs: httpmock.StatusStringResponse(204, "cool")) }, askStubs: func(as *prompt.AskStubber) { - as.StubOne(0) - as.Stub([]*prompt.QuestionStub{ - { - Name: "greeting", - Default: true, - }, - { - Name: "name", - Value: "scully", - }, - }) + as.StubPrompt("Select a workflow").AnswerDefault() + as.StubPrompt("greeting").AnswerWith("hi") + as.StubPrompt("name").AnswerWith("scully") }, wantBody: map[string]interface{}{ "inputs": map[string]interface{}{ @@ -638,12 +630,12 @@ jobs: }, "github.com"), nil } - as, teardown := prompt.InitAskStubber() - defer teardown() - if tt.askStubs != nil { - tt.askStubs(as) - } t.Run(tt.name, func(t *testing.T) { + as := prompt.NewAskStubber(t) + if tt.askStubs != nil { + tt.askStubs(as) + } + err := runRun(tt.opts) if tt.wantErr { assert.Error(t, err) diff --git a/pkg/cmd/workflow/view/view_test.go b/pkg/cmd/workflow/view/view_test.go index b035070bf..dd89cf83e 100644 --- a/pkg/cmd/workflow/view/view_test.go +++ b/pkg/cmd/workflow/view/view_test.go @@ -13,7 +13,6 @@ import ( "github.com/cli/cli/v2/pkg/cmdutil" "github.com/cli/cli/v2/pkg/httpmock" "github.com/cli/cli/v2/pkg/iostreams" - "github.com/cli/cli/v2/pkg/prompt" "github.com/google/shlex" "github.com/stretchr/testify/assert" ) @@ -189,7 +188,6 @@ func TestViewRun(t *testing.T) { name string opts *ViewOptions httpStubs func(*httpmock.Registry) - askStubs func(*prompt.AskStubber) tty bool wantOut string wantErrOut string @@ -417,12 +415,6 @@ func TestViewRun(t *testing.T) { browser := &cmdutil.TestBrowser{} tt.opts.Browser = browser - as, teardown := prompt.InitAskStubber() - defer teardown() - if tt.askStubs != nil { - tt.askStubs(as) - } - t.Run(tt.name, func(t *testing.T) { err := runView(tt.opts) if tt.wantErr {