-
-
Notifications
You must be signed in to change notification settings - Fork 358
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Use istanbul-lib-hook to wrap require and support vm hooks #308
Changes from 2 commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -13,7 +13,9 @@ try { | |
include: process.env.NYC_INCLUDE ? process.env.NYC_INCLUDE.split(',') : [], | ||
enableCache: process.env.NYC_CACHE === 'enable', | ||
sourceMap: process.env.NYC_SOURCE_MAP === 'enable', | ||
instrumenter: process.env.NYC_INSTRUMENTER | ||
instrumenter: process.env.NYC_INSTRUMENTER, | ||
hookRunInContext: process.env.NYC_HOOK_RUN_IN_CONTEXT === 'true', | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. For consistency, I might stick with setting the environment variable to |
||
hookCreateScript: process.env.NYC_HOOK_CREATE_SCRIPT === 'true' | ||
})).wrap() | ||
|
||
sw.runMain() |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,4 +1,4 @@ | ||
var istanbul = require('istanbul') | ||
var istanbul = require('istanbul-lib-instrument') | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I really like this fix. it felt weird leaving in the Istanbul dependency. |
||
var fs = require('fs') | ||
var path = require('path') | ||
|
||
|
@@ -8,10 +8,9 @@ var path = require('path') | |
var indexPath = path.join(__dirname, name) | ||
var source = fs.readFileSync(indexPath, 'utf8') | ||
|
||
var instrumentor = new istanbul.Instrumenter({ | ||
var instrumentor = istanbul.createInstrumenter({ | ||
coverageVariable: '___NYC_SELF_COVERAGE___', | ||
esModules: true, | ||
noAutoWrap: true | ||
esModules: true | ||
}) | ||
|
||
var instrumentedSource = instrumentor.instrumentSync(source, indexPath) | ||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -11,7 +11,7 @@ | |
"build": "node ./build-tests", | ||
"instrument": "node ./build-self-coverage.js", | ||
"run-tests": "tap -t120 --no-cov -b ./test/build/*.js ./test/src/nyc-bin.js", | ||
"report": "istanbul report --include=./.self_coverage/*.json lcov text", | ||
"report": "node ./bin/nyc --temp-directory ./.self_coverage/ -r text -r lcov report", | ||
"cover": "npm run clean && npm run build && npm run instrument && npm run run-tests && npm run report", | ||
"dev": "npm run clean && npm run build && npm run run-tests", | ||
"version": "standard-version" | ||
|
@@ -72,7 +72,6 @@ | |
"author": "Ben Coe <[email protected]>", | ||
"license": "ISC", | ||
"dependencies": { | ||
"append-transform": "^0.4.0", | ||
"arrify": "^1.0.1", | ||
"caching-transform": "^1.0.0", | ||
"convert-source-map": "^1.1.2", | ||
|
@@ -82,6 +81,7 @@ | |
"foreground-child": "^1.5.3", | ||
"glob": "^7.0.3", | ||
"istanbul-lib-coverage": "^1.0.0-alpha.4", | ||
"istanbul-lib-hook": "^1.0.0-alpha.4", | ||
"istanbul-lib-instrument": "^1.1.0-alpha.1", | ||
"istanbul-lib-report": "^1.0.0-alpha.3", | ||
"istanbul-lib-source-maps": "^1.0.0-alpha.10", | ||
|
@@ -105,9 +105,9 @@ | |
"exists-sync": "0.0.3", | ||
"forking-tap": "^0.1.1", | ||
"is-windows": "^0.2.0", | ||
"istanbul": "^0.4.4", | ||
"lodash": "^4.12.0", | ||
"newline-regex": "^0.2.1", | ||
"requirejs": "^2.2.0", | ||
"sanitize-filename": "^1.5.3", | ||
"sinon": "^1.15.3", | ||
"source-map-support": "^0.4.1", | ||
|
@@ -123,7 +123,6 @@ | |
"url": "[email protected]:bcoe/nyc.git" | ||
}, | ||
"bundledDependencies": [ | ||
"append-transform", | ||
"arrify", | ||
"caching-transform", | ||
"convert-source-map", | ||
|
@@ -134,6 +133,7 @@ | |
"glob", | ||
"istanbul-lib-coverage", | ||
"istanbul-lib-instrument", | ||
"istanbul-lib-hook", | ||
"istanbul-lib-report", | ||
"istanbul-lib-source-maps", | ||
"istanbul-reports", | ||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,16 @@ | ||
// RequireJS uses `vm.runInThisContext` | ||
// make sure we add hooks for it as well | ||
|
||
var rjs = require('requirejs'), | ||
assert = require('assert'); | ||
|
||
rjs.config({ | ||
baseUrl : __dirname, | ||
nodeRequire : require | ||
}); | ||
|
||
rjs(['./lib/lorem'], function(lorem){ | ||
var result = lorem(1, 2, 3); | ||
assert.equal(9, result); | ||
}); | ||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,11 @@ | ||
define(function(){ | ||
|
||
function sum(a, b) { | ||
return a + b; | ||
} | ||
|
||
return { | ||
sum : sum | ||
}; | ||
|
||
}); |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,7 @@ | ||
define(['./ipsum'], function (ipsum) { | ||
|
||
return function exec(a, b, c){ | ||
return ipsum.sum(a, b) * c; | ||
}; | ||
|
||
}); |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,10 @@ | ||
{ | ||
"name": "hooktest", | ||
"version": "1.1.1", | ||
"description": "AMD/ requirejs test project", | ||
"main": "index.js", | ||
"dependencies": { | ||
"requirejs": "^2.2.0" | ||
}, | ||
"private": true | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -5,6 +5,7 @@ var fs = require('fs') | |
var spawn = require('child_process').spawn | ||
var isWindows = require('is-windows')() | ||
var fixturesCLI = path.resolve(__dirname, '../fixtures/cli') | ||
var fixturesHooks = path.resolve(__dirname, '../fixtures/hooks') | ||
var fakebin = path.resolve(fixturesCLI, 'fakebin') | ||
var bin = path.resolve(__dirname, '../../bin/nyc') | ||
var rimraf = require('rimraf') | ||
|
@@ -388,4 +389,25 @@ describe('the nyc cli', function () { | |
}) | ||
}) | ||
}) | ||
|
||
describe('hooks', function () { | ||
it('provides coverage for requireJS and AMD modules', function (done) { | ||
var args = [bin, '--hook-run-in-context', process.execPath, './index.js'] | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. lovely! I'm definitely finding myself writing more and more tests that directly exercise the bin which I don't think is a bad thing -- for one catches regressions I've introduced in the actual command line parsing. |
||
|
||
var proc = spawn(process.execPath, args, { | ||
cwd: fixturesHooks, | ||
env: process.env | ||
}) | ||
var stdout = '' | ||
proc.stdout.on('data', function (chunk) { | ||
stdout += chunk | ||
}) | ||
proc.on('close', function (code) { | ||
code.should.equal(0) | ||
stdout.should.match(/ipsum\.js/) | ||
stdout.should.match(/lorem\.js/) | ||
done() | ||
}) | ||
}) | ||
}) | ||
}) |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
we've tried to be fairly batteries included so far, for instance we already instrument subprocesses right out of the gate. I might be tempted to default this to
true
. I'm happy with the argument name 👍