Skip to content

Commit 52b69ef

Browse files
coreyfarrellbcoe
authored andcommitted
fix: Update caching-transform options. (#873)
When updating caching--transform it was missed that the `hash` option has been refactored. This means that two files with identical contents would be treated as identical.
1 parent 624a723 commit 52b69ef

File tree

6 files changed

+50
-17
lines changed

6 files changed

+50
-17
lines changed

index.js

+3-5
Original file line numberDiff line numberDiff line change
@@ -91,13 +91,11 @@ function NYC (config) {
9191
}
9292

9393
NYC.prototype._createTransform = function (ext) {
94-
var _this = this
9594
var opts = {
9695
salt: Hash.salt,
97-
hash: function (code, metadata, salt) {
98-
var hash = Hash(code, metadata.filename)
99-
_this.hashCache[metadata.filename] = hash
100-
return hash
96+
hashData: (input, metadata) => [metadata.filename],
97+
onHash: (input, metadata, hash) => {
98+
this.hashCache[metadata.filename] = hash
10199
},
102100
cacheDir: this.cacheDirectory,
103101
// when running --all we should not load source-file from

lib/hash.js

+6-12
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,8 @@
1-
const CACHE_VERSION = require('../package.json').version
2-
const md5hex = require('md5-hex')
3-
const salt = JSON.stringify({
4-
istanbul: require('istanbul-lib-coverage/package.json').version,
5-
nyc: CACHE_VERSION
6-
})
1+
'use strict'
72

8-
function Hash (code, filename) {
9-
return md5hex([code, filename, salt]) + '_' + CACHE_VERSION
3+
module.exports = {
4+
salt: JSON.stringify({
5+
istanbul: require('istanbul-lib-coverage/package.json').version,
6+
nyc: require('../package.json').version
7+
})
108
}
11-
12-
Hash.salt = salt
13-
14-
module.exports = Hash
+14
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
const path = require('path')
2+
const assert = require('assert')
3+
const file1 = require('./identical-file1.js')
4+
const file2 = require('./identical-file2.js')
5+
6+
assert.equal(file1(), file2())
7+
8+
const cov = (new Function('return this.__coverage__'))()
9+
10+
assert.deepEqual(Object.keys(cov).sort(), [
11+
__filename,
12+
path.resolve('identical-file1.js'),
13+
path.resolve('identical-file2.js')
14+
])

test/fixtures/identical-file1.js

+5
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
function identical() {
2+
return 'identical'
3+
}
4+
5+
module.exports = identical

test/fixtures/identical-file2.js

+5
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
function identical() {
2+
return 'identical'
3+
}
4+
5+
module.exports = identical

test/src/nyc-tap.js

+17
Original file line numberDiff line numberDiff line change
@@ -457,5 +457,22 @@ describe('nyc', function () {
457457
done()
458458
})
459459
})
460+
461+
it('handles identical files', function (done) {
462+
var nyc = new NYC(configUtil.buildYargs(fixtures).parse())
463+
nyc.clearCache()
464+
465+
var args = [bin, process.execPath, './identical-file-runner.js']
466+
467+
var proc = spawn(process.execPath, args, {
468+
cwd: fixtures,
469+
env: {}
470+
})
471+
472+
proc.on('close', function (code) {
473+
code.should.equal(0)
474+
done()
475+
})
476+
})
460477
})
461478
})

0 commit comments

Comments
 (0)