Skip to content

Commit

Permalink
Collecting coverage stats also in the Dredd binary.
Browse files Browse the repository at this point in the history
Making sure coverage is picking up integration tests using ./bin/dredd.
  • Loading branch information
honzajavorek committed Feb 27, 2016
1 parent f4ad156 commit 11f5ca3
Show file tree
Hide file tree
Showing 12 changed files with 126 additions and 55 deletions.
4 changes: 3 additions & 1 deletion .gitignore
Original file line number Diff line number Diff line change
@@ -1,7 +1,9 @@
node_modules
src-cov
CHANGELOG-Generated.md
/lib
/src-cov
/lcov
cov.info
cov.html
.vagrant
*.DS_Store
Expand Down
6 changes: 6 additions & 0 deletions bin/dredd
Original file line number Diff line number Diff line change
@@ -1,6 +1,11 @@
#!/usr/bin/env node

// When changing this file, please don't forget to make corresponding changes
// also in ./bin/dredd-coverage.

var DreddCommand = require('../lib/dredd-command');


var dreddCli = new DreddCommand({
custom: {
cwd: process.cwd(),
Expand All @@ -9,4 +14,5 @@ var dreddCli = new DreddCommand({
exit: process.exit
});


dreddCli.run();
48 changes: 48 additions & 0 deletions bin/dredd-coverage
Original file line number Diff line number Diff line change
@@ -0,0 +1,48 @@
#!/usr/bin/env node

// This is a special version of ./bin/dredd, which is used as Dredd binary
// during integration tests. It collects coverage stats and saves them in file.

var fs = require('fs');

var DreddCommand = require('../src/dredd-command');


var dreddCli = new DreddCommand({
custom: {
cwd: process.cwd(),
argv: process.argv.slice(2)
},
exit: function(exitStatus) {
// Before Dredd exits, we need to collect coverage stats and save them to
// a file. We abuse 'mocha-lcov-reporter' to do this.

// Pretending there is Mocha runner
var EventEmitter = require('events').EventEmitter;
var runner = new EventEmitter();

// Monkey-patching 'process.stdout.write' to catch all stdout in a variable
var content = '';
var write = process.stdout.write;
process.stdout.write = function(stdout) {
content += stdout;
};

// Collecting the stats
var LCov = require('mocha-lcov-reporter');
LCov(runner);
runner.emit('end');

// Undo the monkey-patching
process.stdout.write = write;

// Save stats as lcov file
fs.appendFile('./lcov/dredd-bin.info', content, function (err) {
if (err) { console.error(err); }
process.exit(exitStatus);
});
}
});


dreddCli.run();
4 changes: 3 additions & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,8 @@
"test:bdd": "./scripts/test -w",
"test:hook-handlers": "coffee ./scripts/test-hook-handlers.coffee",
"prepublish": "npm run compile",
"coveralls": "./scripts/cov mocha-lcov-reporter | ./node_modules/coveralls/bin/coveralls.js"
"coverage": "./scripts/cov",
"coveralls": "npm run coverage && cat ./cov.info | ./node_modules/coveralls/bin/coveralls.js"
},
"repository": {
"type": "git",
Expand Down Expand Up @@ -58,6 +59,7 @@
"express": "^4.12.1",
"github-changes": "^1.0.0",
"jscoverage": "~0.6.0",
"lcov-result-merger": "^1.0.2",
"mocha": "^2.1.0",
"mocha-lcov-reporter": "1.0.0",
"nock": "^3.4.1",
Expand Down
34 changes: 24 additions & 10 deletions scripts/cov
Original file line number Diff line number Diff line change
Expand Up @@ -5,21 +5,35 @@
# Example:
# ./scripts/cov html-cov > cov.html

rm -rf ./src-cov

COV=./node_modules/coffee-coverage/bin/coffeecoverage
MOCHA=./node_modules/.bin/mocha
MERGER=./node_modules/.bin/lcov-result-merger

REPORTER=$1
echo "Using Mocha reporter: $REPORTER" 1>&2
$COV --exclude node_modules,.git,test --path relative . ./src-cov 1>&2
cp -r ./test ./src-cov/test

# Cleanup & preparation
rm -rf ./src-cov ./lcov ./cov.info
mkdir ./src-cov ./lcov

# Creating directory with instrumented JS code
$COV --exclude node_modules,.git,test --path=relative . ./src-cov 1>&2
cp ./package.json ./src-cov
cp -r ./test ./src-cov/test

# Special version of ./bin/dredd
mkdir ./src-cov/bin
cp ./bin/dredd-coverage ./src-cov/bin/dredd
chmod +x ./src-cov/bin/dredd

# Testing
find ./src-cov/test/ -name '*-test.coffee' | xargs "$MOCHA" \
--compilers 'coffee:coffee-script/register' \
--reporter "$REPORTER" \
--timeout 120000 \
--recursive
--compilers='coffee:coffee-script/register' \
--reporter='mocha-lcov-reporter' \
--timeout=120000 \
--recursive >> ./lcov/mocha.info 2>&1

# Merging LCOV reports
$MERGER './lcov/*.info' ./cov.info

rm -rf ./src-cov
# Output & cleanup
rm -rf ./src-cov ./lcov
3 changes: 0 additions & 3 deletions scripts/cov-html

This file was deleted.

2 changes: 1 addition & 1 deletion scripts/stress-build
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
# if the test suite fails. It's for finding flaky tests.

command () {
./scripts/mocha
./scripts/test
}

iteration="0"
Expand Down
6 changes: 3 additions & 3 deletions scripts/test
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,8 @@
MOCHA=./node_modules/.bin/mocha

find ./test/ -name '*-test.coffee' | xargs "$MOCHA" \
--compilers 'coffee:coffee-script/register' \
--reporter spec \
--timeout 120000 \
--compilers='coffee:coffee-script/register' \
--reporter=spec \
--timeout=120000 \
--recursive \
"$@"
Loading

0 comments on commit 11f5ca3

Please sign in to comment.