-
Notifications
You must be signed in to change notification settings - Fork 185
/
Copy pathdochtml_test.go
486 lines (446 loc) · 13.1 KB
/
dochtml_test.go
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
// Copyright 2019 The Go Authors. All rights reserved.
// Use of this source code is governed by a BSD-style
// license that can be found in the LICENSE file.
package dochtml
import (
"bufio"
"bytes"
"context"
"flag"
"fmt"
"os"
"go/ast"
"go/doc"
"go/parser"
"go/token"
"path/filepath"
"strings"
"testing"
"github.com/google/go-cmp/cmp"
"github.com/google/go-cmp/cmp/cmpopts"
"github.com/google/safehtml/template"
"github.com/jba/templatecheck"
"golang.org/x/net/html"
"golang.org/x/pkgsite/internal/godoc/dochtml/internal/render"
"golang.org/x/pkgsite/internal/testing/testhelper"
)
var templateFS = template.TrustedFSFromTrustedSource(template.TrustedSourceFromConstant("../../../static"))
var update = flag.Bool("update", false, "update goldens instead of checking against them")
var testRenderOptions = RenderOptions{
FileLinkFunc: func(string) string { return "file" },
SourceLinkFunc: func(ast.Node) string { return "src" },
SinceVersionFunc: func(string) string { return "" },
ModInfo: &ModuleInfo{
ModulePath: "example.com/module",
ResolvedVersion: "v1.2.3",
ModulePackages: map[string]bool{"example.com/module/pkg": true},
},
}
func TestCheckTemplates(t *testing.T) {
LoadTemplates(templateFS)
for _, tm := range []*template.Template{bodyTemplate, outlineTemplate, sidenavTemplate} {
if err := templatecheck.CheckSafe(tm, templateData{}); err != nil {
t.Fatal(err)
}
}
}
func TestRender(t *testing.T) {
ctx := context.Background()
LoadTemplates(templateFS)
for _, pkg := range []string{"everydecl", "comments"} {
t.Run(pkg, func(t *testing.T) {
fset, d := mustLoadPackage(pkg)
parts, err := Render(ctx, fset, d, testRenderOptions)
if err != nil {
t.Fatal(err)
}
compareWithGolden(t, parts, pkg, *update)
bodyDoc, err := html.Parse(strings.NewReader(parts.Body.String()))
if err != nil {
t.Fatal(err)
}
// Check that there are no duplicate id attributes.
t.Run("duplicate ids", func(t *testing.T) {
testDuplicateIDs(t, bodyDoc)
})
if pkg == "everydecl" {
t.Run("ids-and-kinds", func(t *testing.T) {
// Check that the id and data-kind labels are right.
testIDsAndKinds(t, bodyDoc)
})
wantLinks := []render.Link{
{Href: "https://go.googlesource.com/pkgsite", Text: "pkgsite repo"},
{Href: "https://play-with-go.dev", Text: "Play with Go"},
}
if diff := cmp.Diff(wantLinks, parts.Links); diff != "" {
t.Errorf("links mismatch (-want, +got):\n%s", diff)
}
}
})
}
}
func TestRenderDeprecated(t *testing.T) {
t.Helper()
fset, d := mustLoadPackage("deprecated")
parts, err := Render(context.Background(), fset, d, testRenderOptions)
if err != nil {
t.Fatal(err)
}
compareWithGolden(t, parts, "deprecated-on", *update)
}
func compareWithGolden(t *testing.T, parts *Parts, name string, update bool) {
got := fmt.Sprintf("%s\n----\n%s\n----\n%s\n", parts.Body, parts.Outline, parts.MobileOutline)
// Remove blank lines and whitespace around lines.
var b strings.Builder
s := bufio.NewScanner(strings.NewReader(got))
for s.Scan() {
line := strings.TrimSpace(s.Text())
if line != "" {
fmt.Fprintln(&b, line)
}
}
got = b.String()
testhelper.CompareWithGolden(t, got, name+".golden", update)
}
func TestExampleRender(t *testing.T) {
LoadTemplates(templateFS)
ctx := context.Background()
fset, d := mustLoadPackage("example_test")
parts, err := Render(ctx, fset, d, testRenderOptions)
if err != nil {
t.Fatal(err)
}
htmlDoc, err := html.Parse(strings.NewReader(parts.Body.String()))
if err != nil {
t.Fatal(err)
}
got := make(map[string]string)
walk(htmlDoc, func(n *html.Node) {
if attr(n, "class") == "Documentation-exampleDetails js-exampleContainer" {
var b bytes.Buffer
err := html.Render(&b, n)
if err != nil {
t.Fatal(err)
}
got[attr(n, "id")] = b.String()
}
})
for _, test := range []struct {
name string
htmlID string
want string
}{
{
name: "Executable examples (with play buttons)",
htmlID: "example-package-StringsCompare",
want: `<details tabindex="-1" id="example-package-StringsCompare" class="Documentation-exampleDetails js-exampleContainer">
<summary class="Documentation-exampleDetailsHeader">Example (StringsCompare) <a href="#example-package-StringsCompare" aria-label="Go to Example (StringsCompare)">¶</a></summary>
<div class="Documentation-exampleDetailsBody">
<p>executable example
</p>
<pre class="Documentation-exampleCode">package main
import (
"fmt"
"strings"
)
func main() {
// example comment
fmt.Println(strings.Compare("a", "b"))
fmt.Println(strings.Compare("a", "a"))
fmt.Println(strings.Compare("b", "a"))
}
</pre>
<pre><span class="Documentation-exampleOutputLabel">Output:</span>
<span class="Documentation-exampleOutput">-1
0
1
</span></pre>
</div>
<div class="Documentation-exampleButtonsContainer">
<p class="Documentation-exampleError" role="alert" aria-atomic="true"></p>
<button class="Documentation-exampleShareButton" aria-label="Share Code">Share</button>
<button class="Documentation-exampleFormatButton" aria-label="Format Code">Format</button>
<button class="Documentation-exampleRunButton" aria-label="Run Code">Run</button>
</div></details>`,
},
{
name: "Example without output",
htmlID: "example-package-NoOutput",
want: `<details tabindex="-1" id="example-package-NoOutput" class="Documentation-exampleDetails js-exampleContainer">
<summary class="Documentation-exampleDetailsHeader">Example (NoOutput) <a href="#example-package-NoOutput">¶</a></summary>
<div class="Documentation-exampleDetailsBody">
<pre class="Documentation-exampleCode">package main
import (
"fmt"
)
func main() {
fmt.Println("hello")
}
</pre>
</div>
<div class="Documentation-exampleButtonsContainer">
<p class="Documentation-exampleError" role="alert" aria-atomic="true"></p>
<button class="Documentation-exampleShareButton" aria-label="Share Code">Share</button>
<button class="Documentation-exampleFormatButton" aria-label="Format Code">Format</button>
<button class="Documentation-exampleRunButton" aria-label="Run Code">Run</button>
</div></details>`,
},
} {
t.Run(test.name, func(t *testing.T) {
diff := cmp.Diff(test.want, got[test.htmlID])
if diff != "" {
t.Errorf("mismatch (-want, +got):\n%s", diff)
}
})
}
}
func TestLinkHTML(t *testing.T) {
for _, test := range []struct {
name string
in string
link string
want string
}{
{
name: "regular string and link are rendered",
in: `escape.go`,
link: `https://golang.org/src/html/escape.go`,
want: `<a class="class" href="https://golang.org/src/html/escape.go">escape.go</a>`,
},
{
name: "name is escaped",
in: `"File & name" <'[email protected]>`,
link: "",
want: `"File & name" <'[email protected]>`,
},
{
name: "link is escaped",
in: "file.go",
link: `"abc@go's.com"`,
want: `<a class="class" href="%22abc@go%27s.com%22">file.go</a>`,
},
{
name: "file name and link are escaped",
in: `"a's.com@/`,
link: `"x@go's.com"`,
want: `<a class="class" href="%22x@go%27s.com%22">"a's.com@/</a>`,
},
{
name: "HTML injection escaped",
in: `<a href="gfr.con"></a>`,
link: `"><script>bad</script>`,
want: `<a class="class" href="%22%3e%3cscript%3ebad%3c/script%3e"><a href="gfr.con"></a></a>`,
},
} {
t.Run(test.name, func(t *testing.T) {
got := linkHTML(test.in, test.link, "class")
diff := cmp.Diff(test.want, got.String())
if diff != "" {
t.Errorf("mismatch (-want, +got):\n%s", diff)
}
})
}
}
func TestVersionedPkgPath(t *testing.T) {
for _, test := range []struct {
name string
pkgPath string
modInfo *ModuleInfo
want string
}{
{
name: "builtin package is not versioned",
pkgPath: "builtin",
modInfo: &ModuleInfo{
ModulePath: "std",
ResolvedVersion: "v1.14.4",
ModulePackages: map[string]bool{"std/builtin": true, "std/net/http": true},
},
want: "builtin",
},
{
name: "std packages are not versioned",
pkgPath: "net/http",
modInfo: &ModuleInfo{
ModulePath: "std",
ResolvedVersion: "v1.14.4",
ModulePackages: map[string]bool{"std/builtin": true, "std/net/http": true},
},
want: "net/http",
},
{
name: "imports from other modules are not versioned",
pkgPath: "golang.org/x/pkgsite",
modInfo: &ModuleInfo{
ModulePath: "cloud.google.com/go",
ResolvedVersion: "v0.60.0",
ModulePackages: map[string]bool{"cloud.google.com/go/civil": true},
},
want: "golang.org/x/pkgsite",
},
{
name: "imports from other modules with shared prefixes are not versioned",
pkgPath: "golang.org/x/pkgsite",
modInfo: &ModuleInfo{
ModulePath: "golang.org/x/time",
ResolvedVersion: "v1.2.3",
ModulePackages: map[string]bool{"golang.org/x/time/rate": true},
},
want: "golang.org/x/pkgsite",
},
{
name: "imports from same module are versioned",
pkgPath: "golang.org/x/pkgsite/internal/log",
modInfo: &ModuleInfo{
ModulePath: "golang.org/x/pkgsite",
ResolvedVersion: "v1.1.2",
ModulePackages: map[string]bool{"golang.org/x/pkgsite/internal/log": true},
},
want: "golang.org/x/[email protected]/internal/log",
},
{
name: "imports from same module with pseudo version are versioned",
pkgPath: "golang.org/x/pkgsite/internal/log",
modInfo: &ModuleInfo{
ModulePath: "golang.org/x/pkgsite",
ResolvedVersion: "v0.0.0-20200709011933-a59b4ce778c4",
ModulePackages: map[string]bool{"golang.org/x/pkgsite/internal/log": true},
},
want: "golang.org/x/[email protected]/internal/log",
},
{
name: "imports from same v2 module are versioned",
pkgPath: "k8s.io/klog/v2/klogr",
modInfo: &ModuleInfo{
ModulePath: "k8s.io/klog/v2",
ResolvedVersion: "v2.3.0",
ModulePackages: map[string]bool{"k8s.io/klog/v2": true, "k8s.io/klog/v2/klogr": true},
},
want: "k8s.io/klog/[email protected]/klogr",
},
{
name: "imports from older major module version are not versioned",
pkgPath: "rsc.io/quote",
modInfo: &ModuleInfo{
ModulePath: "rsc.io/quote/v3",
ResolvedVersion: "v3.1.0",
ModulePackages: map[string]bool{"rsc.io/quote/v3": true},
},
want: "rsc.io/quote",
},
{
name: "imports from newer major module version are not versioned",
pkgPath: "rsc.io/quote/v3",
modInfo: &ModuleInfo{
ModulePath: "rsc.io/quote",
ResolvedVersion: "v1.5.3",
ModulePackages: map[string]bool{"rsc.io/quote": true},
},
want: "rsc.io/quote/v3",
},
{
name: "imports from nested module are not versioned",
pkgPath: "A/B/C/D",
modInfo: &ModuleInfo{
ModulePath: "A",
ResolvedVersion: "v1.0.0",
ModulePackages: map[string]bool{"A/B": true, "A/B/C": true},
},
want: "A/B/C/D",
},
} {
t.Run(test.name, func(t *testing.T) {
got := versionedPkgPath(test.pkgPath, test.modInfo)
if got != test.want {
t.Errorf("versionedPkgPath(%q) = %q, want %q", test.pkgPath, got, test.want)
}
})
}
}
func testDuplicateIDs(t *testing.T, htmlDoc *html.Node) {
idCounts := map[string]int{}
walk(htmlDoc, func(n *html.Node) {
id := attr(n, "id")
if id != "" {
idCounts[id]++
}
})
var dups []string
for id, n := range idCounts {
if n > 1 {
dups = append(dups, id)
}
}
if len(dups) > 0 {
t.Errorf("duplicate ids: %v", dups)
}
}
func testIDsAndKinds(t *testing.T, htmlDoc *html.Node) {
type attrs struct {
ID, Kind string // export fields for cmp
}
// want is a complete list of id, kind pairs we expect to see the HTML.
want := []attrs{
{"C", "constant"},
{"CT", "constant"},
{"F", "function"},
{"TF", "function"},
{"T.M", "method"},
{"V", "variable"},
{"VT", "variable"},
{"T", "type"},
{"S1", "type"},
{"S1.F", "field"},
{"S2", "type"},
{"S2.S1", "field"},
{"S2.G", "field"},
{"I1", "type"},
{"I1.M1", "method"},
{"I2", "type"},
{"I2.M2", "method"},
{"A", "type"},
{"B", "type"},
}
var got []attrs
walk(htmlDoc, func(n *html.Node) {
if kind := attr(n, "data-kind"); kind != "" {
got = append(got, attrs{attr(n, "id"), kind})
}
})
diff := cmp.Diff(want, got, cmpopts.SortSlices(func(a1, a2 attrs) bool {
return a1.ID < a2.ID
}))
if diff != "" {
t.Errorf("mismatch (-want, +got):\n%s", diff)
}
}
func walk(n *html.Node, f func(*html.Node)) {
f(n)
for c := n.FirstChild; c != nil; c = c.NextSibling {
walk(c, f)
}
}
func attr(n *html.Node, key string) string {
for _, a := range n.Attr {
if a.Key == key {
return a.Val
}
}
return ""
}
// Copied from internal/render/render_test.go, with the slight modification of returning the fset.
func mustLoadPackage(path string) (*token.FileSet, *doc.Package) {
srcName := filepath.Base(path) + ".go"
code, err := os.ReadFile(filepath.Join("testdata", srcName))
if err != nil {
panic(err)
}
fset := token.NewFileSet()
astFile, _ := parser.ParseFile(fset, srcName, code, parser.ParseComments)
files := []*ast.File{astFile}
astPackage, err := doc.NewFromFiles(fset, files, path, doc.AllDecls)
if err != nil {
panic(err)
}
return fset, astPackage
}