Skip to content

Commit 6aa0c07

Browse files
committed
Simplify tests that use StubExecCommand
1 parent 65054fd commit 6aa0c07

File tree

4 files changed

+30
-34
lines changed

4 files changed

+30
-34
lines changed

command/pr_create_test.go

Lines changed: 12 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -18,20 +18,23 @@ func TestPrCreateHelperProcess(*testing.T) {
1818
return
1919
}
2020

21-
statusOutputs := map[string]string{
22-
"clean": "",
23-
"dirty": " M git/git.go",
24-
}
25-
2621
args := test.GetTestHelperProcessArgs()
2722
switch args[1] {
2823
case "status":
29-
fmt.Println(statusOutputs[args[0]])
24+
switch args[0] {
25+
case "clean":
26+
case "dirty":
27+
fmt.Println(" M git/git.go")
28+
default:
29+
fmt.Fprintf(os.Stderr, "unknown scenario: %q", args[0])
30+
os.Exit(1)
31+
}
3032
case "push":
31-
fmt.Println()
33+
default:
34+
fmt.Fprintf(os.Stderr, "unknown command: %q", args[1])
35+
os.Exit(1)
3236
}
33-
34-
defer os.Exit(0)
37+
os.Exit(0)
3538
}
3639

3740
func TestReportsUncommittedChanges(t *testing.T) {

git/git.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -212,9 +212,9 @@ var GitCommand = func(args ...string) *exec.Cmd {
212212

213213
func UncommittedChangeCount() (int, error) {
214214
statusCmd := GitCommand("status", "--porcelain")
215-
output, err := statusCmd.Output()
215+
output, err := utils.PrepareCmd(statusCmd).Output()
216216
if err != nil {
217-
return 0, fmt.Errorf("failed to run git status: %s", err)
217+
return 0, err
218218
}
219219
lines := strings.Split(string(output), "\n")
220220

git/git_test.go

Lines changed: 16 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -2,27 +2,30 @@ package git
22

33
import (
44
"fmt"
5-
"github.com/github/gh-cli/test"
65
"os"
6+
"strings"
77
"testing"
8+
9+
"github.com/github/gh-cli/test"
810
)
911

1012
func TestGitStatusHelperProcess(*testing.T) {
1113
if test.SkipTestHelperProcess() {
1214
return
1315
}
14-
outputs := map[string]test.ExecStub{
15-
"no changes": test.ExecStub{"", 0},
16-
"one change": test.ExecStub{` M poem.txt
17-
`, 0},
18-
"untracked file": test.ExecStub{` M poem.txt
19-
?? new.txt
20-
`, 0},
21-
"boom": test.ExecStub{"", 1},
16+
17+
args := test.GetTestHelperProcessArgs()
18+
switch args[0] {
19+
case "no changes":
20+
case "one change":
21+
fmt.Println(" M poem.txt")
22+
case "untracked file":
23+
fmt.Println(" M poem.txt")
24+
fmt.Println("?? new.txt")
25+
case "boom":
26+
os.Exit(1)
2227
}
23-
output := test.GetExecStub(outputs)
24-
defer os.Exit(output.ExitCode)
25-
fmt.Println(output.Stdout)
28+
os.Exit(0)
2629
}
2730

2831
func Test_UncommittedChangeCount(t *testing.T) {
@@ -48,7 +51,7 @@ func Test_UncommittedChangeCount(t *testing.T) {
4851

4952
GitCommand = test.StubExecCommand("TestGitStatusHelperProcess", "boom")
5053
_, err := UncommittedChangeCount()
51-
if err.Error() != "failed to run git status: exit status 1" {
54+
if !strings.HasSuffix(err.Error(), "git.test: exit status 1") {
5255
t.Errorf("got unexpected error message: %s", err)
5356
}
5457
}

test/helpers.go

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

14-
type ExecStub struct {
15-
Stdout string
16-
ExitCode int
17-
}
18-
1914
func GetTestHelperProcessArgs() []string {
2015
args := os.Args
2116
for len(args) > 0 {
@@ -28,11 +23,6 @@ func GetTestHelperProcessArgs() []string {
2823
return args
2924
}
3025

31-
func GetExecStub(outputs map[string]ExecStub) ExecStub {
32-
args := GetTestHelperProcessArgs()
33-
return outputs[args[0]]
34-
}
35-
3626
func SkipTestHelperProcess() bool {
3727
return os.Getenv("GO_WANT_HELPER_PROCESS") != "1"
3828
}

0 commit comments

Comments
 (0)