Skip to content

Commit 88cf6ce

Browse files
committed
consolidate CmdStubber into test package
1 parent 5187ad4 commit 88cf6ce

File tree

3 files changed

+48
-47
lines changed

3 files changed

+48
-47
lines changed

command/pr_create_test.go

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

1010
"github.com/cli/cli/context"
11+
"github.com/cli/cli/test"
1112
)
1213

1314
func TestPRCreate(t *testing.T) {
@@ -24,7 +25,7 @@ func TestPRCreate(t *testing.T) {
2425
} } } }
2526
`))
2627

27-
cs, cmdTeardown := initCmdStubber()
28+
cs, cmdTeardown := test.InitCmdStubber()
2829
defer cmdTeardown()
2930

3031
cs.Stub("") // git status
@@ -68,7 +69,7 @@ func TestPRCreate_alreadyExists(t *testing.T) {
6869
] } } } }
6970
`))
7071

71-
cs, cmdTeardown := initCmdStubber()
72+
cs, cmdTeardown := test.InitCmdStubber()
7273
defer cmdTeardown()
7374

7475
cs.Stub("") // git status
@@ -88,7 +89,7 @@ func TestPRCreate_web(t *testing.T) {
8889
http := initFakeHTTP()
8990
http.StubRepoResponse("OWNER", "REPO")
9091

91-
cs, cmdTeardown := initCmdStubber()
92+
cs, cmdTeardown := test.InitCmdStubber()
9293
defer cmdTeardown()
9394

9495
cs.Stub("") // git status
@@ -123,7 +124,7 @@ func TestPRCreate_ReportsUncommittedChanges(t *testing.T) {
123124
} } } }
124125
`))
125126

126-
cs, cmdTeardown := initCmdStubber()
127+
cs, cmdTeardown := test.InitCmdStubber()
127128
defer cmdTeardown()
128129

129130
cs.Stub(" M git/git.go") // git status
@@ -193,7 +194,7 @@ func TestPRCreate_cross_repo_same_branch(t *testing.T) {
193194
} } } }
194195
`))
195196

196-
cs, cmdTeardown := initCmdStubber()
197+
cs, cmdTeardown := test.InitCmdStubber()
197198
defer cmdTeardown()
198199

199200
cs.Stub("") // git status
@@ -242,7 +243,7 @@ func TestPRCreate_survey_defaults_multicommit(t *testing.T) {
242243
} } } }
243244
`))
244245

245-
cs, cmdTeardown := initCmdStubber()
246+
cs, cmdTeardown := test.InitCmdStubber()
246247
defer cmdTeardown()
247248

248249
cs.Stub("") // git status
@@ -312,7 +313,7 @@ func TestPRCreate_survey_defaults_monocommit(t *testing.T) {
312313
} } } }
313314
`))
314315

315-
cs, cmdTeardown := initCmdStubber()
316+
cs, cmdTeardown := test.InitCmdStubber()
316317
defer cmdTeardown()
317318

318319
cs.Stub("") // git status
@@ -383,7 +384,7 @@ func TestPRCreate_survey_autofill(t *testing.T) {
383384
} } } }
384385
`))
385386

386-
cs, cmdTeardown := initCmdStubber()
387+
cs, cmdTeardown := test.InitCmdStubber()
387388
defer cmdTeardown()
388389

389390
cs.Stub("") // git status
@@ -426,7 +427,7 @@ func TestPRCreate_defaults_error_autofill(t *testing.T) {
426427
http := initFakeHTTP()
427428
http.StubRepoResponse("OWNER", "REPO")
428429

429-
cs, cmdTeardown := initCmdStubber()
430+
cs, cmdTeardown := test.InitCmdStubber()
430431
defer cmdTeardown()
431432

432433
cs.Stub("") // git status
@@ -442,7 +443,7 @@ func TestPRCreate_defaults_error_web(t *testing.T) {
442443
http := initFakeHTTP()
443444
http.StubRepoResponse("OWNER", "REPO")
444445

445-
cs, cmdTeardown := initCmdStubber()
446+
cs, cmdTeardown := test.InitCmdStubber()
446447
defer cmdTeardown()
447448

448449
cs.Stub("") // git status
@@ -463,7 +464,7 @@ func TestPRCreate_defaults_error_interactive(t *testing.T) {
463464
} } } }
464465
`))
465466

466-
cs, cmdTeardown := initCmdStubber()
467+
cs, cmdTeardown := test.InitCmdStubber()
467468
defer cmdTeardown()
468469

469470
cs.Stub("") // git status

command/testing.go

Lines changed: 0 additions & 36 deletions
Original file line numberDiff line numberDiff line change
@@ -3,51 +3,15 @@ package command
33
import (
44
"errors"
55
"fmt"
6-
"os/exec"
76
"reflect"
87

98
"github.com/AlecAivazis/survey/v2"
109
"github.com/AlecAivazis/survey/v2/core"
1110

1211
"github.com/cli/cli/api"
1312
"github.com/cli/cli/context"
14-
"github.com/cli/cli/internal/run"
15-
"github.com/cli/cli/test"
1613
)
1714

18-
// TODO this is split between here and test/helpers.go. I did that because otherwise our test
19-
// package would have to import utils (for utils.Runnable) which felt wrong. while utils_test
20-
// currently doesn't import test helpers, i don't see why that ought to be precluded.
21-
// I'm wondering if this is a case for having a global package just for storing Interfaces.
22-
type CmdStubber struct {
23-
Stubs []*test.OutputStub
24-
Count int
25-
Calls []*exec.Cmd
26-
}
27-
28-
func initCmdStubber() (*CmdStubber, func()) {
29-
cs := CmdStubber{}
30-
teardown := run.SetPrepareCmd(createStubbedPrepareCmd(&cs))
31-
return &cs, teardown
32-
}
33-
34-
func (cs *CmdStubber) Stub(desiredOutput string) {
35-
// TODO maybe have some kind of command mapping but going simple for now
36-
cs.Stubs = append(cs.Stubs, &test.OutputStub{[]byte(desiredOutput)})
37-
}
38-
39-
func createStubbedPrepareCmd(cs *CmdStubber) func(*exec.Cmd) run.Runnable {
40-
return func(cmd *exec.Cmd) run.Runnable {
41-
cs.Calls = append(cs.Calls, cmd)
42-
call := cs.Count
43-
cs.Count += 1
44-
if call >= len(cs.Stubs) {
45-
panic(fmt.Sprintf("more execs than stubs. most recent call: %v", cmd))
46-
}
47-
return cs.Stubs[call]
48-
}
49-
}
50-
5115
type askStubber struct {
5216
Asks [][]*survey.Question
5317
Count int

test/helpers.go

Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,12 @@
11
package test
22

3+
import (
4+
"fmt"
5+
"os/exec"
6+
7+
"github.com/cli/cli/internal/run"
8+
)
9+
310
// OutputStub implements a simple utils.Runnable
411
type OutputStub struct {
512
Out []byte
@@ -12,3 +19,32 @@ func (s OutputStub) Output() ([]byte, error) {
1219
func (s OutputStub) Run() error {
1320
return nil
1421
}
22+
23+
type CmdStubber struct {
24+
Stubs []*OutputStub
25+
Count int
26+
Calls []*exec.Cmd
27+
}
28+
29+
func InitCmdStubber() (*CmdStubber, func()) {
30+
cs := CmdStubber{}
31+
teardown := run.SetPrepareCmd(createStubbedPrepareCmd(&cs))
32+
return &cs, teardown
33+
}
34+
35+
func (cs *CmdStubber) Stub(desiredOutput string) {
36+
// TODO maybe have some kind of command mapping but going simple for now
37+
cs.Stubs = append(cs.Stubs, &OutputStub{[]byte(desiredOutput)})
38+
}
39+
40+
func createStubbedPrepareCmd(cs *CmdStubber) func(*exec.Cmd) run.Runnable {
41+
return func(cmd *exec.Cmd) run.Runnable {
42+
cs.Calls = append(cs.Calls, cmd)
43+
call := cs.Count
44+
cs.Count += 1
45+
if call >= len(cs.Stubs) {
46+
panic(fmt.Sprintf("more execs than stubs. most recent call: %v", cmd))
47+
}
48+
return cs.Stubs[call]
49+
}
50+
}

0 commit comments

Comments
 (0)