Port more legacy stubs to the new ask stubber

This commit is contained in:
Mislav Marohnić 2022-01-14 19:34:15 +01:00
parent a33b5a55c4
commit e43cb2b880
14 changed files with 80 additions and 147 deletions

View file

@ -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)
}

View file

@ -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)
}

View file

@ -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)
}

View file

@ -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)

View file

@ -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)
}

View file

@ -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{}
}

View file

@ -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 != "" {

View file

@ -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)

View file

@ -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 {

View file

@ -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)

View file

@ -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,

View file

@ -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)

View file

@ -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)

View file

@ -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 {