From c6c27113fe4485a916299205aeaafbe16f26a8fd Mon Sep 17 00:00:00 2001 From: nilvng Date: Sun, 17 Nov 2024 16:36:10 +1100 Subject: [PATCH] issue #2329: add happy path unit test --- pkg/cmd/pr/checkout/checkout_test.go | 52 ++++++++++++++++-- pkg/cmd/pr/checkout/fixtures/prList.json | 67 ++++++++++++++++++++++++ 2 files changed, 115 insertions(+), 4 deletions(-) create mode 100644 pkg/cmd/pr/checkout/fixtures/prList.json diff --git a/pkg/cmd/pr/checkout/checkout_test.go b/pkg/cmd/pr/checkout/checkout_test.go index 1eda5dda2..d2962f2d9 100644 --- a/pkg/cmd/pr/checkout/checkout_test.go +++ b/pkg/cmd/pr/checkout/checkout_test.go @@ -14,6 +14,7 @@ import ( "github.com/cli/cli/v2/internal/config" "github.com/cli/cli/v2/internal/gh" "github.com/cli/cli/v2/internal/ghrepo" + "github.com/cli/cli/v2/internal/prompter" "github.com/cli/cli/v2/internal/run" "github.com/cli/cli/v2/pkg/cmd/pr/shared" "github.com/cli/cli/v2/pkg/cmdutil" @@ -63,10 +64,12 @@ func stubPR(repo, prHead string) (ghrepo.Interface, *api.PullRequest) { func Test_checkoutRun(t *testing.T) { tests := []struct { - name string - opts *CheckoutOptions - httpStubs func(*httpmock.Registry) - runStubs func(*run.CommandStubber) + name string + opts *CheckoutOptions + httpStubs func(*httpmock.Registry) + runStubs func(*run.CommandStubber) + promptStubs func(*prompter.MockPrompter) + remotes map[string]string wantStdout string wantStderr string @@ -158,6 +161,41 @@ func Test_checkoutRun(t *testing.T) { cs.Register(`git config branch\.foobar\.merge refs/heads/feature`, 0, "") }, }, + { + name: "with no selected PR args, prompts for choice", + opts: &CheckoutOptions{ + SelectorArg: "", + Finder: func() shared.PRFinder { + baseRepo, pr := stubPR("OWNER/REPO:master", "OWNER/REPO:feature") + finder := shared.NewMockFinder("123", pr, baseRepo) + return finder + }(), + BaseRepo: func() (ghrepo.Interface, error) { + return ghrepo.New("OWNER", "REPO"), nil + }, + Config: func() (gh.Config, error) { + return config.NewBlankConfig(), nil + }, + }, + httpStubs: func(reg *httpmock.Registry) { + reg.Register(httpmock.GraphQL(`query PullRequestList\b`), httpmock.FileResponse("./fixtures/prList.json")) + }, + promptStubs: func(pm *prompter.MockPrompter) { + pm.RegisterSelect("Check out a specific PR?", + []string{"32 New feature", "29 Fixed bad bug", "28 Improve documentation"}, + func(_, _ string, opts []string) (int, error) { + return prompter.IndexFor(opts, "32 New feature") + }) + }, + runStubs: func(cs *run.CommandStubber) { + cs.Register(`git show-ref --verify -- refs/heads/feature`, 1, "") + cs.Register(`git fetch origin \+refs/heads/feature:refs/remotes/origin/feature`, 0, "") + cs.Register(`git checkout -b feature --track origin/feature`, 0, "") + }, + remotes: map[string]string{ + "origin": "OWNER/REPO", + }, + }, } for _, tt := range tests { t.Run(tt.name, func(t *testing.T) { @@ -180,6 +218,12 @@ func Test_checkoutRun(t *testing.T) { tt.runStubs(cmdStubs) } + pm := prompter.NewMockPrompter(t) + tt.opts.Prompter = pm + if tt.promptStubs != nil { + tt.promptStubs(pm) + } + opts.Remotes = func() (context.Remotes, error) { if len(tt.remotes) == 0 { return nil, errors.New("no remotes") diff --git a/pkg/cmd/pr/checkout/fixtures/prList.json b/pkg/cmd/pr/checkout/fixtures/prList.json new file mode 100644 index 000000000..f915df136 --- /dev/null +++ b/pkg/cmd/pr/checkout/fixtures/prList.json @@ -0,0 +1,67 @@ +{ + "data": { + "repository": { + "pullRequests": { + "totalCount": 3, + "nodes": [ + { + "number": 32, + "title": "New feature", + "url": "https://github.com/OWNER/REPO/pull/32", + "createdAt": "2022-08-24T20:01:12Z", + "headRefName": "feature", + "state": "OPEN", + "isDraft": true, + "isCrossRepository": false, + "headRepository": { + "name": "REPO" + }, + "headRepositoryOwner": { + "login": "OWNER" + }, + "maintainerCanModify": true + }, + { + "number": 29, + "title": "Fixed bad bug", + "url": "https://github.com/OWNER/REPO/pull/29", + "createdAt": "2022-07-20T19:01:12Z", + "headRefName": "bug-fix", + "state": "OPEN", + "isDraft": false, + "isCrossRepository": false, + "headRepository": { + "name": "REPO" + }, + "headRepositoryOwner": { + "login": "OWNER" + }, + "maintainerCanModify": true + }, + { + "number": 28, + "state": "MERGED", + "isDraft": false, + "title": "Improve documentation", + "createdAt": "2020-01-26T19:01:12Z", + "url": "https://github.com/OWNER/REPO/pull/28", + "isCrossRepository": false, + "headRefName": "docs", + "headRepository": { + "name": "REPO" + }, + "headRepositoryOwner": { + "login": "OWNER" + }, + "maintainerCanModify": true + } + ], + "pageInfo": { + "hasNextPage": false, + "endCursor": "" + } + } + } + } + } + \ No newline at end of file