-
Notifications
You must be signed in to change notification settings - Fork 522
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat(builtin): enable coverage on nodejs_test
- Loading branch information
Fabian Wiles
committed
Sep 21, 2019
1 parent
cefc2ae
commit 632c3ff
Showing
38 changed files
with
9,052 additions
and
335 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
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,105 @@ | ||
|
||
const path = require('path') | ||
const fs = require('fs'); | ||
|
||
const sep = path.sep; | ||
|
||
const FILE_PROTOCOL = 'file://'; | ||
const IS_TEST_FILE = /[^a-zA-Z0-9](spec|test)\.js$/i; | ||
// TODO: needs proper test | ||
const IS_EXTERNAL = new RegExp(`${sep}external${sep}`); | ||
const IS_THIRD_PARTY = new RegExp(`${sep}build_bazel_rules_nodejs${sep}third_party${sep}`); | ||
const IS_LINKER_SCRIPT = | ||
new RegExp(`${sep}build_bazel_rules_nodejs${sep}internal${sep}linker${sep}index.js`); | ||
const NODE_LOADER_SCRIPT = process.argv[2]; | ||
|
||
// Run Bazel with --define=VERBOSE_LOGS=1 to enable this logging | ||
const VERBOSE_LOGS = !!process.env['VERBOSE_LOGS']; | ||
|
||
function log_verbose(...m) { | ||
if (VERBOSE_LOGS) console.error('[process_coverage.js]', ...m); | ||
} | ||
|
||
function panic(m) { | ||
throw new Error(`Internal error! Please run again with | ||
--define=VERBOSE_LOG=1 | ||
and file an issue: https://github.com/bazelbuild/rules_nodejs/issues/new?template=bug_report.md | ||
Include as much of the build output as you can without disclosing anything confidential. | ||
Error: | ||
${m} | ||
`); | ||
} | ||
|
||
function shouldCoverFile(filePath) { | ||
// some coverage collected is from internal nodejs modules | ||
// these modules don't prefix with a file protocol | ||
// so we can filter them out here | ||
if (!filePath.startsWith(FILE_PROTOCOL)) { | ||
return false; | ||
} | ||
const trimmedPath = filePath.replace(FILE_PROTOCOL, ''); | ||
|
||
// filter out all in third_party | ||
// such as /third_party/github.com/source-map-support | ||
if (IS_THIRD_PARTY.test(trimmedPath)) { | ||
return false; | ||
} | ||
// fitler out all "external" - this should include node_modules | ||
if (IS_EXTERNAL.test(trimmedPath)) { | ||
return false; | ||
} | ||
// don't show coverage for the spec files themselves | ||
if (IS_TEST_FILE.test(trimmedPath)) { | ||
return false; | ||
} | ||
// filter out scripts added by rules_nodejs | ||
if (IS_LINKER_SCRIPT.test(trimmedPath)) { | ||
return false; | ||
} | ||
|
||
// filter out the node_launcher.sh script | ||
if (trimmedPath.includes(NODE_LOADER_SCRIPT)) { | ||
return false; | ||
} | ||
|
||
return true; | ||
} | ||
|
||
function main() { | ||
const v8_coverage_package = path.resolve(process.cwd(), '../build_bazel_rules_nodejs/third_party/npm/node_modules/v8-coverage'); | ||
const Report = require(v8_coverage_package); | ||
|
||
const covDir = process.env.NODE_V8_COVERAGE; | ||
const reportReadDir = path.join(covDir, 'tmp') | ||
|
||
if(!fs.existsSync(reportReadDir)) { | ||
fs.mkdirSync(reportReadDir) | ||
} | ||
|
||
// TODO: allow report type to be configurable | ||
const report = new Report(covDir, ['text-summary']); | ||
|
||
const rawV8CoverageFiles = fs.readdirSync(covDir); | ||
// Only store the files we actually want coverage for | ||
// This also converts them to instanbul format | ||
// when we hit report.store() | ||
for (const filePath of rawV8CoverageFiles) { | ||
const fullPath = path.join(covDir, filePath); | ||
if (fs.statSync(fullPath).isFile()) { | ||
const file = fs.readFileSync(fullPath); | ||
const v8Cov = JSON.parse(file); | ||
|
||
const filteredResult = v8Cov.result.filter(s => { | ||
return shouldCoverFile(s.url); | ||
}); | ||
log_verbose('code covered files', filteredResult.map(a => a.url)) | ||
|
||
// when we store them like this it also converts them to istanbul format | ||
report.store(filteredResult) | ||
} | ||
} | ||
|
||
report.generateReport(); | ||
} | ||
|
||
main(); |
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,10 @@ | ||
load("//:defs.bzl", "nodejs_test") | ||
|
||
nodejs_test( | ||
name = "coverage", | ||
data = [ | ||
"produces-coverage.js", | ||
"produces-coverage.spec.js", | ||
], | ||
entry_point = ":produces-coverage.spec.js", | ||
) |
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,3 @@ | ||
exports.greaterThan5 = function(input) { | ||
return input > 5; | ||
} |
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,13 @@ | ||
const lib = require('./produces-coverage'); | ||
const assert = require('assert'); | ||
|
||
if (!process.env['NODE_V8_COVERAGE']) { | ||
console.log( | ||
`expected process.env['NODE_V8_COVERAGE'] to be defined - run this with: bazel coverage`); | ||
} | ||
|
||
function test() { | ||
assert.equal(lib.greaterThan5(6), true); | ||
} | ||
|
||
test(); |
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
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
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
Oops, something went wrong.