Unverified Commit 671d6ade authored by Renée Kooi's avatar Renée Kooi Committed by GitHub
Browse files

Use `tmp` module throughout tests (#498)

* Use `tmp` module throughout tests

It was already being used in some places, and is a bit less verbose.

* Create ./tmp before running tests.

* Remove unused dependency

* debug

* is it less flakey with an unreleased version of tmp?
parent d907c26d
var dedent = require('dedent')
var rimraf = require('rimraf')
var path = require('path')
var tape = require('tape')
var tmp = require('tmp')
var fs = require('fs')
var os = require('os')
var bankai = require('../')
var tmpDirname
function cleanup () {
rimraf.sync(tmpDirname)
}
tape('run an asset pipeline', function (assert) {
assert.on('end', cleanup)
var script = dedent`
1 + 1
`
......@@ -23,14 +15,13 @@ tape('run an asset pipeline', function (assert) {
hello planet
`
var dirname = 'asset-pipeline-' + (Math.random() * 1e4).toFixed()
tmpDirname = path.join(os.tmpdir(), dirname)
var assetDirname = path.join(os.tmpdir(), dirname, 'assets')
var tmpDir = tmp.dirSync({ dir: path.join(__dirname, '../tmp'), unsafeCleanup: true })
assert.on('end', tmpDir.removeCallback)
var assetDirname = path.join(tmpDir.name, 'assets')
var tmpScriptname = path.join(tmpDirname, 'index.js')
var tmpScriptname = path.join(tmpDir.name, 'index.js')
var tmpFilename = path.join(assetDirname, 'file.txt')
fs.mkdirSync(tmpDirname)
fs.mkdirSync(assetDirname)
fs.writeFileSync(tmpScriptname, script)
fs.writeFileSync(tmpFilename, file)
......@@ -53,8 +44,6 @@ tape('run an asset pipeline', function (assert) {
})
tape('use correct asset dir when entry point is a dir', function (assert) {
assert.on('end', cleanup)
var script = dedent`
document.body.textContent = 'Whatever'
`
......@@ -62,19 +51,18 @@ tape('use correct asset dir when entry point is a dir', function (assert) {
a file!!!
`
var dirname = 'asset-pipeline-' + (Math.random() * 1e4).toFixed()
tmpDirname = path.join(os.tmpdir(), dirname)
var assetDirname = path.join(os.tmpdir(), dirname, 'assets')
var tmpDir = tmp.dirSync({ dir: path.join(__dirname, '../tmp'), unsafeCleanup: true })
assert.on('end', tmpDir.removeCallback)
var assetDirname = path.join(tmpDir.name, 'assets')
var tmpScriptname = path.join(tmpDirname, 'index.js')
var tmpScriptname = path.join(tmpDir.name, 'index.js')
var tmpFilename = path.join(assetDirname, 'file.txt')
fs.mkdirSync(tmpDirname)
fs.mkdirSync(assetDirname)
fs.writeFileSync(tmpScriptname, script)
fs.writeFileSync(tmpFilename, file)
var compiler = bankai(tmpDirname, { watch: false })
var compiler = bankai(tmpDir.name, { watch: false })
compiler.on('error', function (name, sub, err) {
assert.error(err, 'no error')
......
var assertHtml = require('assert-html')
var dedent = require('dedent')
var mkdirp = require('mkdirp')
var rimraf = require('rimraf')
var path = require('path')
var tape = require('tape')
var tmp = require('tmp')
var fs = require('fs')
var bankai = require('../')
var tmpDirname
function cleanup () {
rimraf.sync(tmpDirname)
}
tape('renders some HTML', function (assert) {
assert.on('end', cleanup)
var expected = `
<!DOCTYPE html>
<html lang="en-US" dir="ltr">
......@@ -39,11 +31,11 @@ tape('renders some HTML', function (assert) {
1 + 1
`
var dirname = 'document-pipeline-' + (Math.random() * 1e4).toFixed()
tmpDirname = path.join(__dirname, '../tmp', dirname)
var tmpScriptname = path.join(tmpDirname, 'index.js')
var tmpDir = tmp.dirSync({ dir: path.join(__dirname, '../tmp'), unsafeCleanup: true })
assert.on('end', tmpDir.removeCallback)
var tmpScriptname = path.join(tmpDir.name, 'index.js')
mkdirp.sync(tmpDirname)
mkdirp.sync(tmpDir.name)
fs.writeFileSync(tmpScriptname, script)
var compiler = bankai(tmpScriptname, { watch: false })
......@@ -108,13 +100,12 @@ tape('server render choo apps', function (assert) {
else app.mount('body')
`
var dirname = 'document-pipeline-' + (Math.random() * 1e4).toFixed()
tmpDirname = path.join(__dirname, '../tmp', dirname)
var tmpScriptname = path.join(tmpDirname, 'index.js')
var assetDirname = path.join(tmpDirname, 'assets')
var tmpDir = tmp.dirSync({ dir: path.join(__dirname, '../tmp'), unsafeCleanup: true })
assert.on('end', tmpDir.removeCallback)
var tmpScriptname = path.join(tmpDir.name, 'index.js')
var assetDirname = path.join(tmpDir.name, 'assets')
var fontFilename = path.join(assetDirname, 'font.woff')
mkdirp.sync(tmpDirname)
mkdirp.sync(assetDirname)
fs.writeFileSync(tmpScriptname, script)
......@@ -149,8 +140,6 @@ tape('server render choo apps', function (assert) {
})
tape('server render choo apps with root set', function (assert) {
assert.on('end', cleanup)
var expected = `
<!DOCTYPE html>
<html lang="en-US" dir="ltr">
......@@ -184,13 +173,12 @@ tape('server render choo apps with root set', function (assert) {
else app.mount('body')
`
var dirname = 'document-pipeline-' + (Math.random() * 1e4).toFixed()
tmpDirname = path.join(__dirname, '../tmp', dirname)
var tmpScriptname = path.join(tmpDirname, 'index.js')
var assetDirname = path.join(tmpDirname, 'assets')
var tmpDir = tmp.dirSync({ dir: path.join(__dirname, '../tmp'), unsafeCleanup: true })
assert.on('end', tmpDir.removeCallback)
var tmpScriptname = path.join(tmpDir.name, 'index.js')
var assetDirname = path.join(tmpDir.name, 'assets')
var fontFilename = path.join(assetDirname, 'font.woff')
mkdirp.sync(tmpDirname)
mkdirp.sync(assetDirname)
fs.writeFileSync(tmpScriptname, script)
......@@ -225,7 +213,6 @@ tape('server render choo apps with root set', function (assert) {
})
tape('custom index.html template', function (assert) {
assert.on('end', cleanup)
assert.plan(3)
var template = `
......@@ -248,13 +235,12 @@ tape('custom index.html template', function (assert) {
module.exports = app.mount('body')
`
var dirname = 'document-pipeline-' + (Math.random() * 1e4).toFixed()
tmpDirname = path.join(__dirname, '../tmp', dirname)
mkdirp.sync(tmpDirname)
fs.writeFileSync(path.join(tmpDirname, 'index.js'), file)
fs.writeFileSync(path.join(tmpDirname, 'index.html'), template)
var tmpDir = tmp.dirSync({ dir: path.join(__dirname, '../tmp'), unsafeCleanup: true })
assert.on('end', tmpDir.removeCallback)
fs.writeFileSync(path.join(tmpDir.name, 'index.js'), file)
fs.writeFileSync(path.join(tmpDir.name, 'index.html'), template)
var compiler = bankai(tmpDirname, { watch: false })
var compiler = bankai(tmpDir.name, { watch: false })
compiler.documents('/', function (err, res) {
assert.error(err, 'no error writing document')
var body = res.buffer.toString('utf8')
......@@ -264,7 +250,6 @@ tape('custom index.html template', function (assert) {
})
tape('mount choo app into given selector', function (assert) {
assert.on('end', cleanup)
assert.plan(3)
var template = `
......@@ -287,13 +272,12 @@ tape('mount choo app into given selector', function (assert) {
module.exports = app.mount('#app')
`
var dirname = 'document-pipeline-' + (Math.random() * 1e4).toFixed()
tmpDirname = path.join(__dirname, '../tmp', dirname)
mkdirp.sync(tmpDirname)
fs.writeFileSync(path.join(tmpDirname, 'index.js'), file)
fs.writeFileSync(path.join(tmpDirname, 'index.html'), template)
var tmpDir = tmp.dirSync({ dir: path.join(__dirname, '../tmp'), unsafeCleanup: true })
assert.on('end', tmpDir.removeCallback)
fs.writeFileSync(path.join(tmpDir.name, 'index.js'), file)
fs.writeFileSync(path.join(tmpDir.name, 'index.html'), template)
var compiler = bankai(tmpDirname, { watch: false })
var compiler = bankai(tmpDir.name, { watch: false })
compiler.documents('/', function (err, res) {
assert.error(err, 'no error writing document')
var body = res.buffer.toString('utf8')
......@@ -303,7 +287,6 @@ tape('mount choo app into given selector', function (assert) {
})
tape('inlines critical css', function (assert) {
assert.on('end', cleanup)
assert.plan(3)
var file = `
......@@ -326,12 +309,11 @@ tape('inlines critical css', function (assert) {
module.exports = app.mount('body')
`
var dirname = 'document-pipeline-' + (Math.random() * 1e4).toFixed()
tmpDirname = path.join(__dirname, '../tmp', dirname)
mkdirp.sync(tmpDirname)
fs.writeFileSync(path.join(tmpDirname, 'index.js'), file)
var tmpDir = tmp.dirSync({ dir: path.join(__dirname, '../tmp'), unsafeCleanup: true })
assert.on('end', tmpDir.removeCallback)
fs.writeFileSync(path.join(tmpDir.name, 'index.js'), file)
var compiler = bankai(tmpDirname, { watch: false })
var compiler = bankai(tmpDir.name, { watch: false })
compiler.documents('/', function (err, res) {
assert.error(err, 'no error writing document')
var body = res.buffer.toString('utf8')
......
var dedent = require('dedent')
var rimraf = require('rimraf')
var path = require('path')
var tape = require('tape')
var fs = require('fs')
var os = require('os')
var http = require('http')
var tmp = require('tmp')
var fs = require('fs')
var bankai = require('../http')
var tmpDirname, tmpScriptname
var tmpScriptname
function cleanup () {
rimraf.sync(tmpDirname)
}
function setup () {
function setup (assert) {
var script = dedent`
var css = require('sheetify')
var html = require('bel')
......@@ -28,13 +23,13 @@ function setup () {
hello planet
`
var dirname = 'manifest-pipeline-' + (Math.random() * 1e4).toFixed()
tmpDirname = path.join(os.tmpdir(), dirname)
var contentDirname = path.join(tmpDirname, 'content')
var assetDirname = path.join(tmpDirname, 'assets')
var tmpDir = tmp.dirSync({ dir: path.join(__dirname, '../tmp'), unsafeCleanup: true })
assert.on('end', tmpDir.removeCallback)
var contentDirname = path.join(tmpDir.name, 'content')
var assetDirname = path.join(tmpDir.name, 'assets')
var assetSubdirname = path.join(assetDirname, 'images')
tmpScriptname = path.join(tmpDirname, 'index.js')
tmpScriptname = path.join(tmpDir.name, 'index.js')
var tmpFilename = path.join(contentDirname, 'file.txt')
var tmpAssetJsFilename = path.join(assetDirname, 'file.js')
var tmpAssetCssFilename = path.join(assetDirname, 'file.css')
......@@ -42,7 +37,6 @@ function setup () {
var tmpJpgFilename = path.join(assetDirname, 'file.jpg')
var tmpJpgSubFilename = path.join(assetSubdirname, 'file.jpg')
fs.mkdirSync(tmpDirname)
fs.mkdirSync(contentDirname)
fs.mkdirSync(assetDirname)
fs.mkdirSync(assetSubdirname)
......@@ -56,7 +50,7 @@ function setup () {
}
tape('should route urls appropriately', function (assert) {
setup()
setup(assert)
var handler = bankai(tmpScriptname, { watch: false, quiet: true })
var server = http.createServer(function (req, res) {
handler(req, res, function () {
......@@ -69,8 +63,6 @@ tape('should route urls appropriately', function (assert) {
console.log('listening on port 3030')
})
assert.on('end', cleanup)
var urls = [
'/bundle.js',
'/bundle.js?cache=busted',
......
var path = require('path')
var mkdirp = require('mkdirp')
mkdirp.sync(path.join(__dirname, '../tmp'))
require('./assets')
require('./document')
require('./http')
......
var dedent = require('dedent')
var rimraf = require('rimraf')
var path = require('path')
var tape = require('tape')
var tmp = require('tmp')
var fs = require('fs')
var os = require('os')
var bankai = require('../')
var tmpDirname
function cleanup () {
rimraf.sync(tmpDirname)
}
tape('read a manifest', function (assert) {
assert.on('end', cleanup)
assert.plan(5)
var script = dedent`
1 + 1
......@@ -36,12 +29,11 @@ tape('read a manifest', function (assert) {
}
`
var dirname = 'manifest-pipeline-' + (Math.random() * 1e4).toFixed()
tmpDirname = path.join(os.tmpdir(), dirname)
var tmpScriptname = path.join(tmpDirname, 'index.js')
var tmpManifestname = path.join(tmpDirname, 'manifest.json')
var tmpDir = tmp.dirSync({ dir: path.join(__dirname, '../tmp'), unsafeCleanup: true })
assert.on('end', tmpDir.removeCallback)
var tmpScriptname = path.join(tmpDir.name, 'index.js')
var tmpManifestname = path.join(tmpDir.name, 'manifest.json')
fs.mkdirSync(tmpDirname)
fs.writeFileSync(tmpScriptname, script)
fs.writeFileSync(tmpManifestname, manifest)
......@@ -59,18 +51,16 @@ tape('read a manifest', function (assert) {
})
tape('should provide a default manifest', function (assert) {
assert.on('end', cleanup)
assert.plan(3)
var script = dedent`
1 + 1
`
var dirname = 'manifest-pipeline-' + (Math.random() * 1e4).toFixed()
tmpDirname = path.join(os.tmpdir(), dirname)
var tmpScriptname = path.join(tmpDirname, 'index.js')
var tmpDir = tmp.dirSync({ dir: path.join(__dirname, '../tmp'), unsafeCleanup: true })
assert.on('end', tmpDir.removeCallback)
var tmpScriptname = path.join(tmpDir.name, 'index.js')
fs.mkdirSync(tmpDirname)
fs.writeFileSync(tmpScriptname, script)
var compiler = bankai(tmpScriptname, { watch: false })
......@@ -87,7 +77,6 @@ tape('should provide a default manifest', function (assert) {
tape('should watch the manifest for changes', function (assert) {
assert.on('end', function () {
compiler.close()
cleanup()
})
assert.plan(12)
......@@ -103,12 +92,11 @@ tape('should watch the manifest for changes', function (assert) {
{ "name": "bar" }
`
var dirname = 'manifest-pipeline-' + (Math.random() * 1e4).toFixed()
tmpDirname = path.join(os.tmpdir(), dirname)
var tmpScriptname = path.join(tmpDirname, 'index.js')
var tmpManifestname = path.join(tmpDirname, 'manifest.json')
var tmpDir = tmp.dirSync({ dir: path.join(__dirname, '../tmp'), unsafeCleanup: true })
assert.on('end', tmpDir.removeCallback)
var tmpScriptname = path.join(tmpDir.name, 'index.js')
var tmpManifestname = path.join(tmpDir.name, 'manifest.json')
fs.mkdirSync(tmpDirname)
fs.writeFileSync(tmpScriptname, script)
fs.writeFileSync(tmpManifestname, manifest1)
......
var dedent = require('dedent')
var rimraf = require('rimraf')
var mkdirp = require('mkdirp')
var path = require('path')
var tape = require('tape')
var tmp = require('tmp')
var fs = require('fs')
var vm = require('vm')
var os = require('os')
var tmp = require('tmp')
var bankai = require('../')
......@@ -16,17 +14,16 @@ tape('run a JS pipeline', function (assert) {
1 + 1
`
var filename = 'js-pipeline-' + (Math.random() * 1e4).toFixed() + '.js'
var tmpFilename = path.join(os.tmpdir(), filename)
fs.writeFileSync(tmpFilename, file)
var tmpFile = tmp.fileSync({ dir: path.join(__dirname, '../tmp'), discardDescriptor: true, postfix: '.js' })
assert.on('end', tmpFile.removeCallback)
fs.writeFileSync(tmpFile.name, file)
var compiler = bankai(tmpFilename, { watch: false })
var compiler = bankai(tmpFile.name, { watch: false })
compiler.scripts('bundle.js', function (err, res) {
assert.error(err, 'no error writing script')
assert.ok(res, 'output exists')
assert.ok(res.buffer, 'output buffer exists')
assert.ok(res.hash, 'output hash exists')
rimraf.sync(tmpFilename)
})
})
......@@ -36,17 +33,15 @@ tape('use a folder as an entry point', function (assert) {
console.log(1 + 1)
`
var dirname = 'js-pipeline-' + (Math.random() * 1e4).toFixed()
var tmpDirname = path.join(os.tmpdir(), dirname)
var tmpScriptname = path.join(tmpDirname, 'index.js')
fs.mkdirSync(tmpDirname)
var tmpDir = tmp.dirSync({ dir: path.join(__dirname, '../tmp'), unsafeCleanup: true })
assert.on('end', tmpDir.removeCallback)
var tmpScriptname = path.join(tmpDir.name, 'index.js')
fs.writeFileSync(tmpScriptname, file)
var compiler = bankai(tmpDirname, { watch: false })
var compiler = bankai(tmpDir.name, { watch: false })
compiler.scripts('bundle.js', function (err, res) {
assert.error(err, 'no error writing script')
assert.notEqual(res.buffer.toString('utf8').indexOf('console.log'), -1, 'contains js')
rimraf.sync(tmpDirname)
})
})
......@@ -56,14 +51,13 @@ tape('return an error if an incorrect script is selected', function (assert) {
1 + 1
`
var filename = 'js-pipeline-' + (Math.random() * 1e4).toFixed() + '.js'
var tmpFilename = path.join(os.tmpdir(), filename)
fs.writeFileSync(tmpFilename, file)
var tmpFile = tmp.fileSync({ dir: path.join(__dirname, '../tmp'), discardDescriptor: true, postfix: '.js' })
assert.on('end', tmpFile.removeCallback)
fs.writeFileSync(tmpFile.name, file)
var compiler = bankai(tmpFilename, { watch: false })
var compiler = bankai(tmpFile.name, { watch: false })
compiler.scripts('bad-bad-not-good.js', function (err, res) {
assert.ok(err, 'error writing script')
rimraf.sync(tmpFilename)
})
})
......@@ -78,19 +72,17 @@ tape('output multiple bundles if `split-require` is used', function (assert) {
module.exports = null
`
var tmpDirname = path.join(__dirname, '../tmp', 'js-pipeline-' + (Math.random() * 1e4).toFixed())
mkdirp.sync(tmpDirname)
fs.writeFileSync(path.join(tmpDirname, 'app.js'), file)
fs.writeFileSync(path.join(tmpDirname, 'dynamic.js'), dynamicFile)
var tmpDir = tmp.dirSync({ dir: path.join(__dirname, '../tmp'), unsafeCleanup: true })
assert.on('end', tmpDir.removeCallback)
fs.writeFileSync(path.join(tmpDir.name, 'app.js'), file)
fs.writeFileSync(path.join(tmpDir.name, 'dynamic.js'), dynamicFile)
var compiler = bankai(path.join(tmpDirname, 'app.js'), { watch: false })
var compiler = bankai(path.join(tmpDir.name, 'app.js'), { watch: false })
compiler.scripts('bundle.js', function (err, res) {
assert.error(err, 'error writing main bundle')
assert.error(err, 'no error writing main bundle')
var dynamicName = /bundle-\d+\.js/.exec(res.buffer.toString('utf-8'))
compiler.scripts(dynamicName[0], function (err, res) {
assert.error(err, 'error writing dynamic bundle')
rimraf.sync(tmpDirname)
assert.error(err, 'no error writing dynamic bundle')
})
})
})
......@@ -116,13 +108,13 @@ tape('use custom babel config for local files, but not for dependencies', functi
require('a-module-with-babelrc')
`
var tmpDirname = path.join(__dirname, '../tmp', 'js-pipeline-' + (Math.random() * 1e4).toFixed())
mkdirp.sync(tmpDirname)
fs.writeFileSync(path.join(tmpDirname, 'plugin.js'), babelPlugin)
fs.writeFileSync(path.join(tmpDirname, '.babelrc'), babelrc)
fs.writeFileSync(path.join(tmpDirname, 'app.js'), file)
var tmpDir = tmp.dirSync({ dir: path.join(__dirname, '../tmp'), unsafeCleanup: true })
assert.on('end', tmpDir.removeCallback)
fs.writeFileSync(path.join(tmpDir.name, 'plugin.js'), babelPlugin)
fs.writeFileSync(path.join(tmpDir.name, '.babelrc'), babelrc)
fs.writeFileSync(path.join(tmpDir.name, 'app.js'), file)
var compiler = bankai(path.join(tmpDirname, 'app.js'), { watch: false })
var compiler = bankai(path.join(tmpDir.name, 'app.js'), { watch: false })
compiler.on('error', assert.error)
compiler.scripts('bundle.js', function (err, res) {
assert.error(err, 'error building .babelrc dependency')
......@@ -146,12 +138,13 @@ tape('skip babel for dependencies if babelifyDeps is false', function (assert) {
`
var filename = 'js-pipeline-' + (Math.random() * 1e4).toFixed() + '.js'
const outputDir = tmp.dirSync({unsafeCleanup: true})
var tmpFilename = path.join(outputDir.name, filename)
var tmpDir = tmp.dirSync({ dir: path.join(__dirname, '../tmp'), unsafeCleanup: true })
assert.on('end', tmpDir.removeCallback)
var tmpFilename = path.join(tmpDir.name, filename)
fs.writeFileSync(tmpFilename, file)
const nodeModulesDir = path.join(outputDir.name, 'node_modules')
mkdirp.sync(nodeModulesDir)
fs.writeFileSync(path.join(nodeModulesDir, 'mydep.js'), depFile)
mkdirp.sync(path.join(tmpDir.name, 'node_modules'))
fs.writeFileSync(path.join(tmpDir.name, 'node_modules', 'mydep.js'), depFile)
var compiler = bankai(tmpFilename, { watch: false, babelifyDeps: false })
compiler.scripts('bundle.js', function (err, node) {
......@@ -161,7 +154,6 @@ tape('skip babel for dependencies if babelifyDeps is false', function (assert) {
const compiledJs = node.buffer.toString('utf8')
assert.notOk(/['"]use strict['"]/.test(compiledJs))
outputDir.removeCallback()
})
})
......@@ -178,12 +170,12 @@ tape('use custom browserslist config', function (assert) {
}
`
var tmpDirname = path.join(__dirname, '../tmp', 'js-pipeline-' + (Math.random() * 1e4).toFixed())
mkdirp.sync(tmpDirname)
fs.writeFileSync(path.join(tmpDirname, '.browserslistrc'), browserslist)
fs.writeFileSync(path.join(tmpDirname, 'app.js'), file)
var tmpDir = tmp.dirSync({ dir: path.join(__dirname, '../tmp'), unsafeCleanup: true })
assert.on('end', tmpDir.removeCallback)
fs.writeFileSync(path.join(tmpDir.name, '.browserslistrc'), browserslist)
fs.writeFileSync(path.join(tmpDir.name, 'app.js'), file)
var compiler = bankai(path.join(tmpDirname, 'app.js'), { watch: false })