gunk-dev/the-valley

detect_test.go

sourcec14b0e68e9c3main's published tip history
5.6 KiB
1package valley
2
3import (
4 "context"
5 "os"
6 "os/exec"
7 "path/filepath"
8 "strings"
9 "testing"
10)
11
12// newHelp is how a valley with change records lists its verbs.
13const newHelp = `valley — integrator verbs for a valley project
14
15usage: valley <verb> [args]
16
17 pending list branches on origin not merged into origin/main
18 ready --change <id> --head <sha> <branch>
19 mark a head ready as a version of a change
20 changes [--all] [--json]
21 list change records
22 approve <change> file an integration request
23`
24
25// oldHelp is a valley from before change records: no ready verb, and
26// "changes" only in prose.
27const oldHelp = `valley — S1 integrator verbs for a valley project
28
29usage: valley <verb> [args]
30
31 pending list branches on origin not merged into origin/main
32 review <branch> page through the branch's log and diff vs origin/main;
33 approving runs every required check and
34 changes nothing required
35 approve <change> file an integration request
36`
37
38// TestDetect runs detection against real git: a repo with a non-GitHub
39// origin whose commits do and don't carry the valley's policy, with a fake
40// valley on PATH.
41func TestDetect(t *testing.T) {
42 t.Setenv("GIT_CONFIG_GLOBAL", filepath.Join(t.TempDir(), "gitconfig"))
43 t.Setenv("GIT_CONFIG_NOSYSTEM", "1")
44 t.Setenv("GIT_AUTHOR_NAME", "test")
45 t.Setenv("GIT_AUTHOR_EMAIL", "test@example.com")
46 t.Setenv("GIT_COMMITTER_NAME", "test")
47 t.Setenv("GIT_COMMITTER_EMAIL", "test@example.com")
48
49 root := t.TempDir()
50 origin, repo := filepath.Join(root, "origin.git"), filepath.Join(root, "repo")
51 runGit(t, root, "init", "-q", "--bare", "-b", "main", origin)
52 runGit(t, root, "clone", "-q", origin, repo)
53 commit := func(path, content string) string {
54 t.Helper()
55 full := filepath.Join(repo, path)
56 if err := os.MkdirAll(filepath.Dir(full), 0o755); err != nil {
57 t.Fatal(err)
58 }
59 if err := os.WriteFile(full, []byte(content), 0o644); err != nil {
60 t.Fatal(err)
61 }
62 runGit(t, repo, "add", path)
63 runGit(t, repo, "commit", "-q", "-m", "add "+path)
64 return runGit(t, repo, "rev-parse", "HEAD")
65 }
66 plain := commit("README", "hello\n")
67 withValley := commit("policy/valley", "site\n")
68 runGit(t, repo, "rm", "-q", "policy/valley")
69 runGit(t, repo, "commit", "-q", "-m", "drop the policy")
70 withInstance := commit("policy/instance/host.cue", "host: \"x\"\n")
71 runGit(t, repo, "rm", "-q", "-r", "policy")
72 runGit(t, repo, "commit", "-q", "-m", "drop it again")
73 instanceFile := commit("policy/instance", "not a directory\n")
74
75 // A head pushed from elsewhere (a sandbox) that this clone hasn't
76 // fetched: detection fetches its branch.
77 other := filepath.Join(root, "other")
78 runGit(t, root, "clone", "-q", repo, other)
79 runGit(t, other, "remote", "set-url", "origin", origin)
80 runGit(t, repo, "push", "-q", "origin", "HEAD:main")
81 runGit(t, other, "fetch", "-q", "origin")
82 runGit(t, other, "checkout", "-q", "-b", "agent/sandbox", "origin/main")
83 if err := os.MkdirAll(filepath.Join(other, "policy"), 0o755); err != nil {
84 t.Fatal(err)
85 }
86 if err := os.WriteFile(filepath.Join(other, "policy", "valley"), []byte("site\n"), 0o644); err != nil {
87 t.Fatal(err)
88 }
89 runGit(t, other, "add", "policy")
90 runGit(t, other, "commit", "-q", "-m", "from the sandbox")
91 runGit(t, other, "push", "-q", "origin", "agent/sandbox")
92 sandboxHead := runGit(t, other, "rev-parse", "HEAD")
93
94 bin := t.TempDir()
95 t.Setenv("PATH", bin+string(os.PathListSeparator)+os.Getenv("PATH"))
96 fakeValley := func(help string) {
97 t.Helper()
98 script := "#!/bin/sh\ncat <<'EOF'\n" + help + "EOF\n"
99 if err := os.WriteFile(filepath.Join(bin, "valley"), []byte(script), 0o755); err != nil {
100 t.Fatal(err)
101 }
102 }
103
104 ctx := context.Background()
105 fakeValley(newHelp)
106 for _, tc := range []struct {
107 name string
108 branch string
109 head string
110 want bool
111 }{
112 {"no policy", "main", plain, false},
113 {"policy/valley", "main", withValley, true},
114 {"policy/instance/", "main", withInstance, true},
115 {"policy/instance is a file", "main", instanceFile, false},
116 {"head origin lacks too", "main", "0123456789abcdef0123456789abcdef01234567", false},
117 {"head pushed from a sandbox", "agent/sandbox", sandboxHead, true},
118 } {
119 if got := Detect(ctx, repo, tc.branch, tc.head); got != tc.want {
120 t.Errorf("%s: Detect = %v, want %v", tc.name, got, tc.want)
121 }
122 }
123
124 // A valley from before change records lacks the verbs: not a valley.
125 fakeValley(oldHelp)
126 if Detect(ctx, repo, "main", withValley) {
127 t.Error("a valley without ready and changes should not be detected")
128 }
129
130 // Returning a change to draft needs unready too, which a valley from
131 // before drafts lacks.
132 fakeValley(newHelp)
133 if DetectUnready(ctx, repo, withValley) {
134 t.Error("a valley without unready should not be detected for unready")
135 }
136 fakeValley(newHelp + " unready <change> return a ready change to draft\n")
137 if !DetectUnready(ctx, repo, withValley) {
138 t.Error("a valley that lists unready should be detected for unready")
139 }
140 if DetectUnready(ctx, repo, plain) {
141 t.Error("a head without the policy should not be detected for unready")
142 }
143
144 // A GitHub origin gets PRs, whatever the tree carries.
145 fakeValley(newHelp)
146 runGit(t, repo, "remote", "set-url", "origin", "git@github.com:acme/site.git")
147 if Detect(ctx, repo, "main", withValley) {
148 t.Error("a GitHub origin should not be detected as a valley")
149 }
150}
151
152func runGit(t *testing.T, dir string, args ...string) string {
153 t.Helper()
154 cmd := exec.Command("git", args...)
155 cmd.Dir = dir
156 out, err := cmd.CombinedOutput()
157 if err != nil {
158 t.Fatalf("git %s: %v\n%s", strings.Join(args, " "), err, out)
159 }
160 return strings.TrimSpace(string(out))
161}