|
| 1 | +/** |
| 2 | + * Copyright (c) Meta Platforms, Inc. and affiliates. |
| 3 | + * |
| 4 | + * This source code is licensed under the MIT license found in the |
| 5 | + * LICENSE file in the root directory of this source tree. |
| 6 | + * |
| 7 | + * @flow strict-local |
| 8 | + * @format |
| 9 | + */ |
| 10 | + |
| 11 | +import type {BuildParameters} from '../../flow-types'; |
| 12 | + |
| 13 | +import rootRelativeCacheKeys from '../rootRelativeCacheKeys'; |
| 14 | + |
| 15 | +const buildParameters: BuildParameters = { |
| 16 | + computeDependencies: false, |
| 17 | + computeSha1: false, |
| 18 | + dependencyExtractor: null, |
| 19 | + enableSymlinks: false, |
| 20 | + extensions: ['a'], |
| 21 | + forceNodeFilesystemAPI: false, |
| 22 | + hasteImplModulePath: null, |
| 23 | + ignorePattern: /a/, |
| 24 | + mocksPattern: /a/, |
| 25 | + platforms: ['a'], |
| 26 | + retainAllFiles: false, |
| 27 | + rootDir: '/root', |
| 28 | + roots: ['a', 'b'], |
| 29 | + skipPackageJson: false, |
| 30 | + cacheBreaker: 'a', |
| 31 | +}; |
| 32 | + |
| 33 | +jest.mock( |
| 34 | + '/haste/1', |
| 35 | + () => ({ |
| 36 | + getCacheKey: () => 'haste/1', |
| 37 | + }), |
| 38 | + {virtual: true}, |
| 39 | +); |
| 40 | +jest.mock( |
| 41 | + '/haste/2', |
| 42 | + () => ({ |
| 43 | + getCacheKey: () => 'haste/2', |
| 44 | + }), |
| 45 | + {virtual: true}, |
| 46 | +); |
| 47 | +jest.mock( |
| 48 | + '/extractor/1', |
| 49 | + () => ({ |
| 50 | + getCacheKey: () => 'extractor/1', |
| 51 | + }), |
| 52 | + {virtual: true}, |
| 53 | +); |
| 54 | +jest.mock( |
| 55 | + '/extractor/2', |
| 56 | + () => ({ |
| 57 | + getCacheKey: () => 'extractor/2', |
| 58 | + }), |
| 59 | + {virtual: true}, |
| 60 | +); |
| 61 | + |
| 62 | +it('returns a distinct cache key for any change', () => { |
| 63 | + const { |
| 64 | + hasteImplModulePath: _, |
| 65 | + dependencyExtractor: __, |
| 66 | + rootDir: ___, |
| 67 | + ...simpleParameters |
| 68 | + } = buildParameters; |
| 69 | + |
| 70 | + const varyDefault = <T: $Keys<typeof simpleParameters>>( |
| 71 | + key: T, |
| 72 | + newVal: BuildParameters[T], |
| 73 | + ): BuildParameters => { |
| 74 | + // $FlowFixMe[invalid-computed-prop] Can't use a union for a computed prop |
| 75 | + return {...buildParameters, [key]: newVal}; |
| 76 | + }; |
| 77 | + |
| 78 | + const configs = Object.keys(simpleParameters).map(key => { |
| 79 | + switch (key) { |
| 80 | + // Boolean |
| 81 | + case 'computeDependencies': |
| 82 | + case 'computeSha1': |
| 83 | + case 'enableSymlinks': |
| 84 | + case 'forceNodeFilesystemAPI': |
| 85 | + case 'retainAllFiles': |
| 86 | + case 'skipPackageJson': |
| 87 | + return varyDefault(key, !buildParameters[key]); |
| 88 | + // Strings |
| 89 | + case 'cacheBreaker': |
| 90 | + return varyDefault(key, 'foo'); |
| 91 | + // String arrays |
| 92 | + case 'extensions': |
| 93 | + case 'platforms': |
| 94 | + case 'roots': |
| 95 | + return varyDefault(key, ['foo']); |
| 96 | + // Regexp |
| 97 | + case 'mocksPattern': |
| 98 | + case 'ignorePattern': |
| 99 | + return varyDefault(key, /foo/); |
| 100 | + default: |
| 101 | + (key: empty); |
| 102 | + throw new Error('Unrecognised key in build parameters: ' + key); |
| 103 | + } |
| 104 | + }); |
| 105 | + configs.push(buildParameters); |
| 106 | + configs.push({...buildParameters, dependencyExtractor: '/extractor/1'}); |
| 107 | + configs.push({...buildParameters, dependencyExtractor: '/extractor/2'}); |
| 108 | + configs.push({...buildParameters, hasteImplModulePath: '/haste/1'}); |
| 109 | + configs.push({...buildParameters, hasteImplModulePath: '/haste/2'}); |
| 110 | + |
| 111 | + // Generate hashes for each config |
| 112 | + const configHashes = configs.map( |
| 113 | + config => rootRelativeCacheKeys(config).relativeConfigHash, |
| 114 | + ); |
| 115 | + |
| 116 | + // We expect them all to have distinct hashes |
| 117 | + const seen = new Map<string, number>(); |
| 118 | + for (const [i, configHash] of configHashes.entries()) { |
| 119 | + const seenIndex = seen.get(configHash); |
| 120 | + if (seenIndex != null) { |
| 121 | + // Two configs have the same hash - let Jest print the differences |
| 122 | + expect(configs[seenIndex]).toEqual(configs[i]); |
| 123 | + } |
| 124 | + seen.set(configHash, i); |
| 125 | + } |
| 126 | +}); |
0 commit comments