Skip to content

Commit

Permalink
Rework test to check hashes and contents of all files and throw if two
Browse files Browse the repository at this point in the history
files with different content share the same hash; fix another issue for
exports that was uncovered by this.
  • Loading branch information
lukastaegert committed Feb 15, 2019
1 parent ac50033 commit 8c65e47
Show file tree
Hide file tree
Showing 34 changed files with 54 additions and 89 deletions.
2 changes: 1 addition & 1 deletion src/Chunk.ts
Expand Up @@ -667,6 +667,7 @@ export default class Chunk {
if (!this.renderedSource) return '';
const hash = sha256();
hash.update(this.renderedSource.toString());
hash.update(Object.keys(this.exportNames).join(','));
return (this.renderedHash = hash.digest('hex'));
}

Expand Down Expand Up @@ -709,7 +710,6 @@ export default class Chunk {
[addons.intro, addons.outro, addons.banner, addons.footer].map(addon => addon || '').join(':')
);
hash.update(options.format);
hash.update(Object.keys(this.exportNames).join(','));
this.visitDependencies(dep => {
if (dep instanceof ExternalModule) hash.update(':' + dep.renderPath);
else hash.update(dep.getRenderedHash());
Expand Down
@@ -0,0 +1,5 @@
define(['./chunk-main2-0a527b43-amd.js'], function (main2) { 'use strict';

main2.log(main2.dep);

});

This file was deleted.

This file was deleted.

@@ -0,0 +1,7 @@
define(['./chunk-main2-0a527b43-amd.js'], function (main2) { 'use strict';



return main2.log;

});
@@ -0,0 +1,5 @@
'use strict';

var main2 = require('./chunk-main2-328c93f2-cjs.js');

main2.log(main2.dep);

This file was deleted.

This file was deleted.

@@ -0,0 +1,7 @@
'use strict';

var main2 = require('./chunk-main2-328c93f2-cjs.js');



module.exports = main2.log;

This file was deleted.

@@ -0,0 +1,3 @@
import { a as log, b as dep } from './chunk-main2-d7d48284-esm.js';

log(dep);

This file was deleted.

@@ -0,0 +1 @@
export { a as default } from './chunk-main2-d7d48284-esm.js';
@@ -1,4 +1,4 @@
System.register(['./chunk-main2-878d81e1-system.js'], function (exports, module) {
System.register(['./chunk-main2-d134c6d9-system.js'], function (exports, module) {
'use strict';
var log, dep;
return {
Expand Down
@@ -1,4 +1,4 @@
System.register(['./chunk-main2-878d81e1-system.js'], function (exports, module) {
System.register(['./chunk-main2-d134c6d9-system.js'], function (exports, module) {
'use strict';
return {
setters: [function (module) {
Expand Down
50 changes: 13 additions & 37 deletions test/file-hashes/index.js
Expand Up @@ -21,51 +21,27 @@ runTestSuiteWithSamples('file hashes', path.resolve(__dirname, 'samples'), (dir,
)
)
)
.then(mapGeneratedToHashMap)
)
).then(([hashMap1, hashMap2]) => {
for (const name of config.expectedEqualHashes || []) {
checkChunkExists(hashMap1, hashMap2, name);
assert.equal(
hashMap1[name],
hashMap2[name],
`Expected hashes for chunk "${name}" to be equal but they were different.`
);
}
for (const name of config.expectedDifferentHashes || []) {
checkChunkExists(hashMap1, hashMap2, name);
assert.notEqual(
hashMap1[name],
hashMap2[name],
`Expected hashes for chunk "${name}" to be different but they were equal.`
);
}
).then(([generated1, generated2]) => {
const fileContentsByHash = new Map();
addAndCheckFileContentsByHash(fileContentsByHash, generated1);
addAndCheckFileContentsByHash(fileContentsByHash, generated2);
});
});
}
);
});

function mapGeneratedToHashMap(generated) {
const hashMap = {};
function addAndCheckFileContentsByHash(fileContentsByHash, generated) {
for (const chunk of generated.output) {
let name = chunk.name;
let index = 1;
while (hashMap[name]) {
name = `${chunk.name}${index++}`;
const hash = chunk.fileName;
if (fileContentsByHash.has(hash)) {
assert.equal(
fileContentsByHash.get(hash),
chunk.code,
'Two chunks contained different code even though the hashes were the same.'
);
}
hashMap[name] = chunk.fileName;
fileContentsByHash.set(hash, chunk.code);
}
return hashMap;
}

function checkChunkExists(hashMap1, hashMap2, name) {
[hashMap1, hashMap2].forEach((hashMap, index) =>
assert.ok(
hashMap[name],
`Bundle ${index + 1} did not contain chunk "${name}", found chunks: ${Object.keys(hashMap)
.map(key => `"${key}"`)
.join(', ')}`
)
);
}
3 changes: 1 addition & 2 deletions test/file-hashes/samples/banner/_config.js
Expand Up @@ -11,6 +11,5 @@ module.exports = {
output: {
banner: 'console.log(2);'
}
},
expectedDifferentHashes: ['main']
}
};
4 changes: 1 addition & 3 deletions test/file-hashes/samples/content/_config.js
Expand Up @@ -5,7 +5,5 @@ module.exports = {
},
options2: {
input: { main: 'main2', dep: 'dep2' }
},
expectedDifferentHashes: ['main'],
expectedEqualHashes: ['dep']
}
};
3 changes: 1 addition & 2 deletions test/file-hashes/samples/dependency-content/_config.js
Expand Up @@ -5,6 +5,5 @@ module.exports = {
},
options2: {
input: { main: 'main2', dep: 'dep2' }
},
expectedDifferentHashes: ['main', 'dep']
}
};
3 changes: 1 addition & 2 deletions test/file-hashes/samples/exports/_config.js
Expand Up @@ -5,6 +5,5 @@ module.exports = {
},
options2: {
input: ['main2', 'other']
},
expectedDifferentHashes: ['chunk']
}
};
Expand Up @@ -7,7 +7,5 @@ module.exports = {
options2: {
input: { mainA: 'main2a', mainB: 'main2b' },
external: ['external', 'external2']
},
expectedEqualHashes: ['mainA'],
expectedDifferentHashes: ['mainB']
}
};
3 changes: 1 addition & 2 deletions test/file-hashes/samples/footer/_config.js
Expand Up @@ -11,6 +11,5 @@ module.exports = {
output: {
footer: 'console.log(2);'
}
},
expectedDifferentHashes: ['main']
}
};
3 changes: 1 addition & 2 deletions test/file-hashes/samples/format/_config.js
Expand Up @@ -11,6 +11,5 @@ module.exports = {
output: {
format: 'cjs'
}
},
expectedDifferentHashes: ['main']
}
};
3 changes: 1 addition & 2 deletions test/file-hashes/samples/intro/_config.js
Expand Up @@ -11,6 +11,5 @@ module.exports = {
output: {
intro: 'console.log(2);'
}
},
expectedDifferentHashes: ['main']
}
};
3 changes: 1 addition & 2 deletions test/file-hashes/samples/outro/_config.js
Expand Up @@ -11,6 +11,5 @@ module.exports = {
output: {
outro: 'console.log(2);'
}
},
expectedDifferentHashes: ['main']
}
};
2 changes: 1 addition & 1 deletion test/misc/bundle-information.js
Expand Up @@ -26,7 +26,7 @@ describe('The bundle object', () => {
.then(({ output }) => {
assert.deepEqual(
output.map(chunk => chunk.fileName),
['input1-6b4c6b1b.js', 'input2-95ec3647.js', 'generated-chunk-e9283962.js'],
['input1-eebe7cfb.js', 'input2-370063a6.js', 'generated-chunk-e9283962.js'],
'fileName'
);
assert.deepEqual(
Expand Down

0 comments on commit 8c65e47

Please sign in to comment.