-
Notifications
You must be signed in to change notification settings - Fork 0
/
main_test.go
99 lines (82 loc) · 2.56 KB
/
main_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
package main
import (
"os"
"path/filepath"
"strings"
"testing"
"golang.org/x/net/html"
)
func TestBuild(t *testing.T) {
// Set up temporary directories for the test
tempOutputDir, err := os.MkdirTemp("", "site_test_output")
if err != nil {
t.Fatalf("Failed to create temp output directory: %v", err)
}
defer os.RemoveAll(tempOutputDir)
// Run the build process with the temporary output directory
build(tempOutputDir)
// Check that the index page was created
indexFilePath := filepath.Join(tempOutputDir, "index.html")
if _, err := os.Stat(indexFilePath); os.IsNotExist(err) {
t.Fatalf("Index page was not created")
}
// Parse the HTML and check for parsing errors
doc := parseHTML(t, indexFilePath)
// Check that the index page has the correct title
checkHTMLTitle(t, doc, "Dan Croak")
// Clean up the test environment
os.RemoveAll(tempOutputDir)
}
func parseHTML(t *testing.T, filePath string) *html.Node {
content, err := os.ReadFile(filePath)
if err != nil {
t.Fatalf("Failed to read file: %v", err)
}
doc, err := html.Parse(strings.NewReader(string(content)))
if err != nil {
t.Fatalf("Failed to parse HTML: %v", err)
}
return doc
}
func checkHTMLTitle(t *testing.T, doc *html.Node, expectedTitle string) {
var title string
var inHead bool
var traverse func(*html.Node)
traverse = func(node *html.Node) {
if node.Type == html.ElementNode {
if node.Data == "head" {
inHead = true
} else if node.Data == "title" && inHead && node.FirstChild != nil {
title = node.FirstChild.Data
}
}
for child := node.FirstChild; child != nil; child = child.NextSibling {
traverse(child)
}
if node.Type == html.ElementNode && node.Data == "head" {
inHead = false
}
}
traverse(doc)
if title != expectedTitle {
t.Errorf("Expected title '%s', got '%s'", expectedTitle, title)
}
}
func TestPreProcessEmbed(t *testing.T) {
// Set the working directory to the directory containing the test files
wd, err := os.Getwd()
if err != nil {
t.Fatalf("Failed to get working directory: %v", err)
}
// Path to the test article and code files
articlePath := filepath.Join(wd, "articles", "dev-data.md")
// Run the preProcess function on the test article
_, body := preProcess(articlePath)
// Check that the body contains the expected embedded code lines
if !strings.Contains(string(body), "pg_dump") {
t.Errorf("Expected body to contain embedded code line %q, got: %s", "pg_dump", body)
}
if !strings.Contains(string(body), "pg_restore") {
t.Errorf("Expected body to contain embedded code line %q, got: %s", "pg_restore", body)
}
}