MCPcopy
hub / github.com/cli/cli / TestPromptingPRResolver

Function TestPromptingPRResolver

pkg/cmd/pr/checkout/checkout_test.go:400–459  ·  view source on GitHub ↗
(t *testing.T)

Source from the content-addressed store, hash-verified

398}
399
400func TestPromptingPRResolver(t *testing.T) {
401 t.Run("when the PR Lister has results, then we prompt for a choice", func(t *testing.T) {
402 t.Parallel()
403
404 ios, _, _, _ := iostreams.Test()
405
406 baseRepo, pr1 := _stubPR("OWNER/REPO:master", "OWNER/REPO:feature", 32, "New feature", "OPEN", false)
407 _, pr2 := _stubPR("OWNER/REPO:master", "OWNER/REPO:bug-fix", 29, "Fixed bad bug", "OPEN", false)
408 _, pr3 := _stubPR("OWNER/REPO:master", "OWNER/REPO:docs", 28, "Improve documentation", "OPEN", true)
409 lister := shared.NewMockLister(&api.PullRequestAndTotalCount{
410 TotalCount: 3,
411 PullRequests: []api.PullRequest{
412 *pr1, *pr2, *pr3,
413 }, SearchCapped: false}, nil)
414 lister.ExpectFields([]string{"number", "title", "state", "isDraft", "headRefName", "headRepository", "headRepositoryOwner", "isCrossRepository", "maintainerCanModify"})
415
416 pm := prompter.NewMockPrompter(t)
417 pm.RegisterSelect("Select a pull request",
418 []string{"32\tOPEN New feature [feature]", "29\tOPEN Fixed bad bug [bug-fix]", "28\tDRAFT Improve documentation [docs]"},
419 func(_, _ string, opts []string) (int, error) {
420 return prompter.IndexFor(opts, "32\tOPEN New feature [feature]")
421 })
422
423 resolver := &promptingPRResolver{
424 io: ios,
425 prompter: pm,
426
427 prLister: lister,
428
429 baseRepo: baseRepo,
430 }
431
432 resolvedPR, resolvedBaseRepo, err := resolver.Resolve()
433 require.NoError(t, err)
434 require.Equal(t, pr1, resolvedPR)
435 require.True(t, ghrepo.IsSame(baseRepo, resolvedBaseRepo), "expected repos to be the same")
436 })
437
438 t.Run("when the PR lister has no results, then we return an error", func(t *testing.T) {
439 t.Parallel()
440
441 ios, _, _, _ := iostreams.Test()
442
443 lister := shared.NewMockLister(&api.PullRequestAndTotalCount{
444 TotalCount: 0,
445 PullRequests: []api.PullRequest{},
446 }, nil)
447
448 resolver := &promptingPRResolver{
449 io: ios,
450 prLister: lister,
451 baseRepo: ghrepo.New("OWNER", "REPO"),
452 }
453
454 _, _, err := resolver.Resolve()
455 var noResultsErr cmdutil.NoResultsError
456 require.ErrorAs(t, err, &noResultsErr)
457 require.Equal(t, "no open pull requests in OWNER/REPO", noResultsErr.Error())

Callers

nothing calls this directly

Calls 12

ResolveMethod · 0.95
ErrorMethod · 0.95
TestFunction · 0.92
NewMockListerFunction · 0.92
NewMockPrompterFunction · 0.92
IndexForFunction · 0.92
IsSameFunction · 0.92
NewFunction · 0.92
_stubPRFunction · 0.85
EqualMethod · 0.80
RunMethod · 0.65
ExpectFieldsMethod · 0.45

Tested by

no test coverage detected