-
Notifications
You must be signed in to change notification settings - Fork 0
/
main_test.go
140 lines (130 loc) · 3.72 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
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
package main
import (
"fmt"
"os"
"path/filepath"
"testing"
)
func cleanup(dist string) {
os.RemoveAll(dist)
}
func TestCheckError(t *testing.T) {
defer func() {
if r := recover(); r == nil {
t.Errorf("expected check to panic, got %v", r)
}
}()
check(fmt.Errorf("test error"))
}
func TestInitApp(t *testing.T) {
srcTest := "src_test"
app, err := InitApp(srcTest)
defer cleanup(app.DistDir)
if err != nil {
t.Errorf("expected InitApp to return no error, got %v", err)
}
if app.SrcDir != srcTest {
t.Errorf("expected SrcDir to be %v, got %v", srcTest, app.SrcDir)
}
if len(app.SiteTemplate) == 0 {
t.Errorf("expected SiteTemplate to be populated")
}
if len(app.Pages) == 0 {
t.Errorf("expected Pages to be populated")
}
if len(app.Layouts) == 0 {
t.Errorf("expected Layouts to be populated")
}
if app.DistDir != "dist" {
t.Errorf("expected DistDir to be 'dist', got %v", app.DistDir)
}
// check that non-page files were moved
_, err = os.Stat(filepath.Join(app.DistDir, "static", "main.css"))
if err != nil {
t.Errorf("expected main.css to exist, got %v", err)
}
// check that non-page md files were not moved
_, err = os.Stat(filepath.Join(app.DistDir, "README.html"))
if err == nil {
t.Errorf("expected README.html to not exist, got %v", err)
}
_, err = os.Stat(filepath.Join(app.DistDir, "README.md"))
if err == nil {
t.Errorf("expected README.md to not exist, got %v", err)
}
}
func TestInitAppEmptyTemplate(t *testing.T) {
srcTest := "src_test_empty_template"
app, err := InitApp(srcTest)
defer cleanup(app.DistDir)
if err == nil {
t.Errorf("expected InitApp to return an error, got %v", err)
}
}
func TestRenderPage(t *testing.T) {
srcTest := "src_test"
app, _ := InitApp(srcTest)
defer cleanup(app.DistDir)
for _, page := range app.Pages {
err := app.renderPage(page)
if err != nil {
t.Errorf("expected renderPage to return no error, got %v", err)
}
}
// check that the files were created
_, err := os.Stat(filepath.Join(app.DistDir, "index.html"))
if err != nil {
t.Errorf("expected index.html to exist, got %v", err)
}
_, err = os.Stat(filepath.Join(app.DistDir, "pages", "example.html"))
if err != nil {
t.Errorf("expected example.html to exist, got %v", err)
}
}
func TestGetPage(t *testing.T) {
srcTest := "src_test"
app, _ := InitApp(srcTest)
defer cleanup(app.DistDir)
page, err := app.getPage(filepath.Join(srcTest, "pages", "example.md"))
if err != nil {
t.Errorf("expected getPage to return no error, got %v", err)
}
if page.Title != "Example page title" {
t.Errorf("expected page title to be 'Example page title', got %v", page.Title)
}
if page.Layout != "pages" {
t.Errorf("expected page layout to be 'pages', got %v", page.Layout)
}
}
func TestBuild(t *testing.T) {
srcTest := "src_test"
defer cleanup("dist")
Build(srcTest)
// check that the files were created
_, err := os.Stat(filepath.Join("dist", "index.html"))
if err != nil {
t.Errorf("expected index.html to exist, got %v", err)
}
_, err = os.Stat(filepath.Join("dist", "pages", "example.html"))
if err != nil {
t.Errorf("expected example.html to exist, got %v", err)
}
}
func TestBuildNoSquatchFile(t *testing.T) {
srcTest := "src_test"
defer cleanup("dist")
defer os.Rename(filepath.Join(srcTest, ".bak"), filepath.Join(srcTest, ".squatch"))
err := os.Rename(filepath.Join(srcTest, ".squatch"), filepath.Join(srcTest, ".bak"))
if err != nil {
t.Errorf("expected to rename .squatch, got %v", err)
}
Build(srcTest)
_, err = os.Stat(filepath.Join("dist", "index.html"))
if err != nil {
t.Errorf("expected index.html to exist, got %v", err)
}
_, err = os.Stat(filepath.Join("dist", "pages", "example.html"))
if err != nil {
t.Errorf("expected example.html to exist, got %v", err)
}
}