X Tutup
Skip to content

Commit d0c2c81

Browse files
Interactive template selection test for issue create
1 parent f1ea794 commit d0c2c81

File tree

4 files changed

+93
-3
lines changed

4 files changed

+93
-3
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`, "./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

0 commit comments

Comments
 (0)
X Tutup