Skip to content

Commit

Permalink
→ Fix common cross-platform issue with file path normalization. (#86)
Browse files Browse the repository at this point in the history
* → Fix common cross-platform issue with file path normalization. Between gulp3 & gulp4 the way file paths may have changed.
Reference: SBoudrias/gulp-istanbul/#48

* → Fix failing unit tests
Re: SBoudrias/gulp-istanbul/#48

* → Rename `getNormalizedPath` to `normalizePathSep`, refactor function per @SBoudrias
Re: SBoudrias/gulp-istanbul/#48
Re: SBoudrias/gulp-istanbul/#86
  • Loading branch information
afloyd authored and SBoudrias committed Aug 14, 2016
1 parent d34854f commit d93e057
Show file tree
Hide file tree
Showing 2 changed files with 16 additions and 10 deletions.
19 changes: 12 additions & 7 deletions index.js
Expand Up @@ -16,6 +16,10 @@ var PluginError = gutil.PluginError;
var PLUGIN_NAME = 'gulp-istanbul';
var COVERAGE_VARIABLE = '$$cov_' + new Date().getTime() + '$$';

function normalizePathSep(filepath) {
return filepath.replace(/\//g, path.sep);
}

var plugin = module.exports = function (opts) {
opts = opts || {};
_.defaults(opts, {
Expand All @@ -35,7 +39,7 @@ var plugin = module.exports = function (opts) {
sourceMapWithCode: true,
sourceContent: fileContents,
sourceMapRoot: file.sourceMap.sourceRoot,
file: file.path
file: normalizePathSep(file.path)
}
});
}
Expand All @@ -46,11 +50,12 @@ var plugin = module.exports = function (opts) {
return cb(new PluginError(PLUGIN_NAME, 'streams not supported'));
}

instrumenter.instrument(fileContents, file.path, function (err, code) {
var filepath = normalizePathSep(file.path);
instrumenter.instrument(fileContents, filepath, function (err, code) {
if (err) {
return cb(new PluginError(
PLUGIN_NAME,
'Unable to parse ' + file.path + '\n\n' + err.message + '\n'
'Unable to parse ' + filepath + '\n\n' + err.message + '\n'
));
}

Expand All @@ -72,7 +77,7 @@ var plugin = module.exports = function (opts) {
if (covStubMatch !== null) {
var covStub = JSON.parse(covStubMatch[0]);
global[opts.coverageVariable] = global[opts.coverageVariable] || {};
global[opts.coverageVariable][file.path] = covStub;
global[opts.coverageVariable][path.resolve(filepath)] = covStub;
}
}

Expand All @@ -86,16 +91,16 @@ plugin.hookRequire = function (options) {

istanbul.hook.unhookRequire();
istanbul.hook.hookRequire(function (path) {
return !!fileMap[path];
return !!fileMap[normalizePathSep(path)];
}, function (code, path) {
return fileMap[path];
return fileMap[normalizePathSep(path)];
}, options);

return through(function (file, enc, cb) {
// If the file is already required, delete it from the cache otherwise the covered
// version will be ignored.
delete require.cache[path.resolve(file.path)];
fileMap[file.path] = file.contents.toString();
fileMap[normalizePathSep(file.path)] = file.contents.toString();
return cb();
});
};
Expand Down
7 changes: 4 additions & 3 deletions test/main.js
Expand Up @@ -10,6 +10,7 @@ var isparta = require('isparta');
var mocha = require('gulp-mocha');
var sourcemaps = require('gulp-sourcemaps');
var Report = require('istanbul').Report;
var path = require('path');

var out = process.stdout.write.bind(process.stdout);

Expand Down Expand Up @@ -48,7 +49,7 @@ describe('gulp-istanbul', function () {

it('throw when receiving a stream', function (done) {
var srcFile = new gutil.File({
path: 'test/fixtures/lib/add.js',
path: path.join('test', 'fixtures', 'lib', 'add.js'),
cwd: 'test/',
base: 'test/fixtures/lib',
contents: fs.createReadStream('test/fixtures/lib/add.js')
Expand All @@ -65,13 +66,13 @@ describe('gulp-istanbul', function () {

it('handles invalid JS files', function (done) {
var srcFile = new gutil.File({
path: 'test/fixtures/lib/add.js',
path: path.join('test', 'fixtures', 'lib', 'add.js'),
cwd: 'test/',
base: 'test/fixtures/lib',
contents: new Buffer('var a {}')
});
this.stream.on('error', function (err) {
assert(err.message.indexOf('test/fixtures/lib/add.js') >= 0);
assert(err.message.indexOf(path.join('test', 'fixtures', 'lib', 'add.js')) >= 0);
done();
});
this.stream.write(srcFile);
Expand Down

0 comments on commit d93e057

Please sign in to comment.