Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: Add support for env.NYC_CONFIG_OVERRIDE
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
Showing
5 changed files
with
85 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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 | | ||
-------------------------|----------|----------|----------|----------|-------------------| | ||
` |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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() | ||
}) | ||
}) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,2 @@ | ||
const config = JSON.parse(process.env.NYC_CONFIG) | ||
console.log('in module', {include: config.include}) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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') | ||
} |