From f0224147dbe420d17ec03867199a4b0cb7e88fbe Mon Sep 17 00:00:00 2001 From: Archimedes Trajano Date: Thu, 23 Feb 2023 03:44:32 -0500 Subject: [PATCH] fix: lower length of key to improve usage in win32 (#13827) --- CHANGELOG.md | 1 + .../jest-create-cache-key-function/README.md | 5 ++-- .../src/__tests__/index.test.ts | 19 ++++++++++++++ .../src/index.ts | 26 +++++++++++++++---- 4 files changed, 44 insertions(+), 7 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index 5175123005fc..cde23dbe2ce7 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -4,6 +4,7 @@ - `[jest-changed-files]` Support Sapling ([#13941](https://github.com/facebook/jest/pull/13941)) - `[jest-cli, jest-config, @jest/core, jest-haste-map, @jest/reporters, jest-runner, jest-runtime, @jest/types]` Add `workerThreads` configuration option to allow using [worker threads](https://nodejs.org/dist/latest/docs/api/worker_threads.html) for parallelization ([#13939](https://github.com/facebook/jest/pull/13939)) +- `[@jest/create-cache-key-function]` Allow passing `length` argument to `createCacheKey()` function and set its default value to `16` on Windows ([#13827](https://github.com/facebook/jest/pull/13827)) - `[jest-message-util]` Add support for [AggregateError](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/AggregateError) ([#13946](https://github.com/facebook/jest/pull/13946)) - `[jest-worker]` Add `start` method to worker farms ([#13937](https://github.com/facebook/jest/pull/13937)) diff --git a/packages/jest-create-cache-key-function/README.md b/packages/jest-create-cache-key-function/README.md index cd014e038c6b..531f65264946 100644 --- a/packages/jest-create-cache-key-function/README.md +++ b/packages/jest-create-cache-key-function/README.md @@ -10,14 +10,15 @@ $ npm install --save-dev @jest/create-cache-key-function ## API -### `createCacheKey(files?: Array, values?: Array): GetCacheKeyFunction` +### `createCacheKey(files?: Array, values?: Array, length?: number): GetCacheKeyFunction` -Get a function that can generate cache keys using source code, provided files and provided values. +Returns a function that can be used to generate cache keys based on source code of provided files and provided values. #### Parameters - `files`: [Optional] Array of absolute paths to files whose code should be accounted for when generating cache key - `values`: [Optional] Array of string values that should be accounted for when generating cache key +- `length`: [Optional] Length of the resulting key. The default is `32`, or `16` on Windows. **Note:** diff --git a/packages/jest-create-cache-key-function/src/__tests__/index.test.ts b/packages/jest-create-cache-key-function/src/__tests__/index.test.ts index 5a026a091f27..f15be30ea631 100644 --- a/packages/jest-create-cache-key-function/src/__tests__/index.test.ts +++ b/packages/jest-create-cache-key-function/src/__tests__/index.test.ts @@ -15,6 +15,9 @@ beforeEach(() => { process.env.NODE_ENV = 'test'; BABEL_ENV = process.env.BABEL_ENV; process.env.BABEL_ENV = 'test'; + Object.defineProperty(process, 'platform', { + value: 'linux', + }); }); afterEach(() => { @@ -44,3 +47,19 @@ test('creation of a cache key', () => { expect(hashA).not.toEqual(hashB); expect(hashA).not.toEqual(hashC); }); + +test('creation of a cache key on win32', () => { + Object.defineProperty(process, 'platform', { + value: 'win32', + }); + const createCacheKeyFunction = interopRequireDefault( + require('../index'), + ).default; + const createCacheKey = createCacheKeyFunction([], ['value']); + const hashA = createCacheKey('test', 'test.js', null, { + config: {}, + instrument: false, + }); + + expect(hashA).toHaveLength(16); +}); diff --git a/packages/jest-create-cache-key-function/src/index.ts b/packages/jest-create-cache-key-function/src/index.ts index f3cce222d303..215a3f4cc2ac 100644 --- a/packages/jest-create-cache-key-function/src/index.ts +++ b/packages/jest-create-cache-key-function/src/index.ts @@ -40,7 +40,11 @@ type NewGetCacheKeyFunction = ( type GetCacheKeyFunction = OldGetCacheKeyFunction | NewGetCacheKeyFunction; -function getGlobalCacheKey(files: Array, values: Array) { +function getGlobalCacheKey( + files: Array, + values: Array, + length: number, +) { return [ process.env.NODE_ENV, process.env.BABEL_ENV, @@ -52,10 +56,13 @@ function getGlobalCacheKey(files: Array, values: Array) { createHash('sha1'), ) .digest('hex') - .substring(0, 32); + .substring(0, length); } -function getCacheKeyFunction(globalCacheKey: string): GetCacheKeyFunction { +function getCacheKeyFunction( + globalCacheKey: string, + length: number, +): GetCacheKeyFunction { return (sourceText, sourcePath, configString, options) => { // Jest 27 passes a single options bag which contains `configString` rather than as a separate argument. // We can hide that API difference, though, so this module is usable for both jest@<27 and jest@>=27 @@ -71,13 +78,22 @@ function getCacheKeyFunction(globalCacheKey: string): GetCacheKeyFunction { .update('\0', 'utf8') .update(instrument ? 'instrument' : '') .digest('hex') - .substring(0, 32); + .substring(0, length); }; } +/** + * Returns a function that can be used to generate cache keys based on source code of provided files and provided values. + * + * @param files - Array of absolute paths to files whose code should be accounted for when generating cache key + * @param values - Array of string values that should be accounted for when generating cache key + * @param length - Length of the resulting key. The default is `32`, or `16` on Windows. + * @returns A function that can be used to generate cache keys. + */ export default function createCacheKey( files: Array = [], values: Array = [], + length = process.platform === 'win32' ? 16 : 32, ): GetCacheKeyFunction { - return getCacheKeyFunction(getGlobalCacheKey(files, values)); + return getCacheKeyFunction(getGlobalCacheKey(files, values, length), length); }