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