From d93e0579a830ab4e90fc44dd61629122d99e1ae6 Mon Sep 17 00:00:00 2001 From: Austin Floyd Date: Sun, 14 Aug 2016 19:42:04 -0400 Subject: [PATCH] =?UTF-8?q?=E2=86=92=20Fix=20common=20cross-platform=20iss?= =?UTF-8?q?ue=20with=20file=20path=20normalization.=20(#86)?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit * → 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 --- index.js | 19 ++++++++++++------- test/main.js | 7 ++++--- 2 files changed, 16 insertions(+), 10 deletions(-) diff --git a/index.js b/index.js index 043ced9..553236c 100644 --- a/index.js +++ b/index.js @@ -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, { @@ -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) } }); } @@ -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' )); } @@ -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; } } @@ -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(); }); }; diff --git a/test/main.js b/test/main.js index e9d2ba6..62ce565 100644 --- a/test/main.js +++ b/test/main.js @@ -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); @@ -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') @@ -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);