gunk-dev/the-valley

flow_test.go

source6a6791929c36main's published tip history
6.3 KiB
1package main
2
3import (
4 "encoding/json"
5 "errors"
6 "io"
7 "os"
8 "os/exec"
9 "path/filepath"
10 "slices"
11 "strconv"
12 "strings"
13 "testing"
14)
15
16// flowRun is what the stand-in valley-flow (fakeFlow) saw.
17type flowRun struct {
18 Argv []string `json:"argv"`
19 Stdin string `json:"stdin"`
20 Env map[string]string `json:"env"`
21}
22
23// flowEnvNames are the variables fakeFlow reports, when set.
24var flowEnvNames = []string{"VALLEY_TEST_CALLER", "VALLEY_VERSION", "GIT_NO_REPLACE_OBJECTS", "GIT_REPLACE_REF_BASE"}
25
26// fakeFlow stands in for valley-flow when this test binary runs under that
27// name (TestMain): it prints its argv, its stdin and the variables of
28// flowEnvNames it was given, and exits with $VALLEY_TEST_FLOW_STATUS.
29func fakeFlow() int {
30 stdin, _ := io.ReadAll(os.Stdin)
31 seen := flowRun{Argv: os.Args, Stdin: string(stdin), Env: map[string]string{}}
32 for _, name := range flowEnvNames {
33 if v, ok := os.LookupEnv(name); ok {
34 seen.Env[name] = v
35 }
36 }
37 json.NewEncoder(os.Stdout).Encode(seen)
38 status, _ := strconv.Atoi(os.Getenv("VALLEY_TEST_FLOW_STATUS"))
39 return status
40}
41
42// valleyAsMain runs this test binary as valley's main (TestMain), in a
43// directory of its own outside any repository, with PATH holding bin
44// alone unless extra sets it, and extra added to the environment, and
45// returns its stdout, stderr and exit status.
46func valleyAsMain(t *testing.T, bin string, stdin string, extra []string, args ...string) (string, string, int) {
47 t.Helper()
48 return valleyAsMainIn(t, t.TempDir(), bin, stdin, extra, args...)
49}
50
51// valleyAsMainIn is valleyAsMain in dir.
52func valleyAsMainIn(t *testing.T, dir, bin string, stdin string, extra []string, args ...string) (string, string, int) {
53 t.Helper()
54 self, err := os.Executable()
55 if err != nil {
56 t.Fatal(err)
57 }
58 cmd := exec.Command(self, args...)
59 cmd.Dir = dir
60 for _, kv := range os.Environ() {
61 if name, _, _ := strings.Cut(kv, "="); !slices.Contains(flowEnvNames, name) {
62 cmd.Env = append(cmd.Env, kv)
63 }
64 }
65 cmd.Env = append(cmd.Env, "VALLEY_TEST_AS_MAIN=1", "PATH="+bin,
66 "GIT_CEILING_DIRECTORIES="+filepath.Dir(dir), "HOME="+dir)
67 cmd.Env = append(cmd.Env, extra...)
68 // TestMain reads one byte before it runs main.
69 cmd.Stdin = strings.NewReader("\x00" + stdin)
70 var stdout, stderr strings.Builder
71 cmd.Stdout, cmd.Stderr = &stdout, &stderr
72 err = cmd.Run()
73 status := 0
74 if ee := (*exec.ExitError)(nil); errors.As(err, &ee) {
75 status = ee.ExitCode()
76 } else if err != nil {
77 t.Fatal(err)
78 }
79 return stdout.String(), stderr.String(), status
80}
81
82// flowBin is a PATH directory whose valley-flow is this test binary.
83func flowBin(t *testing.T) string {
84 t.Helper()
85 self, err := os.Executable()
86 if err != nil {
87 t.Fatal(err)
88 }
89 bin := t.TempDir()
90 if err := os.Symlink(self, filepath.Join(bin, flowHelper)); err != nil {
91 t.Fatal(err)
92 }
93 return bin
94}
95
96// valley flow runs valley-flow from PATH, outside any checkout, with every
97// argument after flow, help and version flags included, its stdin and the
98// environment valley was started in less the stamp, and exits with its
99// status.
100func TestFlowRunsTheHelper(t *testing.T) {
101 bin := flowBin(t)
102 for _, args := range [][]string{
103 {},
104 {"--help"},
105 {"-h"},
106 {"--version"},
107 {"launch", "--help"},
108 {"launch", "--repo", "app", "--", "fix the build", "-h"},
109 } {
110 out, errs, status := valleyAsMain(t, bin, "", nil, append([]string{"flow"}, args...)...)
111 var seen flowRun
112 if err := json.Unmarshal([]byte(out), &seen); err != nil || status != 0 || errs != "" {
113 t.Fatalf("valley flow %v: exit %d, stdout %q, stderr %q", args, status, out, errs)
114 }
115 if want := append([]string{flowHelper}, args...); !slices.Equal(seen.Argv, want) {
116 t.Errorf("valley flow %v: the helper's argv is %q, want %q", args, seen.Argv, want)
117 }
118 }
119
120 // Its exit status, stdin and environment.
121 out, errs, status := valleyAsMain(t, bin, "the prompt\n", []string{
122 "VALLEY_TEST_FLOW_STATUS=7", "VALLEY_TEST_CALLER=kept", "VALLEY_VERSION=0.1.0-0000000",
123 "GIT_REPLACE_REF_BASE=refs/elsewhere/",
124 }, "flow", "status")
125 if status != 7 || errs != "" {
126 t.Fatalf("a helper exiting 7: exit %d, stderr %q", status, errs)
127 }
128 var seen flowRun
129 if err := json.Unmarshal([]byte(out), &seen); err != nil {
130 t.Fatalf("stdout %q: %v", out, err)
131 }
132 if seen.Stdin != "the prompt\n" {
133 t.Errorf("the helper read %q on stdin", seen.Stdin)
134 }
135 want := map[string]string{"VALLEY_TEST_CALLER": "kept", "GIT_REPLACE_REF_BASE": "refs/elsewhere/"}
136 if len(seen.Env) != len(want) || seen.Env["VALLEY_TEST_CALLER"] != "kept" || seen.Env["GIT_REPLACE_REF_BASE"] != "refs/elsewhere/" {
137 t.Errorf("the helper's environment has %v, want %v: the caller's, less the stamp and with git's as given", seen.Env, want)
138 }
139
140 // A valley-flow in a relative PATH entry, ahead of bin, is passed
141 // over, even where Go would run it.
142 dir := t.TempDir()
143 if err := os.Mkdir(filepath.Join(dir, "rel"), 0o755); err != nil {
144 t.Fatal(err)
145 }
146 if err := os.WriteFile(filepath.Join(dir, "rel", flowHelper), []byte("not a program\n"), 0o755); err != nil {
147 t.Fatal(err)
148 }
149 out, errs, status = valleyAsMainIn(t, dir, bin, "", []string{"PATH=rel:" + bin, "GODEBUG=execerrdot=0"}, "flow", "--version")
150 if err := json.Unmarshal([]byte(out), &seen); err != nil || status != 0 || errs != "" {
151 t.Errorf("a valley-flow in a relative PATH entry ahead of bin: exit %d, stdout %q, stderr %q", status, out, errs)
152 }
153}
154
155// Without valley-flow on PATH, flow says so in one line and exits 127,
156// whatever its arguments, help flags included. A valley-flow that cannot
157// be executed exits 126.
158func TestFlowWithoutHelper(t *testing.T) {
159 empty := t.TempDir()
160 for _, args := range [][]string{{"flow"}, {"flow", "--help"}, {"flow", "launch", "-h"}} {
161 t.Setenv("PATH", empty)
162 status, out, errs := command(t, args...)
163 if status != 127 || out != "" || errs != "valley: flow runs valley-flow, which is not on PATH\n" {
164 t.Errorf("valley %s: %d %q %q", strings.Join(args, " "), status, out, errs)
165 }
166 }
167
168 broken := t.TempDir()
169 helper := filepath.Join(broken, flowHelper)
170 if err := os.WriteFile(helper, []byte("not a program\n"), 0o755); err != nil {
171 t.Fatal(err)
172 }
173 out, errs, status := valleyAsMain(t, broken, "", nil, "flow", "--version")
174 if status != 126 || out != "" || !strings.HasPrefix(errs, "valley: "+helper+": ") || strings.Count(errs, "\n") != 1 {
175 t.Errorf("an unexecutable valley-flow: exit %d, stdout %q, stderr %q", status, out, errs)
176 }
177}