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

-C flag for generating and reporting test coverage for all files in working directory #116

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
46 changes: 45 additions & 1 deletion bin/expresso
Original file line number Diff line number Diff line change
Expand Up @@ -96,6 +96,12 @@ var quiet = false;
var jsonCoverage = false;
var jsonFile;

/**
* Directory where instrumented JS code will be stored
*/

var coverageDirectory = ".jscoverage";

/**
* Usage documentation.
*/
Expand All @@ -105,7 +111,8 @@ var usage = ''
+ '\n'
+ '\n[bold]{Options}:'
+ '\n -g, --growl Enable growl notifications'
+ '\n -c, --coverage Generate and report test coverage'
+ '\n -c, --coverage Generate and report test coverage for files in ./lib'
+ '\n -C, --cwd-coverage Generate and report test coverage for files in working directory'
+ '\n -j, --json PATH Used in conjunction with --coverage, ouput JSON coverage to PATH'
+ '\n -q, --quiet Suppress coverage report if 100%'
+ '\n -t, --timeout MS Timeout in milliseconds, defaults to 2000'
Expand Down Expand Up @@ -178,6 +185,40 @@ while (args.length) {
throw new Error('--timeout requires an argument');
}
break;
case '-C':
case '--cwd-coverage':
// Check if files were changed since the last run of jscoverage,
// to avoid running it again
var currentStat = fs.statSync('.')
try {
var tempStat = fs.statSync(coverageDirectory);
if (currentStat.mtime.valueOf() > tempStat.mtime.valueOf()) {
defer = true;
}
} catch (e) {
// Coverage directory does not exist
defer = true;
}
if (defer) {
// Unlink the coverage directory and create a temporary one. This is necessary because
// node-jscoverage won't accept a destination directory nested in the source one
childProcess.exec('rm -fr '+coverageDirectory+' && mktemp -d "/tmp/expresso-XXXXXXXXXXXX"', function(err, stdout){
if (err) throw err;
// Remove newline at the end of mktemp output
var tempPath = stdout.slice(0, -1);
// Run node-jscoverage and move the results to the coverage directory
childProcess.exec('node-jscoverage . '+tempPath+' && mv '+tempPath+' '+coverageDirectory, function(err) {
if (err) throw err;
process.chdir(coverageDirectory);
cwd = process.cwd();
run(files);
})
})
} else {
process.chdir(coverageDirectory);
cwd = process.cwd();
}
break;
case '-c':
case '--cov':
case '--coverage':
Expand Down Expand Up @@ -215,6 +256,9 @@ while (args.length) {
break;
default:
if (file_matcher.test(arg)) {
if (arg.charAt(0) === '/') {
throw new Error('expresso requires all provided paths to be relative');
}
files.push(arg);
}
break;
Expand Down