Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: lower length of key to improve usage in win32 #13827

Merged
merged 33 commits into from
Feb 23, 2023
Merged
Show file tree
Hide file tree
Changes from 31 commits
Commits
Show all changes
33 commits
Select commit Hold shift + click to select a range
30d1230
fix: lower length of key to improve usage in win32
trajano Jan 26, 2023
4555dfa
lint fix
trajano Jan 27, 2023
d79fe07
Removed default on getGlobalCacheKey and placed in the exported function
trajano Jan 27, 2023
2aca02e
Update CHANGELOG.md
trajano Jan 27, 2023
643fd29
Update CHANGELOG.md
trajano Jan 27, 2023
64d3a61
Update index.ts
trajano Jan 27, 2023
6341ece
Update CHANGELOG.md
trajano Jan 27, 2023
522d7b8
Update index.ts
trajano Jan 27, 2023
fa8bee7
Update index.ts
trajano Jan 28, 2023
a8a7911
Update packages/jest-create-cache-key-function/src/index.ts
trajano Jan 30, 2023
6fb185b
Update index.ts
trajano Jan 30, 2023
9f3b767
Update index.ts
trajano Jan 30, 2023
7b60763
Update README.md
trajano Jan 30, 2023
1e2a2db
Update index.ts
trajano Jan 30, 2023
77551f8
Update packages/jest-create-cache-key-function/src/index.ts
trajano Jan 30, 2023
77e7ab3
Update packages/jest-create-cache-key-function/src/index.ts
trajano Jan 30, 2023
99ef72d
Update index.ts
trajano Jan 30, 2023
edfea07
Update packages/jest-create-cache-key-function/README.md
trajano Jan 30, 2023
530f949
Update packages/jest-create-cache-key-function/README.md
trajano Jan 30, 2023
0a36e2b
Update packages/jest-create-cache-key-function/src/index.ts
trajano Jan 30, 2023
0889626
Merge branch 'main' into patch-1
SimenB Feb 2, 2023
f849b2c
Update index.test.ts
trajano Feb 2, 2023
6b8361f
Update index.ts
trajano Feb 2, 2023
b1fcfb9
Update index.ts
trajano Feb 15, 2023
714a93d
Update index.test.ts
trajano Feb 15, 2023
da16001
Merge branch 'main' into patch-1
trajano Feb 15, 2023
77cc75d
Update index.test.ts
trajano Feb 15, 2023
b4d7955
Merge branch 'main' into patch-1
SimenB Feb 23, 2023
2f075e2
move changelog entry
SimenB Feb 23, 2023
e75fa73
pass length to both substrings
SimenB Feb 23, 2023
9536286
move changelog entry again
SimenB Feb 23, 2023
e0d2f39
default to linux platform in tests
SimenB Feb 23, 2023
e59df8d
Merge branch 'main' into patch-1
SimenB Feb 23, 2023
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
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-worker]` Add `start` method to worker farms ([#13937](https://github.com/facebook/jest/pull/13937))

### Fixes
Expand Down
5 changes: 3 additions & 2 deletions packages/jest-create-cache-key-function/README.md
Original file line number Diff line number Diff line change
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
Original file line number Diff line number Diff line change
Expand Up @@ -9,17 +9,22 @@ import {interopRequireDefault} from 'jest-util';

let NODE_ENV: string;
let BABEL_ENV: string;
let PLATFORM: string;

beforeEach(() => {
NODE_ENV = process.env.NODE_ENV;
process.env.NODE_ENV = 'test';
BABEL_ENV = process.env.BABEL_ENV;
process.env.BABEL_ENV = 'test';
PLATFORM = process.platform;
});

afterEach(() => {
process.env.NODE_ENV = NODE_ENV;
process.env.BABEL_ENV = BABEL_ENV;
Object.defineProperty(process, 'platform', {
value: PLATFORM,
});
});

test('creation of a cache key', () => {
Expand All @@ -44,3 +49,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);
trajano marked this conversation as resolved.
Show resolved Hide resolved
});
26 changes: 21 additions & 5 deletions packages/jest-create-cache-key-function/src/index.ts
Original file line number Diff line number Diff line change
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);
}