gunk-dev/the-valley

status_test.go

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