Skip to content

Commit

Permalink
test nonzero exit code
Browse files Browse the repository at this point in the history
  • Loading branch information
nate smith committed Oct 30, 2019
1 parent ab115ef commit ee0fe61
Showing 1 changed file with 22 additions and 11 deletions.
33 changes: 22 additions & 11 deletions git/git_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,24 +7,29 @@ import (
"testing"
)

var _outputs map[string]string
type outputSpec struct {
Stdout string
ExitCode int
}

var _outputs map[string]outputSpec

func init() {
_outputs = map[string]string{
"no changes": "",
"one change": ` M poem.txt
`,
"untracked file": ` M poem.txt
_outputs = map[string]outputSpec{
"no changes": outputSpec{"", 0},
"one change": outputSpec{` M poem.txt
`, 0},
"untracked file": outputSpec{` M poem.txt
?? new.txt
`,
`, 0},
"boom": outputSpec{"", 1},
}
}

func TestHelperProcess(*testing.T) {
if os.Getenv("GO_WANT_HELPER_PROCESS") != "1" {
return
}
defer os.Exit(0)
args := os.Args
for len(args) > 0 {
if args[0] == "--" {
Expand All @@ -33,7 +38,9 @@ func TestHelperProcess(*testing.T) {
}
args = args[1:]
}
fmt.Println(_outputs[args[0]])
output := _outputs[args[0]]
defer os.Exit(output.ExitCode)
fmt.Println(output.Stdout)
}

func StubGit(desiredOutput string) func(...string) *exec.Cmd {
Expand All @@ -57,8 +64,6 @@ func Test_UncommittedChangeCount(t *testing.T) {
GitCommand = origGitCommand
}()

// TODO handle git status exiting poorly

cases := map[string]int{
"no changes": 0,
"one change": 1,
Expand All @@ -73,4 +78,10 @@ func Test_UncommittedChangeCount(t *testing.T) {
t.Errorf("got unexpected ucc value: %d for case %s", ucc, k)
}
}

GitCommand = StubGit("boom")
_, err := UncommittedChangeCount()
if err.Error() != "failed to run git status: exit status 1" {
t.Errorf("got unexpected error message: %s", err)
}
}

0 comments on commit ee0fe61

Please sign in to comment.