|
| 1 | +/* |
| 2 | + * Licensed to Elasticsearch B.V. under one or more contributor |
| 3 | + * license agreements. See the NOTICE file distributed with |
| 4 | + * this work for additional information regarding copyright |
| 5 | + * ownership. Elasticsearch B.V. licenses this file to you under |
| 6 | + * the Apache License, Version 2.0 (the "License"); you may |
| 7 | + * not use this file except in compliance with the License. |
| 8 | + * You may obtain a copy of the License at |
| 9 | + * |
| 10 | + * http://www.apache.org/licenses/LICENSE-2.0 |
| 11 | + * |
| 12 | + * Unless required by applicable law or agreed to in writing, |
| 13 | + * software distributed under the License is distributed on an |
| 14 | + * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY |
| 15 | + * KIND, either express or implied. See the License for the |
| 16 | + * specific language governing permissions and limitations |
| 17 | + * under the License. |
| 18 | + */ |
| 19 | + |
| 20 | +import Path from 'path'; |
| 21 | + |
| 22 | +import jestDiff from 'jest-diff'; |
| 23 | +import { REPO_ROOT, createAbsolutePathSerializer } from '@kbn/dev-utils'; |
| 24 | + |
| 25 | +import { reformatJestDiff, getOptimizerCacheKey, diffCacheKey } from './cache_keys'; |
| 26 | +import { OptimizerConfig } from './optimizer_config'; |
| 27 | + |
| 28 | +jest.mock('./get_changes.ts', () => ({ |
| 29 | + getChanges: async () => |
| 30 | + new Map([ |
| 31 | + ['/foo/bar/a', 'modified'], |
| 32 | + ['/foo/bar/b', 'modified'], |
| 33 | + ['/foo/bar/c', 'deleted'], |
| 34 | + ]), |
| 35 | +})); |
| 36 | + |
| 37 | +jest.mock('./get_mtimes.ts', () => ({ |
| 38 | + getMtimes: async (paths: string[]) => new Map(paths.map(path => [path, 12345])), |
| 39 | +})); |
| 40 | + |
| 41 | +jest.mock('execa'); |
| 42 | + |
| 43 | +jest.mock('fs', () => { |
| 44 | + const realFs = jest.requireActual('fs'); |
| 45 | + return { |
| 46 | + ...realFs, |
| 47 | + readFile: jest.fn(realFs.readFile), |
| 48 | + }; |
| 49 | +}); |
| 50 | + |
| 51 | +expect.addSnapshotSerializer(createAbsolutePathSerializer()); |
| 52 | + |
| 53 | +jest.requireMock('execa').mockImplementation(async (cmd: string, args: string[], opts: object) => { |
| 54 | + expect(cmd).toBe('git'); |
| 55 | + expect(args).toEqual([ |
| 56 | + 'log', |
| 57 | + '-n', |
| 58 | + '1', |
| 59 | + '--pretty=format:%H', |
| 60 | + '--', |
| 61 | + expect.stringContaining('kbn-optimizer'), |
| 62 | + ]); |
| 63 | + expect(opts).toEqual({ |
| 64 | + cwd: REPO_ROOT, |
| 65 | + }); |
| 66 | + |
| 67 | + return { |
| 68 | + stdout: '<last commit sha>', |
| 69 | + }; |
| 70 | +}); |
| 71 | + |
| 72 | +describe('getOptimizerCacheKey()', () => { |
| 73 | + it('uses latest commit, bootstrap cache, and changed files to create unique value', async () => { |
| 74 | + jest |
| 75 | + .requireMock('fs') |
| 76 | + .readFile.mockImplementation( |
| 77 | + (path: string, enc: string, cb: (err: null, file: string) => void) => { |
| 78 | + expect(path).toBe( |
| 79 | + Path.resolve(REPO_ROOT, 'packages/kbn-optimizer/target/.bootstrap-cache') |
| 80 | + ); |
| 81 | + expect(enc).toBe('utf8'); |
| 82 | + cb(null, '<bootstrap cache>'); |
| 83 | + } |
| 84 | + ); |
| 85 | + |
| 86 | + const config = OptimizerConfig.create({ |
| 87 | + repoRoot: REPO_ROOT, |
| 88 | + }); |
| 89 | + |
| 90 | + await expect(getOptimizerCacheKey(config)).resolves.toMatchInlineSnapshot(` |
| 91 | + Object { |
| 92 | + "bootstrap": "<bootstrap cache>", |
| 93 | + "deletedPaths": Array [ |
| 94 | + "/foo/bar/c", |
| 95 | + ], |
| 96 | + "lastCommit": "<last commit sha>", |
| 97 | + "modifiedTimes": Object { |
| 98 | + "/foo/bar/a": 12345, |
| 99 | + "/foo/bar/b": 12345, |
| 100 | + }, |
| 101 | + "workerConfig": Object { |
| 102 | + "browserslistEnv": "dev", |
| 103 | + "cache": true, |
| 104 | + "dist": false, |
| 105 | + "optimizerCacheKey": "♻", |
| 106 | + "profileWebpack": false, |
| 107 | + "repoRoot": <absolute path>, |
| 108 | + "watch": false, |
| 109 | + }, |
| 110 | + } |
| 111 | + `); |
| 112 | + }); |
| 113 | +}); |
| 114 | + |
| 115 | +describe('diffCacheKey()', () => { |
| 116 | + it('returns undefined if values are equal', () => { |
| 117 | + expect(diffCacheKey('1', '1')).toBe(undefined); |
| 118 | + expect(diffCacheKey(1, 1)).toBe(undefined); |
| 119 | + expect(diffCacheKey(['1', '2', { a: 'b' }], ['1', '2', { a: 'b' }])).toBe(undefined); |
| 120 | + expect( |
| 121 | + diffCacheKey( |
| 122 | + { |
| 123 | + a: '1', |
| 124 | + b: '2', |
| 125 | + }, |
| 126 | + { |
| 127 | + b: '2', |
| 128 | + a: '1', |
| 129 | + } |
| 130 | + ) |
| 131 | + ).toBe(undefined); |
| 132 | + }); |
| 133 | + |
| 134 | + it('returns a diff if the values are different', () => { |
| 135 | + expect(diffCacheKey(['1', '2', { a: 'b' }], ['1', '2', { b: 'a' }])).toMatchInlineSnapshot(` |
| 136 | + "[32m- Expected[39m |
| 137 | + [31m+ Received[39m |
| 138 | +
|
| 139 | + [2m Array [[22m |
| 140 | + [2m \\"1\\",[22m |
| 141 | + [2m \\"2\\",[22m |
| 142 | + [2m Object {[22m |
| 143 | + [32m- \\"a\\": \\"b\\",[39m |
| 144 | + [31m+ \\"b\\": \\"a\\",[39m |
| 145 | + [2m },[22m |
| 146 | + [2m ][22m" |
| 147 | + `); |
| 148 | + expect( |
| 149 | + diffCacheKey( |
| 150 | + { |
| 151 | + a: '1', |
| 152 | + b: '1', |
| 153 | + }, |
| 154 | + { |
| 155 | + b: '2', |
| 156 | + a: '2', |
| 157 | + } |
| 158 | + ) |
| 159 | + ).toMatchInlineSnapshot(` |
| 160 | + "[32m- Expected[39m |
| 161 | + [31m+ Received[39m |
| 162 | +
|
| 163 | + [2m Object {[22m |
| 164 | + [32m- \\"a\\": \\"1\\",[39m |
| 165 | + [32m- \\"b\\": \\"1\\",[39m |
| 166 | + [31m+ \\"a\\": \\"2\\",[39m |
| 167 | + [31m+ \\"b\\": \\"2\\",[39m |
| 168 | + [2m }[22m" |
| 169 | + `); |
| 170 | + }); |
| 171 | +}); |
| 172 | + |
| 173 | +describe('reformatJestDiff()', () => { |
| 174 | + it('reformats large jestDiff output to focus on the changed lines', () => { |
| 175 | + const diff = jestDiff( |
| 176 | + { |
| 177 | + a: ['1', '1', '1', '1', '1', '1', '1', '2', '1', '1', '1', '1', '1', '1', '1', '1', '1'], |
| 178 | + }, |
| 179 | + { |
| 180 | + b: ['1', '1', '1', '1', '1', '1', '1', '1', '1', '1', '1', '1', '2', '1', '1', '1', '1'], |
| 181 | + } |
| 182 | + ); |
| 183 | + |
| 184 | + expect(reformatJestDiff(diff)).toMatchInlineSnapshot(` |
| 185 | + "[32m- Expected[39m |
| 186 | + [31m+ Received[39m |
| 187 | +
|
| 188 | + [2m Object {[22m |
| 189 | + [32m- \\"a\\": Array [[39m |
| 190 | + [31m+ \\"b\\": Array [[39m |
| 191 | + [2m \\"1\\",[22m |
| 192 | + [2m \\"1\\",[22m |
| 193 | + [2m ...[22m |
| 194 | + [2m \\"1\\",[22m |
| 195 | + [2m \\"1\\",[22m |
| 196 | + [32m- \\"2\\",[39m |
| 197 | + [2m \\"1\\",[22m |
| 198 | + [2m \\"1\\",[22m |
| 199 | + [2m ...[22m |
| 200 | + [2m \\"1\\",[22m |
| 201 | + [2m \\"1\\",[22m |
| 202 | + [31m+ \\"2\\",[39m |
| 203 | + [2m \\"1\\",[22m |
| 204 | + [2m \\"1\\",[22m |
| 205 | + [2m ...[22m" |
| 206 | + `); |
| 207 | + }); |
| 208 | +}); |
0 commit comments