issue #2329: return error when not running interactively

This commit is contained in:
nilvng 2024-12-01 21:13:54 +11:00
parent e4dced01f7
commit d92e529629
2 changed files with 24 additions and 3 deletions

View file

@ -111,6 +111,10 @@ func checkoutRun(opts *CheckoutOptions) error {
}
default:
if !opts.IO.CanPrompt() {
return cmdutil.FlagErrorf("must provide a pull request number (or URL or branch) when not running interactively")
}
httpClient, err := opts.HttpClient()
if err != nil {
return err

View file

@ -64,8 +64,10 @@ func stubPR(repo, prHead string) (ghrepo.Interface, *api.PullRequest) {
func Test_checkoutRun(t *testing.T) {
tests := []struct {
name string
opts *CheckoutOptions
name string
opts *CheckoutOptions
tty bool
httpStubs func(*httpmock.Registry)
runStubs func(*run.CommandStubber)
promptStubs func(*prompter.MockPrompter)
@ -191,7 +193,15 @@ func Test_checkoutRun(t *testing.T) {
},
},
{
name: "with no selected PR args, prompts for choice",
name: "with no selected PR args and not stdin tty, return error",
opts: &CheckoutOptions{
SelectorArg: "",
},
tty: false,
wantErr: true,
},
{
name: "with no selected PR args and stdin tty, prompts for choice",
opts: &CheckoutOptions{
SelectorArg: "",
Finder: func() shared.PRFinder {
@ -206,6 +216,7 @@ func Test_checkoutRun(t *testing.T) {
return config.NewBlankConfig(), nil
},
},
tty: true,
httpStubs: func(reg *httpmock.Registry) {
reg.Register(httpmock.GraphQL(`query PullRequestList\b`), httpmock.FileResponse("./fixtures/prList.json"))
},
@ -232,6 +243,12 @@ func Test_checkoutRun(t *testing.T) {
opts := tt.opts
ios, _, stdout, stderr := iostreams.Test()
if tt.tty {
ios.SetStdinTTY(tt.tty)
ios.SetStdoutTTY(tt.tty)
ios.SetStderrTTY(tt.tty)
}
opts.IO = ios
httpReg := &httpmock.Registry{}
defer httpReg.Verify(t)