MCPcopy Create free account
hub / github.com/cli/cli / TestPromptingPRResolver

Function TestPromptingPRResolver

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

Source from the content-addressed store, hash-verified

791}
792
793func TestPromptingPRResolver(t *testing.T) {
794 t.Run("when the PR Lister has results, then we prompt for a choice", func(t *testing.T) {
795 t.Parallel()
796
797 ios, _, _, _ := iostreams.Test()
798
799 baseRepo, pr1 := _stubPR("OWNER/REPO:master", "OWNER/REPO:feature", 32, "New feature", "OPEN", false)
800 _, pr2 := _stubPR("OWNER/REPO:master", "OWNER/REPO:bug-fix", 29, "Fixed bad bug", "OPEN", false)
801 _, pr3 := _stubPR("OWNER/REPO:master", "OWNER/REPO:docs", 28, "Improve documentation", "OPEN", true)
802 lister := shared.NewMockLister(&api.PullRequestAndTotalCount{
803 TotalCount: 3,
804 PullRequests: []api.PullRequest{
805 *pr1, *pr2, *pr3,
806 }, SearchCapped: false}, nil)
807 lister.ExpectFields([]string{"number", "title", "state", "isDraft", "headRefName", "headRepository", "headRepositoryOwner", "isCrossRepository", "maintainerCanModify"})
808
809 pm := prompter.NewMockPrompter(t)
810 pm.RegisterSelect("Select a pull request",
811 []string{"32\tOPEN New feature [feature]", "29\tOPEN Fixed bad bug [bug-fix]", "28\tDRAFT Improve documentation [docs]"},
812 func(_, _ string, opts []string) (int, error) {
813 return prompter.IndexFor(opts, "32\tOPEN New feature [feature]")
814 })
815
816 resolver := &promptingPRResolver{
817 io: ios,
818 prompter: pm,
819
820 prLister: lister,
821
822 baseRepo: baseRepo,
823 }
824
825 resolvedPR, resolvedBaseRepo, err := resolver.Resolve()
826 require.NoError(t, err)
827 require.Equal(t, pr1, resolvedPR)
828 require.True(t, ghrepo.IsSame(baseRepo, resolvedBaseRepo), "expected repos to be the same")
829 })
830
831 t.Run("when the PR lister has no results, then we return an error", func(t *testing.T) {
832 t.Parallel()
833
834 ios, _, _, _ := iostreams.Test()
835
836 lister := shared.NewMockLister(&api.PullRequestAndTotalCount{
837 TotalCount: 0,
838 PullRequests: []api.PullRequest{},
839 }, nil)
840
841 resolver := &promptingPRResolver{
842 io: ios,
843 prLister: lister,
844 baseRepo: ghrepo.New("OWNER", "REPO"),
845 }
846
847 _, _, err := resolver.Resolve()
848 var noResultsErr cmdutil.NoResultsError
849 require.ErrorAs(t, err, &noResultsErr)
850 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