From d92e529629f2ccc0c87c4a49e0b2f199602bfb3c Mon Sep 17 00:00:00 2001 From: nilvng Date: Sun, 1 Dec 2024 21:13:54 +1100 Subject: [PATCH] issue #2329: return error when not running interactively --- pkg/cmd/pr/checkout/checkout.go | 4 ++++ pkg/cmd/pr/checkout/checkout_test.go | 23 ++++++++++++++++++++--- 2 files changed, 24 insertions(+), 3 deletions(-) diff --git a/pkg/cmd/pr/checkout/checkout.go b/pkg/cmd/pr/checkout/checkout.go index 54ecf17f6..e78eb026b 100644 --- a/pkg/cmd/pr/checkout/checkout.go +++ b/pkg/cmd/pr/checkout/checkout.go @@ -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 diff --git a/pkg/cmd/pr/checkout/checkout_test.go b/pkg/cmd/pr/checkout/checkout_test.go index 86e7e6d70..73055e1f8 100644 --- a/pkg/cmd/pr/checkout/checkout_test.go +++ b/pkg/cmd/pr/checkout/checkout_test.go @@ -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)