diff --git a/index.js b/index.js index 4e2c990a5..cd8b6b1dd 100755 --- a/index.js +++ b/index.js @@ -91,13 +91,11 @@ function NYC (config) { } NYC.prototype._createTransform = function (ext) { - var _this = this var opts = { salt: Hash.salt, - hash: function (code, metadata, salt) { - var hash = Hash(code, metadata.filename) - _this.hashCache[metadata.filename] = hash - return hash + hashData: (input, metadata) => [metadata.filename], + onHash: (input, metadata, hash) => { + this.hashCache[metadata.filename] = hash }, cacheDir: this.cacheDirectory, // when running --all we should not load source-file from diff --git a/lib/hash.js b/lib/hash.js index a36372071..918f3773a 100644 --- a/lib/hash.js +++ b/lib/hash.js @@ -1,14 +1,8 @@ -const CACHE_VERSION = require('../package.json').version -const md5hex = require('md5-hex') -const salt = JSON.stringify({ - istanbul: require('istanbul-lib-coverage/package.json').version, - nyc: CACHE_VERSION -}) +'use strict' -function Hash (code, filename) { - return md5hex([code, filename, salt]) + '_' + CACHE_VERSION +module.exports = { + salt: JSON.stringify({ + istanbul: require('istanbul-lib-coverage/package.json').version, + nyc: require('../package.json').version + }) } - -Hash.salt = salt - -module.exports = Hash diff --git a/test/fixtures/identical-file-runner.js b/test/fixtures/identical-file-runner.js new file mode 100755 index 000000000..3c35cae1b --- /dev/null +++ b/test/fixtures/identical-file-runner.js @@ -0,0 +1,14 @@ +const path = require('path') +const assert = require('assert') +const file1 = require('./identical-file1.js') +const file2 = require('./identical-file2.js') + +assert.equal(file1(), file2()) + +const cov = (new Function('return this.__coverage__'))() + +assert.deepEqual(Object.keys(cov).sort(), [ + __filename, + path.resolve('identical-file1.js'), + path.resolve('identical-file2.js') +]) diff --git a/test/fixtures/identical-file1.js b/test/fixtures/identical-file1.js new file mode 100644 index 000000000..f4c919dc1 --- /dev/null +++ b/test/fixtures/identical-file1.js @@ -0,0 +1,5 @@ +function identical() { + return 'identical' +} + +module.exports = identical diff --git a/test/fixtures/identical-file2.js b/test/fixtures/identical-file2.js new file mode 100644 index 000000000..f4c919dc1 --- /dev/null +++ b/test/fixtures/identical-file2.js @@ -0,0 +1,5 @@ +function identical() { + return 'identical' +} + +module.exports = identical diff --git a/test/src/nyc-tap.js b/test/src/nyc-tap.js index c21165c03..7fe188124 100644 --- a/test/src/nyc-tap.js +++ b/test/src/nyc-tap.js @@ -457,5 +457,22 @@ describe('nyc', function () { done() }) }) + + it('handles identical files', function (done) { + var nyc = new NYC(configUtil.buildYargs(fixtures).parse()) + nyc.clearCache() + + var args = [bin, process.execPath, './identical-file-runner.js'] + + var proc = spawn(process.execPath, args, { + cwd: fixtures, + env: {} + }) + + proc.on('close', function (code) { + code.should.equal(0) + done() + }) + }) }) })