-
Notifications
You must be signed in to change notification settings - Fork 0
/
testhelper_test.go
53 lines (45 loc) · 1002 Bytes
/
testhelper_test.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
package main
import (
"fmt"
"os"
"os/exec"
"testing"
)
type TestRunner struct {
fixture string
}
func (r TestRunner) Run(command string, args ...string) ([]byte, error) {
if r.fixture == "" {
r.fixture = "default"
}
cs := []string{"-test.run=TestHelperProcess", "--", r.fixture, command}
cs = append(cs, args...)
cmd := exec.Command(os.Args[0], cs...)
cmd.Env = []string{"GO_WANT_HELPER_PROCESS=1"}
out, err := cmd.CombinedOutput()
return out, err
}
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] == "--" {
args = args[1:]
break
}
args = args[1:]
}
if len(args) == 0 {
fmt.Fprintf(os.Stderr, "No command\n")
os.Exit(2)
}
fixture, cmd, args := args[0], args[1], args[2:]
if _, found := outputFixture[cmd]; !found {
fmt.Fprintf(os.Stderr, "Unknown command %q\n", cmd)
os.Exit(2)
}
fmt.Fprintln(os.Stdout, outputFixture[cmd][fixture])
}