Skip to content

Commit 2621bcc

Browse files
committed
Use RunCommand in tests to ensure flags reset between runs
1 parent 723cd06 commit 2621bcc

File tree

10 files changed

+76
-133
lines changed

10 files changed

+76
-133
lines changed

command/completion_test.go

Lines changed: 3 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -1,49 +1,34 @@
11
package command
22

33
import (
4-
"bytes"
54
"strings"
65
"testing"
76
)
87

98
func TestCompletion_bash(t *testing.T) {
10-
out := bytes.Buffer{}
11-
completionCmd.SetOut(&out)
12-
13-
RootCmd.SetArgs([]string{"completion"})
14-
_, err := RootCmd.ExecuteC()
9+
outStr, err := RunCommand(completionCmd, `completion`)
1510
if err != nil {
1611
t.Fatal(err)
1712
}
1813

19-
outStr := out.String()
2014
if !strings.Contains(outStr, "complete -o default -F __start_gh gh") {
2115
t.Errorf("problem in bash completion:\n%s", outStr)
2216
}
2317
}
2418

2519
func TestCompletion_zsh(t *testing.T) {
26-
out := bytes.Buffer{}
27-
completionCmd.SetOut(&out)
28-
29-
RootCmd.SetArgs([]string{"completion", "-s", "zsh"})
30-
_, err := RootCmd.ExecuteC()
20+
outStr, err := RunCommand(completionCmd, `completion -s zsh`)
3121
if err != nil {
3222
t.Fatal(err)
3323
}
3424

35-
outStr := out.String()
3625
if !strings.Contains(outStr, "#compdef _gh gh") {
3726
t.Errorf("problem in zsh completion:\n%s", outStr)
3827
}
3928
}
4029

4130
func TestCompletion_unsupported(t *testing.T) {
42-
out := bytes.Buffer{}
43-
completionCmd.SetOut(&out)
44-
45-
RootCmd.SetArgs([]string{"completion", "-s", "fish"})
46-
_, err := RootCmd.ExecuteC()
31+
_, err := RunCommand(completionCmd, `completion -s fish`)
4732
if err == nil || err.Error() != "unsupported shell type: fish" {
4833
t.Fatal(err)
4934
}

command/issue.go

Lines changed: 9 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -15,25 +15,21 @@ import (
1515

1616
func init() {
1717
RootCmd.AddCommand(issueCmd)
18-
issueCmd.AddCommand(issueCreateCmd)
1918
issueCmd.AddCommand(issueStatusCmd)
2019
issueCmd.AddCommand(issueViewCmd)
20+
21+
issueCmd.AddCommand(issueCreateCmd)
2122
issueCreateCmd.Flags().StringP("title", "t", "",
2223
"Supply a title. Will prompt for one otherwise.")
2324
issueCreateCmd.Flags().StringP("body", "b", "",
2425
"Supply a body. Will prompt for one otherwise.")
2526
issueCreateCmd.Flags().BoolP("web", "w", false, "Open the web browser to create an issue")
2627

27-
issueListCmd := &cobra.Command{
28-
Use: "list",
29-
Short: "List and filter issues in this repository",
30-
RunE: issueList,
31-
}
28+
issueCmd.AddCommand(issueListCmd)
3229
issueListCmd.Flags().StringP("assignee", "a", "", "Filter by assignee")
3330
issueListCmd.Flags().StringSliceP("label", "l", nil, "Filter by label")
3431
issueListCmd.Flags().StringP("state", "s", "", "Filter by state (open|closed|all)")
3532
issueListCmd.Flags().IntP("limit", "L", 30, "Maximum number of issues to fetch")
36-
issueCmd.AddCommand((issueListCmd))
3733
}
3834

3935
var issueCmd = &cobra.Command{
@@ -46,6 +42,11 @@ var issueCreateCmd = &cobra.Command{
4642
Short: "Create a new issue",
4743
RunE: issueCreate,
4844
}
45+
var issueListCmd = &cobra.Command{
46+
Use: "list",
47+
Short: "List and filter issues in this repository",
48+
RunE: issueList,
49+
}
4950
var issueStatusCmd = &cobra.Command{
5051
Use: "status",
5152
Short: "Show status of relevant issues",
@@ -191,7 +192,7 @@ func issueView(cmd *cobra.Command, args []string) error {
191192
return fmt.Errorf("invalid issue number: '%s'", args[0])
192193
}
193194

194-
fmt.Printf("Opening %s in your browser.\n", openURL)
195+
cmd.Printf("Opening %s in your browser.\n", openURL)
195196
return utils.OpenInBrowser(openURL)
196197
}
197198

command/issue_test.go

Lines changed: 6 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,6 @@ import (
99
"regexp"
1010
"testing"
1111

12-
"github.com/github/gh-cli/test"
1312
"github.com/github/gh-cli/utils"
1413
)
1514

@@ -21,7 +20,7 @@ func TestIssueStatus(t *testing.T) {
2120
defer jsonFile.Close()
2221
http.StubResponse(200, jsonFile)
2322

24-
output, err := test.RunCommand(RootCmd, "issue status")
23+
output, err := RunCommand(issueStatusCmd, "issue status")
2524
if err != nil {
2625
t.Errorf("error running command `issue status`: %v", err)
2726
}
@@ -49,7 +48,7 @@ func TestIssueList(t *testing.T) {
4948
defer jsonFile.Close()
5049
http.StubResponse(200, jsonFile)
5150

52-
output, err := test.RunCommand(RootCmd, "issue list")
51+
output, err := RunCommand(issueListCmd, "issue list")
5352
if err != nil {
5453
t.Errorf("error running command `issue list`: %v", err)
5554
}
@@ -73,7 +72,7 @@ func TestIssueList_withFlags(t *testing.T) {
7372

7473
http.StubResponse(200, bytes.NewBufferString(`{"data": {}}`)) // Since we are testing that the flags are passed, we don't care about the response
7574

76-
_, err := test.RunCommand(RootCmd, "issue list -a probablyCher -l web,bug -s open")
75+
_, err := RunCommand(issueListCmd, "issue list -a probablyCher -l web,bug -s open")
7776
if err != nil {
7877
t.Errorf("error running command `issue list`: %v", err)
7978
}
@@ -108,7 +107,7 @@ func TestIssueView(t *testing.T) {
108107
})
109108
defer restoreCmd()
110109

111-
output, err := test.RunCommand(RootCmd, "issue view 8")
110+
output, err := RunCommand(issueViewCmd, "issue view 8")
112111
if err != nil {
113112
t.Errorf("error running command `issue view`: %v", err)
114113
}
@@ -141,11 +140,7 @@ func TestIssueCreate(t *testing.T) {
141140
} } } }
142141
`))
143142

144-
out := bytes.Buffer{}
145-
issueCreateCmd.SetOut(&out)
146-
147-
RootCmd.SetArgs([]string{"issue", "create", "-t", "hello", "-b", "cash rules everything around me"})
148-
_, err := RootCmd.ExecuteC()
143+
output, err := RunCommand(issueCreateCmd, `issue create -t hello -b "cash rules everything around me"`)
149144
if err != nil {
150145
t.Errorf("error running command `issue create`: %v", err)
151146
}
@@ -166,5 +161,5 @@ func TestIssueCreate(t *testing.T) {
166161
eq(t, reqBody.Variables.Input.Title, "hello")
167162
eq(t, reqBody.Variables.Input.Body, "cash rules everything around me")
168163

169-
eq(t, out.String(), "https://github.com/OWNER/REPO/issues/12\n")
164+
eq(t, output, "https://github.com/OWNER/REPO/issues/12\n")
170165
}

command/pr.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -249,7 +249,7 @@ func prView(cmd *cobra.Command, args []string) error {
249249
}
250250
}
251251

252-
fmt.Printf("Opening %s in your browser.\n", openURL)
252+
cmd.Printf("Opening %s in your browser.\n", openURL)
253253
return utils.OpenInBrowser(openURL)
254254
}
255255

command/pr_checkout_test.go

Lines changed: 14 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -50,9 +50,9 @@ func TestPRCheckout_sameRepo(t *testing.T) {
5050
})
5151
defer restoreCmd()
5252

53-
RootCmd.SetArgs([]string{"pr", "checkout", "123"})
54-
_, err := prCheckoutCmd.ExecuteC()
53+
output, err := RunCommand(prCheckoutCmd, `pr checkout 123`)
5554
eq(t, err, nil)
55+
eq(t, output, "")
5656

5757
eq(t, len(ranCommands), 4)
5858
eq(t, strings.Join(ranCommands[0], " "), "git fetch origin +refs/heads/feature:refs/remotes/origin/feature")
@@ -101,9 +101,9 @@ func TestPRCheckout_existingBranch(t *testing.T) {
101101
})
102102
defer restoreCmd()
103103

104-
RootCmd.SetArgs([]string{"pr", "checkout", "123"})
105-
_, err := prCheckoutCmd.ExecuteC()
104+
output, err := RunCommand(prCheckoutCmd, `pr checkout 123`)
106105
eq(t, err, nil)
106+
eq(t, output, "")
107107

108108
eq(t, len(ranCommands), 3)
109109
eq(t, strings.Join(ranCommands[0], " "), "git fetch origin +refs/heads/feature:refs/remotes/origin/feature")
@@ -152,9 +152,9 @@ func TestPRCheckout_differentRepo_remoteExists(t *testing.T) {
152152
})
153153
defer restoreCmd()
154154

155-
RootCmd.SetArgs([]string{"pr", "checkout", "123"})
156-
_, err := prCheckoutCmd.ExecuteC()
155+
output, err := RunCommand(prCheckoutCmd, `pr checkout 123`)
157156
eq(t, err, nil)
157+
eq(t, output, "")
158158

159159
eq(t, len(ranCommands), 4)
160160
eq(t, strings.Join(ranCommands[0], " "), "git fetch robot-fork +refs/heads/feature:refs/remotes/robot-fork/feature")
@@ -203,9 +203,9 @@ func TestPRCheckout_differentRepo(t *testing.T) {
203203
})
204204
defer restoreCmd()
205205

206-
RootCmd.SetArgs([]string{"pr", "checkout", "123"})
207-
_, err := prCheckoutCmd.ExecuteC()
206+
output, err := RunCommand(prCheckoutCmd, `pr checkout 123`)
208207
eq(t, err, nil)
208+
eq(t, output, "")
209209

210210
eq(t, len(ranCommands), 4)
211211
eq(t, strings.Join(ranCommands[0], " "), "git fetch origin refs/pull/123/head:feature")
@@ -254,9 +254,9 @@ func TestPRCheckout_differentRepo_existingBranch(t *testing.T) {
254254
})
255255
defer restoreCmd()
256256

257-
RootCmd.SetArgs([]string{"pr", "checkout", "123"})
258-
_, err := prCheckoutCmd.ExecuteC()
257+
output, err := RunCommand(prCheckoutCmd, `pr checkout 123`)
259258
eq(t, err, nil)
259+
eq(t, output, "")
260260

261261
eq(t, len(ranCommands), 2)
262262
eq(t, strings.Join(ranCommands[0], " "), "git fetch origin refs/pull/123/head:feature")
@@ -303,9 +303,9 @@ func TestPRCheckout_differentRepo_currentBranch(t *testing.T) {
303303
})
304304
defer restoreCmd()
305305

306-
RootCmd.SetArgs([]string{"pr", "checkout", "123"})
307-
_, err := prCheckoutCmd.ExecuteC()
306+
output, err := RunCommand(prCheckoutCmd, `pr checkout 123`)
308307
eq(t, err, nil)
308+
eq(t, output, "")
309309

310310
eq(t, len(ranCommands), 2)
311311
eq(t, strings.Join(ranCommands[0], " "), "git fetch origin refs/pull/123/head")
@@ -352,9 +352,9 @@ func TestPRCheckout_maintainerCanModify(t *testing.T) {
352352
})
353353
defer restoreCmd()
354354

355-
RootCmd.SetArgs([]string{"pr", "checkout", "123"})
356-
_, err := prCheckoutCmd.ExecuteC()
355+
output, err := RunCommand(prCheckoutCmd, `pr checkout 123`)
357356
eq(t, err, nil)
357+
eq(t, output, "")
358358

359359
eq(t, len(ranCommands), 4)
360360
eq(t, strings.Join(ranCommands[0], " "), "git fetch origin refs/pull/123/head:feature")

command/pr_create_test.go

Lines changed: 6 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -65,11 +65,7 @@ func TestPRCreate(t *testing.T) {
6565
git.GitCommand = origGitCommand
6666
}()
6767

68-
out := bytes.Buffer{}
69-
prCreateCmd.SetOut(&out)
70-
71-
RootCmd.SetArgs([]string{"pr", "create", "-t", "mytitle", "-b", "mybody"})
72-
_, err := prCreateCmd.ExecuteC()
68+
output, err := RunCommand(prCreateCmd, `pr create -t "my title" -b "my body"`)
7369
eq(t, err, nil)
7470

7571
bodyBytes, _ := ioutil.ReadAll(http.Requests[1].Body)
@@ -87,12 +83,12 @@ func TestPRCreate(t *testing.T) {
8783
json.Unmarshal(bodyBytes, &reqBody)
8884

8985
eq(t, reqBody.Variables.Input.RepositoryID, "REPOID")
90-
eq(t, reqBody.Variables.Input.Title, "mytitle")
91-
eq(t, reqBody.Variables.Input.Body, "mybody")
86+
eq(t, reqBody.Variables.Input.Title, "my title")
87+
eq(t, reqBody.Variables.Input.Body, "my body")
9288
eq(t, reqBody.Variables.Input.BaseRefName, "master")
9389
eq(t, reqBody.Variables.Input.HeadRefName, "feature")
9490

95-
eq(t, out.String(), "https://github.com/OWNER/REPO/pull/12\n")
91+
eq(t, output, "https://github.com/OWNER/REPO/pull/12\n")
9692
}
9793

9894
func TestPRCreate_ReportsUncommittedChanges(t *testing.T) {
@@ -123,14 +119,10 @@ func TestPRCreate_ReportsUncommittedChanges(t *testing.T) {
123119
git.GitCommand = origGitCommand
124120
}()
125121

126-
out := bytes.Buffer{}
127-
prCreateCmd.SetOut(&out)
128-
129-
RootCmd.SetArgs([]string{"pr", "create", "-t", "mytitle", "-b", "mybody"})
130-
_, err := prCreateCmd.ExecuteC()
122+
output, err := RunCommand(prCreateCmd, `pr create -t "my title" -b "my body"`)
131123
eq(t, err, nil)
132124

133-
eq(t, out.String(), `Warning: 1 uncommitted change
125+
eq(t, output, `Warning: 1 uncommitted change
134126
https://github.com/OWNER/REPO/pull/12
135127
`)
136128
}

0 commit comments

Comments
 (0)