X Tutup
Skip to content

Commit 7555aa9

Browse files
author
nate smith
committed
first pass at generalizing process stubbing
1 parent ee0fe61 commit 7555aa9

File tree

2 files changed

+50
-46
lines changed

2 files changed

+50
-46
lines changed

git/git_test.go

Lines changed: 13 additions & 46 deletions
Original file line numberDiff line numberDiff line change
@@ -2,62 +2,29 @@ package git
22

33
import (
44
"fmt"
5+
"github.com/github/gh-cli/test"
56
"os"
6-
"os/exec"
77
"testing"
88
)
99

10-
type outputSpec struct {
11-
Stdout string
12-
ExitCode int
13-
}
14-
15-
var _outputs map[string]outputSpec
16-
17-
func init() {
18-
_outputs = map[string]outputSpec{
19-
"no changes": outputSpec{"", 0},
20-
"one change": outputSpec{` M poem.txt
10+
func TestGitStatusHelperProcess(*testing.T) {
11+
if test.SkipTestHelperProcess() {
12+
return
13+
}
14+
outputs := map[string]test.ExecStub{
15+
"no changes": test.ExecStub{"", 0},
16+
"one change": test.ExecStub{` M poem.txt
2117
`, 0},
22-
"untracked file": outputSpec{` M poem.txt
18+
"untracked file": test.ExecStub{` M poem.txt
2319
?? new.txt
2420
`, 0},
25-
"boom": outputSpec{"", 1},
26-
}
27-
}
28-
29-
func TestHelperProcess(*testing.T) {
30-
if os.Getenv("GO_WANT_HELPER_PROCESS") != "1" {
31-
return
21+
"boom": test.ExecStub{"", 1},
3222
}
33-
args := os.Args
34-
for len(args) > 0 {
35-
if args[0] == "--" {
36-
args = args[1:]
37-
break
38-
}
39-
args = args[1:]
40-
}
41-
output := _outputs[args[0]]
23+
output := test.GetExecStub(outputs)
4224
defer os.Exit(output.ExitCode)
4325
fmt.Println(output.Stdout)
4426
}
4527

46-
func StubGit(desiredOutput string) func(...string) *exec.Cmd {
47-
return func(args ...string) *exec.Cmd {
48-
cs := []string{"-test.run=TestHelperProcess", "--", desiredOutput}
49-
cs = append(cs, args...)
50-
env := []string{
51-
"GO_WANT_HELPER_PROCESS=1",
52-
}
53-
54-
cmd := exec.Command(os.Args[0], cs...)
55-
cmd.Env = append(env, os.Environ()...)
56-
return cmd
57-
}
58-
59-
}
60-
6128
func Test_UncommittedChangeCount(t *testing.T) {
6229
origGitCommand := GitCommand
6330
defer func() {
@@ -71,15 +38,15 @@ func Test_UncommittedChangeCount(t *testing.T) {
7138
}
7239

7340
for k, v := range cases {
74-
GitCommand = StubGit(k)
41+
GitCommand = test.StubExecCommand("TestGitStatusHelperProcess", k)
7542
ucc, _ := UncommittedChangeCount()
7643

7744
if ucc != v {
7845
t.Errorf("got unexpected ucc value: %d for case %s", ucc, k)
7946
}
8047
}
8148

82-
GitCommand = StubGit("boom")
49+
GitCommand = test.StubExecCommand("TestGitStatusHelperProcess", "boom")
8350
_, err := UncommittedChangeCount()
8451
if err.Error() != "failed to run git status: exit status 1" {
8552
t.Errorf("got unexpected error message: %s", err)

test/helpers.go

Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,43 @@ import (
1111
"github.com/spf13/cobra"
1212
)
1313

14+
type ExecStub struct {
15+
Stdout string
16+
ExitCode int
17+
}
18+
19+
func GetExecStub(outputs map[string]ExecStub) ExecStub {
20+
args := os.Args
21+
for len(args) > 0 {
22+
if args[0] == "--" {
23+
args = args[1:]
24+
break
25+
}
26+
args = args[1:]
27+
}
28+
return outputs[args[0]]
29+
}
30+
31+
func SkipTestHelperProcess() bool {
32+
return os.Getenv("GO_WANT_HELPER_PROCESS") != "1"
33+
}
34+
35+
func StubExecCommand(testHelper string, desiredOutput string) func(...string) *exec.Cmd {
36+
return func(args ...string) *exec.Cmd {
37+
cs := []string{
38+
fmt.Sprintf("-test.run=%s", testHelper),
39+
"--", desiredOutput}
40+
cs = append(cs, args...)
41+
env := []string{
42+
"GO_WANT_HELPER_PROCESS=1",
43+
}
44+
45+
cmd := exec.Command(os.Args[0], cs...)
46+
cmd.Env = append(env, os.Environ()...)
47+
return cmd
48+
}
49+
}
50+
1451
type TempGitRepo struct {
1552
Remote string
1653
TearDown func()

0 commit comments

Comments
 (0)
X Tutup