Skip to content

Commit

Permalink
closes #14 and #17, new files option and function support for replace
Browse files Browse the repository at this point in the history
  • Loading branch information
maenu committed Feb 2, 2014
1 parent 9fcb73e commit d2212fb
Show file tree
Hide file tree
Showing 4 changed files with 184 additions and 96 deletions.
14 changes: 13 additions & 1 deletion README.md
Expand Up @@ -43,13 +43,24 @@ The supported types are:
* [`lcovonly`](http://gotwarlost.github.com/istanbul/public/apidocs/classes/LcovOnlyReport.html) * [`lcovonly`](http://gotwarlost.github.com/istanbul/public/apidocs/classes/LcovOnlyReport.html)
* [`cobertura`](http://gotwarlost.github.com/istanbul/public/apidocs/classes/CoberturaReport.html) * [`cobertura`](http://gotwarlost.github.com/istanbul/public/apidocs/classes/CoberturaReport.html)


### templateOptions.files

Type: `String|Array`
Default: `**/*`

A globbing pattern or multiple patterns for the source files to instrument.
All source files that do match will be instrumented, those who don't won't.
E.g. `['**/*', '!src/main/js/uninteresting.js']` will result in all source files being instrumented except `src/main/js/uninteresting.js`.

### templateOptions.replace ### templateOptions.replace


Type: `Boolean` Type: `Boolean|Function`
Default: `true` Default: `true`


Whether or not the `src` scripts are replaced by the paths to their instrumented versions. Whether or not the `src` scripts are replaced by the paths to their instrumented versions.
This is useful when you want the mixed-in template to work with the original sources, and you want to serve the instrumented sources by redirecting request on the server side. This is useful when you want the mixed-in template to work with the original sources, and you want to serve the instrumented sources by redirecting request on the server side.
If you don't want the sources to be replaced, set it to `false`.
If it is a function, it receives the arguments `ìnstrumentedSource` and `source` which represent the URI to the instrumented file and the uninstrumented file respectively, relative to the directory specified by [`outfile`](https://github.com/gruntjs/grunt-contrib-jasmine#optionsoutfile).


### templateOptions.thresholds ### templateOptions.thresholds
Type: `Object` Type: `Object`
Expand Down Expand Up @@ -161,6 +172,7 @@ If your mixed-in template simply includes the sources, as the default template d
If your mixed-in template loads the sources differently, e.g. directly from the file system, you may need to reconfigure the mixed-in template. If your mixed-in template loads the sources differently, e.g. directly from the file system, you may need to reconfigure the mixed-in template.
## Change Log ## Change Log
* v0.3.1, 02.02.14, closes #14 and #17, added `files` option and function support for `replace` option
* v0.3.0, 02.02.14, fixes #18 and #26, now requires grunt-contrib-jasmine v0.6.0 * v0.3.0, 02.02.14, fixes #18 and #26, now requires grunt-contrib-jasmine v0.6.0
* v0.2.6, 02.02.14, merged #13 from @llacroix, windows paths are converted to URIs * v0.2.6, 02.02.14, merged #13 from @llacroix, windows paths are converted to URIs
* v0.2.5, 10.08.13, reporter is now moved to and loaded from jasmine's temporary directory, fixes #11 * v0.2.5, 10.08.13, reporter is now moved to and loaded from jasmine's temporary directory, fixes #11
Expand Down
5 changes: 3 additions & 2 deletions package.json
@@ -1,7 +1,7 @@
{ {
"name": "grunt-template-jasmine-istanbul", "name": "grunt-template-jasmine-istanbul",
"description": "Code coverage template mix-in for grunt-contrib-jasmine, using istanbul", "description": "Code coverage template mix-in for grunt-contrib-jasmine, using istanbul",
"version": "0.3.0", "version": "0.3.1",
"keywords": [ "keywords": [
"grunt", "grunt",
"template", "template",
Expand All @@ -17,7 +17,8 @@
}, },
"main": "src/main/js/template.js", "main": "src/main/js/template.js",
"dependencies": { "dependencies": {
"istanbul": ">=0.1.30" "istanbul": ">=0.1.30",
"lodash": ">=2.4.1"
}, },
"peerDependencies": { "peerDependencies": {
"grunt": ">=0.4.0", "grunt": ">=0.4.0",
Expand Down
78 changes: 50 additions & 28 deletions src/main/js/template.js
Expand Up @@ -8,6 +8,7 @@
var path = require('path'); var path = require('path');
var istanbul = require('istanbul'); var istanbul = require('istanbul');
var grunt = require('grunt'); var grunt = require('grunt');
var _ = require('lodash');


var REPORTER = __dirname + '/reporter.js'; var REPORTER = __dirname + '/reporter.js';
var TMP_REPORTER = 'grunt-template-jasmine-istanbul/reporter.js'; var TMP_REPORTER = 'grunt-template-jasmine-istanbul/reporter.js';
Expand All @@ -28,33 +29,29 @@ var getUri = function (file) {
}; };


/** /**
* Instruments the specified sources and moves the instrumented sources to the * Instruments the specified source and moves the instrumented source to the
* temporary location, recreating the original directory structure. * temporary location, recreating the original directory structure.
* *
* @private * @private
* @method instrument * @method instrument
* *
* @param {Array} sources The paths of the original sources * @param {String} source The path of the original source
* @param {String} tmp The path to the temporary directory * @param {String} tmp The path to the temporary directory
* *
* @return {Array} The paths to the instrumented sources * @return {String} The path to the instrumented source
*/ */
var instrument = function (sources, tmp) { var instrument = function (source, tmp) {
var instrumenter = new istanbul.Instrumenter(); var instrumenter = new istanbul.Instrumenter();
var instrumentedSources = []; var instrumentedSourceText = instrumenter.instrumentSync(
sources.forEach(function (source) { grunt.file.read(source), source);
var instrumentedSource = instrumenter.instrumentSync( var instrumentedSource = source;
grunt.file.read(source), source); // don't try to write "C:" as part of a folder name on Windows
var tmpSource = source; if (process.platform == 'win32') {
// don't try to write "C:" as part of a folder name on Windows instrumentedSource = instrumentedSource.replace(/^([a-z]):/i, '$1');
if (process.platform == 'win32') { }
tmpSource = tmpSource.replace(/^([a-z]):/i, '$1'); instrumentedSource = path.join(tmp, instrumentedSource);
} grunt.file.write(instrumentedSource, instrumentedSourceText);
tmpSource = path.join(tmp, tmpSource); return instrumentedSource;
grunt.file.write(tmpSource, instrumentedSource);
instrumentedSources.push(tmpSource);
});
return instrumentedSources;
}; };


/** /**
Expand Down Expand Up @@ -127,7 +124,7 @@ var checkThresholds = function (collector, options) {
}); });
var finalSummary = istanbul.utils.mergeSummaryObjects.apply(null, var finalSummary = istanbul.utils.mergeSummaryObjects.apply(null,
summaries); summaries);
grunt.util._.each(options, function (threshold, metric) { _.each(options, function (threshold, metric) {
var actual = finalSummary[metric]; var actual = finalSummary[metric];
if(!actual) { if(!actual) {
grunt.warn('unrecognized metric: ' + metric); grunt.warn('unrecognized metric: ' + metric);
Expand Down Expand Up @@ -165,7 +162,7 @@ var processMixedInTemplate = function (grunt, task, context) {
if (template.process) { if (template.process) {
return template.process(grunt, task, mixedInContext); return template.process(grunt, task, mixedInContext);
} else { } else {
return grunt.util._.template(grunt.file.read(template), mixedInContext); return _.template(grunt.file.read(template), mixedInContext);
} }
}; };


Expand All @@ -189,18 +186,43 @@ exports.process = function (grunt, task, context) {
grunt.file.copy(REPORTER, tmpReporter); grunt.file.copy(REPORTER, tmpReporter);
context.scripts.reporters.unshift(getUri(tmpReporter)); context.scripts.reporters.unshift(getUri(tmpReporter));
// instrument sources // instrument sources
var sources = []; var files = context.options.files || '**/*';
var replacements = [];
context.scripts.src.forEach(function (source) { context.scripts.src.forEach(function (source) {
sources.push(path.join(outputDirectory, source)) var instrumentedSource = path.join(outputDirectory, source);
}); if (!grunt.file.isMatch(files, instrumentedSource)) {
var instrumentedSources = instrument(sources, context.temp); return;
instrumentedSources.forEach(function (instrumentedSource, i) { }
instrumentedSources[i] = getUri(path.relative(outputDirectory, instrumentedSource = instrument(instrumentedSource, context.temp);
instrumentedSource = getUri(path.relative(outputDirectory,
instrumentedSource)); instrumentedSource));
replacements.push({
from: source,
to: instrumentedSource
});
}); });
// replace sources // replace sources
if (context.options.replace == null || context.options.replace) { if (typeof context.options.replace == 'function') {
context.scripts.src = instrumentedSources; replacements.forEach(function (replacement) {
// call replace with the original and the instrumented source paths
replacement.to = context.options.replace(replacement.to,
replacement.from);
});
}
if (context.options.replace != false) {
// replace instrumented sources and keep uninstrumented
context.scripts.src = context.scripts.src.map(function (source) {
var instrumentedSource = null;
replacements.forEach(function (replacement) {
if (replacement.from == source) {
instrumentedSource = replacement.to;
}
});
if (instrumentedSource) {
return instrumentedSource;
}
return source;
});
} }
// listen to coverage event dispatched by reporter // listen to coverage event dispatched by reporter
task.phantomjs.on('jasmine.coverage', function (coverage) { task.phantomjs.on('jasmine.coverage', function (coverage) {
Expand Down

0 comments on commit d2212fb

Please sign in to comment.