-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathrepresentative_test.go
More file actions
84 lines (78 loc) · 1.68 KB
/
representative_test.go
File metadata and controls
84 lines (78 loc) · 1.68 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
package representative_test
import (
"bytes"
"net/url"
"os"
"strings"
"testing"
"eagain.net/go/representative"
"github.com/andybalholm/cascadia"
"github.com/google/go-cmp/cmp"
"golang.org/x/net/html"
)
func TestSlidesStaticPath(t *testing.T) {
buf := new(bytes.Buffer)
if err := representative.Convert(
buf,
"testdata/simple.slide",
&url.URL{Path: "xyzzy/foo/bar"},
); err != nil {
t.Fatalf("convert: %v", err)
}
tree, err := html.Parse(buf)
if err != nil {
t.Fatalf("parse slide html: %v", err)
}
sel := cascadia.MustCompile(`script[src]`)
nodes := cascadia.QueryAll(tree, sel)
seen := false
for _, n := range nodes {
for _, attr := range n.Attr {
if attr.Namespace == "" && attr.Key == "src" {
seen = true
if !strings.HasPrefix(attr.Val, "xyzzy/foo/bar/") {
t.Errorf("script source points outside static url: %v", attr.Val)
}
}
}
}
if !seen {
t.Error("no asset scripts used, something must be wrong")
}
}
func readdirnames(dir string) ([]string, error) {
fis, err := os.ReadDir(dir)
if err != nil {
return nil, err
}
var r []string
for _, fi := range fis {
r = append(r, fi.Name())
}
return r, nil
}
func TestWriteAssets(t *testing.T) {
const staticDir = "testdata/static"
if err := representative.WriteAssets(staticDir); err != nil {
t.Fatalf("writeAssets: %v", err)
}
names, err := readdirnames(staticDir)
if err != nil {
t.Fatalf("readdir: %v", err)
}
want := []string{
"article.css",
"favicon.ico",
"jquery-ui.js",
"jquery.js",
"notes.css",
"notes.js",
"play.js",
"playground.js",
"slides.js",
"styles.css",
}
if diff := cmp.Diff(want, names); diff != "" {
t.Errorf("wrong assets:\n%s", diff)
}
}