gunk-dev/the-valley

main_test.go

sourceb919222bf28dmain's published tip history
5.1 KiB
1package main
2
3import (
4 "bufio"
5 "io"
6 "strings"
7 "testing"
8 "unicode/utf8"
9)
10
11func mustReason(t *testing.T, in string) string {
12 t.Helper()
13 got, err := reason(strings.NewReader(in))
14 if err != nil {
15 t.Fatal(err)
16 }
17 return got
18}
19
20// A refusal as git prints it: the remote's lines, padded the way git pads
21// them when its output is not a terminal, then its own framing.
22func TestRefusal(t *testing.T) {
23 in := "remote: main is protected \n" +
24 "To ../mirror.git\n" +
25 " ! [remote rejected] main -> main (pre-receive hook declined)\n" +
26 "error: failed to push some refs to '../mirror.git'\n" +
27 "hint: Updates were rejected\n\n"
28 want := "remote: main is protected · ! [remote rejected] main -> main (pre-receive hook declined)"
29 if got := mustReason(t, in); got != want {
30 t.Errorf("got %q, want %q", got, want)
31 }
32}
33
34func TestEmpty(t *testing.T) {
35 if got := mustReason(t, ""); got != "" {
36 t.Errorf("got %q from no output", got)
37 }
38}
39
40func TestControlCharacters(t *testing.T) {
41 in := "remote: \x1b[2Kerased\r\x7f \u009b31mred\x00\n"
42 got := mustReason(t, in)
43 if want := "remote: [2Kerased 31mred"; got != want {
44 t.Errorf("got %q, want %q", got, want)
45 }
46}
47
48func TestInvalidUTF8(t *testing.T) {
49 got := mustReason(t, "remote: bad \xff\xfe byte\n")
50 if !utf8.ValidString(got) || !strings.Contains(got, "bad") {
51 t.Errorf("got %q, want valid UTF-8 keeping the text", got)
52 }
53}
54
55// A long line is replaced whole, however long, and the lines around it
56// are kept.
57func TestLongLine(t *testing.T) {
58 long := "remote: https://operator:s3cret-token@" + strings.Repeat("x", 1<<20)
59 in := io.MultiReader(
60 strings.NewReader("remote: before\n"),
61 strings.NewReader(long),
62 strings.NewReader("\nremote: after\n"),
63 )
64 got, err := reason(in)
65 if err != nil {
66 t.Fatal(err)
67 }
68 if want := "remote: before · " + tooLong + " · remote: after"; got != want {
69 t.Errorf("got %q, want %q", got, want)
70 }
71}
72
73// readLine holds a line of exactly lineLimit bytes whole, and cuts one
74// byte more.
75func TestLineLimit(t *testing.T) {
76 exact := strings.Repeat("y", lineLimit)
77 for _, c := range []struct {
78 in string
79 cut bool
80 }{{exact, false}, {exact + "y", true}} {
81 line, cut, _ := readLine(bufio.NewReader(strings.NewReader(c.in + "\n")))
82 if cut != c.cut || (!cut && string(line) != c.in) {
83 t.Errorf("a line of %d bytes: cut %v, want %v", len(c.in), cut, c.cut)
84 }
85 }
86}
87
88// A word holding any of the marks is replaced whole, whatever its shape,
89// and a word or phrase holding none stands.
90func TestWords(t *testing.T) {
91 for _, w := range []string{
92 "git@example.org:app.git?access_token=s3cret",
93 "https://example.org/a,https://operator:s3cret@example.org/b",
94 "https://example.org/rules?access_token=s3cret",
95 "https://example.org?token=a@s3cret",
96 "https://operator:s3cret@example.org/app.git",
97 "https://operator:prefix\u00a0S3CRET\u00a0suffix@example.org/app.git",
98 "ssh://git@example.org/app.git",
99 "operator:s3cret@example.org",
100 "access_token%3Ds3cret",
101 "token=s3cret",
102 "'https://example.org/app.git/':",
103 } {
104 got := mustReason(t, "remote: see "+w+" now\n")
105 if want := "remote: see " + dropped + " now"; got != want {
106 t.Errorf("%q: got %q, want %q", w, got, want)
107 }
108 }
109 for _, p := range []string{
110 "refs/heads/main",
111 "! [remote rejected] main -> main (pre-receive hook declined)",
112 "main is protected: changes must be made through a review, and 2 checks must pass.",
113 "host:path",
114 "../mirror.git",
115 "fatal: Could not read from remote repository.",
116 } {
117 if got := mustReason(t, "remote: "+p+"\n"); got != "remote: "+p {
118 t.Errorf("%q: got %q", p, got)
119 }
120 }
121}
122
123// A tab separates words as a space does, so a credential behind one is a
124// word of its own, and is dropped.
125func TestTab(t *testing.T) {
126 got := mustReason(t, "remote: see\thttps://operator:s3cret@example.org\tnow\n")
127 if want := "remote: see " + dropped + " now"; got != want {
128 t.Errorf("got %q, want %q", got, want)
129 }
130}
131
132func TestLastLines(t *testing.T) {
133 var in strings.Builder
134 for _, w := range []string{"one", "two", "three", "four", "five", "six", "seven"} {
135 in.WriteString("remote: " + w + "\n")
136 }
137 want := "remote: three · remote: four · remote: five · remote: six · remote: seven"
138 if got := mustReason(t, in.String()); got != want {
139 t.Errorf("got %q, want %q", got, want)
140 }
141}
142
143// The reason keeps its last reasonLimit bytes, and a multi-byte character
144// at the cut is dropped rather than split.
145func TestCutAtACharacter(t *testing.T) {
146 // "é" is two bytes, and the filler puts the cut one byte into one.
147 prefix, filler, last := "remote: ", strings.Repeat("é", 400), "remote: the conclusion!"
148 cutAt := len(prefix+filler+separator+last) - reasonLimit
149 if cutAt < len(prefix) || (cutAt-len(prefix))%2 != 1 {
150 t.Fatalf("the cut at %d falls outside a character, so the test proves nothing", cutAt)
151 }
152 got := mustReason(t, prefix+filler+"\n"+last+"\n")
153 if len(got) > reasonLimit {
154 t.Errorf("the reason is %d bytes, over %d", len(got), reasonLimit)
155 }
156 if !utf8.ValidString(got) {
157 t.Errorf("the cut split a character: %q", got[:8])
158 }
159 if !strings.HasPrefix(got, "é") || !strings.HasSuffix(got, separator+last) {
160 t.Errorf("got %q, want whole characters up to the conclusion", got)
161 }
162}