[6.7] [chance] ensure random words are unique (#35302) (#35313)

Backports the following commits to 6.7:
 - [chance] ensure random words are unique  (#35302)
This commit is contained in:
Spencer 2019-04-18 11:58:32 -07:00 committed by GitHub
parent 375f9c61e1
commit da37619e5e
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23

View file

@ -33,6 +33,18 @@ import { PUBLIC_PATH_PLACEHOLDER } from '../../public_path_placeholder';
const chance = new Chance();
const outputFixture = resolve(__dirname, './fixtures/output');
const randomWordsCache = new Set();
const uniqueRandomWord = () => {
const word = chance.word();
if (randomWordsCache.has(word)) {
return uniqueRandomWord();
}
randomWordsCache.add(word);
return word;
};
function replaceAll(source, replace, replaceWith) {
return source.split(replace).join(replaceWith);
}
@ -170,7 +182,7 @@ describe('optimizer/bundle route', () => {
describe('js file with placeholder', () => {
it('responds with no content-length and modified file data', async () => {
const basePublicPath = `/${chance.word()}`;
const basePublicPath = `/${uniqueRandomWord()}`;
const server = createServer({ basePublicPath });
const response = await server.inject({
@ -207,7 +219,7 @@ describe('optimizer/bundle route', () => {
describe('css file with placeholder', () => {
it('responds with no content-length and modified file data', async () => {
const basePublicPath = `/${chance.word()}`;
const basePublicPath = `/${uniqueRandomWord()}`;
const server = createServer({ basePublicPath });
const response = await server.inject({
@ -304,8 +316,8 @@ describe('optimizer/bundle route', () => {
});
it('is unique per basePublicPath although content is the same', async () => {
const basePublicPath1 = `/${chance.word()}`;
const basePublicPath2 = `/${chance.word()}`;
const basePublicPath1 = `/${uniqueRandomWord()}`;
const basePublicPath2 = `/${uniqueRandomWord()}`;
const [resp1, resp2] = await Promise.all([
createServer({ basePublicPath: basePublicPath1 }).inject({