X Tutup
Skip to content

Commit ed8aaa8

Browse files
committed
add CmdStubber
1 parent 8769299 commit ed8aaa8

File tree

8 files changed

+139
-178
lines changed

8 files changed

+139
-178
lines changed

command/issue_test.go

Lines changed: 7 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@ import (
1010
"strings"
1111
"testing"
1212

13+
"github.com/cli/cli/test"
1314
"github.com/cli/cli/utils"
1415
)
1516

@@ -226,7 +227,7 @@ func TestIssueView(t *testing.T) {
226227
var seenCmd *exec.Cmd
227228
restoreCmd := utils.SetPrepareCmd(func(cmd *exec.Cmd) utils.Runnable {
228229
seenCmd = cmd
229-
return &outputStub{}
230+
return &test.OutputStub{}
230231
})
231232
defer restoreCmd()
232233

@@ -260,7 +261,7 @@ func TestIssueView_numberArgWithHash(t *testing.T) {
260261
var seenCmd *exec.Cmd
261262
restoreCmd := utils.SetPrepareCmd(func(cmd *exec.Cmd) utils.Runnable {
262263
seenCmd = cmd
263-
return &outputStub{}
264+
return &test.OutputStub{}
264265
})
265266
defer restoreCmd()
266267

@@ -383,7 +384,7 @@ func TestIssueView_notFound(t *testing.T) {
383384
var seenCmd *exec.Cmd
384385
restoreCmd := utils.SetPrepareCmd(func(cmd *exec.Cmd) utils.Runnable {
385386
seenCmd = cmd
386-
return &outputStub{}
387+
return &test.OutputStub{}
387388
})
388389
defer restoreCmd()
389390

@@ -430,7 +431,7 @@ func TestIssueView_urlArg(t *testing.T) {
430431
var seenCmd *exec.Cmd
431432
restoreCmd := utils.SetPrepareCmd(func(cmd *exec.Cmd) utils.Runnable {
432433
seenCmd = cmd
433-
return &outputStub{}
434+
return &test.OutputStub{}
434435
})
435436
defer restoreCmd()
436437

@@ -515,7 +516,7 @@ func TestIssueCreate_web(t *testing.T) {
515516
var seenCmd *exec.Cmd
516517
restoreCmd := utils.SetPrepareCmd(func(cmd *exec.Cmd) utils.Runnable {
517518
seenCmd = cmd
518-
return &outputStub{}
519+
return &test.OutputStub{}
519520
})
520521
defer restoreCmd()
521522

@@ -541,7 +542,7 @@ func TestIssueCreate_webTitleBody(t *testing.T) {
541542
var seenCmd *exec.Cmd
542543
restoreCmd := utils.SetPrepareCmd(func(cmd *exec.Cmd) utils.Runnable {
543544
seenCmd = cmd
544-
return &outputStub{}
545+
return &test.OutputStub{}
545546
})
546547
defer restoreCmd()
547548

command/pr_checkout_test.go

Lines changed: 13 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@ import (
77
"testing"
88

99
"github.com/cli/cli/context"
10+
"github.com/cli/cli/test"
1011
"github.com/cli/cli/utils"
1112
)
1213

@@ -46,7 +47,7 @@ func TestPRCheckout_sameRepo(t *testing.T) {
4647
return &errorStub{"exit status: 1"}
4748
default:
4849
ranCommands = append(ranCommands, cmd.Args)
49-
return &outputStub{}
50+
return &test.OutputStub{}
5051
}
5152
})
5253
defer restoreCmd()
@@ -98,7 +99,7 @@ func TestPRCheckout_urlArg(t *testing.T) {
9899
return &errorStub{"exit status: 1"}
99100
default:
100101
ranCommands = append(ranCommands, cmd.Args)
101-
return &outputStub{}
102+
return &test.OutputStub{}
102103
}
103104
})
104105
defer restoreCmd()
@@ -147,7 +148,7 @@ func TestPRCheckout_branchArg(t *testing.T) {
147148
return &errorStub{"exit status: 1"}
148149
default:
149150
ranCommands = append(ranCommands, cmd.Args)
150-
return &outputStub{}
151+
return &test.OutputStub{}
151152
}
152153
})
153154
defer restoreCmd()
@@ -193,10 +194,10 @@ func TestPRCheckout_existingBranch(t *testing.T) {
193194
restoreCmd := utils.SetPrepareCmd(func(cmd *exec.Cmd) utils.Runnable {
194195
switch strings.Join(cmd.Args, " ") {
195196
case "git show-ref --verify --quiet refs/heads/feature":
196-
return &outputStub{}
197+
return &test.OutputStub{}
197198
default:
198199
ranCommands = append(ranCommands, cmd.Args)
199-
return &outputStub{}
200+
return &test.OutputStub{}
200201
}
201202
})
202203
defer restoreCmd()
@@ -248,7 +249,7 @@ func TestPRCheckout_differentRepo_remoteExists(t *testing.T) {
248249
return &errorStub{"exit status: 1"}
249250
default:
250251
ranCommands = append(ranCommands, cmd.Args)
251-
return &outputStub{}
252+
return &test.OutputStub{}
252253
}
253254
})
254255
defer restoreCmd()
@@ -300,7 +301,7 @@ func TestPRCheckout_differentRepo(t *testing.T) {
300301
return &errorStub{"exit status 1"}
301302
default:
302303
ranCommands = append(ranCommands, cmd.Args)
303-
return &outputStub{}
304+
return &test.OutputStub{}
304305
}
305306
})
306307
defer restoreCmd()
@@ -349,10 +350,10 @@ func TestPRCheckout_differentRepo_existingBranch(t *testing.T) {
349350
restoreCmd := utils.SetPrepareCmd(func(cmd *exec.Cmd) utils.Runnable {
350351
switch strings.Join(cmd.Args, " ") {
351352
case "git config branch.feature.merge":
352-
return &outputStub{[]byte("refs/heads/feature\n")}
353+
return &test.OutputStub{[]byte("refs/heads/feature\n")}
353354
default:
354355
ranCommands = append(ranCommands, cmd.Args)
355-
return &outputStub{}
356+
return &test.OutputStub{}
356357
}
357358
})
358359
defer restoreCmd()
@@ -399,10 +400,10 @@ func TestPRCheckout_differentRepo_currentBranch(t *testing.T) {
399400
restoreCmd := utils.SetPrepareCmd(func(cmd *exec.Cmd) utils.Runnable {
400401
switch strings.Join(cmd.Args, " ") {
401402
case "git config branch.feature.merge":
402-
return &outputStub{[]byte("refs/heads/feature\n")}
403+
return &test.OutputStub{[]byte("refs/heads/feature\n")}
403404
default:
404405
ranCommands = append(ranCommands, cmd.Args)
405-
return &outputStub{}
406+
return &test.OutputStub{}
406407
}
407408
})
408409
defer restoreCmd()
@@ -452,7 +453,7 @@ func TestPRCheckout_maintainerCanModify(t *testing.T) {
452453
return &errorStub{"exit status 1"}
453454
default:
454455
ranCommands = append(ranCommands, cmd.Args)
455-
return &outputStub{}
456+
return &test.OutputStub{}
456457
}
457458
})
458459
defer restoreCmd()

command/pr_create_test.go

Lines changed: 33 additions & 55 deletions
Original file line numberDiff line numberDiff line change
@@ -3,45 +3,14 @@ package command
33
import (
44
"bytes"
55
"encoding/json"
6-
"fmt"
76
"io/ioutil"
8-
"os"
9-
"os/exec"
107
"strings"
118
"testing"
129

1310
"github.com/cli/cli/context"
14-
"github.com/cli/cli/git"
15-
"github.com/cli/cli/test"
1611
"github.com/cli/cli/utils"
1712
)
1813

19-
func TestPrCreateHelperProcess(*testing.T) {
20-
if test.SkipTestHelperProcess() {
21-
return
22-
}
23-
24-
args := test.GetTestHelperProcessArgs()
25-
switch args[1] {
26-
case "log":
27-
fmt.Println("123,cool,\"\"")
28-
case "status":
29-
switch args[0] {
30-
case "clean":
31-
case "dirty":
32-
fmt.Println(" M git/git.go")
33-
default:
34-
fmt.Fprintf(os.Stderr, "unknown scenario: %q", args[0])
35-
os.Exit(1)
36-
}
37-
case "push":
38-
default:
39-
fmt.Fprintf(os.Stderr, "unknown command: %q", args[1])
40-
os.Exit(1)
41-
}
42-
os.Exit(0)
43-
}
44-
4514
func TestPRCreate(t *testing.T) {
4615
initBlankContext("OWNER/REPO", "feature")
4716
http := initFakeHTTP()
@@ -52,11 +21,13 @@ func TestPRCreate(t *testing.T) {
5221
} } } }
5322
`))
5423

55-
origGitCommand := git.GitCommand
56-
git.GitCommand = test.StubExecCommand("TestPrCreateHelperProcess", "clean")
57-
defer func() {
58-
git.GitCommand = origGitCommand
59-
}()
24+
cs := CmdStubber{}
25+
teardown := utils.SetPrepareCmd(createStubbedPrepareCmd(&cs))
26+
defer teardown()
27+
28+
cs.Stub("") // git status
29+
cs.Stub("1234567890,commit 0\n2345678901,commit 1") // git log
30+
cs.Stub("") // git push
6031

6132
output, err := RunCommand(prCreateCmd, `pr create -t "my title" -b "my body"`)
6233
eq(t, err, nil)
@@ -89,22 +60,25 @@ func TestPRCreate_web(t *testing.T) {
8960
http := initFakeHTTP()
9061
http.StubRepoResponse("OWNER", "REPO")
9162

92-
ranCommands := [][]string{}
93-
restoreCmd := utils.SetPrepareCmd(func(cmd *exec.Cmd) utils.Runnable {
94-
ranCommands = append(ranCommands, cmd.Args)
95-
return &outputStub{}
96-
})
97-
defer restoreCmd()
63+
cs := CmdStubber{}
64+
teardown := utils.SetPrepareCmd(createStubbedPrepareCmd(&cs))
65+
defer teardown()
66+
67+
cs.Stub("") // git status
68+
cs.Stub("1234567890,commit 0\n2345678901,commit 1") // git log
69+
cs.Stub("") // git push
70+
cs.Stub("") // browser
9871

9972
output, err := RunCommand(prCreateCmd, `pr create --web`)
10073
eq(t, err, nil)
10174

10275
eq(t, output.String(), "")
10376
eq(t, output.Stderr(), "Opening github.com/OWNER/REPO/compare/master...feature in your browser.\n")
10477

105-
eq(t, len(ranCommands), 4)
106-
eq(t, strings.Join(ranCommands[1], " "), "git push --set-upstream origin HEAD:feature")
107-
eq(t, ranCommands[3][len(ranCommands[3])-1], "https://github.com/OWNER/REPO/compare/master...feature?expand=1")
78+
eq(t, len(cs.Calls), 4)
79+
eq(t, strings.Join(cs.Calls[2].Args, " "), "git push --set-upstream origin HEAD:feature")
80+
browserCall := cs.Calls[3].Args
81+
eq(t, browserCall[len(browserCall)-1], "https://github.com/OWNER/REPO/compare/master...feature?expand=1")
10882
}
10983

11084
func TestPRCreate_ReportsUncommittedChanges(t *testing.T) {
@@ -118,11 +92,13 @@ func TestPRCreate_ReportsUncommittedChanges(t *testing.T) {
11892
} } } }
11993
`))
12094

121-
origGitCommand := git.GitCommand
122-
git.GitCommand = test.StubExecCommand("TestPrCreateHelperProcess", "dirty")
123-
defer func() {
124-
git.GitCommand = origGitCommand
125-
}()
95+
cs := CmdStubber{}
96+
teardown := utils.SetPrepareCmd(createStubbedPrepareCmd(&cs))
97+
defer teardown()
98+
99+
cs.Stub(" M git/git.go") // git status
100+
cs.Stub("1234567890,commit 0\n2345678901,commit 1") // git log
101+
cs.Stub("") // git push
126102

127103
output, err := RunCommand(prCreateCmd, `pr create -t "my title" -b "my body"`)
128104
eq(t, err, nil)
@@ -183,11 +159,13 @@ func TestPRCreate_cross_repo_same_branch(t *testing.T) {
183159
} } } }
184160
`))
185161

186-
origGitCommand := git.GitCommand
187-
git.GitCommand = test.StubExecCommand("TestPrCreateHelperProcess", "clean")
188-
defer func() {
189-
git.GitCommand = origGitCommand
190-
}()
162+
cs := CmdStubber{}
163+
teardown := utils.SetPrepareCmd(createStubbedPrepareCmd(&cs))
164+
defer teardown()
165+
166+
cs.Stub("") // git status
167+
cs.Stub("1234567890,commit 0\n2345678901,commit 1") // git log
168+
cs.Stub("") // git push
191169

192170
output, err := RunCommand(prCreateCmd, `pr create -t "cross repo" -b "same branch"`)
193171
eq(t, err, nil)

0 commit comments

Comments
 (0)
X Tutup