-
Notifications
You must be signed in to change notification settings - Fork 8
Expand file tree
/
Copy pathtemplate_render_test.go
More file actions
68 lines (58 loc) · 1.85 KB
/
Copy pathtemplate_render_test.go
File metadata and controls
68 lines (58 loc) · 1.85 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
package patchbin
import (
"bytes"
"html/template"
"testing"
)
func TestPatchFileTemplateRendersLineDiffOnly(t *testing.T) {
files, _, err := ParsePatch(sampleDiff)
if err != nil {
t.Fatalf("ParsePatch: %v", err)
}
semanticChanges := AnalyzeSemanticChanges(files[0])
for i := range semanticChanges {
semanticChanges[i].HunkAnchor = hunkAnchor(1, "foo.go", semanticChanges[i].HunkIndex)
}
hunks := make([]PatchHunk, 0, len(files[0].TextFragments))
for i, frag := range files[0].TextFragments {
hunks = append(hunks, PatchHunk{
Anchor: hunkAnchor(1, "foo.go", i),
DiffText: template.HTML(frag.String()),
})
}
pf := &PatchFile{
File: files[0],
DisplayName: "foo.go",
FileAnchor: "patch-1-foo.go",
Adds: 5,
Dels: 2,
Hunks: hunks,
SemanticChanges: semanticChanges,
}
tmpl := getTemplate("pr.html")
if tmpl == nil {
t.Fatalf("getTemplate returned nil")
}
patchFileTmpl := tmpl.Lookup("patch-file")
if patchFileTmpl == nil {
t.Fatalf("patch-file template not found")
}
var buf bytes.Buffer
if err := patchFileTmpl.Execute(&buf, pf); err != nil {
t.Fatalf("execute: %v", err)
}
out := buf.String()
t.Logf("rendered output:\n%s", out)
// Per-file details should be pure line-diff now -- the semantic
// breakdown lives only in the patch-level summary, so repeating it
// here would be redundant.
if bytes.Contains(buf.Bytes(), []byte("semantic-diff")) {
t.Errorf("expected no semantic-diff content inside patch-file, semantic breakdown belongs in the summary only")
}
if !bytes.Contains(buf.Bytes(), []byte(`<details class="details-min" open id="patch-1-foo.go">`)) {
t.Errorf("expected patch-file details element to be open by default")
}
if !bytes.Contains(buf.Bytes(), []byte("patch-1-foo.go-hunk-0")) {
t.Errorf("expected hunk anchor in rendered line diff")
}
}