Skip to content

Commit

Permalink
Browse files Browse the repository at this point in the history
chore: Convert some tap tests to run parallel and use snapshots. (#1075)
  • Loading branch information
coreyfarrell committed Apr 24, 2019
1 parent 56591fa commit 600c867
Show file tree
Hide file tree
Showing 14 changed files with 2,314 additions and 1,966 deletions.
3 changes: 0 additions & 3 deletions .babelrc

This file was deleted.

25 changes: 25 additions & 0 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

12 changes: 8 additions & 4 deletions package.json
Expand Up @@ -4,12 +4,14 @@
"description": "the Istanbul command line interface",
"main": "index.js",
"scripts": {
"pretest": "npm run clean && npm run instrument",
"test": "tap -t360 --no-cov -b ./test/*.js && npm run report",
"posttest": "standard",
"lint": "standard",
"pretest": "npm run lint && npm run clean && npm run instrument",
"test": "tap -t360 --no-cov -b test/*.js",
"snap": "cross-env TAP_SNAPSHOT=1 tap -t360 --no-cov -b test/*.js",
"posttest": "npm run report",
"clean": "rimraf ./.nyc_output ./node_modules/.cache ./.self_coverage ./test/fixtures/.nyc_output ./test/fixtures/node_modules/.cache ./self-coverage",
"instrument": "node ./build-self-coverage.js",
"report": "node ./self-coverage/bin/nyc report --temp-dir ./.self_coverage/ -r text -r lcov",
"report": "node ./bin/nyc report --temp-dir ./.self_coverage/ -r text -r lcov",
"release": "standard-version"
},
"bin": {
Expand Down Expand Up @@ -96,9 +98,11 @@
"any-path": "^1.3.0",
"chai": "^4.2.0",
"coveralls": "^3.0.3",
"cross-env": "^5.2.0",
"is-windows": "^1.0.2",
"lodash": "^4.17.11",
"newline-regex": "^0.2.1",
"pify": "^4.0.1",
"requirejs": "^2.3.6",
"sanitize-filename": "^1.6.1",
"source-map-support": "^0.5.12",
Expand Down
566 changes: 566 additions & 0 deletions tap-snapshots/test-nyc-integration.js-TAP.test.js

Large diffs are not rendered by default.

2 changes: 1 addition & 1 deletion test/fixtures/cli/half-covered-failing.js
@@ -1,6 +1,6 @@
var a = 0

throw new Error
process.exit(1)

if (a === 0) {
a++;
Expand Down
12 changes: 12 additions & 0 deletions test/helpers/index.js
@@ -0,0 +1,12 @@
'use strict'

const { fixturesCLI, nycBin } = require('./paths')

module.exports = {
fixturesCLI,
nycBin,
testSuccess: require('./test-success'),
testFailure: require('./test-failure'),
runNYC: require('./run-nyc'),
tempDirSetup: require('./temp-dir-setup')
}
8 changes: 8 additions & 0 deletions test/helpers/paths.js
@@ -0,0 +1,8 @@
'use strict'

const path = require('path')

module.exports = {
nycBin: require.resolve('../../self-coverage/bin/nyc'),
fixturesCLI: path.resolve(__dirname, '../fixtures/cli')
}
40 changes: 40 additions & 0 deletions test/helpers/run-nyc.js
@@ -0,0 +1,40 @@
'use strict'

const { nycBin, fixturesCLI } = require('./paths')
const spawn = require('./spawn')

const env = {
PATH: process.env.PATH
}

function sanitizeString (str, cwd, leavePathSep) {
/*
* File paths are different on different systems:
* - make everything relative to cwd
* - replace full node path with 'node'
* - replace all Windows path separators ('\\') with POSIX path separators
*/
str = str
.split(cwd).join('.')
.split(process.execPath).join('node')

if (!leavePathSep) {
str = str.replace(/\\/g, '/')
}

return str
}

function runNYC ({ args, tempDir, leavePathSep, cwd = fixturesCLI }) {
const runArgs = [nycBin].concat(tempDir ? ['--temp-dir', tempDir] : [], args)
return spawn(process.execPath, runArgs, {
cwd: cwd,
env
}).then(({ status, stderr, stdout }) => ({
status,
stderr: sanitizeString(stderr, cwd, leavePathSep),
stdout: sanitizeString(stdout, cwd, leavePathSep)
}))
}

module.exports = runNYC
23 changes: 23 additions & 0 deletions test/helpers/spawn.js
@@ -0,0 +1,23 @@
const cp = require('child_process')

function spawn (exe, args, opts) {
return new Promise((resolve, reject) => {
const proc = cp.spawn(exe, args, opts)
const stdout = []
const stderr = []

proc.stdout.on('data', buf => stdout.push(buf))
proc.stderr.on('data', buf => stderr.push(buf))

proc.on('error', reject)
proc.on('close', status => {
resolve({
status,
stdout: Buffer.concat(stdout).toString(),
stderr: Buffer.concat(stderr).toString()
})
})
})
}

module.exports = spawn
33 changes: 33 additions & 0 deletions test/helpers/temp-dir-setup.js
@@ -0,0 +1,33 @@
'use strict'

const path = require('path')
const fs = require('fs')
const makeDir = require('make-dir')
const _rimraf = require('rimraf')
const pify = require('pify')

const rimraf = pify(_rimraf)
const mkdtemp = pify(fs.mkdtemp)

function tempDirSetup (t, testFile) {
const { dir, name } = path.parse(testFile)
const tempDirBase = path.resolve(dir, 'temp-dir-' + name)

makeDir.sync(tempDirBase)

// Do not use arrow function for beforeEach
// or afterEach, they need this from tap.
t.beforeEach(function () {
return mkdtemp(tempDirBase + '/').then(tempDir => {
this.tempDir = tempDir
})
})

t.afterEach(function () {
return rimraf(this.tempDir)
})

t.tearDown(() => rimraf(tempDirBase))
}

module.exports = tempDirSetup
14 changes: 14 additions & 0 deletions test/helpers/test-failure.js
@@ -0,0 +1,14 @@
'use strict'

const runNYC = require('./run-nyc')

function testFailure (t, opts) {
opts.tempDir = t.tempDir
return runNYC(opts).then(({ status, stderr, stdout }) => {
t.equal(status, 1)
t.matchSnapshot(stderr, 'stderr')
t.matchSnapshot(stdout, 'stdout')
})
}

module.exports = testFailure
14 changes: 14 additions & 0 deletions test/helpers/test-success.js
@@ -0,0 +1,14 @@
'use strict'

const runNYC = require('./run-nyc')

function testSuccess (t, opts) {
opts.tempDir = t.tempDir
return runNYC(opts).then(({ status, stderr, stdout }) => {
t.equal(status, 0)
t.equal(stderr, '')
t.matchSnapshot(stdout, 'stdout')
})
}

module.exports = testSuccess

0 comments on commit 600c867

Please sign in to comment.