-
Notifications
You must be signed in to change notification settings - Fork 521
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 18, 2019
1 parent
9072ddb
commit a444b3d
Showing
37 changed files
with
9,047 additions
and
324 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
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,86 @@ | ||
|
||
const path = require('path') | ||
const fs = require('fs'); | ||
|
||
const FILE_PROTOCOL = 'file://'; | ||
const IS_EXTERNAL = /\/external\// | ||
const rules_nodejs_bootstrap_scripts = ['build_bazel_rules_nodejs/internal/linker/index.js', 'build_bazel_rules_nodejs/internal/node/process_coverage.js'] | ||
|
||
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') | ||
const cwd = process.cwd(); | ||
|
||
if(!fs.existsSync(reportReadDir)) { | ||
fs.mkdirSync(reportReadDir) | ||
} | ||
|
||
// TODO: how do we filter out $target_loader | ||
const filesToCover = fs.readFileSync(process.env['RUNFILES_MANIFEST_FILE'], 'utf8') | ||
.split('\n') | ||
.filter(line => { | ||
if(line.length === 0) { | ||
return false; | ||
} | ||
// left is the module name I think | ||
// right is the file being used on disk | ||
const [left, right] = line.split(' '); | ||
// filter non js | ||
if(!left.endsWith('.js')) { | ||
return false; | ||
} | ||
// filter out all in thirt_party | ||
if(left.startsWith('build_bazel_rules_nodejs/third_party')) { | ||
return false; | ||
} | ||
// fitler out all "external" - this should include node_modules | ||
if(IS_EXTERNAL.test(right)) { | ||
return false; | ||
} | ||
// filter out scripts added by rules_nodejs | ||
if(rules_nodejs_bootstrap_scripts.includes(left)) { | ||
return false; | ||
} | ||
|
||
return true; | ||
}) | ||
.map(line => require.resolve(line.split(' ')[1])) | ||
|
||
// TODO: allow report type to be configurable | ||
const report = new Report(process.env.NODE_V8_COVERAGE, ['text-summary']); | ||
|
||
const rawV8CoverageFiles = fs.readdirSync(covDir); | ||
// Only store the files we acataully want coverage for | ||
// Also convert them to instanbul format | ||
// TODO: since we know all files that should be coveregd | ||
// we should generate empty v8 coverage objects for the files that have had 0 code paths | ||
// covered, but are included in this set of runfiles | ||
// this will stablize the % covered stats | ||
for(const filelPath of rawV8CoverageFiles) { | ||
const fullPath = path.join(covDir, filelPath); | ||
if(fs.statSync(fullPath).isFile()) { | ||
const file = fs.readFileSync(fullPath); | ||
const result = JSON.parse(file); | ||
|
||
// const filteredResult = result.result.filter(s => allFiles.includes(s.url)) | ||
const filteredResult = result.result.filter(s => { | ||
// some of these urls are nodejs internal paths | ||
// so only use the ones that are real files | ||
if(s.url.startsWith(FILE_PROTOCOL)) { | ||
const url = s.url.replace(FILE_PROTOCOL, ''); | ||
return filesToCover.includes(require.resolve(url)) | ||
} | ||
return false; | ||
}); | ||
// 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,8 @@ | ||
load("//:defs.bzl", "nodejs_binary") | ||
|
||
nodejs_binary( | ||
name = "coverage", | ||
coverage = True, | ||
data = ["produces-coverage.js"], | ||
entry_point = ":produces-coverage.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,9 @@ | ||
function doMath(input) { | ||
if(input > 5) { | ||
return input + 1; | ||
} | ||
return input + 3; | ||
} | ||
console.log('NODE_V8_COVERAGE', process.env['NODE_V8_COVERAGE']) | ||
console.log(doMath(3)); | ||
// console.log(doMath(6)); |
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.