-
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.
fixup! feat(builtin): enable coverage on nodejs_test
- Loading branch information
Fabian Wiles
committed
Feb 2, 2020
1 parent
d3b28f3
commit bc9a059
Showing
8 changed files
with
211 additions
and
70 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 |
---|---|---|
@@ -1,55 +1,100 @@ | ||
|
||
// this is what prints out all the other stuff | ||
// https://github.com/bazelbuild/bazel/blob/b3763e9c003398d265470dcbc9d925990ee9a2b2/tools/test/collect_coverage.sh#L175-L181 | ||
|
||
// this handles merging v8 reports from a single test suite and pushes them into bazels output file | ||
// to be picked up by a combined report later on | ||
|
||
const fs = require('fs'); | ||
const path = require('path'); | ||
const c8Report = require('c8/lib/report') | ||
const crypto = require('crypto'); | ||
|
||
|
||
// called from | ||
// https://github.com/bazelbuild/bazel/blob/b3763e9c003398d265470dcbc9d925990ee9a2b2/tools/test/collect_coverage.sh#L175-L181 | ||
|
||
|
||
function getArgValues(name, args) { | ||
const values = args.filter(a => a.startsWith('--' + name)).map(v => v.split('=')[1]); | ||
return values; | ||
} | ||
function getArgValue(name, args) { | ||
return getArgValues(name, args)[0]; | ||
} | ||
|
||
async function main() { | ||
const args = process.argv.splice(2); | ||
|
||
const coverageDir = getArgValue('coverage_dir', args); | ||
const outputFile = getArgValue('output_file', args); | ||
const sourceFileManifest = getArgValue('source_file_manifest', args); | ||
const filters = getArgValues('filter_sources', args); | ||
|
||
const sourceFiles = fs.readFileSync(sourceFileManifest).toString('utf8').split('\n'); | ||
|
||
const c8OutputDir = path.join(process.env.TEST_TMPDIR, crypto.randomBytes(4).toString('hex')); | ||
|
||
fs.mkdirSync(c8OutputDir); | ||
|
||
await c8Report({ | ||
include: sourceFiles, | ||
exclude: filters, | ||
reportsDirectory: c8OutputDir, | ||
tempDirectory: coverageDir, | ||
// resolve: ''resolve paths to alternate base directory'' | ||
reporter: 'lcov' | ||
}).run(); | ||
|
||
const lcovReport = fs.readdirSync(c8OutputDir)[0]; | ||
|
||
|
||
fs.copyFileSync(path.join(c8OutputDir, lcovReport), outputFile) | ||
} | ||
|
||
main(); | ||
/* THIS FILE GENERATED FROM .ts; see BUILD.bazel */ /* clang-format off */var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, generator) { | ||
return new (P || (P = Promise))(function (resolve, reject) { | ||
function fulfilled(value) { try { step(generator.next(value)); } catch (e) { reject(e); } } | ||
function rejected(value) { try { step(generator["throw"](value)); } catch (e) { reject(e); } } | ||
function step(result) { result.done ? resolve(result.value) : new P(function (resolve) { resolve(result.value); }).then(fulfilled, rejected); } | ||
step((generator = generator.apply(thisArg, _arguments || [])).next()); | ||
}); | ||
}; | ||
(function (factory) { | ||
if (typeof module === "object" && typeof module.exports === "object") { | ||
var v = factory(require, exports); | ||
if (v !== undefined) module.exports = v; | ||
} | ||
else if (typeof define === "function" && define.amd) { | ||
define("build_bazel_rules_nodejs/internal/coverage/lcov_merger", ["require", "exports", "fs", "path", "crypto"], factory); | ||
} | ||
})(function (require, exports) { | ||
"use strict"; | ||
Object.defineProperty(exports, "__esModule", { value: true }); | ||
/** | ||
* @license | ||
* Copyright 2017 The Bazel Authors. All rights reserved. | ||
* | ||
* Licensed under the Apache License, Version 2.0 (the "License"); | ||
* you may not use this file except in compliance with the License. | ||
* | ||
* You may obtain a copy of the License at | ||
* http://www.apache.org/licenses/LICENSE-2.0 | ||
* | ||
* Unless required by applicable law or agreed to in writing, software | ||
* distributed under the License is distributed on an "AS IS" BASIS, | ||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
* See the License for the specific language governing permissions and | ||
* limitations under the License. | ||
*/ | ||
const fs = require("fs"); | ||
const path = require("path"); | ||
const crypto = require("crypto"); | ||
// unfortunnately there's no types for this at the time | ||
// change to an import if some become avilable | ||
// see https://github.com/bcoe/c8/issues/195 | ||
const c8Report = require('c8/lib/report'); | ||
/** | ||
* If there are multiple args with the same name, returns an array of all of them | ||
*/ | ||
function getArgValues(name, args) { | ||
const values = args.filter(a => a.startsWith('--' + name)).map(v => v.split('=')[1]); | ||
return values; | ||
} | ||
/** | ||
* returns the first occurance of the argument by the provided name | ||
*/ | ||
function getArgValue(name, args) { | ||
return getArgValues(name, args)[0]; | ||
} | ||
/** | ||
* This script is called from https://github.com/bazelbuild/bazel/blob/master/tools/test/collect_coverage.sh#L175-L181 | ||
* when collecting coverage. | ||
* It's designed to collect the coverage of one target, since in nodejs and using NODEJS_V8_COVERAGE it may produce more than one | ||
* coverage file, however bazel expects there to be only one lcov file. | ||
* So this collects up the v8 coverage json's merges them and converts them to lcov for bazel to pick up later | ||
*/ | ||
function main() { | ||
return __awaiter(this, void 0, void 0, function* () { | ||
const args = process.argv.splice(2); | ||
// TODO: validate these args exist | ||
// otherwise we'll get hard to debug path errors | ||
const coverageDir = getArgValue('coverage_dir', args); | ||
const outputFile = getArgValue('output_file', args); | ||
const sourceFileManifest = getArgValue('source_file_manifest', args); | ||
const filters = getArgValues('filter_sources', args); | ||
const instrumentedSourceFiles = fs.readFileSync(sourceFileManifest).toString('utf8').split('\n'); | ||
// c8 will name the output report file something that bazel wont expect | ||
// so we give it a dir that it can write to | ||
// later on we'll move and rename it into output_file as bazel expects | ||
const tmpdir = process.env['TEST_TMPDIR']; | ||
const c8OutputDir = path.join(tmpdir, crypto.randomBytes(4).toString('hex')); | ||
fs.mkdirSync(c8OutputDir); | ||
// see https://github.com/bcoe/c8/blob/master/lib/report.js | ||
// for more info on this function | ||
yield c8Report({ | ||
// TODO: check if these filters work here, otherwise apply them beforehand | ||
include: instrumentedSourceFiles, | ||
exclude: filters, | ||
reportsDirectory: c8OutputDir, | ||
// tempDirectory as actually the dir that c8 will read from for the v8 json files | ||
tempDirectory: coverageDir, | ||
// we may need to use resolve here to change the base dir, from the docs: "resolve paths to alternate base directory" | ||
// resolve: '' | ||
// TODO: check that lcov is correct here, we may need to do another conversion | ||
reporter: 'lcov' | ||
}).run(); | ||
// this assumes there's only one report from c8 | ||
const lcovReport = fs.readdirSync(c8OutputDir)[0]; | ||
// mopves the report into the fiels bazel expects | ||
fs.copyFileSync(path.join(c8OutputDir, lcovReport), outputFile); | ||
}); | ||
} | ||
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 |
---|---|---|
@@ -1,4 +1,92 @@ | ||
console.log(process.argv) | ||
/** | ||
* @license | ||
* Copyright 2017 The Bazel Authors. All rights reserved. | ||
* | ||
* Licensed under the Apache License, Version 2.0 (the "License"); | ||
* you may not use this file except in compliance with the License. | ||
* | ||
* You may obtain a copy of the License at | ||
* http://www.apache.org/licenses/LICENSE-2.0 | ||
* | ||
* Unless required by applicable law or agreed to in writing, software | ||
* distributed under the License is distributed on an "AS IS" BASIS, | ||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
* See the License for the specific language governing permissions and | ||
* limitations under the License. | ||
*/ | ||
import * as crypto from 'crypto'; | ||
import * as fs from 'fs'; | ||
import * as path from 'path'; | ||
|
||
// called from | ||
// https://github.com/bazelbuild/bazel/blob/b3763e9c003398d265470dcbc9d925990ee9a2b2/tools/test/collect_coverage.sh#L175-L181 | ||
// unfortunnately there's no types for this at the time | ||
// change to an import if some become avilable | ||
// see https://github.com/bcoe/c8/issues/195 | ||
const c8Report = require('c8/lib/report'); | ||
|
||
/** | ||
* If there are multiple args with the same name, returns an array of all of them | ||
*/ | ||
function getArgValues(name: string, args: string[]): string[] { | ||
const values = args.filter(a => a.startsWith('--' + name)).map(v => v.split('=')[1]); | ||
return values; | ||
} | ||
|
||
/** | ||
* returns the first occurance of the argument by the provided name | ||
*/ | ||
function getArgValue(name: string, args: string[]): string { | ||
return getArgValues(name, args)[0]; | ||
} | ||
|
||
/** | ||
* This script is called from | ||
* https://github.com/bazelbuild/bazel/blob/master/tools/test/collect_coverage.sh#L175-L181 when | ||
* collecting coverage. It's designed to collect the coverage of one target, since in nodejs and | ||
* using NODEJS_V8_COVERAGE it may produce more than one coverage file, however bazel expects there | ||
* to be only one lcov file. So this collects up the v8 coverage json's merges them and converts | ||
* them to lcov for bazel to pick up later | ||
*/ | ||
async function main(): Promise<void> { | ||
const args = process.argv.splice(2); | ||
|
||
// TODO: validate these args exist | ||
// otherwise we'll get hard to debug path errors | ||
const coverageDir = getArgValue('coverage_dir', args); | ||
const outputFile = getArgValue('output_file', args); | ||
const sourceFileManifest = getArgValue('source_file_manifest', args); | ||
const filters = getArgValues('filter_sources', args); | ||
|
||
const instrumentedSourceFiles = fs.readFileSync(sourceFileManifest).toString('utf8').split('\n'); | ||
|
||
// c8 will name the output report file something that bazel wont expect | ||
// so we give it a dir that it can write to | ||
// later on we'll move and rename it into output_file as bazel expects | ||
const tmpdir = process.env['TEST_TMPDIR'] as string; | ||
const c8OutputDir = path.join(tmpdir, crypto.randomBytes(4).toString('hex')); | ||
fs.mkdirSync(c8OutputDir); | ||
|
||
// see https://github.com/bcoe/c8/blob/master/lib/report.js | ||
// for more info on this function | ||
await c8Report({ | ||
// TODO: check if these filters work here, otherwise apply them beforehand | ||
include: instrumentedSourceFiles, | ||
exclude: filters, | ||
reportsDirectory: c8OutputDir, | ||
// tempDirectory as actually the dir that c8 will read from for the v8 json files | ||
tempDirectory: coverageDir, | ||
// we may need to use resolve here to change the base dir, from the docs: "resolve paths to | ||
// alternate base directory" | ||
// resolve: '' | ||
// TODO: check that lcov is correct here, we may need to do another conversion | ||
reporter: 'lcov' | ||
}).run(); | ||
|
||
// this assumes there's only one report from c8 | ||
const lcovReport = fs.readdirSync(c8OutputDir)[0]; | ||
|
||
|
||
// mopves the report into the fiels bazel expects | ||
fs.copyFileSync(path.join(c8OutputDir, lcovReport), outputFile) | ||
} | ||
|
||
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
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