[7.x] [kbn/optimizer] exclude "cache" config from cache key, diff actually compared values (#67232) (#67542)

Co-authored-by: spalger <spalger@users.noreply.github.com>
Co-authored-by: Elastic Machine <elasticmachine@users.noreply.github.com>
This commit is contained in:
Spencer 2020-05-27 13:49:47 -07:00 committed by GitHub
parent 9349f32f0e
commit 6b22503c88
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
4 changed files with 23 additions and 17 deletions

View file

@ -31,7 +31,7 @@ export interface WorkerConfig {
readonly optimizerCacheKey: unknown; readonly optimizerCacheKey: unknown;
} }
export type CacheableWorkerConfig = Omit<WorkerConfig, 'watch' | 'profileWebpack'>; export type CacheableWorkerConfig = Omit<WorkerConfig, 'watch' | 'profileWebpack' | 'cache'>;
export function parseWorkerConfig(json: string): WorkerConfig { export function parseWorkerConfig(json: string): WorkerConfig {
try { try {

View file

@ -208,8 +208,8 @@ it('emits "bundle not cached" event when optimizerCacheKey is outdated, includes
"diff": "- Expected "diff": "- Expected
+ Received + Received
- old - \\"old\\"
+ optimizerCacheKey", + \\"optimizerCacheKey\\"",
"reason": "optimizer cache key mismatch", "reason": "optimizer cache key mismatch",
"type": "bundle not cached", "type": "bundle not cached",
}, },
@ -291,8 +291,8 @@ it('emits "bundle not cached" event when cacheKey is outdated', async () => {
"diff": "- Expected "diff": "- Expected
+ Received + Received
- old - \\"old\\"
+ new", + \\"new\\"",
"reason": "cache key mismatch", "reason": "cache key mismatch",
"type": "bundle not cached", "type": "bundle not cached",
}, },

View file

@ -100,7 +100,6 @@ describe('getOptimizerCacheKey()', () => {
}, },
"workerConfig": Object { "workerConfig": Object {
"browserslistEnv": "dev", "browserslistEnv": "dev",
"cache": true,
"dist": false, "dist": false,
"optimizerCacheKey": "♻", "optimizerCacheKey": "♻",
"repoRoot": <absolute path>, "repoRoot": <absolute path>,
@ -134,13 +133,13 @@ describe('diffCacheKey()', () => {
"- Expected "- Expected
+ Received + Received
 Array [  [
 \\"1\\",  \\"1\\",
 \\"2\\",  \\"2\\",
 Object {  {
- \\"a\\": \\"b\\", - \\"a\\": \\"b\\"
+ \\"b\\": \\"a\\", + \\"b\\": \\"a\\"
 },  }
 ]"  ]"
`); `);
expect( expect(
@ -158,11 +157,11 @@ describe('diffCacheKey()', () => {
"- Expected "- Expected
+ Received + Received
 Object {  {
- \\"a\\": \\"1\\", - \\"a\\": \\"1\\",
- \\"b\\": \\"1\\", - \\"b\\": \\"1\\"
+ \\"a\\": \\"2\\", + \\"a\\": \\"2\\",
+ \\"b\\": \\"2\\", + \\"b\\": \\"2\\"
 }"  }"
`); `);
}); });

View file

@ -48,11 +48,18 @@ function omit<T, K extends keyof T>(obj: T, keys: K[]): Omit<T, K> {
} }
export function diffCacheKey(expected?: unknown, actual?: unknown) { export function diffCacheKey(expected?: unknown, actual?: unknown) {
if (jsonStable(expected) === jsonStable(actual)) { const expectedJson = jsonStable(expected, {
space: ' ',
});
const actualJson = jsonStable(actual, {
space: ' ',
});
if (expectedJson === actualJson) {
return; return;
} }
return reformatJestDiff(jestDiff(expected, actual)); return reformatJestDiff(jestDiff(expectedJson, actualJson));
} }
export function reformatJestDiff(diff: string | null) { export function reformatJestDiff(diff: string | null) {
@ -178,7 +185,7 @@ export async function getOptimizerCacheKey(config: OptimizerConfig) {
bootstrap, bootstrap,
deletedPaths, deletedPaths,
modifiedTimes: {} as Record<string, number>, modifiedTimes: {} as Record<string, number>,
workerConfig: omit(config.getWorkerConfig('♻'), ['watch', 'profileWebpack']), workerConfig: omit(config.getWorkerConfig('♻'), ['watch', 'profileWebpack', 'cache']),
}; };
const mtimes = await getMtimes(modifiedPaths); const mtimes = await getMtimes(modifiedPaths);