-
Notifications
You must be signed in to change notification settings - Fork 0
/
project_test.go
321 lines (242 loc) · 7.66 KB
/
project_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
package main
import (
"io/ioutil"
"os"
"path/filepath"
"testing"
"time"
)
func TestCheckNilError(t *testing.T) {
var err error = nil
checkError(err)
}
func TestCreateProject(t *testing.T) {
const project string = "test_dir"
params := &Parameters{
ProjectName: project,
}
createProject(params)
assert(t, checkIfExists(project), true)
assert(t, checkIfExists(filepath.Join(project, "source")), true)
assert(t, checkIfExists(filepath.Join(project, "source", "posts")), true)
assert(t, checkIfExists(filepath.Join(project, "source", "pages")), true)
assert(t, checkIfExists(filepath.Join(project, "source", "data")), true)
assert(t, checkIfExists(filepath.Join(project, "themes")), true)
assert(t, checkIfExists(filepath.Join(project, "themes", "default", "static")), true)
assert(t, checkIfExists(filepath.Join(project, "source", "index.md")), true)
assert(t, checkIfExists(filepath.Join(project, "build")), false)
assert(t, checkIfExists(filepath.Join(project, "scipio.toml")), true)
os.RemoveAll(project)
}
func getDefaultConfig() *config {
build := buildConfig{RemoveBuildDir: true}
return &config{Build: build, OutputExtension: ".html", LinksBeginWithSlash: false}
}
func TestCleanBuild(t *testing.T) {
const project string = "test_dir"
params := &Parameters{
ProjectName: project,
}
createProject(params)
buildProject(getDefaultConfig(), params)
createFile(filepath.Join(project, "build", "test_file"))
assert(t, checkIfExists(filepath.Join(project, "build", "test_file")), true)
cleanBuild(params)
assert(t, checkIfExists(filepath.Join(project, "build", "test_file")), false)
os.RemoveAll(project)
}
func createTestSourceFiles(project string) {
filePost, _ := os.Create(filepath.Join(project, "source", "posts", "post_1.md"))
defer filePost.Close()
post := `---
title: Article 1
created: 1950-05-15T00:00:00Z
description: Description of Article 1
keywords: scipio, tests, go
tags: tag1, tag2
---
Body of Article 1.
- *Test 1*
- Go
- Scipio
`
filePost.WriteString(post)
filePage, _ := os.Create(filepath.Join(project, "source", "pages", "page_1.md"))
defer filePage.Close()
page := `---
title: Article 2
created: 1960-05-15T00:00:00Z
description: Description of Article 2
keywords: go, and, stuff
tags: tag1, tag2
---
Body of Article 2.
`
filePage.WriteString(page)
fileIndex, _ := os.Create(filepath.Join(project, "source", "index.md"))
defer fileIndex.Close()
index := `---
title: Index Page
created: 1970-05-15T00:00:00Z
description: Index Page Description
keywords: go, and, stuff
tags: tag1, tag2
---
Home Page
`
fileIndex.WriteString(index)
}
func createTestTheme(project string) {
fileTopMenuTemplate, err := os.Create(filepath.Join(project, "themes", "default", "top_menu.html"))
defer fileTopMenuTemplate.Close()
checkError(err)
filePost, err := os.Create(filepath.Join(project, "themes", "default", "post.html"))
defer filePost.Close()
checkError(err)
filePostList, err := os.Create(filepath.Join(project, "themes", "default", "posts.html"))
defer filePostList.Close()
checkError(err)
fileTopMenuTemplate.WriteString("top menu")
postTemplate := `<html>
<head>
<meta name="description" content="{{description}}">
<meta name="keywords" content="{{keywords}}">
<meta name="author" content="">
<link rel="stylesheet" type="text/css" href="static/styles.css">
</head>
<body>
{{#include top_menu.html}}
<div>{{@article-2}}</div>
<h1>{{title}}</h1>
{{date}}
<p>{{body}}</p>
</body>
</html>`
filePost.WriteString(postTemplate)
filePage, errPage := os.Create(filepath.Join(project, "themes", "default", "page.html"))
defer filePage.Close()
checkError(errPage)
pageTemplate := `<html>
<head>
<meta name="description" content="{{description}}">
<meta name="keywords" content="{{keywords}}">
<meta name="author" content="">
<link rel="stylesheet" type="text/css" href="static/styles.css">
</head>
<body>
{{#include top_menu.html}}
<div>{{@article-2}}</div>
<h1>{{title}}</h1>
{{date}}
<p>{{body}}</p>
</body>
</html>`
filePage.WriteString(pageTemplate)
fileIndex, errIndex := os.Create(filepath.Join(project, "themes", "default", "index.html"))
defer fileIndex.Close()
checkError(errIndex)
indexTemplate := `<html>
<head>
<meta name="description" content="{{description}}">
<meta name="keywords" content="{{keywords}}">
<meta name="author" content="">
<link rel="stylesheet" type="text/css" href="static/styles.css">
</head>
<body>
{{#include top_menu.html}}
<div>{{@article-2}}</div>
<h1>{{title}}</h1>
{{date}}
<p>{{body}}</p>
{{posts-begin}}
<li>{{post_link}}</li>
<span class="date">{{post_date}}</span>
{{posts-end}}
</body>
</html>`
fileIndex.WriteString(indexTemplate)
}
func TestParseSourceFile(t *testing.T) {
const project string = "test_dir"
params := &Parameters{
ProjectName: project,
}
createProject(params)
createTestSourceFiles(project)
data := parseSourceFile(filepath.Join(project, "source", "posts", "post_1.md"), POST)
assert(t, data.title, "Article 1")
assert(t, data.description, "Description of Article 1")
assert(t, data.keywords[0], "scipio")
assert(t, data.keywords[1], "tests")
assert(t, data.keywords[2], "go")
assert(t, len(data.keywords), 3)
assert(t, data.tags[0], "tag1")
assert(t, data.tags[1], "tag2")
assert(t, len(data.tags), 2)
assert(t, data.created, time.Date(1950, time.May, 15, 0, 0, 0, 0, time.UTC))
assert(t, data.entryType, POST)
assert(t, data.body, `Body of Article 1.
- *Test 1*
- Go
- Scipio`)
os.RemoveAll(project)
}
func TestBuildProject(t *testing.T) {
const project string = "test_dir"
params := &Parameters{
ProjectName: project,
}
createProject(params)
createDir(filepath.Join(project, "themes", "default"))
createTestSourceFiles(project)
createTestTheme(project)
buildProject(getDefaultConfig(), params)
html, err := ioutil.ReadFile(filepath.Join(project, "build", "article-1.html"))
checkError(err)
expected := `<html>
<head>
<meta name="description" content="Description of Article 1">
<meta name="keywords" content="scipio, tests, go">
<meta name="author" content="">
<link rel="stylesheet" type="text/css" href="static/styles.css">
</head>
<body>
top menu
<div><a href="article-2.html" title="Description of Article 2">Article 2</a></div>
<h1>Article 1</h1>
1950-05-15
<p><p>Body of Article 1.</p>
<ul>
<li><p><em>Test 1</em></p></li>
<li><p>Go</p></li>
<li><p>Scipio</p></li>
</ul>
</p>
</body>
</html>`
assert(t, expected, string(html))
indexHtml, indexErr := ioutil.ReadFile(filepath.Join(project, "build", "index.html"))
checkError(indexErr)
expectedIndex := `<html>
<head>
<meta name="description" content="Index Page Description">
<meta name="keywords" content="go, and, stuff">
<meta name="author" content="">
<link rel="stylesheet" type="text/css" href="static/styles.css">
</head>
<body>
top menu
<div><a href="article-2.html" title="Description of Article 2">Article 2</a></div>
<h1>Index Page</h1>
1970-05-15
<p><p>Home Page</p>
</p>
<li><a href="welcome-to-your-new-website.html" title="Description of the first post">Welcome to your new website</a></li>
<span class="date">2021-01-27</span>
<li><a href="article-1.html" title="Description of Article 1">Article 1</a></li>
<span class="date">1950-05-15</span>
</body>
</html>`
assert(t, expectedIndex, string(indexHtml))
os.RemoveAll(project)
}