From 0e20f6ce0b13b212912368b871b0a99c7e85b9d2 Mon Sep 17 00:00:00 2001 From: Mark Wubben Date: Wed, 9 Dec 2015 14:42:41 +0000 Subject: [PATCH] wip! fix tests to work without tap --coverage Annoyingly they now don't work *with* tap --coverage, but on the other hand the fixes seem legit: Spawning tests need to run via bin/nyc otherwise they end up relying on tap's coverage reporting. The 'runs reports for all JSON in output directory' test should spawn a subprocess that itself spawns more processes, else only a single file would be generated. The 'handles corrupt JSON files' test doesn't need to spawn a subprocess, it just has to inject the corrupt JSON file into the output directory. --- test/fixtures/spawn.js | 5 +++ test/nyc-test.js | 87 +++++++++++++++++------------------------- 2 files changed, 40 insertions(+), 52 deletions(-) create mode 100755 test/fixtures/spawn.js diff --git a/test/fixtures/spawn.js b/test/fixtures/spawn.js new file mode 100755 index 000000000..7cc3e2d05 --- /dev/null +++ b/test/fixtures/spawn.js @@ -0,0 +1,5 @@ +#!/usr/bin/env node + +var spawn = require('child_process').spawn +spawn(process.execPath, ['sigint.js'], { cwd: __dirname }) +spawn(process.execPath, ['sigterm.js'], { cwd: __dirname }) diff --git a/test/nyc-test.js b/test/nyc-test.js index 685fdcd26..946c3028d 100644 --- a/test/nyc-test.js +++ b/test/nyc-test.js @@ -36,6 +36,17 @@ function cleanup () { rimraf.sync(tempDir) } +// Use bin/nyc.js to run the file in a child process. This should cause a +// coverage report to be written to the .nyc_output directory within the +// fixturesDir. +function coverChild (file, cb) { + spawn( + process.execPath, + [path.resolve(__dirname, '..', 'bin', 'nyc.js'), file], + { cwd: fixturesDir } + ).on('close', cb) +} + // If set, NYC_TEST_SPAWN contains the index of the test that should be // executed. Otherwise the test runner should be initialized. var spawnNthTest = parseInt(process.env.NYC_TEST_SPAWN || '-1', 10) @@ -246,19 +257,11 @@ describe('nyc', function () { }) function testSignal (signal, done) { - var nyc = (new NYC({ - cwd: process.cwd() - })).wrap() - - var proc = spawn(process.execPath, ['./test/fixtures/' + signal + '.js'], { - cwd: process.cwd(), - env: process.env, - stdio: 'inherit' - }) + var nyc = instantiate({ cwd: fixturesDir }) - proc.on('close', function () { + coverChild(path.join(fixturesDir, signal + '.js'), function () { var reports = _.filter(nyc._loadReports(), function (report) { - return report['./test/fixtures/' + signal + '.js'] + return report['./' + signal + '.js'] }) reports.length.should.equal(1) return done() @@ -286,24 +289,17 @@ describe('nyc', function () { describe('report', function () { it('runs reports for all JSON in output directory', function (done) { - var nyc = new NYC({ - cwd: process.cwd() - }) - var proc = spawn(process.execPath, ['./test/fixtures/sigint.js'], { - cwd: process.cwd(), - env: process.env, - stdio: 'inherit' - }) + var nyc = instantiate({ cwd: fixturesDir }) var start = fs.readdirSync(nyc.tmpDirectory()).length - proc.on('close', function () { + coverChild(path.join(fixturesDir, 'spawn.js'), function () { nyc.report( null, { add: function (report) { // the subprocess we ran should output reports // for files in the fixtures directory. - Object.keys(report).should.match(/.\/test\/fixtures\//) + Object.keys(report).should.match(/\.\/(spawn|sigint|sigterm)\.js/) } }, { @@ -323,33 +319,25 @@ describe('nyc', function () { }) it('handles corrupt JSON files', function (done) { - var nyc = new NYC({ - cwd: process.cwd() - }) - var proc = spawn(process.execPath, ['./test/fixtures/sigint.js'], { - cwd: process.cwd(), - env: process.env, - stdio: 'inherit' - }) - - fs.writeFileSync('./.nyc_output/bad.json', '}', 'utf-8') + var nyc = instantiate({ cwd: fixturesDir }) - proc.on('close', function () { - nyc.report( - null, - { - add: function (report) {} - }, - { - add: function (reporter) {}, - write: function () { - // we should get here without exception. - fs.unlinkSync('./.nyc_output/bad.json') - return done() - } + var bad = path.join(fixturesDir, '.nyc_output', 'bad.json') + fs.writeFileSync(bad, '}', 'utf-8') + + nyc.report( + null, + { + add: function (report) {} + }, + { + add: function (reporter) {}, + write: function () { + // we should get here without exception. + fs.unlinkSync(bad) + return done() } - ) - }) + } + ) }) it('handles multiple reporters', function (done) { @@ -359,13 +347,8 @@ describe('nyc', function () { cwd: fixturesDir, reporter: reporters }) - var proc = spawn(process.execPath, ['./test/fixtures/sigint.js'], { - cwd: process.cwd(), - env: process.env, - stdio: 'inherit' - }) - proc.on('close', function () { + coverChild(path.join(fixturesDir, 'sigint.js'), function () { nyc.report( null, {