-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathdiff_test.go
More file actions
696 lines (659 loc) · 18.3 KB
/
Copy pathdiff_test.go
File metadata and controls
696 lines (659 loc) · 18.3 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
package reviewer
import (
"flag"
"os"
"path/filepath"
"strings"
"testing"
)
// Golden files record what the diff renderer emits. Regenerate them with
//
// go test ./... -run TestRenderDiffBody -update
//
// and read the resulting diff before committing it.
var update = flag.Bool("update", false, "rewrite the golden files under testdata/")
func TestDetectKind(t *testing.T) {
tests := []struct {
name string
content string
want Kind
}{
{
name: "git diff",
content: `diff --git a/main.go b/main.go
index 1111111..2222222 100644
--- a/main.go
+++ b/main.go
@@ -1,3 +1,3 @@
package main
-func old() {}
+func new() {}
`,
want: KindDiff,
},
{
name: "plain diff -u without a git header",
content: `--- a/main.go 2026-01-01 00:00:00
+++ b/main.go 2026-01-02 00:00:00
@@ -1 +1 @@
-old
+new
`,
want: KindDiff,
},
{
name: "hunk header alone",
content: `@@ -10,4 +10,4 @@ func RenderSpec() {
ctx
-old
+new
`,
want: KindDiff,
},
{
name: "markdown",
content: `---
title: Spec
---
# Heading
Some prose with a --- rule below.
---
`,
want: KindMarkdown,
},
{
// The trap: a spec that quotes a diff is still a spec. Reading it as a diff would
// strip its formatting and orphan every existing comment anchor.
name: "markdown containing a diff in a fenced code block",
content: "# Spec\n\nSee the change:\n\n```diff\ndiff --git a/main.go b/main.go\n--- a/main.go\n+++ b/main.go\n@@ -1 +1 @@\n-old\n+new\n```\n\nDone.\n",
want: KindMarkdown,
},
{
name: "markdown with a tilde fence",
content: "# Spec\n\n~~~\n@@ -1 +1 @@\n~~~\n",
want: KindMarkdown,
},
{
name: "empty",
content: "",
want: KindMarkdown,
},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
if got := DetectKind([]byte(tt.content)); got != tt.want {
t.Errorf("DetectKind() = %q, want %q", got, tt.want)
}
})
}
}
func TestParseUnifiedDiffSingleFile(t *testing.T) {
files, err := ParseUnifiedDiff([]byte(`diff --git a/main.go b/main.go
index 1111111..2222222 100644
--- a/main.go
+++ b/main.go
@@ -10,4 +10,4 @@ func main() {
ctx
-old line
+new line
ctx2
`))
if err != nil {
t.Fatalf("ParseUnifiedDiff() error = %v", err)
}
if len(files) != 1 {
t.Fatalf("got %d files, want 1", len(files))
}
f := files[0]
if f.OldPath != "main.go" || f.NewPath != "main.go" {
t.Errorf("paths = %q/%q, want main.go/main.go", f.OldPath, f.NewPath)
}
if len(f.Hunks) != 1 {
t.Fatalf("got %d hunks, want 1", len(f.Hunks))
}
if got, want := f.Hunks[0].Header, "@@ -10,4 +10,4 @@ func main() {"; got != want {
t.Errorf("hunk header = %q, want %q", got, want)
}
want := []Line{
{Kind: LineContext, Content: "ctx", OldNo: 10, NewNo: 10},
{Kind: LineDelete, Content: "old line", OldNo: 11},
{Kind: LineAdd, Content: "new line", NewNo: 11},
{Kind: LineContext, Content: "ctx2", OldNo: 12, NewNo: 12},
}
assertLines(t, f.Lines(), want)
}
func TestParseUnifiedDiffFileVariants(t *testing.T) {
tests := []struct {
name string
content string
wantFiles int
wantDisplay []string
}{
{
name: "multiple files",
content: `diff --git a/a.go b/a.go
--- a/a.go
+++ b/a.go
@@ -1 +1 @@
-a
+A
diff --git a/b.go b/b.go
--- a/b.go
+++ b/b.go
@@ -1 +1 @@
-b
+B
`,
wantFiles: 2,
wantDisplay: []string{"a.go", "b.go"},
},
{
name: "added file",
content: `diff --git a/new.go b/new.go
new file mode 100644
index 0000000..1111111
--- /dev/null
+++ b/new.go
@@ -0,0 +1,2 @@
+package main
+func main() {}
`,
wantFiles: 1,
wantDisplay: []string{"new.go"},
},
{
// Two deletions in one diff both have /dev/null on the new side. Falling back to the
// old path is what keeps their anchors apart.
name: "two deleted files keep distinct display paths",
content: `diff --git a/gone.go b/gone.go
deleted file mode 100644
--- a/gone.go
+++ /dev/null
@@ -1 +0,0 @@
-package main
diff --git a/also-gone.go b/also-gone.go
deleted file mode 100644
--- a/also-gone.go
+++ /dev/null
@@ -1 +0,0 @@
-package main
`,
wantFiles: 2,
wantDisplay: []string{"gone.go", "also-gone.go"},
},
{
name: "rename with edits is found under its new path",
content: `diff --git a/old/name.go b/new/name.go
similarity index 90%
rename from old/name.go
rename to new/name.go
--- a/old/name.go
+++ b/new/name.go
@@ -1 +1 @@
-package old
+package new
`,
wantFiles: 1,
wantDisplay: []string{"new/name.go"},
},
{
name: "mode change only, no hunks",
content: `diff --git a/script.sh b/script.sh
old mode 100644
new mode 100755
`,
wantFiles: 1,
wantDisplay: []string{"script.sh"},
},
{
name: "empty input",
content: "",
wantFiles: 0,
wantDisplay: nil,
},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
files, err := ParseUnifiedDiff([]byte(tt.content))
if err != nil {
t.Fatalf("ParseUnifiedDiff() error = %v", err)
}
if len(files) != tt.wantFiles {
t.Fatalf("got %d files, want %d", len(files), tt.wantFiles)
}
for i, want := range tt.wantDisplay {
if got := files[i].DisplayPath(); got != want {
t.Errorf("file %d display path = %q, want %q", i, got, want)
}
}
})
}
}
// Status is what the sidebar draws its mark from, so the four cases are pinned down here
// rather than left to the renderer's golden files alone.
func TestFileStatus(t *testing.T) {
tests := []struct {
name string
file File
want FileStatus
}{
{name: "added", file: File{OldPath: devNull, NewPath: "new.go"}, want: FileAdded},
{name: "deleted", file: File{OldPath: "gone.go", NewPath: devNull}, want: FileDeleted},
{name: "renamed", file: File{OldPath: "old/name.go", NewPath: "new/name.go"}, want: FileRenamed},
{name: "modified", file: File{OldPath: "main.go", NewPath: "main.go"}, want: FileModified},
// A header-only entry (a mode change) names the same file on both sides.
{name: "mode change only", file: File{OldPath: "script.sh", NewPath: "script.sh"}, want: FileModified},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
if got := tt.file.Status(); got != tt.want {
t.Errorf("Status() = %q, want %q", got, tt.want)
}
})
}
}
func TestParseUnifiedDiffNoNewlineMarker(t *testing.T) {
files, err := ParseUnifiedDiff([]byte(`diff --git a/a.txt b/a.txt
--- a/a.txt
+++ b/a.txt
@@ -1 +1 @@
-old
\ No newline at end of file
+new
`))
if err != nil {
t.Fatalf("ParseUnifiedDiff() error = %v", err)
}
assertLines(t, files[0].Lines(), []Line{
{Kind: LineDelete, Content: "old", OldNo: 1},
{Kind: LineMeta, Content: "No newline at end of file"},
{Kind: LineAdd, Content: "new", NewNo: 1},
})
}
// A combined diff has one column per parent, so a line has no single before/after and the
// anchor coordinate system cannot describe it. Rejecting it beats guessing.
func TestParseUnifiedDiffRejectsCombinedDiff(t *testing.T) {
_, err := ParseUnifiedDiff([]byte(`diff --cc merged.go
index 1111111,2222222..3333333
--- a/merged.go
+++ b/merged.go
@@@ -1,2 -1,2 +1,2 @@@
- one
-two
++three
`))
if err == nil {
t.Fatal("want an error for a combined diff, got nil")
}
if !strings.Contains(err.Error(), "combined") {
t.Errorf("error = %v, want it to mention combined diffs", err)
}
}
func TestDiffAnchorRoundTrip(t *testing.T) {
tests := []struct {
name string
path string
start, end int
anchor string
}{
{name: "single line", path: "render.go", start: 3, end: 3, anchor: "render.go#3-3"},
{name: "range", path: "cli/command/serve.go", start: 12, end: 15, anchor: "cli/command/serve.go#12-15"},
// The split is on the last '#', so a path that contains one still reads back whole.
{name: "path containing a hash", path: "notes/a#1-2.md", start: 3, end: 4, anchor: "notes/a#1-2.md#3-4"},
{name: "path containing a colon", path: "weird:name.go", start: 1, end: 2, anchor: "weird:name.go#1-2"},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
got := FormatDiffAnchor(tt.path, tt.start, tt.end)
if got != tt.anchor {
t.Fatalf("FormatDiffAnchor() = %q, want %q", got, tt.anchor)
}
path, start, end, ok := ParseDiffAnchor(got)
if !ok {
t.Fatalf("ParseDiffAnchor(%q) reported not-an-anchor", got)
}
if path != tt.path || start != tt.start || end != tt.end {
t.Errorf("ParseDiffAnchor() = %q,%d,%d; want %q,%d,%d", path, start, end, tt.path, tt.start, tt.end)
}
})
}
}
// Anything that is not a diff anchor has to be recognisable as such, because the Markdown
// review shares every code path that handles anchors.
func TestParseDiffAnchorRejectsNonDiffAnchors(t *testing.T) {
for _, anchor := range []string{
"spec-element-7",
"",
"main.go#",
"main.go#12",
"main.go#L12-L15",
"main.go#0-3", // 1-based; 0 is not a line
"main.go#5-3", // end before start
"main.go#1-2x", // trailing junk
"main.go# 1-2", // space is not a digit
"main.go#1--2", // not two numbers
} {
if _, _, _, ok := ParseDiffAnchor(anchor); ok {
t.Errorf("ParseDiffAnchor(%q) accepted a non-anchor", anchor)
}
}
}
func TestDiffFileAnchorRoundTrip(t *testing.T) {
for _, path := range []string{"render.go", "cli/command/serve.go", "notes/a#1-2.md"} {
anchor := FormatDiffFileAnchor(path)
got, ok := ParseDiffFileAnchor(anchor)
if !ok {
t.Fatalf("ParseDiffFileAnchor(%q) reported not-an-anchor", anchor)
}
if got != path {
t.Errorf("ParseDiffFileAnchor(%q) = %q, want %q", anchor, got, path)
}
// The two anchor forms must not be mistaken for each other, in either direction.
if _, _, _, ok := ParseDiffAnchor(anchor); ok {
t.Errorf("ParseDiffAnchor accepted the whole-file anchor %q", anchor)
}
if _, ok := ParseDiffFileAnchor(FormatDiffAnchor(path, 1, 2)); ok {
t.Errorf("ParseDiffFileAnchor accepted the line-range anchor for %q", path)
}
}
for _, anchor := range []string{"spec-element-7", "", "main.go#files", "main.go#FILE", "file"} {
if _, ok := ParseDiffFileAnchor(anchor); ok {
t.Errorf("ParseDiffFileAnchor(%q) accepted a non-anchor", anchor)
}
}
}
func TestRenderDiffBody(t *testing.T) {
names, err := filepath.Glob(filepath.Join("testdata", "*.diff"))
if err != nil {
t.Fatal(err)
}
if len(names) == 0 {
t.Fatal("no sample diffs under testdata/")
}
for _, name := range names {
t.Run(filepath.Base(name), func(t *testing.T) {
content, err := os.ReadFile(name)
if err != nil {
t.Fatal(err)
}
files, err := ParseUnifiedDiff(content)
if err != nil {
t.Fatalf("ParseUnifiedDiff() error = %v", err)
}
got := renderDiffBody(files)
golden := strings.TrimSuffix(name, ".diff") + ".golden.html"
if *update {
if err := os.WriteFile(golden, []byte(got), 0644); err != nil {
t.Fatal(err)
}
return
}
want, err := os.ReadFile(golden)
if err != nil {
t.Fatalf("missing golden file (run with -update): %v", err)
}
if got != string(want) {
t.Errorf("rendered body does not match %s\n--- got ---\n%s", golden, got)
}
})
}
}
// Escaping is not optional on this path: nothing upstream of RenderDiff produces HTML, so every
// diff-derived string reaches the page raw unless it is escaped here.
func TestRenderDiffEscapesEveryDiffDerivedString(t *testing.T) {
files, err := ParseUnifiedDiff([]byte(`diff --git a/x">.go b/x">.go
--- a/x">.go
+++ b/x">.go
@@ -55,10 +80,11 @@ func (s *ReviewSession) Done() <-chan struct{} {
- fmt.Println("<script>alert(1)</script>")
+ fmt.Println("safe & sound")
`))
if err != nil {
t.Fatalf("ParseUnifiedDiff() error = %v", err)
}
body := renderDiffBody(files)
for _, unwanted := range []string{
"<script>alert(1)</script>", // line content in a text node
`<-chan`, // hunk header carrying a channel type
`data-file="x">.go"`, // a quote in a path would break out of the attribute
} {
if strings.Contains(body, unwanted) {
t.Errorf("unescaped %q survived into the rendered body", unwanted)
}
}
for _, wanted := range []string{
"<script>alert(1)</script>",
"<-chan",
"safe & sound",
`data-file="x">.go"`,
} {
if !strings.Contains(body, wanted) {
t.Errorf("expected escaped %q in the rendered body", wanted)
}
}
}
// Render is the single entry point every call site uses; it has to pick the renderer itself.
func TestRenderDispatchesOnContent(t *testing.T) {
diffHTML, err := Render([]byte(`diff --git a/a.go b/a.go
--- a/a.go
+++ b/a.go
@@ -1 +1 @@
-old
+new
`))
if err != nil {
t.Fatalf("Render(diff) error = %v", err)
}
if !strings.Contains(string(diffHTML), `class="diff-line`) {
t.Error("a diff did not render as a diff")
}
if strings.Contains(string(diffHTML), "initializeCommentableElements();") {
t.Error("diff mode must not run the Markdown block-comment initializer")
}
mdHTML, err := Render([]byte("# Heading\n\nProse.\n"))
if err != nil {
t.Fatalf("Render(markdown) error = %v", err)
}
if !strings.Contains(string(mdHTML), "<h1>Heading</h1>") {
t.Error("markdown did not render as markdown")
}
if !strings.Contains(string(mdHTML), "initializeCommentableElements();") {
t.Error("markdown mode lost the block-comment initializer")
}
}
func TestDiffStats(t *testing.T) {
files, err := ParseUnifiedDiff([]byte(`diff --git a/a.go b/a.go
--- a/a.go
+++ b/a.go
@@ -1,2 +1,3 @@
ctx
-old
+new
+extra
`))
if err != nil {
t.Fatalf("ParseUnifiedDiff() error = %v", err)
}
if got, want := diffStats(files), "1 file · +2 −1"; got != want {
t.Errorf("diffStats() = %q, want %q", got, want)
}
}
func assertLines(t *testing.T, got, want []Line) {
t.Helper()
if len(got) != len(want) {
t.Fatalf("got %d lines, want %d: %+v", len(got), len(want), got)
}
for i := range want {
if got[i] != want[i] {
t.Errorf("line %d = %+v, want %+v", i, got[i], want[i])
}
}
}
func TestWhitespaceOnlyMask(t *testing.T) {
tests := []struct {
name string
lines []Line
want []bool
}{
{
name: "indent-only change folds both sides",
lines: []Line{
{Kind: LineDelete, Content: "\t\tfoo(bar)"},
{Kind: LineAdd, Content: " foo(bar)"},
},
want: []bool{true, true},
},
{
name: "inner spacing change folds both sides",
lines: []Line{
{Kind: LineDelete, Content: "foo( a , b )"},
{Kind: LineAdd, Content: "foo(a, b)"},
},
want: []bool{true, true},
},
{
name: "trailing whitespace removal folds both sides",
lines: []Line{
{Kind: LineDelete, Content: "foo() "},
{Kind: LineAdd, Content: "foo()"},
},
want: []bool{true, true},
},
{
name: "a real edit is never folded",
lines: []Line{
{Kind: LineDelete, Content: "foo()"},
{Kind: LineAdd, Content: "bar()"},
},
want: []bool{false, false},
},
{
// The safe-side approximation: unequal block lengths mean the 1:1 pairing is not
// trustworthy, so nothing in the block folds even though two rows would match.
name: "unequal block lengths fold nothing",
lines: []Line{
{Kind: LineDelete, Content: "\tfoo()"},
{Kind: LineDelete, Content: "\tbar()"},
{Kind: LineAdd, Content: " foo()"},
{Kind: LineAdd, Content: " bar()"},
{Kind: LineAdd, Content: " baz()"},
},
want: []bool{false, false, false, false, false},
},
{
name: "an addition with no deletion to pair with never folds",
lines: []Line{
{Kind: LineContext, Content: "func foo() {"},
{Kind: LineAdd, Content: ""},
{Kind: LineContext, Content: "\treturn"},
},
want: []bool{false, false, false},
},
{
name: "a deletion with no addition to pair with never folds",
lines: []Line{
{Kind: LineDelete, Content: "\tfoo()"},
{Kind: LineContext, Content: "\tbar()"},
},
want: []bool{false, false},
},
{
name: "a mixed block folds only when every pair is whitespace-only",
lines: []Line{
{Kind: LineDelete, Content: "\tfoo()"},
{Kind: LineDelete, Content: "\tbar()"},
{Kind: LineAdd, Content: " foo()"},
{Kind: LineAdd, Content: " baz()"},
},
want: []bool{false, false, false, false},
},
{
name: "two separate blocks are judged independently",
lines: []Line{
{Kind: LineDelete, Content: "\tfoo()"},
{Kind: LineAdd, Content: " foo()"},
{Kind: LineContext, Content: "\tsep()"},
{Kind: LineDelete, Content: "old()"},
{Kind: LineAdd, Content: "new()"},
},
want: []bool{true, true, false, false, false},
},
{
// "\ No newline at end of file" carries meaning about the line it follows, so it
// breaks the run rather than being paired over.
name: "a meta line breaks the block",
lines: []Line{
{Kind: LineDelete, Content: "\tfoo()"},
{Kind: LineMeta, Content: `\ No newline at end of file`},
{Kind: LineAdd, Content: " foo()"},
},
want: []bool{false, false, false},
},
{
name: "no lines",
lines: nil,
want: nil,
},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
got := whitespaceOnlyMask(tt.lines)
if len(got) != len(tt.want) {
t.Fatalf("whitespaceOnlyMask() length = %d, want %d", len(got), len(tt.want))
}
for i := range tt.want {
if got[i] != tt.want[i] {
t.Errorf("whitespaceOnlyMask()[%d] = %v, want %v (content %q)", i, got[i], tt.want[i], tt.lines[i].Content)
}
}
})
}
}
func TestRenderDiffBodyMarksWhitespaceOnlyLines(t *testing.T) {
files, err := ParseUnifiedDiff([]byte(`diff --git a/main.go b/main.go
--- a/main.go
+++ b/main.go
@@ -1,4 +1,4 @@
package main
- foo(bar)
- old()
+ foo(bar)
+ new()
`))
if err != nil {
t.Fatalf("ParseUnifiedDiff() error = %v", err)
}
body := renderDiffBody(files)
// Nothing folds: the block pairs 1:1 but old()/new() is a real edit, so the whole block
// stays visible.
if strings.Contains(body, "data-ws-only") {
t.Errorf("renderDiffBody() marked a block containing a real edit as whitespace-only:\n%s", body)
}
}
func TestRenderDiffBodyKeepsLineIndicesStableWhenFolding(t *testing.T) {
files, err := ParseUnifiedDiff([]byte(`diff --git a/main.go b/main.go
--- a/main.go
+++ b/main.go
@@ -1,3 +1,3 @@
package main
- foo(bar)
+ foo(bar)
`))
if err != nil {
t.Fatalf("ParseUnifiedDiff() error = %v", err)
}
body := renderDiffBody(files)
for _, want := range []string{
`data-line-index="1"`,
`data-line-index="2"`,
`data-line-index="3"`,
} {
if !strings.Contains(body, want) {
t.Errorf("renderDiffBody() dropped %s; folding must not renumber lines:\n%s", want, body)
}
}
if strings.Count(body, "data-ws-only") != 2 {
t.Errorf("renderDiffBody() marked %d lines whitespace-only, want 2 (the -/+ pair):\n%s",
strings.Count(body, "data-ws-only"), body)
}
}