Skip to content

Commit

Permalink
feat: Use source base name to prefix cache files (#1144)
Browse files Browse the repository at this point in the history
  • Loading branch information
coreyfarrell authored and JaKXz committed Aug 23, 2019
1 parent fc1bbbf commit 5c1b7a9
Show file tree
Hide file tree
Showing 4 changed files with 57 additions and 3 deletions.
3 changes: 2 additions & 1 deletion index.js
Original file line number Diff line number Diff line change
Expand Up @@ -94,9 +94,10 @@ class NYC {
}

_createTransform (ext) {
var opts = {
const opts = {
salt: Hash.salt(this.config),
hashData: (input, metadata) => [metadata.filename],
filenamePrefix: metadata => path.parse(metadata.filename).name + '-',
onHash: (input, metadata, hash) => {
this.hashCache[metadata.filename] = hash
},
Expand Down
11 changes: 9 additions & 2 deletions lib/source-maps.js
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,13 @@ function SourceMaps (opts) {
this._sourceMapCache = libSourceMaps.createSourceMapStore()
}

SourceMaps.prototype.cachedPath = function (source, hash) {
return path.join(
this.cacheDirectory,
`${path.parse(source).name}-${hash}.map`
)
}

SourceMaps.prototype.purgeCache = function () {
this._sourceMapCache = libSourceMaps.createSourceMapStore()
this.loadedMaps = {}
Expand All @@ -20,7 +27,7 @@ SourceMaps.prototype.extractAndRegister = function (code, filename, hash) {
var sourceMap = convertSourceMap.fromSource(code) || convertSourceMap.fromMapFileSource(code, path.dirname(filename))
if (sourceMap) {
if (this.cache && hash) {
var mapPath = path.join(this.cacheDirectory, hash + '.map')
const mapPath = this.cachedPath(filename, hash)
fs.writeFileSync(mapPath, sourceMap.toJSON())
} else {
this._sourceMapCache.registerMap(filename, sourceMap.sourcemap)
Expand All @@ -43,7 +50,7 @@ SourceMaps.prototype.reloadCachedSourceMaps = function (report) {
var hash = fileReport.contentHash
if (!(hash in this.loadedMaps)) {
try {
var mapPath = path.join(this.cacheDirectory, hash + '.map')
const mapPath = this.cachedPath(absFile, hash)
this.loadedMaps[hash] = JSON.parse(fs.readFileSync(mapPath, 'utf8'))
} catch (e) {
// set to false to avoid repeatedly trying to load the map
Expand Down
20 changes: 20 additions & 0 deletions tap-snapshots/test-nyc-integration.js-TAP.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -369,6 +369,26 @@ All files | 100 | 100 | 100 | 100 |
`

exports[`test/nyc-integration.js TAP caches inline source-maps > stdout 1`] = `
----------|---------|----------|---------|---------|-------------------
File | % Stmts | % Branch | % Funcs | % Lines | Uncovered Line #s
----------|---------|----------|---------|---------|-------------------
All files | 100 | 100 | 100 | 100 |
s2.js | 100 | 100 | 100 | 100 |
----------|---------|----------|---------|---------|-------------------
`

exports[`test/nyc-integration.js TAP caches source-maps from .map files > stdout 1`] = `
----------|---------|----------|---------|---------|-------------------
File | % Stmts | % Branch | % Funcs | % Lines | Uncovered Line #s
----------|---------|----------|---------|---------|-------------------
All files | 80 | 100 | 50 | 80 |
s1.js | 80 | 100 | 50 | 80 | 7
----------|---------|----------|---------|---------|-------------------
`

exports[`test/nyc-integration.js TAP can run "npm test" which directly invokes a test file > stdout 1`] = `
> @ test .
Expand Down
26 changes: 26 additions & 0 deletions test/nyc-integration.js
Original file line number Diff line number Diff line change
Expand Up @@ -404,6 +404,32 @@ t.test('--all uses source-maps to exclude original sources from reports', t => t
cwd: fixturesSourceMaps
}))

t.test('caches source-maps from .map files', t => {
return testSuccess(t, {
args: [
process.execPath,
'./instrumented/s1.min.js'
],
cwd: fixturesSourceMaps
}).then(() => {
const files = fs.readdirSync(path.resolve(fixturesSourceMaps, 'node_modules/.cache/nyc'))
t.true(files.some(f => f.startsWith('s1.min-') && f.endsWith('.map')))
})
})

t.test('caches inline source-maps', t => {
return testSuccess(t, {
args: [
process.execPath,
'./instrumented/s2.min.js'
],
cwd: fixturesSourceMaps
}).then(() => {
const files = fs.readdirSync(path.resolve(fixturesSourceMaps, 'node_modules/.cache/nyc'))
t.true(files.some(f => f.startsWith('s2.min-') && f.endsWith('.map')))
})
})

t.test('appropriately instruments file with corresponding .map file', t => testSuccess(t, {
args: [
'--cache=false',
Expand Down

0 comments on commit 5c1b7a9

Please sign in to comment.