Skip to content

Commit 1445e3d

Browse files
committed
code coverage POC
1 parent b488b19 commit 1445e3d

File tree

5 files changed

+54
-1
lines changed

5 files changed

+54
-1
lines changed

.gitignore

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -89,3 +89,6 @@ test.tap
8989
# Xcode workspaces and project folders
9090
*.xcodeproj
9191
*.xcworkspace
92+
/coverage
93+
/lib-cov
94+
node.gyp.orig

Makefile

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -610,6 +610,15 @@ jslint-ci:
610610
$(NODE) tools/jslint.js -f tap -o test-eslint.tap benchmark lib src test \
611611
tools/doc tools/eslint-rules tools/jslint.js
612612

613+
cover:
614+
@mkdir -p coverage/
615+
bash tools/coverage/patch-node-gyp.sh
616+
./configure
617+
@istanbul instrument --output lib-cov/ ./lib/
618+
619+
cover-test: cover
620+
bash tools/coverage/run-tests.sh
621+
613622
CPPLINT_EXCLUDE ?=
614623
CPPLINT_EXCLUDE += src/node_lttng.cc
615624
CPPLINT_EXCLUDE += src/node_root_certs.h

test/common.js

Lines changed: 17 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -325,6 +325,10 @@ if (global.Symbol) {
325325
knownGlobals.push(Symbol);
326326
}
327327

328+
if (process.env.CORE_COVERAGE && global.__coverage__) {
329+
knownGlobals.push(__coverage__);
330+
}
331+
328332
function leakedGlobals() {
329333
var leaked = [];
330334

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

339343
// Turn this off if the test should not check for global leaks.
340-
exports.globalCheck = true;
344+
exports.globalCheck = !process.env.CORE_COVERAGE;
341345

342346
process.on('exit', function() {
347+
if (process.env.CORE_COVERAGE) {
348+
const path = require('path');
349+
const fs = require('fs');
350+
const testName = path.basename(require.main.filename);
351+
const covDir = path.join(__dirname, '..', 'coverage', testName);
352+
try {
353+
fs.mkdirSync(covDir);
354+
} catch (err) {}
355+
fs.writeFileSync(path.join(covDir, 'coverage.json'),
356+
JSON.stringify(global.__coverage__),
357+
'utf8');
358+
}
343359
if (!exports.globalCheck) return;
344360
var leaked = leakedGlobals();
345361
if (leaked.length > 0) {

tools/coverage/patch-node-gyp.sh

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
#!/bin/bash
2+
3+
set -e
4+
5+
cd $(dirname $0)/../..
6+
7+
if grep -q "lib-cov/internal/bootstrap_node.js" node.gyp; then
8+
echo "node.gyp already updated...skipping"
9+
exit 0
10+
fi
11+
12+
sed -i ".orig" "s/'lib\//'lib-cov\//" node.gyp
13+
14+
echo "successfully updated node.gyp"

tools/coverage/run-tests.sh

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
#!/bin/bash
2+
3+
set -e
4+
5+
cd $(dirname $0)/../..
6+
7+
make test CORE_COVERAGE=1 || echo "Test suite failed with coverage."
8+
9+
echo "Generating coverage reports. This may take a bit."
10+
11+
istanbul report

0 commit comments

Comments
 (0)