gunk-dev/the-valley

pending_test.go

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