gunk-dev/the-valley

status_test.go

source71e69b2e4721main's published tip history
5.0 KiB
1package main
2
3import (
4 "bytes"
5 "os"
6 "os/exec"
7 "strings"
8 "testing"
9)
10
11const (
12 sha1a = "a1b2c3d4e5f60718293a4b5c6d7e8f9012345678"
13 sha1b = "2222222222222222222222222222222222222222"
14 sha256a = "3333333333333333333333333333333333333333333333333333333333333333"
15 fp = "0123456789abcdef"
16)
17
18// An outcome record is read as a decision only when every field is the
19// shape the integrator writes. Everything else is unknown, and none of it
20// may read as a landing.
21func TestMemoAcceptsOnlyTheRecordsTheIntegratorWrites(t *testing.T) {
22 for _, c := range []struct {
23 record, decision, failedAt string
24 }{
25 {sha1a + " " + sha1b + " " + fp + " land\n", "land", ""},
26 {sha1a + " " + sha1b + " " + fp + " stale", "stale", ""},
27 {sha1a + " " + sha1b + " " + fp + " reject\n\n\n", "reject", ""},
28 {sha1a + " " + sha1b + " " + fp + " failed 2026-09-21T12:00:00Z\n", "failed", "2026-09-21T12:00:00Z"},
29 {sha1a + " " + sha1b + " " + fp + " refused-not-ready\n", "refused-not-ready", ""},
30 {sha1a + " " + sha1b + " " + fp + " refused-merge\n", "refused-merge", ""},
31 {sha256a + " " + sha256a + " " + fp + " land\n", "land", ""},
32 {" " + sha1a + "\t" + sha1b + " " + fp + "\tland \n", "land", ""},
33
34 {"", "unknown", ""},
35 {"not an outcome\n", "unknown", ""},
36 {sha1a + " " + sha1b + " " + fp + "\n", "unknown", ""},
37 {sha1a + " " + sha1b + " " + fp + " merged\n", "unknown", ""},
38 {sha1a + " " + sha1b + " " + fp + " land 2026-09-21T12:00:00Z\n", "unknown", ""},
39 {sha1a + " " + sha1b + " " + fp + " refused-not-ready 2026-09-21T12:00:00Z\n", "unknown", ""},
40 {sha1a + " " + sha1b + " " + fp + " refused\n", "unknown", ""},
41 {sha1a + " " + sha1b + " " + fp + " failed\n", "unknown", ""},
42 {sha1a + " " + sha1b + " " + fp + " failed 2026-09-21T12:00:00Z extra\n", "unknown", ""},
43 {sha1a + " " + sha1b + " " + fp + " land\n" + sha1a + "\n", "unknown", ""},
44 {sha1a + " " + sha1b + " " + fp + " land\x00\n", "unknown", ""},
45 {strings.ToUpper(sha1a) + " " + sha1b + " " + fp + " land\n", "unknown", ""},
46 {sha1a[:39] + " " + sha1b + " " + fp + " land\n", "unknown", ""},
47 {sha1a + "1 " + sha1b + " " + fp + " land\n", "unknown", ""},
48 {sha1a + " HEAD " + fp + " land\n", "unknown", ""},
49 {sha1a + " " + sha1b + " " + fp[:15] + " land\n", "unknown", ""},
50 {sha1a + " " + sha1b + " " + fp + "0 land\n", "unknown", ""},
51 {sha1a + " " + sha1b + " " + fp + " land\r\n", "unknown", ""},
52 } {
53 got := parseMemo(c.record)
54 if got.decision != c.decision || got.failedAt != c.failedAt {
55 t.Errorf("parseMemo(%q) = %q, %q; want %q, %q",
56 c.record, got.decision, got.failedAt, c.decision, c.failedAt)
57 }
58 }
59}
60
61// The commands status suggests are pasted back into a shell, and before
62// this program they were printed by bash's printf %q. Every printable
63// ASCII character is held to what bash itself prints, alone, inside a
64// word and opening one, and so are the control characters a shell names.
65func TestShellQuoteMatchesBash(t *testing.T) {
66 words := []string{
67 "topic", "topic/one", `odd/"q"&<x>`, "#hash", "a#b", "~home", "a:~b", "a=~b", "a~b",
68 "a b", "tab\there", "nl\nx", "\x7f", "\x1b[0m", "\a\b\f\v\r",
69 }
70 for c := byte(' '); c < 0x7f; c++ {
71 words = append(words, string(c), "a"+string(c)+"b", string(c)+"z")
72 }
73 compareWithBash(t, "C", words)
74
75 // Beyond ASCII, bash's answer depends on the locale. Compare against a
76 // UTF-8 one where this machine has it, and skip where it does not.
77 if bashQuote(t, "C.UTF-8", []string{"é"})[0] != "é" {
78 t.Skip("no UTF-8 locale for bash here; the non-ASCII cases are not compared")
79 }
80 compareWithBash(t, "C.UTF-8", []string{
81 "café", "топик/один", "日本 語", "bad\xffbyte", "\xc3", "a\u0085b", "mixed'é\"",
82 })
83}
84
85func compareWithBash(t *testing.T, locale string, words []string) {
86 t.Helper()
87 want := bashQuote(t, locale, words)
88 for i, w := range words {
89 if got := shellQuote(w); got != want[i] {
90 t.Errorf("shellQuote(%q) = %s; bash says %s", w, got, want[i])
91 }
92 }
93}
94
95func bashQuote(t *testing.T, locale string, words []string) []string {
96 t.Helper()
97 bash, err := exec.LookPath("bash")
98 if err != nil {
99 t.Skip("no bash to compare with")
100 }
101 cmd := exec.Command(bash, append([]string{"-c", `for w; do printf '%q\0' "$w"; done`, "bash"}, words...)...)
102 cmd.Env = append(os.Environ(), "LC_ALL="+locale)
103 out, err := cmd.Output()
104 if err != nil {
105 t.Fatalf("bash: %v", err)
106 }
107 got := strings.Split(strings.TrimSuffix(string(out), "\x00"), "\x00")
108 if len(got) != len(words) {
109 t.Fatalf("bash quoted %d words of %d", len(got), len(words))
110 }
111 return got
112}
113
114// A change name JSON cannot carry exactly is refused before origin is
115// read, so the JSON form never prints an object naming some other change.
116func TestJSONRefusesANameThatIsNotUTF8(t *testing.T) {
117 var stdout, stderr bytes.Buffer
118 if code := run([]string{"status", "--json", "topic/\xff"}, &stdout, &stderr); code == 0 {
119 t.Fatal("a change name that is not UTF-8 was accepted")
120 }
121 if stdout.Len() != 0 {
122 t.Errorf("a refusal printed %q", stdout.String())
123 }
124 if !strings.Contains(stderr.String(), "not UTF-8") {
125 t.Errorf("the refusal did not say why: %q", stderr.String())
126 }
127}