X Tutup
Skip to content

Commit ab115ef

Browse files
author
nate smith
committed
add cases to test for
1 parent fdbf85e commit ab115ef

File tree

1 file changed

+23
-30
lines changed

1 file changed

+23
-30
lines changed

git/git_test.go

Lines changed: 23 additions & 30 deletions
Original file line numberDiff line numberDiff line change
@@ -7,29 +7,19 @@ import (
77
"testing"
88
)
99

10-
var _cases map[string]string
10+
var _outputs map[string]string
1111

1212
func init() {
13-
_cases = map[string]string{
14-
"foobar": `fart
15-
bar
16-
town`,
13+
_outputs = map[string]string{
14+
"no changes": "",
15+
"one change": ` M poem.txt
16+
`,
17+
"untracked file": ` M poem.txt
18+
?? new.txt
19+
`,
1720
}
1821
}
1922

20-
func StubbedGit(args ...string) *exec.Cmd {
21-
cs := []string{"-test.run=TestHelperProcess", "--", "git"}
22-
cs = append(cs, args...)
23-
env := []string{
24-
"GO_WANT_HELPER_PROCESS=1",
25-
}
26-
27-
cmd := exec.Command(os.Args[0], cs...)
28-
cmd.Env = append(env, os.Environ()...)
29-
fmt.Printf("%+v", cmd.Env)
30-
return cmd
31-
}
32-
3323
func TestHelperProcess(*testing.T) {
3424
if os.Getenv("GO_WANT_HELPER_PROCESS") != "1" {
3525
return
@@ -43,21 +33,19 @@ func TestHelperProcess(*testing.T) {
4333
}
4434
args = args[1:]
4535
}
46-
c, args := args[0], args[1:]
47-
fmt.Println(_cases[c])
36+
fmt.Println(_outputs[args[0]])
4837
}
4938

50-
func StubGit(c string) func(...string) *exec.Cmd {
39+
func StubGit(desiredOutput string) func(...string) *exec.Cmd {
5140
return func(args ...string) *exec.Cmd {
52-
cs := []string{"-test.run=TestHelperProcess", "--", c}
41+
cs := []string{"-test.run=TestHelperProcess", "--", desiredOutput}
5342
cs = append(cs, args...)
5443
env := []string{
5544
"GO_WANT_HELPER_PROCESS=1",
5645
}
5746

5847
cmd := exec.Command(os.Args[0], cs...)
5948
cmd.Env = append(env, os.Environ()...)
60-
fmt.Printf("%+v", cmd.Env)
6149
return cmd
6250
}
6351

@@ -69,15 +57,20 @@ func Test_UncommittedChangeCount(t *testing.T) {
6957
GitCommand = origGitCommand
7058
}()
7159

72-
GitCommand = StubGit("foobar")
60+
// TODO handle git status exiting poorly
7361

74-
ucc, err := UncommittedChangeCount()
75-
if err != nil {
76-
t.Fatalf("failed to run UncommittedChangeCount: %s", err)
62+
cases := map[string]int{
63+
"no changes": 0,
64+
"one change": 1,
65+
"untracked file": 2,
7766
}
7867

79-
if ucc != 10 {
80-
t.Errorf("got unexpected ucc value: %d", ucc)
81-
}
68+
for k, v := range cases {
69+
GitCommand = StubGit(k)
70+
ucc, _ := UncommittedChangeCount()
8271

72+
if ucc != v {
73+
t.Errorf("got unexpected ucc value: %d for case %s", ucc, k)
74+
}
75+
}
8376
}

0 commit comments

Comments
 (0)
X Tutup