Skip to content

Commit

Permalink
fix: adds CLI integration testing, where there was no integration tes…
Browse files Browse the repository at this point in the history
…ting before.
  • Loading branch information
addaleax authored and bcoe committed Apr 9, 2016
1 parent 545cf91 commit 3403ca1
Show file tree
Hide file tree
Showing 11 changed files with 214 additions and 1 deletion.
3 changes: 2 additions & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@
"clean": "rimraf ./.nyc_output ./node_modules/.cache ./.self_coverage ./test/fixtures/.nyc_output ./test/fixtures/node_modules/.cache *covered.js ./lib/*covered.js",
"build": "node ./build-tests",
"instrument": "node ./build-self-coverage.js",
"run-tests": "tap --no-cov -b ./test/build/*.js ./test/src/source-map-cache.js",
"run-tests": "tap --no-cov -b ./test/build/*.js ./test/src/source-map-cache.js ./test/src/nyc-bin.js",
"report": "istanbul report --include=./.self_coverage/*.json lcov text",
"cover": "npm run clean && npm run build && npm run instrument && npm run run-tests && npm run report",
"dev": "npm run clean && npm run build && npm run run-tests",
Expand Down Expand Up @@ -106,6 +106,7 @@
"standard": "^6.0.8",
"standard-version": "^1.1.0",
"tap": "^5.7.1",
"which": "^1.2.4",
"zero-fill": "^2.2.3"
},
"repository": {
Expand Down
2 changes: 2 additions & 0 deletions test/fixtures/cli/fakebin/.gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
node
npm
9 changes: 9 additions & 0 deletions test/fixtures/cli/fakebin/npm-template.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
// shebang gets added before this line
var which = require('which')
var assert = require('assert')
var foreground = require('foreground-child')

// strip first PATH folder
process.env.PATH = process.env.PATH.replace(/^.+?[:;]/, '')

foreground('npm', process.argv.slice(2))
9 changes: 9 additions & 0 deletions test/fixtures/cli/half-covered-failing.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
var a = 0

throw new Error

if (a === 0) {
a++;
a--;
a++;
}
9 changes: 9 additions & 0 deletions test/fixtures/cli/half-covered.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
var a = 0

a++

if (a === 0) {
a++;
a--;
a++;
}
3 changes: 3 additions & 0 deletions test/fixtures/cli/package.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
{
"private": true
}
9 changes: 9 additions & 0 deletions test/fixtures/cli/run-npm-test-recursive/half-covered.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
var a = 0

a++

if (a === 0) {
a++;
a--;
a++;
}
8 changes: 8 additions & 0 deletions test/fixtures/cli/run-npm-test-recursive/package.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
{
"private": true,
"scripts": {
"test": "npm run test:deeper",
"test:deeper": "npm run test:even-deeper",
"test:even-deeper": "node ./half-covered.js"
}
}
9 changes: 9 additions & 0 deletions test/fixtures/cli/run-npm-test/half-covered.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
var a = 0

a++

if (a === 0) {
a++;
a--;
a++;
}
6 changes: 6 additions & 0 deletions test/fixtures/cli/run-npm-test/package.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
{
"private": true,
"scripts": {
"test": "node ./half-covered.js"
}
}
148 changes: 148 additions & 0 deletions test/src/nyc-bin.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,148 @@
/* global describe, it */

var path = require('path')
var fs = require('fs')
var spawn = require('child_process').spawn
var isWindows = require('is-windows')()
var fixturesCLI = path.resolve(__dirname, '../fixtures/cli')
var fakebin = path.resolve(fixturesCLI, 'fakebin')
var bin = path.resolve(__dirname, '../../bin/nyc')

require('chai').should()
require('tap').mochaGlobals()

describe('the nyc cli', function () {
var env = { PATH: process.env.PATH }

describe('--check-coverage', function () {
it('fails when the expected coverage is below a threshold', function (done) {
var args = [bin, '--check-coverage', '--lines', '51', process.execPath, './half-covered.js']
var message = 'ERROR: Coverage for lines (50%) does not meet global threshold (51%)'

var proc = spawn(process.execPath, args, {
cwd: fixturesCLI,
env: env
})

var stderr = ''
proc.stderr.on('data', function (chunk) {
stderr += chunk
})

proc.on('close', function (code) {
code.should.not.equal(0)
stderr.trim().should.equal(message)
done()
})
})

it('succeeds when the expected coverage is above a threshold', function (done) {
var args = [bin, '--check-coverage', '--lines', '49', process.execPath, './half-covered.js']

var proc = spawn(process.execPath, args, {
cwd: fixturesCLI,
env: env
})

proc.on('close', function (code) {
code.should.equal(0)
done()
})
})

// https://github.com/bcoe/nyc/issues/209
it('fails in any case when the underlying test failed', function (done) {
var args = [bin, '--check-coverage', '--lines', '49', process.execPath, './half-covered-failing.js']

var proc = spawn(process.execPath, args, {
cwd: fixturesCLI,
env: env
})

proc.on('close', function (code) {
code.should.not.equal(0)
done()
})
})
})

// https://github.com/bcoe/nyc/issues/190
describe('running "npm test"', function () {
it('can run "npm test" which directly invokes a test file', function (done) {
var args = [bin, 'npm', 'test']
var directory = path.resolve(fixturesCLI, 'run-npm-test')
var proc = spawn(process.execPath, args, {
cwd: directory,
env: env
})

proc.on('close', function (code) {
code.should.equal(0)
done()
})
})

it('can run "npm test" which indirectly invokes a test file', function (done) {
var args = [bin, 'npm', 'test']
var directory = path.resolve(fixturesCLI, 'run-npm-test-recursive')
var proc = spawn(process.execPath, args, {
cwd: directory,
env: env
})

proc.on('close', function (code) {
code.should.equal(0)
done()
})
})

function writeFakeNPM (shebang) {
var targetPath = path.resolve(fakebin, 'npm')
var source = fs.readFileSync(path.resolve(fakebin, 'npm-template.js'))
fs.writeFileSync(targetPath, '#!' + shebang + '\n' + source)
fs.chmodSync(targetPath, 493) // 0o755
}

it('can run "npm test", absolute shebang edition', function (done) {
if (isWindows) return done()

writeFakeNPM(process.execPath)

var args = [bin, 'npm', 'test']
var directory = path.resolve(fixturesCLI, 'run-npm-test-recursive')
var proc = spawn(process.execPath, args, {
cwd: directory,
env: {
PATH: fakebin + ':' + env.PATH
}
})

proc.on('close', function (code) {
code.should.equal(0)
done()
})
})

it('can run "npm test", weird bash+dirname shebang edition', function (done) {
if (isWindows) return done()

// This string is taken verbatim from tools/install.py in Node core v5.x
writeFakeNPM('/bin/sh\n// 2>/dev/null; exec "`dirname "$0"`/node" "$0" "$@"')
fs.symlinkSync(process.execPath, path.resolve(fakebin, 'node'))

var args = [bin, 'npm', 'test']
var directory = path.resolve(fixturesCLI, 'run-npm-test-recursive')
var proc = spawn(process.execPath, args, {
cwd: directory,
env: {
PATH: fakebin + ':' + env.PATH
}
})

proc.on('close', function (code) {
code.should.equal(0)
done()
})
})
})
})

0 comments on commit 3403ca1

Please sign in to comment.