Skip to content

Commit

Permalink
feat: Add support for env.NYC_CONFIG_OVERRIDE
Browse files Browse the repository at this point in the history
This allows a process to spawn a child process with an environment
variable NYC_CONFIG_OVERRIDE.  This is a JSON string which overrides any
values in the NYC_CONFIG env.  It is not deleted in the child process,
so will be contagious unless unset, and it is the responsibility of the
caller to manage.

The first intended use case of this feature is to allow node-tap to map
tests to the specific portion of a system under test that they cover.
In this way, unit tests can be more focused, and only re-run when their
specific unit has changed.

There are, of course, many other uses that this could be put to, and it
did not seem appropriate to add a special hook _just_ for overriding
the include list.
  • Loading branch information
isaacs committed Apr 19, 2019
1 parent 9fe9828 commit 30a81ed
Show file tree
Hide file tree
Showing 5 changed files with 85 additions and 0 deletions.
6 changes: 6 additions & 0 deletions bin/wrap.js
Expand Up @@ -16,6 +16,12 @@ if (process.env.NYC_PROCESSINFO_EXTERNAL_ID) {
delete process.env.NYC_PROCESSINFO_EXTERNAL_ID
}

if (process.env.NYC_CONFIG_OVERRIDE) {
var override = JSON.parse(process.env.NYC_CONFIG_OVERRIDE)
config = Object.assign(config, override)
process.env.NYC_CONFIG = JSON.stringify(config)
}

;(new NYC(config)).wrap()

sw.runMain()
20 changes: 20 additions & 0 deletions tap-snapshots/test-config-override.js-TAP.test.js
@@ -0,0 +1,20 @@
/* IMPORTANT
* This snapshot file is auto-generated, but designed for humans.
* It should be checked into source control and tracked carefully.
* Re-generate by setting TAP_SNAPSHOT=1 and running tests.
* Make sure to inspect the output below. Do not ignore changes!
*/
'use strict'
exports[`test/config-override.js TAP spawn that does config overriding > stdout 1`] = `
in parent { include: 'conf-override-root.js' }
in child { include: 'conf-override-module.js' }
in module { include: 'conf-override-module.js' }
-------------------------|----------|----------|----------|----------|-------------------|
File | % Stmts | % Branch | % Funcs | % Lines | Uncovered Line #s |
-------------------------|----------|----------|----------|----------|-------------------|
All files | 77.78 | 50 | 100 | 77.78 | |
conf-override-module.js | 100 | 100 | 100 | 100 | |
conf-override-root.js | 71.43 | 50 | 100 | 71.43 | 22,23 |
-------------------------|----------|----------|----------|----------|-------------------|
`
33 changes: 33 additions & 0 deletions test/config-override.js
@@ -0,0 +1,33 @@
const { spawn } = require('child_process')
const { resolve } = require('path')
const t = require('tap')
const node = process.execPath
const fixturesCLI = resolve(__dirname, './fixtures/cli')
const bin = resolve(__dirname, '../self-coverage/bin/nyc')
const rimraf = require('rimraf').sync
const tmp = 'conf-override-test'

rimraf(resolve(fixturesCLI, tmp))
t.teardown(() => rimraf(resolve(fixturesCLI, tmp)))

t.test('spawn that does config overriding', t => {
const args = [
bin, '-t', tmp,
'--exclude-after-remap=false',
'--include=conf-override-root.js',
node, 'conf-override-root.js'
]
const proc = spawn(node, args, {
cwd: fixturesCLI
})
const out = []
const err = []
proc.stdout.on('data', c => out.push(c))
proc.stderr.on('data', c => err.push(c))
proc.on('close', (code, signal) => {
t.equal(code, 0)
t.equal(signal, null)
t.matchSnapshot(Buffer.concat(out).toString(), 'stdout')
t.end()
})
})
2 changes: 2 additions & 0 deletions test/fixtures/cli/conf-override-module.js
@@ -0,0 +1,2 @@
const config = JSON.parse(process.env.NYC_CONFIG)
console.log('in module', {include: config.include})
24 changes: 24 additions & 0 deletions test/fixtures/cli/conf-override-root.js
@@ -0,0 +1,24 @@
const config = JSON.parse(process.env.NYC_CONFIG)
const { include } = config

if (process.argv[2] !== 'child') {
console.log('in parent', { include })
require('child_process').spawn(process.execPath, [__filename, 'child'], {
cwd: __dirname,
env: Object.assign(
{},
process.env,
{
NYC_CONFIG_OVERRIDE: JSON.stringify({
include: 'conf-override-module.js'
})
}
),
stdio: 'inherit',
})
} else {
// this should run, but not be covered, even though the shebang says to
// the child run ONLY covers the child file, not the dump-root.js
console.log('in child', { include })
require('./conf-override-module.js')
}

0 comments on commit 30a81ed

Please sign in to comment.