| 1 | package main |
| 2 | |
| 3 | import ( |
| 4 | "bytes" |
| 5 | "os/exec" |
| 6 | "regexp" |
| 7 | "strings" |
| 8 | "testing" |
| 9 | ) |
| 10 | |
| 11 | var sgr = regexp.MustCompile("\x1b\\[[0-9;]*m") |
| 12 | |
| 13 | func TestVerdictLines(t *testing.T) { |
| 14 | cases := []struct { |
| 15 | name string |
| 16 | v verdict |
| 17 | want string |
| 18 | glyph string // the glyph as a terminal shows it |
| 19 | }{ |
| 20 | {"every check covered", |
| 21 | verdict{outcome: "approved", name: "nqyw", version: "3", required: 4, passed: 4}, |
| 22 | "✓ nqyw v3 approved · checks passed · landing on main\n", "\x1b[32m✓"}, |
| 23 | {"checks built here", |
| 24 | verdict{outcome: "approved", name: "nqyw", version: "3", required: 4, passed: 4, took: "42s"}, |
| 25 | "✓ nqyw v3 approved · 4 checks passed in 42s · landing on main\n", "\x1b[32m✓"}, |
| 26 | {"no check required", |
| 27 | verdict{outcome: "approved", name: "topic/quiet", required: 0, passed: 0}, |
| 28 | "✓ topic/quiet approved · no checks required · landing on main\n", "\x1b[32m✓"}, |
| 29 | {"a worker builds the rest", |
| 30 | verdict{outcome: "approved", name: "nqyw", version: "3", required: 4, passed: 1, worker: true, waiting: []string{"a", "b", "c"}}, |
| 31 | "◐ nqyw v3 approved · lands when 3 checks pass (1/4 passed)\n", "\x1b[33m◐"}, |
| 32 | {"counts not read", |
| 33 | verdict{outcome: "approved", name: "nqyw", version: "3", required: -1, passed: -1, worker: true, waiting: []string{"a", "b", "c"}}, |
| 34 | "◐ nqyw v3 approved · waiting for 3 checks\n", "\x1b[33m◐"}, |
| 35 | {"nothing builds the rest", |
| 36 | verdict{outcome: "approved", name: "nqyw", version: "3", required: 2, passed: 0, waiting: []string{"a", "b"}, run: "valley approve --build-here nqyw"}, |
| 37 | "◇ nqyw v3 approved · waiting for 2 checks (no worker building)\n run: valley approve --build-here nqyw\n", "\x1b[36m◇"}, |
| 38 | {"state unknown", |
| 39 | verdict{outcome: "unknown", name: "nqyw", version: "3", required: -1, passed: -1}, |
| 40 | "? nqyw v3 approved · state unknown · see: valley status nqyw\n", "?"}, |
| 41 | {"a check failed", |
| 42 | verdict{outcome: "refused", name: "nqyw", version: "3", required: 4, passed: -1, failed: []string{"valley-request"}}, |
| 43 | "⊘ nqyw v3 not approved · 1 of 4 checks failed\n ✗ valley-request — see: valley status nqyw\n", "\x1b[31m⊘"}, |
| 44 | {"a draft", |
| 45 | verdict{outcome: "refused", name: "nqyw", version: "3", required: -1, passed: -1, why: "change is a draft", run: "valley ready nqyw"}, |
| 46 | "⊘ nqyw v3 not approved · change is a draft\n run: valley ready nqyw\n", "\x1b[31m⊘"}, |
| 47 | {"a stale stack", |
| 48 | verdict{outcome: "refused", name: "nqyw", version: "3", required: -1, passed: -1, why: "stack is behind main (includes kpxm v2)", run: "valley rebase nqyw"}, |
| 49 | "⊘ nqyw v3 not approved · stack is behind main (includes kpxm v2)\n run: valley rebase nqyw\n", "\x1b[31m⊘"}, |
| 50 | } |
| 51 | for _, c := range cases { |
| 52 | c.v.target = "main" |
| 53 | if got := c.v.lines(terminal{width: 80}); got != c.want { |
| 54 | t.Errorf("%s, plain:\n got %q\nwant %q", c.name, got, c.want) |
| 55 | } |
| 56 | shown := c.v.lines(terminal{width: 80, bold: true, live: true}) |
| 57 | if plain := sgr.ReplaceAllString(shown, ""); plain != c.want { |
| 58 | t.Errorf("%s, on a terminal, without its styling:\n got %q\nwant %q", c.name, plain, c.want) |
| 59 | } |
| 60 | if !strings.HasPrefix(shown, c.glyph) || !strings.Contains(shown, "\x1b[1m"+c.v.label()+"\x1b[0m") { |
| 61 | t.Errorf("%s, on a terminal: want the glyph as %q and the name in bold, got %q", c.name, c.glyph, shown) |
| 62 | } |
| 63 | } |
| 64 | } |
| 65 | |
| 66 | func TestVerdictPrompt(t *testing.T) { |
| 67 | header := func() []string { return []string{"nqyw v3", "checks: 4 passed", "behind main (integrator merges)"} } |
| 68 | keys := []string{"[a]pprove", "[v]iew diff", "[r]eject", "[s]kip"} |
| 69 | want := "nqyw v3 · checks: 4 passed · behind main (integrator merges)\n[a]pprove [v]iew diff [r]eject [s]kip: " |
| 70 | if got := promptLines(terminal{width: 80}, header(), keys); got != want { |
| 71 | t.Errorf("plain:\n got %q\nwant %q", got, want) |
| 72 | } |
| 73 | shown := promptLines(terminal{width: 80, bold: true}, header(), keys) |
| 74 | if sgr.ReplaceAllString(shown, "") != want || !strings.Contains(shown, "[\x1b[1ma\x1b[0m]pprove") { |
| 75 | t.Errorf("on a terminal: got %q", shown) |
| 76 | } |
| 77 | } |
| 78 | |
| 79 | // checking keeps a passing run's output back, and shows the failed check's |
| 80 | // log and the refusal when one fails. |
| 81 | func TestVerdictChecking(t *testing.T) { |
| 82 | v := verdict{name: "nqyw", version: "3", required: 3, passed: 1, target: "main"} |
| 83 | var out, errs bytes.Buffer |
| 84 | pass := exec.Command("sh", "-c", `echo "check a nix passed"; echo "check b nix passed"; echo "stored refs/x -> y"`) |
| 85 | if status := v.checking([]string{"a", "b"}, pass, false, &out, &errs, nil, nil); status != 0 { |
| 86 | t.Fatalf("a passing run exited %d", status) |
| 87 | } |
| 88 | if out.String() != "◐ nqyw v3 checking 2 checks: a, b\n" || errs.Len() != 0 { |
| 89 | t.Errorf("a passing run printed %q and %q", out.String(), errs.String()) |
| 90 | } |
| 91 | |
| 92 | out.Reset() |
| 93 | errs.Reset() |
| 94 | fail := exec.Command("sh", "-c", `echo "check a nix passed"; echo "check b nix failed"; echo; echo "--- b ---"; echo "b: build log"; echo boom; exit 1`) |
| 95 | if status := v.checking([]string{"a", "b"}, fail, false, &out, &errs, nil, nil); status != 1 { |
| 96 | t.Fatalf("a failing run exited %d", status) |
| 97 | } |
| 98 | got := errs.String() |
| 99 | for _, want := range []string{"--- b ---\nb: build log\nboom\n", "⊘ nqyw v3 not approved · 1 of 3 checks failed\n ✗ b — see: valley status nqyw\n"} { |
| 100 | if !strings.Contains(got, want) { |
| 101 | t.Errorf("a failing run's stderr lacks %q:\n%s", want, got) |
| 102 | } |
| 103 | } |
| 104 | if strings.Contains(got, "check a") { |
| 105 | t.Errorf("a failing run showed the output of a check that passed:\n%s", got) |
| 106 | } |
| 107 | } |