Skip to content

Commit

Permalink
code coverage POC
Browse files Browse the repository at this point in the history
  • Loading branch information
evanlucas committed Apr 15, 2016
1 parent b488b19 commit 1445e3d
Show file tree
Hide file tree
Showing 5 changed files with 54 additions and 1 deletion.
3 changes: 3 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -89,3 +89,6 @@ test.tap
# Xcode workspaces and project folders
*.xcodeproj
*.xcworkspace
/coverage
/lib-cov
node.gyp.orig
9 changes: 9 additions & 0 deletions Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -610,6 +610,15 @@ jslint-ci:
$(NODE) tools/jslint.js -f tap -o test-eslint.tap benchmark lib src test \
tools/doc tools/eslint-rules tools/jslint.js

cover:
@mkdir -p coverage/
bash tools/coverage/patch-node-gyp.sh
./configure
@istanbul instrument --output lib-cov/ ./lib/

cover-test: cover
bash tools/coverage/run-tests.sh

CPPLINT_EXCLUDE ?=
CPPLINT_EXCLUDE += src/node_lttng.cc
CPPLINT_EXCLUDE += src/node_root_certs.h
Expand Down
18 changes: 17 additions & 1 deletion test/common.js
Original file line number Diff line number Diff line change
Expand Up @@ -325,6 +325,10 @@ if (global.Symbol) {
knownGlobals.push(Symbol);
}

if (process.env.CORE_COVERAGE && global.__coverage__) {
knownGlobals.push(__coverage__);
}

function leakedGlobals() {
var leaked = [];

Expand All @@ -337,9 +341,21 @@ function leakedGlobals() {
exports.leakedGlobals = leakedGlobals;

// Turn this off if the test should not check for global leaks.
exports.globalCheck = true;
exports.globalCheck = !process.env.CORE_COVERAGE;

process.on('exit', function() {
if (process.env.CORE_COVERAGE) {
const path = require('path');
const fs = require('fs');
const testName = path.basename(require.main.filename);
const covDir = path.join(__dirname, '..', 'coverage', testName);
try {
fs.mkdirSync(covDir);
} catch (err) {}
fs.writeFileSync(path.join(covDir, 'coverage.json'),
JSON.stringify(global.__coverage__),
'utf8');
}
if (!exports.globalCheck) return;
var leaked = leakedGlobals();
if (leaked.length > 0) {
Expand Down
14 changes: 14 additions & 0 deletions tools/coverage/patch-node-gyp.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
#!/bin/bash

set -e

cd $(dirname $0)/../..

if grep -q "lib-cov/internal/bootstrap_node.js" node.gyp; then
echo "node.gyp already updated...skipping"
exit 0
fi

sed -i ".orig" "s/'lib\//'lib-cov\//" node.gyp

echo "successfully updated node.gyp"
11 changes: 11 additions & 0 deletions tools/coverage/run-tests.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
#!/bin/bash

set -e

cd $(dirname $0)/../..

make test CORE_COVERAGE=1 || echo "Test suite failed with coverage."

echo "Generating coverage reports. This may take a bit."

istanbul report

0 comments on commit 1445e3d

Please sign in to comment.