Skip to content

Commit f6afe1b

Browse files
author
nate smith
committed
use testing hack to mock git call
1 parent 9efe965 commit f6afe1b

File tree

2 files changed

+71
-1
lines changed

2 files changed

+71
-1
lines changed

git/git.go

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -216,9 +216,17 @@ func LocalBranches() ([]string, error) {
216216
return branches, nil
217217
}
218218

219+
var GitCommand = func(args ...string) *exec.Cmd {
220+
return exec.Command("git", args...)
221+
}
222+
219223
func UncommittedChangeCount() (int, error) {
220-
statusCmd := exec.Command("git", "status", "--porcelain")
224+
statusCmd := GitCommand("status", "--porcelain")
225+
fmt.Println("!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!")
226+
fmt.Printf("%+v", statusCmd)
221227
output, err := statusCmd.Output()
228+
fmt.Println(string(output))
229+
fmt.Println("!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!")
222230
if err != nil {
223231
return 0, fmt.Errorf("failed to run git status: %s", err)
224232
}

git/git_test.go

Lines changed: 62 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,62 @@
1+
package git
2+
3+
import (
4+
"fmt"
5+
"os"
6+
"os/exec"
7+
"testing"
8+
)
9+
10+
func StubbedGit(args ...string) *exec.Cmd {
11+
cs := []string{"-test.run=TestHelperProcess", "--", "git"}
12+
cs = append(cs, args...)
13+
env := []string{
14+
"GO_WANT_HELPER_PROCESS=1",
15+
}
16+
17+
cmd := exec.Command(os.Args[0], cs...)
18+
cmd.Env = append(env, os.Environ()...)
19+
fmt.Printf("%+v", cmd.Env)
20+
return cmd
21+
}
22+
23+
func TestHelperProcess(*testing.T) {
24+
if os.Getenv("GO_WANT_HELPER_PROCESS") != "1" {
25+
return
26+
}
27+
defer os.Exit(0)
28+
args := os.Args
29+
for len(args) > 0 {
30+
if args[0] == "--" {
31+
args = args[1:]
32+
break
33+
}
34+
args = args[1:]
35+
}
36+
cmd, args := args[0], args[1:]
37+
switch cmd {
38+
case "git":
39+
fmt.Println("fart")
40+
fmt.Println("town")
41+
}
42+
43+
}
44+
45+
func Test_UncommittedChangeCount(t *testing.T) {
46+
origGitCommand := GitCommand
47+
defer func() {
48+
GitCommand = origGitCommand
49+
}()
50+
51+
GitCommand = StubbedGit
52+
53+
ucc, err := UncommittedChangeCount()
54+
if err != nil {
55+
t.Fatalf("failed to run UncommittedChangeCount: %s", err)
56+
}
57+
58+
if ucc != 10 {
59+
t.Errorf("got unexpected ucc value: %d", ucc)
60+
}
61+
62+
}

0 commit comments

Comments
 (0)