Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Browse files

Fixes #11 by providing a --save-baseline and --baseline-file options

Test run on instanbul codebase

$ istanbul i lib/ -o out --save-baseline
.
Processed [36] files in 0 secs
Saving baseline coverage at: /Users/ananthk/screwdriver-git/istanbul/coverage/coverage-baseline.json

$ istanbul r --format text-summary
Using reporter [text-summary]

=============================== Coverage summary ===============================
Statements   : 0% ( 0/2085 )
Branches     : 0% ( 0/916 )
Functions    : 0% ( 0/376 )
Lines        : 0% ( 0/1505 )
================================================================================
Done
  • Loading branch information...
commit 092f336f05fb2aff79684a5eb55e0982dd3ac808 1 parent 098e04c
@gotwarlost authored
View
42 lib/command/instrument.js
@@ -14,8 +14,35 @@ var path = require('path'),
formatOption = require('../util/help-formatter').formatOption,
util = require('util'),
Command = require('./index'),
+ Collector = require('../collector'),
verbose;
+function BaselineCollector(instrumenter) {
+ this.instrumenter = instrumenter;
+ this.collector = new Collector();
+ this.instrument = instrumenter.instrument.bind(this.instrumenter);
+
+ var origInstrumentSync = instrumenter.instrumentSync;
+ this.instrumentSync = function () {
+ var args = Array.prototype.slice.call(arguments),
+ ret = origInstrumentSync.apply(this.instrumenter, args),
+ baseline = this.instrumenter.lastFileCoverage(),
+ coverage = {};
+ coverage[baseline.path] = baseline;
+ this.collector.add(coverage);
+ return ret;
+ };
+ //monkey patch the instrumenter to call our version instead
+ instrumenter.instrumentSync = this.instrumentSync.bind(this);
+}
+
+BaselineCollector.prototype = {
+ getCoverage: function () {
+ return this.collector.getFinalCoverage();
+ }
+};
+
+
function processFiles(instrumenter, inputDir, outputDir, relativeNames) {
var processor = function (name, callback) {
var inputFile = path.resolve(inputDir, name),
@@ -100,6 +127,8 @@ Command.mix(InstrumentCommand, {
variable: String,
compact: Boolean,
verbose: Boolean,
+ 'save-baseline': Boolean,
+ 'baseline-file': path,
'embed-source': Boolean
},
opts = nopt(config, { v : '--verbose' }, args, 0),
@@ -107,7 +136,9 @@ Command.mix(InstrumentCommand, {
file,
stats,
stream,
- instrumenter = new Instrumenter({ coverageVariable: opts.variable });
+ instrumenter = new Instrumenter({ coverageVariable: opts.variable }),
+ needBaseline = opts['save-baseline'],
+ baselineFile = opts['baseline-file'] || path.resolve(process.cwd(), 'coverage', 'coverage-baseline.json');
verbose = opts.verbose;
if (cmdArgs.length !== 1) {
@@ -123,6 +154,15 @@ Command.mix(InstrumentCommand, {
noCompact: !opts.compact
});
+ if (needBaseline) {
+ mkdirp.sync(path.dirname(baselineFile));
+ instrumenter = new BaselineCollector(instrumenter);
+ process.on('exit', function () {
+ util.puts('Saving baseline coverage at: ' + baselineFile);
+ fs.writeFileSync(baselineFile, JSON.stringify(instrumenter.getCoverage()), 'utf8');
+ });
+ }
+
file = path.resolve(cmdArgs[0]);
stats = fs.statSync(file);
if (stats.isDirectory()) {
View
13 lib/instrumenter.js
@@ -413,7 +413,18 @@
callback(ex);
}
},
-
+ /**
+ * returns the file coverage object for the code that was instrumented
+ * just before calling this method. Note that this represents a
+ * "zero-coverage" object which is not even representative of the code
+ * being loaded in node or a browser (which would increase the statement
+ * counts for mainline code).
+ * @return {Object} a "zero-coverage" file coverage object for the code last instrumented
+ * by this instrumenter
+ */
+ lastFileCoverage: function () {
+ return this.coverState;
+ },
fixColumnPositions: function (coverState) {
var offset = LEADER_WRAP.length,
fixer = function (loc) {
View
2  package.json
@@ -21,7 +21,7 @@
"istanbul": "./lib/cli.js"
},
"dependencies": {
- "esprima": "0.9.x",
+ "esprima": "1.0.x",
"escodegen": "0.0.x",
"handlebars": "1.0.x",
"mkdirp": "0.3.x",
View
9 test/cli/test-instrument-command.js
@@ -78,6 +78,15 @@ module.exports = {
test.done();
});
},
+ "should save baseline coverage when requested": function (test) {
+ var covFile = path.resolve(OUTPUT_DIR, 'cov.json');
+ run([ 'lib/foo.js', '--save-baseline', '--baseline-file=' + covFile ], function (results) {
+ test.ok(results.succeeded());
+ test.ok(existsSync(covFile));
+ test.ok(results.grepOutput(/Saving baseline coverage at/));
+ test.done();
+ });
+ },
"should barf on no args": function (test) {
run([], function (results) {
test.ok(!results.succeeded());
Please sign in to comment.
Something went wrong with that request. Please try again.