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