Skip to content

Instantly share code, notes, and snippets.

@isaacs
Last active April 18, 2019 01:04
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save isaacs/98ccdfc9793c4008242108e80fae5ee0 to your computer and use it in GitHub Desktop.
Save isaacs/98ccdfc9793c4008242108e80fae5ee0 to your computer and use it in GitHub Desktop.
var sw = require('spawn-wrap')
var NYC = require('../index.js')
var config = {}
if (process.env.NYC_CONFIG) config = JSON.parse(process.env.NYC_CONFIG)
config.isChildProcess = true
config._processInfo = {
pid: process.pid,
ppid: process.ppid,
parent: process.env.NYC_PROCESS_ID || null,
root: process.env.NYC_ROOT_ID
}
if (process.env.NYC_PROCESSINFO_EXTERNAL_ID) {
config._processInfo.externalId = 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()
diff --git a/bin/wrap.js b/bin/wrap.js
index 1cff275..4e64c2b 100644
--- a/bin/wrap.js
+++ b/bin/wrap.js
@@ -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()
const config = JSON.parse(process.env.NYC_CONFIG || /* istanbul ignore next */ '{}')
console.log('in dumper', {include: config.include})
#!/usr/local/bin/node ./bin/nyc.js --no-exclude-after-remap --include=dump-root.js node
const config = JSON.parse(process.env.NYC_CONFIG || /* istanbul ignore next */ '{}')
const {include,exclude} = config
const dumper = require.resolve('./dump-config.js')
if (process.argv[2] !== 'child') {
console.error('in parent', {include})
require('child_process').spawn(process.execPath, [__filename, 'child'], {
env: {
...process.env,
NYC_CONFIG_OVERRIDE: JSON.stringify({
include: 'dump-config.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.error('in child', {include})
require(dumper)
}
@isaacs
Copy link
Author

isaacs commented Apr 18, 2019

output

$ ./dump-root.js
in parent { include: 'dump-root.js' }
in child { include: 'dump-config.js' }
in dumper { include: 'dump-config.js' }
----------------|----------|----------|----------|----------|-------------------|
File            |  % Stmts | % Branch |  % Funcs |  % Lines | Uncovered Line #s |
----------------|----------|----------|----------|----------|-------------------|
All files       |       80 |       75 |      100 |       80 |                   |
 dump-config.js |      100 |      100 |      100 |      100 |                   |
 dump-root.js   |       75 |    66.67 |      100 |       75 |             21,22 |
----------------|----------|----------|----------|----------|-------------------|

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment