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 23 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.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
1 change: 1 addition & 0 deletions CHANGELOG.md
Expand Up @@ -6,6 +6,7 @@

- `[expect, @jest/expect]` Provide type of `actual` as a generic argument to `Matchers` to allow better-typed extensions ([#13848](https://github.com/facebook/jest/pull/13848))
- `[jest-circus]` Added explicit mention of test failing because `done()` is not being called in error message ([#13847](https://github.com/facebook/jest/pull/13847))
- `[@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))

### Chore & Maintenance

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 @@ -9,17 +9,20 @@ 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;
process.platform = PLATFORM;
});

test('creation of a cache key', () => {
Expand All @@ -44,3 +47,27 @@ test('creation of a cache key', () => {
expect(hashA).not.toEqual(hashB);
expect(hashA).not.toEqual(hashC);
});

test('creation of a cache key on win32', () => {
process.platform = 'win32';
const createCacheKeyFunction = interopRequireDefault(
require('../index'),
).default;
const createCacheKey = createCacheKeyFunction([], ['value']);
const hashA = createCacheKey('test', 'test.js', null, {
config: {},
instrument: false,
});
const hashB = createCacheKey('test code;', 'test.js', null, {
config: {},
instrument: false,
});
const hashC = createCacheKey('test', 'test.js', null, {
config: {},
instrument: true,
});

expect(hashA).toHaveLength(16);
trajano marked this conversation as resolved.
Show resolved Hide resolved
expect(hashA).not.toEqual(hashB);
expect(hashA).not.toEqual(hashC);
});
19 changes: 16 additions & 3 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,7 +56,7 @@ function getGlobalCacheKey(files: Array<string>, values: Array<string>) {
createHash('sha1'),
)
.digest('hex')
.substring(0, 32);
.substring(0, length);
}

function getCacheKeyFunction(globalCacheKey: string): GetCacheKeyFunction {
Expand All @@ -75,9 +79,18 @@ function getCacheKeyFunction(globalCacheKey: string): GetCacheKeyFunction {
};
}

/**
* 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));
}