Simplify tests that use StubExecCommand

This commit is contained in:
Mislav Marohnić 2019-11-11 17:04:26 +01:00
parent 65054fdc6e
commit 6aa0c071d6
4 changed files with 30 additions and 34 deletions

View file

@ -18,20 +18,23 @@ func TestPrCreateHelperProcess(*testing.T) {
return
}
statusOutputs := map[string]string{
"clean": "",
"dirty": " M git/git.go",
}
args := test.GetTestHelperProcessArgs()
switch args[1] {
case "status":
fmt.Println(statusOutputs[args[0]])
switch args[0] {
case "clean":
case "dirty":
fmt.Println(" M git/git.go")
default:
fmt.Fprintf(os.Stderr, "unknown scenario: %q", args[0])
os.Exit(1)
}
case "push":
fmt.Println()
default:
fmt.Fprintf(os.Stderr, "unknown command: %q", args[1])
os.Exit(1)
}
defer os.Exit(0)
os.Exit(0)
}
func TestReportsUncommittedChanges(t *testing.T) {

View file

@ -212,9 +212,9 @@ var GitCommand = func(args ...string) *exec.Cmd {
func UncommittedChangeCount() (int, error) {
statusCmd := GitCommand("status", "--porcelain")
output, err := statusCmd.Output()
output, err := utils.PrepareCmd(statusCmd).Output()
if err != nil {
return 0, fmt.Errorf("failed to run git status: %s", err)
return 0, err
}
lines := strings.Split(string(output), "\n")

View file

@ -2,27 +2,30 @@ package git
import (
"fmt"
"github.com/github/gh-cli/test"
"os"
"strings"
"testing"
"github.com/github/gh-cli/test"
)
func TestGitStatusHelperProcess(*testing.T) {
if test.SkipTestHelperProcess() {
return
}
outputs := map[string]test.ExecStub{
"no changes": test.ExecStub{"", 0},
"one change": test.ExecStub{` M poem.txt
`, 0},
"untracked file": test.ExecStub{` M poem.txt
?? new.txt
`, 0},
"boom": test.ExecStub{"", 1},
args := test.GetTestHelperProcessArgs()
switch args[0] {
case "no changes":
case "one change":
fmt.Println(" M poem.txt")
case "untracked file":
fmt.Println(" M poem.txt")
fmt.Println("?? new.txt")
case "boom":
os.Exit(1)
}
output := test.GetExecStub(outputs)
defer os.Exit(output.ExitCode)
fmt.Println(output.Stdout)
os.Exit(0)
}
func Test_UncommittedChangeCount(t *testing.T) {
@ -48,7 +51,7 @@ func Test_UncommittedChangeCount(t *testing.T) {
GitCommand = test.StubExecCommand("TestGitStatusHelperProcess", "boom")
_, err := UncommittedChangeCount()
if err.Error() != "failed to run git status: exit status 1" {
if !strings.HasSuffix(err.Error(), "git.test: exit status 1") {
t.Errorf("got unexpected error message: %s", err)
}
}

View file

@ -11,11 +11,6 @@ import (
"github.com/spf13/cobra"
)
type ExecStub struct {
Stdout string
ExitCode int
}
func GetTestHelperProcessArgs() []string {
args := os.Args
for len(args) > 0 {
@ -28,11 +23,6 @@ func GetTestHelperProcessArgs() []string {
return args
}
func GetExecStub(outputs map[string]ExecStub) ExecStub {
args := GetTestHelperProcessArgs()
return outputs[args[0]]
}
func SkipTestHelperProcess() bool {
return os.Getenv("GO_WANT_HELPER_PROCESS") != "1"
}