-
-
Notifications
You must be signed in to change notification settings - Fork 1
/
test.js
129 lines (94 loc) · 4.02 KB
/
test.js
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
const test = require('tape')
const fs = require('fs-extra')
const http = require('http')
const hugo = new (require('./index'))()
async function httpGet (url) {
return new Promise((resolve, reject) => {
http.get(url, (response) => {
const statusCode = response.statusCode
const location = response.headers.location
// Reject if it’s not one of the status codes we are testing.
if (statusCode !== 200 && statusCode !== 404 && statusCode !== 500 && statusCode !== 302) {
reject({statusCode})
}
let body = ''
response.on('data', _ => body += _)
response.on('end', () => {
resolve({statusCode, location, body})
})
})
})
}
test('[hugo.command]', async t => {
const sourcePath = 'test/a-new-site'
if (fs.existsSync(sourcePath)) {
fs.removeSync(sourcePath)
}
const args = `new site ${sourcePath}`
let hasError = false
let output = null
try {
// Tell the built-in Hugo binary to create a new site at ./my-new-site/.
output = await hugo.command(args)
} catch (error) {
hasError = true
}
t.false(hasError, 'does not throw an error')
t.true(output.includes('Congratulations! Your new Hugo site is created'), 'new site is generated')
t.true(fs.existsSync(sourcePath), 'generated site exists where we expect it')
const generatedFiles = fs.readdirSync(sourcePath)
t.true(generatedFiles.join(',') === 'archetypes,config.toml,content,data,layouts,static,themes', 'contents of generated site are as expected')
t.end()
})
test('[hugo.build] ', async t => {
const sourcePath = 'test/site'
const destinationPath = '../public' /* NB. relative to source path */
const baseURL = 'http://localhost:1313'
if (fs.existsSync(destinationPath)) {
fs.removeSync(destinationPath)
}
let hasError = false
let output = null
try {
output = await hugo.build(sourcePath, destinationPath, baseURL)
} catch (error) {
hasError = true
}
t.false(hasError, 'does not throw an error')
const deflatedOutput = output.replace(/\s/g, '')
// Ensure build statistics are as we expect.
t.true(deflatedOutput.includes('Pages|2Paginatorpages|0Non-pagefiles|1Staticfiles|0Processedimages|0Aliases|0Sitemaps|1Cleaned|0'), 'console output statistics are as expected')
const deflatedIndexPageHTML = fs.readFileSync('test/public/index.html', 'utf-8').replace(/\s/g, '')
const expectedDeflatedIndexPageHTML = '<!DOCTYPEhtml><htmllang="en"><head><metaname="generator"content="Hugo0.78.0"/><metacharset="UTF-8"><metaname="viewport"content="width=device-width,initial-scale=1.0"><title>Yes!</title></head><body><h1>Yes!</h1><p>node-hugo<strong>works!</strong></p></body></html>'
// Ensure generated HTML is as we expect.
t.strictEquals(expectedDeflatedIndexPageHTML, deflatedIndexPageHTML, 'index page HTML is as expected')
t.end()
})
test('[hugo.serve] ', async t => {
const sourcePath = 'test/site'
const destinationPath = '../public' /* NB. relative to source path */
const baseURL = 'http://localhost:1313'
if (fs.existsSync(destinationPath)) {
fs.removeSync(destinationPath)
}
const {hugoServerProcess, hugoBuildOutput} = await hugo.serve(sourcePath, destinationPath, baseURL)
const hugoBuildOutputDeflated = hugoBuildOutput.replace(/\s/g, '')
t.false(hugoBuildOutputDeflated.includes('WARN'), 'build did not encounter a warning')
t.true(hugoBuildOutputDeflated.includes('Pages|2'), 'two pages are rendered')
t.true(hugoBuildOutputDeflated.includes('Non-pagefiles|1'), 'one non-page file is rendered')
t.true(hugoBuildOutputDeflated.includes('Sitemaps|1'), 'one sitemap is rendered')
hugoServerProcess.kill()
await new Promise((resolve, reject) => {
hugoServerProcess.on('exit', (code) => {
t.assert(true, 'server process exited via kill()')
resolve()
})
})
t.end()
})
test('[hugo.version]', async t => {
const output = await hugo.command('version')
const versionFromHugo = output.match(/v(\d+\.\d+\.\d+)-/)[1]
t.strictEquals(hugo.version, versionFromHugo, 'Hugo version is correct.')
t.end()
})