Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Code Coverage Support added #74

Closed
wants to merge 21 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
7 changes: 5 additions & 2 deletions README.md
@@ -1,5 +1,8 @@
Nodeunit
========
Nodeunit2
=========

## This is the Same As Stable Nodeunit with Code Coverage Facility.
Visit https://github.com/caolan/nodeunit/pull/74 for more info.

Simple syntax, powerful tools. Nodeunit provides easy async unit testing for
node.js and the browser.
Expand Down
88 changes: 70 additions & 18 deletions bin/nodeunit
Expand Up @@ -2,7 +2,9 @@

var
fs = require('fs'),
path = require('path');
sys = require('sys'),
path = require('path'),
childProcess = require('child_process');

// TODO: remove this when https://github.com/joyent/node/pull/1312
// lands in core.
Expand All @@ -13,6 +15,10 @@ require('../deps/console.log');
//require.paths.push(process.cwd());
var args = (process.ARGV || process.argv).slice(2);

//global indicators for Code Coverage
codeCoverage = {};
codeCoverage.paths = ['lib-cov'];

var files = [];

var testrunner,
Expand All @@ -28,6 +34,8 @@ var usage = "Usage: nodeunit [options] testmodule1.js testfolder [...] \n" +
" --config FILE the path to a JSON file with options\n" +
" --reporter FILE optional path to a reporter file to customize the output\n" +
" --list-reporters list available build-in reporters\n" +
" -cr, --coverage-results display Code Coverage results in details (verbose mode) \n" +
" -ci, --coverage-ignored ignore code coverage support (ideal for identify real line numbers when error occured) \n" +
" -t name, specify a test to run\n" +
" -h, --help display this help and exit\n" +
" -v, --version output version information and exit";
Expand Down Expand Up @@ -88,7 +96,20 @@ args.forEach(function (arg) {
} else if ((arg === '-h') || (arg === '--help')) {
console.log(usage);
process.exit(0);
} else if(arg === '--coverage-results') {
reporter_file = 'codecov'
codeCoverage.verbose = true;
} else if(arg === '-cr') {
codeCoverage.verbose = true;
reporter_file = 'codecov'
} else if(arg === '-ci') {
codeCoverage.ignored = true;
codeCoverage.paths = ['lib'];
reporter_file = 'codecov'
} else {
codeCoverage.enabled = true;
setUpCodeCoverage();
reporter_file = 'codecov'
files.push(arg);
}
});
Expand All @@ -99,27 +120,58 @@ if (files.length === 0) {
process.exit(1);
}

if (config_file) {
content = fs.readFileSync(config_file, 'utf8');
var custom_options = JSON.parse(content);
if(!codeCoverage.enabled) {
startTesting();
}

function startTesting() {

for (var option in custom_options) {
if (typeof option === 'string') {
options[option] = custom_options[option];
if (files.length === 0) {
sys.puts('Files required.');
sys.puts(usage);
process.exit(1);
}

if (config_file) {
content = fs.readFileSync(config_file, 'utf8');
var custom_options = JSON.parse(content);

for (var option in custom_options) {
if (typeof option === 'string') {
options[option] = custom_options[option];
}
}
}
}

var builtin_reporters = require(__dirname + '/../lib/reporters');
if (reporter_file in builtin_reporters) {
testrunner = builtin_reporters[reporter_file];
}
else {
testrunner = require(reporter_file);
var builtin_reporters = require(__dirname + '/../lib/reporters');
if (reporter_file in builtin_reporters) {
testrunner = builtin_reporters[reporter_file];
}
else {
testrunner = require(reporter_file);
}

testrunner.run(files, options);
}

testrunner.run(files, options, function(err) {
if (err) {
process.exit(1);
function setUpCodeCoverage() {

if(codeCoverage.ignored) {
childProcess.exec('mkdir -p node_modules && cd node_modules && rm -rf `ls ../lib` && cd .. && cp -rf lib/* node_modules/', afterCommandExecuted);
} else {
childProcess.exec('rm -fr lib-cov && jscoverage lib lib-cov && mkdir -p node_modules && cp -rf lib-cov/* node_modules/', afterCommandExecuted);
}
});

function afterCommandExecuted(err){

if (err) {

console.log(err.message);
console.log('Install node-jscoverage from\n\thttps://github.com/visionmedia/node-jscoverage ');

} else {

startTesting();
}
}
}
9 changes: 7 additions & 2 deletions lib/nodeunit.js
Expand Up @@ -76,8 +76,13 @@ exports.runFiles = function (paths, opt) {
},
function (err, all_assertions) {
var end = new Date().getTime();
exports.done()
options.done(types.assertionList(all_assertions, end - start));

//send jscoverage result to the reporter if any results exists
if(codeCoverage.enabled && typeof(_$jscoverage) != 'undefined') {
options.done(types.assertionList(all_assertions, end - start), _$jscoverage);
} else {
options.done(types.assertionList(all_assertions, end - start));
}
});
});

Expand Down