Sign in

Search issue titles and bodies, change requests and comments. Private repositories you cannot read are not included.

vole
Clone

Read only, no credentials:

https://rodent.software/vole.git

Write over SSH with a registered key:

git clone ssh://git@rodent.software:2222/vole.git

A free and open-source forge that lives in your Git repo.

Blame · server/commit_test.go

Line attribution on main · 219 line(s)

628d4523
Keenan Nemetz
11d ago
1 package server_test
2
3 // A commit is reviewed one file at a time. The page lists what the commit touched
4 // and renders the diff of the file that is open, so a commit over a hundred files
5 // costs one patch rather than a hundred.
6
7 import (
bef0c093
Keenan Nemetz
9d ago
8 "net/http"
628d4523
Keenan Nemetz
11d ago
9 "os"
10 "path/filepath"
11 "strings"
12 "testing"
13 )
14
15 func TestCommitPageOpensOneFileAtATime(t *testing.T) {
16 t.Parallel()
17 f := newFixture(t)
18 f.publish(f.repo)
19
20 work := workRepo(t, map[string]string{
21 "readme.md": "v1 line\n",
22 "keep.txt": "keep\n",
23 "old.txt": "moved\n",
24 })
25 if _, errs, err := f.git(work, "push", f.sshURL(f.repo), "main:refs/heads/main"); err != nil {
26 t.Fatalf("initial push: %s", errs)
27 }
28 // One commit that adds, modifies, deletes and renames, so the tree has more
29 // than one shape of row and more than one directory.
30 if err := os.WriteFile(filepath.Join(work, "readme.md"), []byte("v1 line\nv2 line\n"), 0o644); err != nil {
31 t.Fatal(err)
32 }
33 if err := os.MkdirAll(filepath.Join(work, "docs"), 0o755); err != nil {
34 t.Fatal(err)
35 }
36 if err := os.WriteFile(filepath.Join(work, "docs", "guide.md"), []byte("a guide\n"), 0o644); err != nil {
37 t.Fatal(err)
38 }
39 if err := os.Remove(filepath.Join(work, "keep.txt")); err != nil {
40 t.Fatal(err)
41 }
42 if _, errs, err := runGit(t, work, "mv", "old.txt", "new.txt"); err != nil {
43 t.Fatalf("mv: %s", errs)
44 }
45 if _, errs, err := runGit(t, work, "add", "-A"); err != nil {
46 t.Fatalf("add: %s", errs)
47 }
48 if _, errs, err := runGit(t, work, "commit", "-qm", "broad change"); err != nil {
49 t.Fatalf("commit: %s", errs)
50 }
51 if _, errs, err := f.git(work, "push", f.sshURL(f.repo), "main:refs/heads/main"); err != nil {
52 t.Fatalf("second push: %s", errs)
53 }
54 sha := mustSHA(t, work)
ed03b3d1
Keenan Nemetz
6h ago
55 commitURL := "/" + f.repo + "/commit/" + sha
628d4523
Keenan Nemetz
11d ago
56
57 // The commit page is the tree beside one file's diff, not every diff at once.
58 code, page := f.get(commitURL)
59 if code != 200 {
60 t.Fatalf("commit page = %d", code)
61 }
62 for _, want := range []string{
63 `id="review-tree"`, `id="review-main"`,
64 `data-status="M"`, `data-status="A"`, `data-status="D"`, `data-status="R"`,
65 commitURL + "/readme.md", commitURL + "/docs/guide.md", commitURL + "/new.txt",
66 } {
67 if !strings.Contains(page, want) {
68 t.Fatalf("commit page has no %q:\n%s", want, page)
69 }
70 }
71 // Only one patch reaches the page. The first file opens, and the rest are rows
72 // in the tree rather than rendered diffs.
73 if n := strings.Count(page, `<section class="diff"`); n != 1 {
74 t.Fatalf("the page rendered %d diffs, want 1:\n%s", n, page)
75 }
76 if !strings.Contains(page, "a guide") {
77 t.Fatalf("the first changed file did not open:\n%s", page)
78 }
79 // The name and the counts share one row, name on the left and counts on the
80 // right, with no second summary line above them.
81 stat := strings.Index(page, `class="diffstat"`)
82 if stat < 0 {
83 t.Fatalf("the diff header has no counts row:\n%s", page)
84 }
85 if strings.Contains(page, `class="diffsum"`) {
86 t.Fatalf("a single-file pane still renders a separate summary line:\n%s", page)
87 }
88 if name := strings.Index(page, `class="path"`); name < 0 || name > stat {
89 t.Fatalf("the file name is not to the left of its counts:\n%s", page)
90 }
91 row := page[stat:]
92 if end := strings.Index(row, "</h3>"); end < 0 || !strings.Contains(row[:end], `<span class="adds">+1</span>`) {
93 t.Fatalf("the header row does not hold the file's counts:\n%s", page)
94 }
95 for _, unwanted := range []string{"v2 line", "moved"} {
96 if strings.Contains(page, unwanted) {
97 t.Fatalf("the page rendered another file's diff (%q):\n%s", unwanted, page)
98 }
99 }
100
101 // A deep link opens that file instead of the first one.
102 code, page = f.get(commitURL + "/readme.md")
103 if code != 200 {
104 t.Fatalf("deep link = %d", code)
105 }
106 if !strings.Contains(page, "v2 line") || !strings.Contains(page, `data-path="readme.md"`) ||
107 strings.Contains(page, "a guide") {
108 t.Fatalf("the deep link did not open readme.md alone:\n%s", page)
109 }
110
111 // A renamed file keeps both of its paths: the diff is a rename, not a new file.
112 _, page = f.get(commitURL + "/new.txt")
113 if !strings.Contains(page, "old.txt → new.txt") {
114 t.Fatalf("the renamed file's diff does not name both paths:\n%s", page)
115 }
116
117 // A tree click returns only the pane it replaces.
118 resp, piece := f.htmxGet(t, commitURL+"/docs/guide.md", "")
119 if resp.StatusCode != 200 || strings.Contains(piece, "<!doctype html>") ||
120 !strings.Contains(piece, `id="review-main"`) || !strings.Contains(piece, "a guide") ||
121 strings.Contains(piece, `id="review-tree"`) {
122 t.Fatalf("commit fragment = %d:\n%s", resp.StatusCode, piece)
123 }
124
125 // A path the commit did not touch is a 404, not an empty pane.
126 if code, _ := f.get(commitURL + "/not-there.txt"); code != 404 {
127 t.Fatalf("untouched path = %d, want 404", code)
128 }
129 }
130
bef0c093
Keenan Nemetz
9d ago
131 // TestCommitPageShowsADeletedFile: a deletion has one path and no new one, and
132 // asking git for both sides used to fail the page outright — the file could not
133 // be reviewed at all.
134 func TestCommitPageShowsADeletedFile(t *testing.T) {
135 t.Parallel()
136 f := newFixture(t)
137 f.publish(f.repo)
138
139 work := workRepo(t, map[string]string{"gone.txt": "first line\nsecond line\n", "keep.txt": "keep\n"})
140 if _, errs, err := f.git(work, "push", f.sshURL(f.repo), "main:refs/heads/main"); err != nil {
141 t.Fatalf("initial push: %s", errs)
142 }
143 if _, errs, err := runGit(t, work, "rm", "-q", "gone.txt"); err != nil {
144 t.Fatalf("rm: %s", errs)
145 }
146 if _, errs, err := runGit(t, work, "commit", "-qm", "drop gone.txt"); err != nil {
147 t.Fatalf("commit: %s", errs)
148 }
149 if _, errs, err := f.git(work, "push", f.sshURL(f.repo), "main:refs/heads/main"); err != nil {
150 t.Fatalf("push: %s", errs)
151 }
ed03b3d1
Keenan Nemetz
6h ago
152 commitURL := "/" + f.repo + "/commit/" + mustSHA(t, work)
bef0c093
Keenan Nemetz
9d ago
153
154 code, page := f.get(commitURL)
155 if code != http.StatusOK {
156 t.Fatalf("commit page = %d:\n%.2000s", code, page)
157 }
158 code, page = f.get(commitURL + "/gone.txt")
159 if code != http.StatusOK {
160 t.Fatalf("deleted file = %d:\n%.2000s", code, page)
161 }
162 if !strings.Contains(page, `data-highlight-side="old">first line<`) {
163 t.Fatalf("the deleted file has no patch:\n%.2000s", page)
e411131b
Keenan Nemetz
9d ago
164 }
165 // A deletion is not a rename: the header names the file once, not "x → x".
166 if !strings.Contains(page, `<span class="path">gone.txt</span>`) {
167 t.Fatalf("the deleted file's header does not name it once:\n%.2000s", page)
bef0c093
Keenan Nemetz
9d ago
168 }
169 }
170
f64c77e4
Keenan Nemetz
9d ago
171 // TestCommitPageShowsTheWholeOfOneFilesPatch: the page renders one file's patch,
172 // not the whole commit's, so the file that is open is shown whole. A byte cut
173 // would hide the end of a large file and, worse, every file named after it.
174 func TestCommitPageShowsTheWholeOfOneFilesPatch(t *testing.T) {
628d4523
Keenan Nemetz
11d ago
175 t.Parallel()
176 f := newFixture(t)
177 f.publish(f.repo)
178
f64c77e4
Keenan Nemetz
9d ago
179 // The file is larger than the 512 KiB that used to bound one file's patch,
180 // and it ends with a marker the cut would have dropped.
181 big := strings.Repeat("x", 513<<10) + "END-OF-BIG-FILE"
628d4523
Keenan Nemetz
11d ago
182 work := workRepo(t, map[string]string{"small.txt": "one\n"})
183 if _, errs, err := f.git(work, "push", f.sshURL(f.repo), "main:refs/heads/main"); err != nil {
184 t.Fatalf("initial push: %s", errs)
185 }
f64c77e4
Keenan Nemetz
9d ago
186 if err := os.WriteFile(filepath.Join(work, "big.txt"), []byte(big+"\n"), 0o644); err != nil {
628d4523
Keenan Nemetz
11d ago
187 t.Fatal(err)
188 }
189 if err := os.WriteFile(filepath.Join(work, "small.txt"), []byte("one\ntwo\n"), 0o644); err != nil {
190 t.Fatal(err)
191 }
192 if _, errs, err := runGit(t, work, "add", "-A"); err != nil {
193 t.Fatalf("add: %s", errs)
194 }
195 if _, errs, err := runGit(t, work, "commit", "-qm", "a big file"); err != nil {
196 t.Fatalf("commit: %s", errs)
197 }
198 if _, errs, err := f.git(work, "push", f.sshURL(f.repo), "main:refs/heads/main"); err != nil {
199 t.Fatalf("push: %s", errs)
200 }
201 sha := mustSHA(t, work)
ed03b3d1
Keenan Nemetz
6h ago
202 commitURL := "/" + f.repo + "/commit/" + sha
628d4523
Keenan Nemetz
11d ago
203
204 _, page := f.get(commitURL + "/big.txt")
f64c77e4
Keenan Nemetz
9d ago
205 if !strings.Contains(page, "END-OF-BIG-FILE") {
206 t.Fatalf("the open file's patch is not whole:\n%.2000s", page)
628d4523
Keenan Nemetz
11d ago
207 }
208 if strings.Contains(page, "showing the first 512 KiB") {
f64c77e4
Keenan Nemetz
9d ago
209 t.Fatalf("the file was reported as cut:\n%.2000s", page)
210 }
211 // The small file is whole too, and the big one did not hide its row.
212 _, page = f.get(commitURL + "/small.txt")
213 if !strings.Contains(page, `data-highlight-side="new">two<`) {
214 t.Fatalf("the small file has no patch:\n%.2000s", page)
628d4523
Keenan Nemetz
11d ago
215 }
216 if !strings.Contains(page, commitURL+"/big.txt") {
f64c77e4
Keenan Nemetz
9d ago
217 t.Fatalf("the big file is missing from the tree:\n%.2000s", page)
628d4523
Keenan Nemetz
11d ago
218 }
219 }