Skip to content

Commit

Permalink
fix: lower length of key to improve usage in win32 (#13827)
Browse files Browse the repository at this point in the history
  • Loading branch information
trajano committed Feb 23, 2023
1 parent 9d4efd1 commit f022414
Show file tree
Hide file tree
Showing 4 changed files with 44 additions and 7 deletions.
1 change: 1 addition & 0 deletions CHANGELOG.md
Expand Up @@ -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))

Expand Down
5 changes: 3 additions & 2 deletions packages/jest-create-cache-key-function/README.md
Expand Up @@ -10,14 +10,15 @@ $ npm install --save-dev @jest/create-cache-key-function

## API

### `createCacheKey(files?: Array<string>, values?: Array<String>): GetCacheKeyFunction`
### `createCacheKey(files?: Array<string>, values?: Array<String>, 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:**

Expand Down
Expand Up @@ -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(() => {
Expand Down Expand Up @@ -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);
});
26 changes: 21 additions & 5 deletions packages/jest-create-cache-key-function/src/index.ts
Expand Up @@ -40,7 +40,11 @@ type NewGetCacheKeyFunction = (

type GetCacheKeyFunction = OldGetCacheKeyFunction | NewGetCacheKeyFunction;

function getGlobalCacheKey(files: Array<string>, values: Array<string>) {
function getGlobalCacheKey(
files: Array<string>,
values: Array<string>,
length: number,
) {
return [
process.env.NODE_ENV,
process.env.BABEL_ENV,
Expand All @@ -52,10 +56,13 @@ function getGlobalCacheKey(files: Array<string>, values: Array<string>) {
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
Expand All @@ -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<string> = [],
values: Array<string> = [],
length = process.platform === 'win32' ? 16 : 32,
): GetCacheKeyFunction {
return getCacheKeyFunction(getGlobalCacheKey(files, values));
return getCacheKeyFunction(getGlobalCacheKey(files, values, length), length);
}

0 comments on commit f022414

Please sign in to comment.