gunk-dev/the-valley

status_test.go

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