Skip to content

Commit c2d7cbf

Browse files
author
Nate Smith
authored
Merge pull request cli#1582 from AliabbasMerchant/interactiveCreationTests
Interactive template selection test for issue create
2 parents ab2c38e + 9e72796 commit c2d7cbf

File tree

7 files changed

+189
-6
lines changed

7 files changed

+189
-6
lines changed

pkg/cmd/issue/create/create.go

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,8 @@ type CreateOptions struct {
2323
IO *iostreams.IOStreams
2424
BaseRepo func() (ghrepo.Interface, error)
2525

26+
RootDirOverride string
27+
2628
RepoOverride string
2729
WebMode bool
2830

@@ -94,9 +96,10 @@ func createRun(opts *CreateOptions) error {
9496
}
9597

9698
var nonLegacyTemplateFiles []string
97-
if opts.RepoOverride == "" {
99+
if opts.RootDirOverride != "" {
100+
nonLegacyTemplateFiles = githubtemplate.FindNonLegacy(opts.RootDirOverride, "ISSUE_TEMPLATE")
101+
} else if opts.RepoOverride == "" {
98102
if rootDir, err := git.ToplevelDir(); err == nil {
99-
// TODO: figure out how to stub this in tests
100103
nonLegacyTemplateFiles = githubtemplate.FindNonLegacy(rootDir, "ISSUE_TEMPLATE")
101104
}
102105
}

pkg/cmd/issue/create/create_test.go

Lines changed: 70 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@ import (
1616
"github.com/cli/cli/pkg/cmdutil"
1717
"github.com/cli/cli/pkg/httpmock"
1818
"github.com/cli/cli/pkg/iostreams"
19+
"github.com/cli/cli/pkg/prompt"
1920
"github.com/cli/cli/test"
2021
"github.com/google/shlex"
2122
"github.com/stretchr/testify/assert"
@@ -29,6 +30,10 @@ func eq(t *testing.T, got interface{}, expected interface{}) {
2930
}
3031

3132
func runCommand(rt http.RoundTripper, isTTY bool, cli string) (*test.CmdOut, error) {
33+
return runCommandWithRootDirOverridden(rt, isTTY, cli, "")
34+
}
35+
36+
func runCommandWithRootDirOverridden(rt http.RoundTripper, isTTY bool, cli string, rootDir string) (*test.CmdOut, error) {
3237
io, _, stdout, stderr := iostreams.Test()
3338
io.SetStdoutTTY(isTTY)
3439
io.SetStdinTTY(isTTY)
@@ -47,7 +52,10 @@ func runCommand(rt http.RoundTripper, isTTY bool, cli string) (*test.CmdOut, err
4752
},
4853
}
4954

50-
cmd := NewCmdCreate(factory, nil)
55+
cmd := NewCmdCreate(factory, func(opts *CreateOptions) error {
56+
opts.RootDirOverride = rootDir
57+
return createRun(opts)
58+
})
5159

5260
argv, err := shlex.Split(cli)
5361
if err != nil {
@@ -126,6 +134,67 @@ func TestIssueCreate(t *testing.T) {
126134
eq(t, output.String(), "https://github.com/OWNER/REPO/issues/12\n")
127135
}
128136

137+
func TestIssueCreate_nonLegacyTemplate(t *testing.T) {
138+
http := &httpmock.Registry{}
139+
defer http.Verify(t)
140+
141+
http.StubResponse(200, bytes.NewBufferString(`
142+
{ "data": { "repository": {
143+
"id": "REPOID",
144+
"hasIssuesEnabled": true
145+
} } }
146+
`))
147+
http.StubResponse(200, bytes.NewBufferString(`
148+
{ "data": { "createIssue": { "issue": {
149+
"URL": "https://github.com/OWNER/REPO/issues/12"
150+
} } } }
151+
`))
152+
153+
as, teardown := prompt.InitAskStubber()
154+
defer teardown()
155+
as.Stub([]*prompt.QuestionStub{
156+
{
157+
Name: "index",
158+
Value: 1,
159+
},
160+
})
161+
as.Stub([]*prompt.QuestionStub{
162+
{
163+
Name: "body",
164+
Default: true,
165+
},
166+
})
167+
as.Stub([]*prompt.QuestionStub{
168+
{
169+
Name: "confirmation",
170+
Value: 0,
171+
},
172+
})
173+
174+
output, err := runCommandWithRootDirOverridden(http, true, `-t hello`, "./fixtures/repoWithNonLegacyIssueTemplates")
175+
if err != nil {
176+
t.Errorf("error running command `issue create`: %v", err)
177+
}
178+
179+
bodyBytes, _ := ioutil.ReadAll(http.Requests[1].Body)
180+
reqBody := struct {
181+
Variables struct {
182+
Input struct {
183+
RepositoryID string
184+
Title string
185+
Body string
186+
}
187+
}
188+
}{}
189+
_ = json.Unmarshal(bodyBytes, &reqBody)
190+
191+
eq(t, reqBody.Variables.Input.RepositoryID, "REPOID")
192+
eq(t, reqBody.Variables.Input.Title, "hello")
193+
eq(t, reqBody.Variables.Input.Body, "I have a suggestion for an enhancement")
194+
195+
eq(t, output.String(), "https://github.com/OWNER/REPO/issues/12\n")
196+
}
197+
129198
func TestIssueCreate_metadata(t *testing.T) {
130199
http := &httpmock.Registry{}
131200
defer http.Verify(t)
Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
---
2+
name: Bug report
3+
about: Report a bug or unexpected behavior
4+
title: Bug Report
5+
labels: bug
6+
7+
---
8+
9+
I wanna report a bug
Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
---
2+
name: Submit a request
3+
about: Propose an improvement
4+
title: Enhancement Proposal
5+
labels: enhancement
6+
7+
---
8+
9+
I have a suggestion for an enhancement

pkg/cmd/pr/create/create.go

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,8 @@ type CreateOptions struct {
2828
Remotes func() (context.Remotes, error)
2929
Branch func() (string, error)
3030

31+
RootDirOverride string
32+
3133
RepoOverride string
3234

3335
Autofill bool
@@ -246,8 +248,11 @@ func createRun(opts *CreateOptions) error {
246248
if !opts.WebMode && !opts.Autofill && interactive {
247249
var nonLegacyTemplateFiles []string
248250
var legacyTemplateFile *string
249-
if rootDir, err := git.ToplevelDir(); err == nil {
250-
// TODO: figure out how to stub this in tests
251+
252+
if opts.RootDirOverride != "" {
253+
nonLegacyTemplateFiles = githubtemplate.FindNonLegacy(opts.RootDirOverride, "PULL_REQUEST_TEMPLATE")
254+
legacyTemplateFile = githubtemplate.FindLegacy(opts.RootDirOverride, "PULL_REQUEST_TEMPLATE")
255+
} else if rootDir, err := git.ToplevelDir(); err == nil {
251256
nonLegacyTemplateFiles = githubtemplate.FindNonLegacy(rootDir, "PULL_REQUEST_TEMPLATE")
252257
legacyTemplateFile = githubtemplate.FindLegacy(rootDir, "PULL_REQUEST_TEMPLATE")
253258
}

pkg/cmd/pr/create/create_test.go

Lines changed: 82 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -31,6 +31,10 @@ func eq(t *testing.T, got interface{}, expected interface{}) {
3131
}
3232

3333
func runCommand(rt http.RoundTripper, remotes context.Remotes, branch string, isTTY bool, cli string) (*test.CmdOut, error) {
34+
return runCommandWithRootDirOverridden(rt, remotes, branch, isTTY, cli, "")
35+
}
36+
37+
func runCommandWithRootDirOverridden(rt http.RoundTripper, remotes context.Remotes, branch string, isTTY bool, cli string, rootDir string) (*test.CmdOut, error) {
3438
io, _, stdout, stderr := iostreams.Test()
3539
io.SetStdoutTTY(isTTY)
3640
io.SetStdinTTY(isTTY)
@@ -60,7 +64,10 @@ func runCommand(rt http.RoundTripper, remotes context.Remotes, branch string, is
6064
},
6165
}
6266

63-
cmd := NewCmdCreate(factory, nil)
67+
cmd := NewCmdCreate(factory, func(opts *CreateOptions) error {
68+
opts.RootDirOverride = rootDir
69+
return createRun(opts)
70+
})
6471
cmd.PersistentFlags().StringP("repo", "R", "", "")
6572

6673
argv, err := shlex.Split(cli)
@@ -239,6 +246,80 @@ func TestPRCreate(t *testing.T) {
239246

240247
eq(t, output.String(), "https://github.com/OWNER/REPO/pull/12\n")
241248
}
249+
func TestPRCreate_nonLegacyTemplate(t *testing.T) {
250+
http := initFakeHTTP()
251+
defer http.Verify(t)
252+
253+
http.StubRepoResponse("OWNER", "REPO")
254+
http.StubResponse(200, bytes.NewBufferString(`
255+
{ "data": { "repository": { "forks": { "nodes": [
256+
] } } } }
257+
`))
258+
http.StubResponse(200, bytes.NewBufferString(`
259+
{ "data": { "repository": { "pullRequests": { "nodes" : [
260+
] } } } }
261+
`))
262+
http.StubResponse(200, bytes.NewBufferString(`
263+
{ "data": { "createPullRequest": { "pullRequest": {
264+
"URL": "https://github.com/OWNER/REPO/pull/12"
265+
} } } }
266+
`))
267+
268+
cs, cmdTeardown := test.InitCmdStubber()
269+
defer cmdTeardown()
270+
271+
cs.Stub("") // git config --get-regexp (determineTrackingBranch)
272+
cs.Stub("") // git show-ref --verify (determineTrackingBranch)
273+
cs.Stub("") // git status
274+
cs.Stub("1234567890,commit 0\n2345678901,commit 1") // git log
275+
cs.Stub("") // git push
276+
277+
as, teardown := prompt.InitAskStubber()
278+
defer teardown()
279+
as.Stub([]*prompt.QuestionStub{
280+
{
281+
Name: "index",
282+
Value: 0,
283+
},
284+
})
285+
as.Stub([]*prompt.QuestionStub{
286+
{
287+
Name: "body",
288+
Default: true,
289+
},
290+
})
291+
as.Stub([]*prompt.QuestionStub{
292+
{
293+
Name: "confirmation",
294+
Value: 0,
295+
},
296+
})
297+
298+
output, err := runCommandWithRootDirOverridden(http, nil, "feature", true, `-t "my title"`, "./fixtures/repoWithNonLegacyPRTemplates")
299+
require.NoError(t, err)
300+
301+
bodyBytes, _ := ioutil.ReadAll(http.Requests[3].Body)
302+
reqBody := struct {
303+
Variables struct {
304+
Input struct {
305+
RepositoryID string
306+
Title string
307+
Body string
308+
BaseRefName string
309+
HeadRefName string
310+
}
311+
}
312+
}{}
313+
_ = json.Unmarshal(bodyBytes, &reqBody)
314+
315+
eq(t, reqBody.Variables.Input.RepositoryID, "REPOID")
316+
eq(t, reqBody.Variables.Input.Title, "my title")
317+
eq(t, reqBody.Variables.Input.Body, "Fixes a bug and Closes an issue")
318+
eq(t, reqBody.Variables.Input.BaseRefName, "master")
319+
eq(t, reqBody.Variables.Input.HeadRefName, "feature")
320+
321+
eq(t, output.String(), "https://github.com/OWNER/REPO/pull/12\n")
322+
}
242323

243324
func TestPRCreate_metadata(t *testing.T) {
244325
http := initFakeHTTP()
Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
---
2+
name: "Bug fix"
3+
about: Fix a bug
4+
5+
---
6+
7+
Fixes a bug and Closes an issue

0 commit comments

Comments
 (0)