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

Function TestPromptingPRResolver

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

Source from the content-addressed store, hash-verified

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