Skip to content
This repository has been archived by the owner on Mar 31, 2024. It is now read-only.

Commit

Permalink
Revert "Temporarily removes kbn-optimizer cache key tests (elastic#58318
Browse files Browse the repository at this point in the history
)"

This reverts commit e64eff0.
  • Loading branch information
spalger committed Feb 24, 2020
1 parent 7bec52a commit 04f2c9e
Showing 1 changed file with 206 additions and 0 deletions.
206 changes: 206 additions & 0 deletions packages/kbn-optimizer/src/optimizer/cache_keys.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,206 @@
/*
* Licensed to Elasticsearch B.V. under one or more contributor
* license agreements. See the NOTICE file distributed with
* this work for additional information regarding copyright
* ownership. Elasticsearch B.V. licenses this file to you under
* the Apache License, Version 2.0 (the "License"); you may
* not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing,
* software distributed under the License is distributed on an
* "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
* KIND, either express or implied. See the License for the
* specific language governing permissions and limitations
* under the License.
*/

import Path from 'path';

import jestDiff from 'jest-diff';
import { REPO_ROOT, createAbsolutePathSerializer } from '@kbn/dev-utils';

import { reformatJestDiff, getOptimizerCacheKey, diffCacheKey } from './cache_keys';
import { OptimizerConfig } from './optimizer_config';

jest.mock('./get_changes.ts', () => ({
getChanges: async () =>
new Map([
['/foo/bar/a', 'modified'],
['/foo/bar/b', 'modified'],
['/foo/bar/c', 'deleted'],
]),
}));

jest.mock('./get_mtimes.ts', () => ({
getMtimes: async (paths: string[]) => new Map(paths.map(path => [path, 12345])),
}));

jest.mock('execa');

jest.mock('fs', () => {
const realFs = jest.requireActual('fs');
jest.spyOn(realFs, 'readFile');
return realFs;
});

expect.addSnapshotSerializer(createAbsolutePathSerializer());

jest.requireMock('execa').mockImplementation(async (cmd: string, args: string[], opts: object) => {
expect(cmd).toBe('git');
expect(args).toEqual([
'log',
'-n',
'1',
'--pretty=format:%H',
'--',
expect.stringContaining('kbn-optimizer'),
]);
expect(opts).toEqual({
cwd: REPO_ROOT,
});

return {
stdout: '<last commit sha>',
};
});

describe('getOptimizerCacheKey()', () => {
it('uses latest commit, bootstrap cache, and changed files to create unique value', async () => {
jest
.requireMock('fs')
.readFile.mockImplementation(
(path: string, enc: string, cb: (err: null, file: string) => void) => {
expect(path).toBe(
Path.resolve(REPO_ROOT, 'packages/kbn-optimizer/target/.bootstrap-cache')
);
expect(enc).toBe('utf8');
cb(null, '<bootstrap cache>');
}
);

const config = OptimizerConfig.create({
repoRoot: REPO_ROOT,
});

await expect(getOptimizerCacheKey(config)).resolves.toMatchInlineSnapshot(`
Object {
"bootstrap": "<bootstrap cache>",
"deletedPaths": Array [
"/foo/bar/c",
],
"lastCommit": "<last commit sha>",
"modifiedTimes": Object {
"/foo/bar/a": 12345,
"/foo/bar/b": 12345,
},
"workerConfig": Object {
"browserslistEnv": "dev",
"cache": true,
"dist": false,
"optimizerCacheKey": "♻",
"profileWebpack": false,
"repoRoot": <absolute path>,
"watch": false,
},
}
`);
});
});

describe('diffCacheKey()', () => {
it('returns undefined if values are equal', () => {
expect(diffCacheKey('1', '1')).toBe(undefined);
expect(diffCacheKey(1, 1)).toBe(undefined);
expect(diffCacheKey(['1', '2', { a: 'b' }], ['1', '2', { a: 'b' }])).toBe(undefined);
expect(
diffCacheKey(
{
a: '1',
b: '2',
},
{
b: '2',
a: '1',
}
)
).toBe(undefined);
});

it('returns a diff if the values are different', () => {
expect(diffCacheKey(['1', '2', { a: 'b' }], ['1', '2', { b: 'a' }])).toMatchInlineSnapshot(`
"- Expected
+ Received
 Array [
 \\"1\\",
 \\"2\\",
 Object {
- \\"a\\": \\"b\\",
+ \\"b\\": \\"a\\",
 },
 ]"
`);
expect(
diffCacheKey(
{
a: '1',
b: '1',
},
{
b: '2',
a: '2',
}
)
).toMatchInlineSnapshot(`
"- Expected
+ Received
 Object {
- \\"a\\": \\"1\\",
- \\"b\\": \\"1\\",
+ \\"a\\": \\"2\\",
+ \\"b\\": \\"2\\",
 }"
`);
});
});

describe('reformatJestDiff()', () => {
it('reformats large jestDiff output to focus on the changed lines', () => {
const diff = jestDiff(
{
a: ['1', '1', '1', '1', '1', '1', '1', '2', '1', '1', '1', '1', '1', '1', '1', '1', '1'],
},
{
b: ['1', '1', '1', '1', '1', '1', '1', '1', '1', '1', '1', '1', '2', '1', '1', '1', '1'],
}
);

expect(reformatJestDiff(diff)).toMatchInlineSnapshot(`
"- Expected
+ Received
 Object {
- \\"a\\": Array [
+ \\"b\\": Array [
 \\"1\\",
 \\"1\\",
 ...
 \\"1\\",
 \\"1\\",
- \\"2\\",
 \\"1\\",
 \\"1\\",
 ...
 \\"1\\",
 \\"1\\",
+ \\"2\\",
 \\"1\\",
 \\"1\\",
 ..."
`);
});
});

0 comments on commit 04f2c9e

Please sign in to comment.