gunk-dev/the-valley

pending_test.go

source5c91b2faf25bmain's published tip history
5.1 KiB
1package main
2
3import (
4 "bytes"
5 "os/exec"
6 "path/filepath"
7 "strings"
8 "testing"
9)
10
11// pending fits its text to 80 columns when stdout is no terminal: a long
12// title is cut short with …, and a branch or a handle is printed whole,
13// even where the line it is on cannot fit. A stack four deep is listed at
14// one indentation, so its deepest entry has the room its second has.
15// Nothing it prints is an escape code.
16func TestPendingFitsEightyColumnsAndKeepsNamesWhole(t *testing.T) {
17 dir := t.TempDir()
18 t.Setenv("HOME", dir)
19 t.Setenv("XDG_CONFIG_HOME", filepath.Join(dir, "config"))
20 t.Setenv("GIT_CONFIG_NOSYSTEM", "1")
21 t.Setenv("VALLEY_KNOWN_SIGNERS", "")
22 origin, work := filepath.Join(dir, "origin.git"), filepath.Join(dir, "work")
23 sh := func(args ...string) {
24 t.Helper()
25 cmd := exec.Command("git", args...)
26 cmd.Dir = work
27 if out, err := cmd.CombinedOutput(); err != nil {
28 t.Fatalf("git %s: %v\n%s", strings.Join(args, " "), err, out)
29 }
30 }
31 if out, err := exec.Command("git", "init", "--quiet", "--bare", "--initial-branch=main", origin).CombinedOutput(); err != nil {
32 t.Fatalf("git init: %v\n%s", err, out)
33 }
34 if out, err := exec.Command("git", "clone", "--quiet", origin, work).CombinedOutput(); err != nil {
35 t.Fatalf("git clone: %v\n%s", err, out)
36 }
37 sh("config", "user.name", "a committer whose name is so long that the line it is on has to cut it short")
38 sh("config", "user.email", "committer@localhost")
39 sh("config", "valley.principal", "klaus-env")
40 sh("checkout", "--quiet", "-b", "main")
41 sh("commit", "--quiet", "--allow-empty", "-m", "base")
42 sh("push", "--quiet", "origin", "main")
43
44 long := "a-branch-whose-name-runs-on-well-past-the-eightieth-column-of-any-terminal-it-is-shown-in"
45 title := "a subject that says far more than one line has room for, " + strings.Repeat("and then some more ", 4)
46 sh("checkout", "--quiet", "-b", long, "main")
47 sh("commit", "--quiet", "--allow-empty", "-m", title+"on the long branch")
48 sh("push", "--quiet", "origin", long)
49 sh("checkout", "--quiet", "-b", "short", "main")
50 sh("commit", "--quiet", "--allow-empty", "-m", title)
51 sh("push", "--quiet", "origin", "short")
52 from := "main"
53 for _, layer := range []string{"1", "2", "3", "4"} {
54 sh("checkout", "--quiet", "-b", "layer-"+layer, from)
55 sh("commit", "--quiet", "--allow-empty", "-m", "layer "+layer+": "+title)
56 sh("push", "--quiet", "origin", "layer-"+layer)
57 from = "layer-" + layer
58 }
59
60 t.Chdir(work)
61 var stdout, stderr bytes.Buffer
62 if code := run([]string{"ready", "--change", strings.Repeat("q", 32), "short"}, &stdout, &stderr); code != 0 {
63 t.Fatalf("ready: %s", stderr.String())
64 }
65 stdout.Reset()
66 if code := run([]string{"pending"}, &stdout, &stderr); code != 0 {
67 t.Fatalf("pending: %s", stderr.String())
68 }
69 out := stdout.String()
70 if strings.Contains(out, "\x1b") {
71 t.Errorf("pending printed an escape code to no terminal:\n%s", out)
72 }
73 if !strings.Contains(out, "\n"+long+"\n") {
74 t.Errorf("the long branch is not whole, on a line of its own:\n%s", out)
75 }
76 if !strings.Contains(out, "\nqqqq v1 ") {
77 t.Errorf("the change is not named by its whole handle and version:\n%s", out)
78 }
79 cut := 0
80 for _, line := range strings.Split(strings.TrimSuffix(out, "\n"), "\n") {
81 if strings.Contains(line, long) {
82 continue
83 }
84 if columns(line) > 80 {
85 t.Errorf("a line is %d columns wide: %q", columns(line), line)
86 }
87 if strings.HasPrefix(line, " a subject that says") {
88 if columns(line) != 80 || !strings.HasSuffix(line, "…") {
89 t.Errorf("a long title is not cut to 80 columns with …: %q", line)
90 }
91 cut++
92 }
93 if strings.HasPrefix(line, "qqqq v1") && !strings.HasSuffix(line, "…") {
94 t.Errorf("the author is not cut short to keep the line within 80 columns: %q", line)
95 }
96 }
97 lines := strings.Split(out, "\n")
98 for layer, lead := range map[string]string{"4": " ", "3": " │ ", "2": " │ ", "1": " "} {
99 listed := false
100 for _, line := range lines {
101 if strings.HasPrefix(line, lead+"layer "+layer+": ") {
102 listed = true
103 if columns(line) != 80 || !strings.HasSuffix(line, "…") {
104 t.Errorf("layer %s's title is not cut to 80 columns with …: %q", layer, line)
105 }
106 }
107 }
108 if !listed {
109 t.Errorf("layer %s's title is not at %q:\n%s", layer, lead, out)
110 }
111 }
112 if !strings.Contains(out, "\n approving it lands the 3 below too\n") || strings.Contains(out, "on layer-") {
113 t.Errorf("the stack four deep is not one list, each layer on the next:\n%s", out)
114 }
115 if cut != 2 {
116 t.Errorf("%d titles were cut short, where the change's and the branch's should be:\n%s", cut, out)
117 }
118}
119
120// fit cuts by columns, not bytes: a wide character takes two, and a
121// combining mark none.
122func TestFitCountsColumns(t *testing.T) {
123 for _, c := range []struct {
124 s string
125 n int
126 want string
127 }{
128 {"short", 10, "short"},
129 {"exactly ten", 11, "exactly ten"},
130 {"one word too many", 12, "one word to…"},
131 {"漢字漢字漢字", 7, "漢字漢…"},
132 {"café au lait", 5, "café au lait"[:6] + "…"},
133 } {
134 if got := fit(c.s, c.n); got != c.want || columns(got) > c.n {
135 t.Errorf("fit(%q, %d) = %q, %d columns; want %q", c.s, c.n, got, columns(got), c.want)
136 }
137 }
138}