MCPcopy
hub / github.com/cli/cli / TestTemplateManager_hasAPI_PullRequest

Function TestTemplateManager_hasAPI_PullRequest

pkg/cmd/pr/shared/templates_test.go:68–117  ·  view source on GitHub ↗
(t *testing.T)

Source from the content-addressed store, hash-verified

66}
67
68func TestTemplateManager_hasAPI_PullRequest(t *testing.T) {
69 rootDir := t.TempDir()
70 legacyTemplateFile := filepath.Join(rootDir, ".github", "PULL_REQUEST_TEMPLATE.md")
71 _ = os.MkdirAll(filepath.Dir(legacyTemplateFile), 0755)
72 _ = os.WriteFile(legacyTemplateFile, []byte("LEGACY"), 0644)
73
74 tr := httpmock.Registry{}
75 httpClient := &http.Client{Transport: &tr}
76 defer tr.Verify(t)
77
78 tr.Register(
79 httpmock.GraphQL(`query PullRequestTemplates\b`),
80 httpmock.StringResponse(`{"data":{"repository":{
81 "pullRequestTemplates": [
82 {"filename": "bug_pr.md", "body": "I fixed a problem"},
83 {"filename": "feature_pr.md", "body": "I added a feature"}
84 ]
85 }}}`))
86
87 pm := &prompter.PrompterMock{}
88 pm.SelectFunc = func(p, _ string, opts []string) (int, error) {
89 if p == "Choose a template" {
90 return prompter.IndexFor(opts, "bug_pr.md")
91 } else {
92 return -1, prompter.NoSuchPromptErr(p)
93 }
94 }
95 m := templateManager{
96 repo: ghrepo.NewWithHost("OWNER", "REPO", "example.com"),
97 rootDir: rootDir,
98 allowFS: true,
99 isPR: true,
100 httpClient: httpClient,
101 detector: &fd.EnabledDetectorMock{},
102 prompter: pm,
103 }
104
105 hasTemplates, err := m.HasTemplates()
106 assert.NoError(t, err)
107 assert.True(t, hasTemplates)
108
109 assert.Equal(t, "LEGACY", string(m.LegacyBody()))
110
111 tpl, err := m.Choose()
112
113 assert.NoError(t, err)
114 assert.Equal(t, "", tpl.NameForSubmit())
115 assert.Equal(t, "I fixed a problem", string(tpl.Body()))
116 assert.Equal(t, "", tpl.Title())
117}
118
119func TestTemplateManagerSelect(t *testing.T) {
120 tests := []struct {

Callers

nothing calls this directly

Calls 15

VerifyMethod · 0.95
RegisterMethod · 0.95
HasTemplatesMethod · 0.95
LegacyBodyMethod · 0.95
ChooseMethod · 0.95
GraphQLFunction · 0.92
StringResponseFunction · 0.92
IndexForFunction · 0.92
NoSuchPromptErrFunction · 0.92
NewWithHostFunction · 0.92
JoinMethod · 0.80
EqualMethod · 0.80

Tested by

no test coverage detected