@@ -2,62 +2,29 @@ package git
22
33import (
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-
6128func 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 )
0 commit comments