Skip to content

Commit

Permalink
move cov to nodejs test
Browse files Browse the repository at this point in the history
  • Loading branch information
Fabian Wiles committed Sep 13, 2019
1 parent 9072ddb commit 3b565be
Show file tree
Hide file tree
Showing 37 changed files with 8,985 additions and 324 deletions.
1 change: 1 addition & 0 deletions internal/node/BUILD.bazel
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,7 @@ exports_files([
"node_repositories.bzl", # Exported to be consumed for generating skydoc.
"node_launcher.sh",
"node_loader.js",
"process_coverage.js",
"BUILD.nodejs_host_os_alias.tpl",
])

Expand Down
14 changes: 13 additions & 1 deletion internal/node/node.bzl
Original file line number Diff line number Diff line change
Expand Up @@ -170,6 +170,7 @@ def _nodejs_binary_impl(ctx):
fail("The node toolchain was not properly configured so %s cannot be executed. Make sure that target_tool_path or target_tool is set." % ctx.attr.name)

node_tool_files.append(ctx.file._link_modules_script)
node_tool_files.append(ctx.file._process_coverage_script)

if not ctx.outputs.templated_args_file:
templated_args = ctx.attr.templated_args
Expand Down Expand Up @@ -204,6 +205,7 @@ def _nodejs_binary_impl(ctx):
"TEMPLATED_env_vars": env_vars,
"TEMPLATED_expected_exit_code": str(expected_exit_code),
"TEMPLATED_link_modules_script": _to_manifest_path(ctx, ctx.file._link_modules_script),
"TEMPLATED_process_coverage_script": _to_manifest_path(ctx, ctx.file._process_coverage_script),
"TEMPLATED_node": node_tool_info.target_tool_path,
"TEMPLATED_repository_args": _to_manifest_path(ctx, ctx.file._repository_args),
"TEMPLATED_script_path": script_path,
Expand Down Expand Up @@ -233,7 +235,7 @@ def _nodejs_binary_impl(ctx):
transitive_files = runfiles,
files = node_tool_files + [
ctx.outputs.loader,
] + ctx.files._source_map_support_files +
] + ctx.files._source_map_support_files + ctx.files._process_coverage_files +

# We need this call to the list of Files.
# Calling the .to_list() method may have some perfs hits,
Expand Down Expand Up @@ -432,6 +434,16 @@ The set of default environment variables is:
default = Label("//internal/linker:index.js"),
allow_single_file = True,
),
"_process_coverage_script": attr.label(
default = Label("//internal/node:process_coverage.js"),
allow_single_file = True,
),
"_process_coverage_files": attr.label_list(
default = [
Label("//third_party/npm/node_modules/v8-coverage:package_contents"),
],
allow_files = True,
),
"_loader_template": attr.label(
default = Label("//internal/node:node_loader.js"),
allow_single_file = True,
Expand Down
15 changes: 13 additions & 2 deletions internal/node/node_launcher.sh
Original file line number Diff line number Diff line change
Expand Up @@ -118,7 +118,7 @@ readonly node=$(rlocation "TEMPLATED_node")
readonly repository_args=$(rlocation "TEMPLATED_repository_args")
readonly script=$(rlocation "TEMPLATED_script_path")
readonly link_modules_script=$(rlocation "TEMPLATED_link_modules_script")

readonly process_coverage_script=$(rlocation "TEMPLATED_process_coverage_script")
source $repository_args

ARGS=()
Expand All @@ -132,6 +132,11 @@ for ARG in "${ALL_ARGS[@]}"; do
esac
done

echo $TEST_TMPDIR
# if [[ -n "$covrage_dir"]]; then
export NODE_V8_COVERAGE="/tmp/coverage/$RANDOM"
# fi

# Link the first-party modules into node_modules directory before running the actual program
if [[ -n "$MODULES_MANIFEST" ]]; then
"${node}" "${link_modules_script}" "${MODULES_MANIFEST}"
Expand All @@ -147,15 +152,21 @@ if [ "${EXPECTED_EXIT_CODE}" -eq "0" ]; then
# handled by the node process.
# If we had merely forked a child process here, we'd be responsible
# for forwarding those OS interactions.
exec "${node}" "${NODE_OPTIONS[@]}" "${script}" "${ARGS[@]}"
"${node}" "${NODE_OPTIONS[@]}" "${script}" "${ARGS[@]}"
# exec terminates execution of this shell script, nothing later will run.
"${node}" "${process_coverage_script}" "${MODULES_MANIFEST}"
exit
fi

set +e
"${node}" "${NODE_OPTIONS[@]}" "${script}" "${ARGS[@]}"
RESULT="$?"
set -e

# if [[ -n "$MODULES_MANIFEST" ]]; then
"${node}" "${process_coverage_script}" "${MODULES_MANIFEST}"
# fi

if [ ${RESULT} != ${EXPECTED_EXIT_CODE} ]; then
echo "Expected exit code to be ${EXPECTED_EXIT_CODE}, but got ${RESULT}" >&2
if [ "${RESULT}" -eq "0" ]; then
Expand Down
35 changes: 35 additions & 0 deletions internal/node/process_coverage.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@

const path = require('path')
const fs = require('fs');

const sourcemap_support_package = path.resolve(process.cwd(), '../build_bazel_rules_nodejs/third_party/npm/node_modules/v8-coverage');

const covDir = process.env.NODE_V8_COVERAGE;

const reportReadDir = path.join(covDir, 'tmp')
const files = fs.readdirSync(covDir);
if(!fs.existsSync(reportReadDir)) {
fs.mkdirSync(reportReadDir)
}

//TODO: get inputs from manifest file
const allowed = ['file:///private/var/tmp/_bazel_fabianwiles/ccf98a8a2c7e81be5a82a567664db09b/execroot/build_bazel_rules_nodejs/bazel-out/darwin-fastbuild/bin/internal/node/test/coverage/coverage.sh.runfiles/build_bazel_rules_nodejs/internal/node/test/coverage/produces-coverage.js'];
const Report = require(sourcemap_support_package);
const report = new Report(process.env.NODE_V8_COVERAGE, ['text-summary']);

// Only store the files we acataully want coverage for
// Also convert them to instanbul format
for(const filelPath of files) {
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 => allowed.includes(s.url))

// when we store them like this it also converts them to istanbul format
report.store(filteredResult)
}
}

report.generateReport();
7 changes: 7 additions & 0 deletions internal/node/test/coverage/BUILD.bazel
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
load("//:defs.bzl", "nodejs_binary", "nodejs_test")

nodejs_binary(
name = "coverage",
data = ["produces-coverage.js"],
entry_point = ":produces-coverage.js",
)
5 changes: 5 additions & 0 deletions internal/node/test/coverage/produces-coverage.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
function doMath(input) {
return input + 3;
}

console.log(doMath(3));
1 change: 0 additions & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -61,7 +61,6 @@
"typeorm": "0.2.18",
"typescript": "3.1.6",
"unidiff": "1.0.1",
"v8-coverage": "1.0.9",
"webpack": "~4.29.3",
"zone.js": "0.8.29"
},
Expand Down
5 changes: 0 additions & 5 deletions packages/jasmine/src/jasmine_node_test.bzl
Original file line number Diff line number Diff line change
Expand Up @@ -74,11 +74,6 @@ def jasmine_node_test(
templated_args = kwargs.pop("templated_args", [])
templated_args.append("$(location :%s_devmode_srcs.MF)" % name)

if coverage:
templated_args.append("--coverage")
else:
templated_args.append("--nocoverage")

if config_file:
# Calculate a label relative to the user's BUILD file
pkg = Label("%s//%s:__pkg__" % (native.repository_name(), native.package_name()))
Expand Down
51 changes: 2 additions & 49 deletions packages/jasmine/src/jasmine_runner.js
Original file line number Diff line number Diff line change
Expand Up @@ -66,14 +66,10 @@ function main(args) {


// first args is always the path to the manifest
const manifest = require.resolve(readArg());
// second is always a flag to enable coverage or not
const coverageArg = readArg();
const enableCoverage = coverageArg === '--coverage';
const manifest = require.resolve(readArg());
// config file is the next arg
const configFile = readArg();

// the relative directory the coverage reporter uses to find anf filter the files
const cwd = process.cwd();

const jrunner = new JasmineRunner({jasmineCore: jasmineCore});
Expand All @@ -88,7 +84,6 @@ function main(args) {

const sourceFiles = allFiles
// Filter out all .spec and .test files so we only report
// coverage against the source files
.filter(f => !IS_TEST_FILE.test(f))
// the jasmine_runner.js gets in here as a file to run
.filter(f => !f.endsWith('jasmine_runner.js'))
Expand Down Expand Up @@ -116,53 +111,11 @@ function main(args) {
// so we need to add it back
jrunner.configureDefaultReporter({});


let covExecutor;
let covDir;
if (enableCoverage) {
// lazily pull these deps in for only when we want to collect coverage
const crypto = require('crypto');
const Execute = require('v8-coverage/src/execute');

// make a tmpdir inside our tmpdir for just this run
covDir = path.join(process.env['TEST_TMPDIR'], String(crypto.randomBytes(4).readUInt32LE(0)));
covExecutor = new Execute({include: sourceFiles, exclude: []});
covExecutor.startProfiler();
}

jrunner.onComplete((passed) => {
let exitCode = passed ? 0 : BAZEL_EXIT_TESTS_FAILED;
if (noSpecsFound) exitCode = BAZEL_EXIT_NO_TESTS_FOUND;

if (enableCoverage) {
const Report = require('v8-coverage/src/report');
covExecutor.stopProfiler((err, data) => {
if (err) {
console.error(err);
process.exit(1);
}
const sourceCoverge = covExecutor.filterResult(data.result);
// we could do this all in memory if we wanted
// just take a look at v8-coverage/src/report.js and reimplement some of those methods
// but we're going to have to write a file at some point for bazel coverage
// so may as well support it now
// the lib expects these paths to exist for some reason
fs.mkdirSync(covDir);
fs.mkdirSync(path.join(covDir, 'tmp'));
// only do a text summary for now
// once we know what format bazel coverage wants we can output
// lcov or some other format
const report = new Report(covDir, ['text-summary']);
report.store(sourceCoverge);
report.generateReport();

process.exit(exitCode);
});
} else {
process.exit(exitCode);
}


process.exit(exitCode);
});

if (TOTAL_SHARDS) {
Expand Down
16 changes: 16 additions & 0 deletions scripts/vendor_npm.sh
Original file line number Diff line number Diff line change
Expand Up @@ -281,5 +281,21 @@ $(cat ${THIRD_PARTY_DIR}/yarn.lock | awk '{print "# "$0}')
)
done

##############################################################################
# c8
##############################################################################
(
prep v8-coverage
ncc src/report.js
echo """filegroup(
name = \"package_contents\",
srcs = glob([\"**/*.js\"]) + [
\"BUILD.bazel\",
],
)
""" >> ${DST_DIR}/BUILD.bazel
build_file_footer
)

rm -rf node_modules
)
Loading

0 comments on commit 3b565be

Please sign in to comment.