gunk-dev/the-valley

pending_test.go

source89496f97a023main's published tip history
10.0 KiB
1package main
2
3import (
4 "bytes"
5 "fmt"
6 "os"
7 "os/exec"
8 "path/filepath"
9 "strings"
10 "testing"
11)
12
13// pending fits its text to 80 columns when stdout is no terminal: a long
14// title is cut short with …, and a branch or a handle is printed whole,
15// even where the line it is on cannot fit. A stack of branches four deep
16// is listed at one indentation, so its deepest entry has the room its
17// second has. Nothing it prints is an escape code.
18func TestPendingFitsEightyColumnsAndKeepsNamesWhole(t *testing.T) {
19 dir := t.TempDir()
20 t.Setenv("HOME", dir)
21 t.Setenv("XDG_CONFIG_HOME", filepath.Join(dir, "config"))
22 t.Setenv("GIT_CONFIG_NOSYSTEM", "1")
23 t.Setenv("VALLEY_KNOWN_SIGNERS", "")
24 origin, work := filepath.Join(dir, "origin.git"), filepath.Join(dir, "work")
25 sh := func(args ...string) {
26 t.Helper()
27 cmd := exec.Command("git", args...)
28 cmd.Dir = work
29 if out, err := cmd.CombinedOutput(); err != nil {
30 t.Fatalf("git %s: %v\n%s", strings.Join(args, " "), err, out)
31 }
32 }
33 if out, err := exec.Command("git", "init", "--quiet", "--bare", "--initial-branch=main", origin).CombinedOutput(); err != nil {
34 t.Fatalf("git init: %v\n%s", err, out)
35 }
36 if out, err := exec.Command("git", "clone", "--quiet", origin, work).CombinedOutput(); err != nil {
37 t.Fatalf("git clone: %v\n%s", err, out)
38 }
39 sh("config", "user.name", "a committer whose name is so long that the line it is on has to cut it short")
40 sh("config", "user.email", "committer@localhost")
41 sh("config", "valley.principal", "alice")
42 sh("checkout", "--quiet", "-b", "main")
43 sh("commit", "--quiet", "--allow-empty", "-m", "base")
44 sh("push", "--quiet", "origin", "main")
45
46 long := "a-branch-whose-name-runs-on-well-past-the-eightieth-column-of-any-terminal-it-is-shown-in"
47 title := "a subject that says far more than one line has room for, " + strings.Repeat("and then some more ", 4)
48 sh("checkout", "--quiet", "-b", long, "main")
49 sh("commit", "--quiet", "--allow-empty", "-m", title+"on the long branch")
50 sh("push", "--quiet", "origin", long)
51 sh("checkout", "--quiet", "-b", "short", "main")
52 sh("commit", "--quiet", "--allow-empty", "-m", title)
53 sh("push", "--quiet", "origin", "short")
54 from := "main"
55 for _, layer := range []string{"1", "2", "3", "4"} {
56 sh("checkout", "--quiet", "-b", "layer-"+layer, from)
57 sh("commit", "--quiet", "--allow-empty", "-m", "layer "+layer+": "+title)
58 sh("push", "--quiet", "origin", "layer-"+layer)
59 from = "layer-" + layer
60 }
61
62 t.Chdir(work)
63 var stdout, stderr bytes.Buffer
64 if code := run([]string{"ready", "--change", strings.Repeat("q", 32), "short"}, &stdout, &stderr, os.Exit); code != 0 {
65 t.Fatalf("ready: %s", stderr.String())
66 }
67 stdout.Reset()
68 if code := run([]string{"pending", "--branches"}, &stdout, &stderr, os.Exit); code != 0 {
69 t.Fatalf("pending: %s", stderr.String())
70 }
71 out := stdout.String()
72 if strings.Contains(out, "\x1b") {
73 t.Errorf("pending printed an escape code to no terminal:\n%s", out)
74 }
75 if !strings.Contains(out, "\n"+long+"\n") {
76 t.Errorf("the long branch is not whole, on a line of its own:\n%s", out)
77 }
78 if !strings.Contains(out, "\n 1 qqqq v1") {
79 t.Errorf("the change is not named by its whole handle and version:\n%s", out)
80 }
81 cut := 0
82 for _, line := range strings.Split(strings.TrimSuffix(out, "\n"), "\n") {
83 if strings.Contains(line, long) {
84 continue
85 }
86 if columns(line) > 80 {
87 t.Errorf("a line is %d columns wide: %q", columns(line), line)
88 }
89 if strings.HasPrefix(strings.TrimLeft(line, " "), "a subject that says") {
90 if columns(line) != 80 || !strings.HasSuffix(line, "…") {
91 t.Errorf("a long title is not cut to 80 columns with …: %q", line)
92 }
93 cut++
94 }
95 if strings.HasPrefix(line, "layer-4 ") && !strings.HasSuffix(line, "…") {
96 t.Errorf("the author is not cut short to keep the line within 80 columns: %q", line)
97 }
98 }
99 lines := strings.Split(out, "\n")
100 for layer, lead := range map[string]string{"4": " ", "3": " │ ", "2": " │ ", "1": " "} {
101 listed := false
102 for _, line := range lines {
103 if strings.HasPrefix(line, lead+"layer "+layer+": ") {
104 listed = true
105 if columns(line) != 80 || !strings.HasSuffix(line, "…") {
106 t.Errorf("layer %s's title is not cut to 80 columns with …: %q", layer, line)
107 }
108 }
109 }
110 if !listed {
111 t.Errorf("layer %s's title is not at %q:\n%s", layer, lead, out)
112 }
113 }
114 if !strings.Contains(out, "\n approving it lands the 3 below too\n") || strings.Contains(out, "on layer-") {
115 t.Errorf("the stack four deep is not one list, each layer on the next:\n%s", out)
116 }
117 if cut != 2 {
118 t.Errorf("%d titles were cut short, where the change's and the branch's should be:\n%s", cut, out)
119 }
120}
121
122// fit cuts by columns, not bytes: a wide character takes two, and a
123// combining mark none.
124func TestFitCountsColumns(t *testing.T) {
125 for _, c := range []struct {
126 s string
127 n int
128 want string
129 }{
130 {"short", 10, "short"},
131 {"exactly ten", 11, "exactly ten"},
132 {"one word too many", 12, "one word to…"},
133 {"漢字漢字漢字", 7, "漢字漢…"},
134 {"café au lait", 5, "café au lait"[:6] + "…"},
135 } {
136 if got := fit(c.s, c.n); got != c.want || columns(got) > c.n {
137 t.Errorf("fit(%q, %d) = %q, %d columns; want %q", c.s, c.n, got, columns(got), c.want)
138 }
139 }
140}
141
142// pending reads the policy at main's tip once for a project, and asks cue
143// no requirement's pin: the cue processes it starts, with --checks or
144// without, are as many for a class requiring nine checks and four ready
145// heads as for one requiring three and one head.
146func TestPendingStartsCueOncePerProject(t *testing.T) {
147 cue, err := exec.LookPath("cue")
148 if err != nil {
149 if os.Getenv("VALLEY_TEST_TOOLS") == "required" {
150 t.Fatal("no cue on PATH, and VALLEY_TEST_TOOLS=required")
151 }
152 t.Skip("no cue to compose a policy with; the nix check provides one")
153 }
154 schema, err := filepath.Abs(filepath.Join("..", "schema", "verification.cue"))
155 if err != nil {
156 t.Fatal(err)
157 }
158 dir := t.TempDir()
159 t.Setenv("HOME", dir)
160 t.Setenv("XDG_CONFIG_HOME", filepath.Join(dir, "config"))
161 t.Setenv("GIT_CONFIG_NOSYSTEM", "1")
162 t.Setenv("VALLEY_KNOWN_SIGNERS", "")
163 t.Setenv("VALLEY_VERIFICATION_SCHEMA", schema)
164 // cue on PATH logs each start and runs the real cue.
165 bin, starts := filepath.Join(dir, "bin"), filepath.Join(dir, "cue-starts")
166 shim := fmt.Sprintf("#!/bin/sh\necho cue >> '%s'\nexec '%s' \"$@\"\n", starts, cue)
167 if err := os.MkdirAll(bin, 0o755); err != nil {
168 t.Fatal(err)
169 }
170 if err := os.WriteFile(filepath.Join(bin, "cue"), []byte(shim), 0o755); err != nil {
171 t.Fatal(err)
172 }
173 t.Setenv("PATH", bin+string(os.PathListSeparator)+os.Getenv("PATH"))
174
175 // project is a repository with its own origin, whose floor has one
176 // class requiring checks c-1 to c-<checks>, and heads ready changes
177 // touching that class. It returns what each listing printed and how
178 // many cue processes it started.
179 project := func(checks, heads int) (printed map[string]string, started map[string]int) {
180 t.Helper()
181 root := filepath.Join(dir, fmt.Sprintf("%d-%d", checks, heads))
182 origin, work := filepath.Join(root, "origin.git"), filepath.Join(root, "work")
183 sh := func(args ...string) {
184 t.Helper()
185 cmd := exec.Command("git", args...)
186 cmd.Dir = work
187 if out, err := cmd.CombinedOutput(); err != nil {
188 t.Fatalf("git %s: %v\n%s", strings.Join(args, " "), err, out)
189 }
190 }
191 if out, err := exec.Command("git", "init", "--quiet", "--bare", "--initial-branch=main", origin).CombinedOutput(); err != nil {
192 t.Fatalf("git init: %v\n%s", err, out)
193 }
194 if out, err := exec.Command("git", "clone", "--quiet", origin, work).CombinedOutput(); err != nil {
195 t.Fatalf("git clone: %v\n%s", err, out)
196 }
197 sh("config", "user.name", "operator")
198 sh("config", "user.email", "operator@example.org")
199 sh("config", "valley.principal", "operator")
200 sh("checkout", "--quiet", "-b", "main")
201 var floor strings.Builder
202 floor.WriteString("package verification\nfloor: {\n")
203 for i := 1; i <= checks; i++ {
204 fmt.Fprintf(&floor, "\tchecks: \"c-%d\": {runner: \"nix\", attribute: \"c-%d\"}\n", i, i)
205 fmt.Fprintf(&floor, "\tclasses: src: {paths: \"src/**\": true, requires: \"c-%d\": true}\n", i)
206 }
207 floor.WriteString("\tunclassified: \"c-1\": true\n}\n")
208 if err := os.MkdirAll(filepath.Join(work, "policy", "instance"), 0o755); err != nil {
209 t.Fatal(err)
210 }
211 if err := os.WriteFile(filepath.Join(work, "policy", "instance", "floor.cue"), []byte(floor.String()), 0o644); err != nil {
212 t.Fatal(err)
213 }
214 sh("add", ".")
215 sh("commit", "--quiet", "-m", "base")
216 sh("push", "--quiet", "origin", "main")
217 t.Chdir(work)
218 var stdout, stderr bytes.Buffer
219 for i := 1; i <= heads; i++ {
220 branch := fmt.Sprintf("head-%d", i)
221 sh("checkout", "--quiet", "-b", branch, "main")
222 if err := os.MkdirAll(filepath.Join(work, "src"), 0o755); err != nil {
223 t.Fatal(err)
224 }
225 if err := os.WriteFile(filepath.Join(work, "src", branch), []byte(branch+"\n"), 0o644); err != nil {
226 t.Fatal(err)
227 }
228 sh("add", ".")
229 sh("commit", "--quiet", "-m", branch)
230 sh("push", "--quiet", "origin", branch)
231 id := strings.Repeat(string(rune('p'+i)), 32)
232 if code := run([]string{"ready", "--change", id, branch}, &stdout, &stderr, os.Exit); code != 0 {
233 t.Fatalf("ready: %s", stderr.String())
234 }
235 }
236 printed, started = map[string]string{}, map[string]int{}
237 for _, args := range [][]string{{"pending"}, {"pending", "--checks"}} {
238 listing := strings.Join(args, " ")
239 os.Remove(starts)
240 stdout.Reset()
241 stderr.Reset()
242 if code := run(args, &stdout, &stderr, os.Exit); code != 0 {
243 t.Fatalf("%s: %s", listing, stderr.String())
244 }
245 log, _ := os.ReadFile(starts)
246 printed[listing], started[listing] = stdout.String(), strings.Count(string(log), "cue\n")
247 }
248 return printed, started
249 }
250
251 _, small := project(3, 1)
252 out, large := project(9, 4)
253 if want := "requires 9 quick checks, 0 slow"; strings.Count(out["pending --checks"], want) != 4 {
254 t.Fatalf("pending --checks does not say %q of each of four heads:\n%s", want, out["pending --checks"])
255 }
256 for listing, n := range small {
257 if n == 0 || large[listing] != n {
258 t.Errorf("%s started %d cue processes for three checks and one head, and %d for nine and four heads", listing, n, large[listing])
259 }
260 }
261}