first pass at generalizing process stubbing

This commit is contained in:
nate smith 2019-10-31 11:10:57 -05:00
parent ee0fe61b04
commit 7555aa9be3
2 changed files with 51 additions and 47 deletions

View file

@ -11,6 +11,43 @@ import (
"github.com/spf13/cobra"
)
type ExecStub struct {
Stdout string
ExitCode int
}
func GetExecStub(outputs map[string]ExecStub) ExecStub {
args := os.Args
for len(args) > 0 {
if args[0] == "--" {
args = args[1:]
break
}
args = args[1:]
}
return outputs[args[0]]
}
func SkipTestHelperProcess() bool {
return os.Getenv("GO_WANT_HELPER_PROCESS") != "1"
}
func StubExecCommand(testHelper string, desiredOutput string) func(...string) *exec.Cmd {
return func(args ...string) *exec.Cmd {
cs := []string{
fmt.Sprintf("-test.run=%s", testHelper),
"--", desiredOutput}
cs = append(cs, args...)
env := []string{
"GO_WANT_HELPER_PROCESS=1",
}
cmd := exec.Command(os.Args[0], cs...)
cmd.Env = append(env, os.Environ()...)
return cmd
}
}
type TempGitRepo struct {
Remote string
TearDown func()