Verified Commit 47033980 authored by Aral Balkan's avatar Aral Balkan
Browse files

Rewrite tests for new server behaviour

parent ad650e75
......@@ -85,14 +85,14 @@ class Hugo {
// Wait for the line telling us that the build is complete and that
// the server is ready and only then return.
let outputSoFar = ''
let hugoBuildOutput = ''
let buildComplete = false
const stdoutHandler = (data) => {
const lines = data.toString('utf-8').split('\n')
lines.forEach(line => {
outputSoFar += `\n${line}`
hugoBuildOutput += `\n${line}`
if (line.startsWith('Built in')) {
// The site has been built. Let’s resolve the promise.
......@@ -108,8 +108,8 @@ class Hugo {
// OK.
resolve({
process: hugoServerProcess,
output: outputSoFar
hugoServerProcess,
hugoBuildOutput
})
}
}
......
......@@ -60,7 +60,7 @@ test('[hugo-build] ', async t => {
test('[hugo-serve] ', async t => {
t.plan(9)
t.plan(5)
const sourcePath = 'test/site'
const destinationPath = '../public' /* NB. relative to source path */
......@@ -70,50 +70,19 @@ test('[hugo-serve] ', async t => {
fs.removeSync(destinationPath)
}
const hugoServerProcess = hugo.serve(sourcePath, destinationPath, baseURL)
let hasError = false
let hasWarning = false
let builtInMessageShown = false
let webServerMessageIsShown = false
let pressCtrlCToStopMessageIsShown = false
hugoServerProcess.stdout.on('data', (data) => {
const lines = data.toString('utf-8').split('\n')
lines.forEach(line => {
const deflatedLine = line.replace(/\s/g, '')
if (line.includes('WARN')) { hasWarning = true }
if (line.includes('Built in')) { builtInMessageShown = true }
if (line.includes('Web Server is available at http://localhost:1313/')) { webServerMessageIsShown = true }
if (line.includes('Press Ctrl+C to stop')) { pressCtrlCToStopMessageIsShown = true } // last message once server is ready
if (line.includes('Pages')) { t.strictEquals(deflatedLine, 'Pages|2', 'two pages are rendered') }
if (line.includes('Non-page files')) { t.strictEquals(deflatedLine, 'Non-pagefiles|1', 'one non-page file is rendered') }
if (line.includes('Sitemaps')) { t.strictEquals(deflatedLine, 'Sitemaps|1', 'one sitemap is rendered') }
const {hugoServerProcess, hugoBuildOutput} = await hugo.serve(sourcePath, destinationPath, baseURL)
})
})
hugoServerProcess.stderr.on('data', (data) => {
hasError = true
hugoServerProcess.on('exit', (code) => {
t.assert(true, 'server process exited via kill()')
t.end()
})
hugoServerProcess.on('close', (code) => {
t.assert(true, 'server process is closed via kill()')
t.false(hasError, 'server did not encounter an error')
t.false(hasWarning, 'server did not encounter a warning')
t.true(builtInMessageShown, 'server output included the “built in N ms” message')
t.true(webServerMessageIsShown, 'server output included “web server is available” message')
t.true(pressCtrlCToStopMessageIsShown, 'server output included “press CTRL+C to stop” message (last message in server launch)')
hugoServerProcess.kill()
})
const hugoBuildOutputDeflated = hugoBuildOutput.replace(/\s/g, '')
setTimeout(() => {
// Stop the process.
hugoServerProcess.kill()
}, 250)
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')
})
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment