Skip to content

Commit

Permalink
wip! fix tests to work without tap --coverage
Browse files Browse the repository at this point in the history
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.
  • Loading branch information
novemberborn committed Dec 9, 2015
1 parent 2221d23 commit 0e20f6c
Show file tree
Hide file tree
Showing 2 changed files with 40 additions and 52 deletions.
5 changes: 5 additions & 0 deletions test/fixtures/spawn.js
Original file line number Diff line number Diff line change
@@ -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 })
87 changes: 35 additions & 52 deletions test/nyc-test.js
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down Expand Up @@ -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()
Expand Down Expand Up @@ -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/)
}
},
{
Expand All @@ -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) {
Expand All @@ -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,
{
Expand Down

0 comments on commit 0e20f6c

Please sign in to comment.