-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathgenerator_test.go
More file actions
94 lines (79 loc) · 2.3 KB
/
Copy pathgenerator_test.go
File metadata and controls
94 lines (79 loc) · 2.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
package main
import (
"os"
"path/filepath"
"strings"
"testing"
"golang.org/x/tools/txtar"
)
func TestGenerateSiteE2E(t *testing.T) {
matches, err := filepath.Glob("testdata/*.txtar")
if err != nil {
t.Fatal(err)
}
if len(matches) == 0 {
t.Fatal("no .txtar files found in testdata/")
}
for _, filename := range matches {
filename := filename
t.Run(filepath.Base(filename), func(t *testing.T) {
ar, err := txtar.ParseFile(filename)
if err != nil {
t.Fatal(err)
}
tmpDir := t.TempDir()
sourceDir := filepath.Join(tmpDir, "source")
outDir := filepath.Join(tmpDir, "out")
if err := os.MkdirAll(sourceDir, 0755); err != nil {
t.Fatal(err)
}
if err := os.MkdirAll(outDir, 0755); err != nil {
t.Fatal(err)
}
sourceFiles := extractFiles(ar, "source/")
wantFiles := extractFiles(ar, "want/")
for name, content := range sourceFiles {
path := filepath.Join(sourceDir, name)
if err := os.WriteFile(path, content, 0644); err != nil {
t.Fatalf("failed to write source file %s: %v", name, err)
}
}
if err := generateSite(sourceDir, outDir, "", "default"); err != nil {
t.Fatalf("generateSite() error = %v", err)
}
for wantFile, wantContent := range wantFiles {
gotPath := filepath.Join(outDir, wantFile)
gotBytes, err := os.ReadFile(gotPath)
if err != nil {
t.Errorf("failed to read generated file %s: %v", wantFile, err)
continue
}
gotStr := string(gotBytes)
wantStr := strings.TrimSpace(string(wantContent))
for _, wantSubstr := range strings.Split(wantStr, "\n") {
wantSubstr = strings.TrimSpace(wantSubstr)
if wantSubstr == "" {
continue
}
if !strings.Contains(gotStr, wantSubstr) {
t.Errorf("file %s missing expected content:\n want substring: %q\n got: %q", wantFile, wantSubstr, gotStr)
}
}
}
hiddenFile := filepath.Join(outDir, ".hidden.html")
if _, err := os.Stat(hiddenFile); err == nil {
t.Errorf("dotfile .hidden.md was generated but should have been ignored")
}
})
}
}
func extractFiles(ar *txtar.Archive, prefix string) map[string][]byte {
files := make(map[string][]byte)
for _, f := range ar.Files {
if strings.HasPrefix(f.Name, prefix) {
name := strings.TrimPrefix(f.Name, prefix)
files[name] = f.Data
}
}
return files
}