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: crash on unix based systems without find #9579

Merged
merged 5 commits into from
Feb 18, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
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 @@ -16,6 +16,7 @@
- `[jest-cli]` Set `coverageProvider` correctly when provided in config ([#9562](https://github.com/facebook/jest/pull/9562))
- `[jest-cli]` Allow specifying `.cjs` and `.mjs` config files by `--config` CLI option ([#9578](https://github.com/facebook/jest/pull/9578))
- `[jest-config]` Ensure pattern of `replacePosixSep` is a string ([#9546]https://github.com/facebook/jest/pull/9546)
- `[jest-haste-map]` Fix crash on unix based systems without find ([#9579](https://github.com/facebook/jest/pull/9579))
- `[jest-matcher-utils]` Fix diff highlight of symbol-keyed object. ([#9499](https://github.com/facebook/jest/pull/9499))
- `[@jest/reporters]` Notifications should be fire&forget rather than having a timeout ([#9567](https://github.com/facebook/jest/pull/9567))
- `[jest-resolve]` Fix module identity preservation with symlinks and browser field resolution ([#9511](https://github.com/facebook/jest/pull/9511))
Expand Down
6 changes: 4 additions & 2 deletions packages/jest-haste-map/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,8 @@
"jest-worker": "^25.1.0",
"micromatch": "^4.0.2",
"sane": "^4.0.3",
"walker": "^1.0.7"
"walker": "^1.0.7",
"which": "^2.0.2"
},
"devDependencies": {
"@jest/test-utils": "^25.1.0",
Expand All @@ -28,7 +29,8 @@
"@types/graceful-fs": "^4.1.2",
"@types/micromatch": "^4.0.0",
"@types/node": "*",
"@types/sane": "^2.0.0"
"@types/sane": "^2.0.0",
"@types/which": "^1.3.2"
},
"optionalDependencies": {
"fsevents": "^2.1.2"
Expand Down
29 changes: 29 additions & 0 deletions packages/jest-haste-map/src/crawlers/__tests__/node.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -124,6 +124,8 @@ jest.mock('fs', () => {
};
});

jest.mock('which', () => jest.fn().mockResolvedValue());

const pearMatcher = path => /pear/.test(path);
const createMap = obj => new Map(Object.keys(obj).map(key => [key, obj[key]]));

Expand Down Expand Up @@ -291,6 +293,33 @@ describe('node crawler', () => {
});
});

it('uses node fs APIs on Unix based OS without find binary', () => {
process.platform = 'linux';
const which = require('which');
which.mockReturnValueOnce(Promise.reject());

nodeCrawl = require('../node');

return nodeCrawl({
data: {
files: new Map(),
},
extensions: ['js'],
ignore: pearMatcher,
rootDir,
roots: ['/project/fruits'],
}).then(({hasteMap, removedFiles}) => {
expect(hasteMap.files).toEqual(
createMap({
'fruits/directory/strawberry.js': ['', 33, 42, 0, '', null],
'fruits/tomato.js': ['', 32, 42, 0, '', null],
}),
);
expect(removedFiles).toEqual(new Map());
expect(which).toBeCalledWith('find');
});
});

it('uses node fs APIs if "forceNodeFilesystemAPI" is set to true, regardless of platform', () => {
process.platform = 'linux';

Expand Down
26 changes: 22 additions & 4 deletions packages/jest-haste-map/src/crawlers/node.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@
import * as fs from 'fs';
import * as path from 'path';
import {spawn} from 'child_process';
import which = require('which');
import H from '../constants';
import * as fastPath from '../lib/fast_path';
import {
Expand All @@ -21,6 +22,21 @@ type Result = Array<[/* id */ string, /* mtime */ number, /* size */ number]>;

type Callback = (result: Result) => void;

async function hasNativeFindSupport(
forceNodeFilesystemAPI: boolean,
): Promise<boolean> {
if (forceNodeFilesystemAPI || process.platform === 'win32') {
return false;
}

try {
await which('find');
return true;
} catch {
return false;
}
}

function find(
roots: Array<string>,
extensions: Array<string>,
Expand Down Expand Up @@ -154,7 +170,7 @@ function findNative(
});
}

export = function nodeCrawl(
export = async function nodeCrawl(
options: CrawlerOptions,
): Promise<{
removedFiles: FileData;
Expand All @@ -169,6 +185,8 @@ export = function nodeCrawl(
roots,
} = options;

const useNativeFind = await hasNativeFindSupport(forceNodeFilesystemAPI);

return new Promise(resolve => {
const callback = (list: Result) => {
const files = new Map();
Expand All @@ -193,10 +211,10 @@ export = function nodeCrawl(
});
};

if (forceNodeFilesystemAPI || process.platform === 'win32') {
find(roots, extensions, ignore, callback);
} else {
if (useNativeFind) {
findNative(roots, extensions, ignore, callback);
} else {
find(roots, extensions, ignore, callback);
}
});
};
2 changes: 1 addition & 1 deletion yarn.lock
Original file line number Diff line number Diff line change
Expand Up @@ -14745,7 +14745,7 @@ which@1.3.1, which@^1.2.1, which@^1.2.9, which@^1.3.0, which@^1.3.1:
dependencies:
isexe "^2.0.0"

which@^2.0.1:
which@^2.0.1, which@^2.0.2:
version "2.0.2"
resolved "https://registry.yarnpkg.com/which/-/which-2.0.2.tgz#7c6a8dd0a636a0327e10b59c9286eee93f3f51b1"
integrity sha512-BLI3Tl1TW3Pvl70l3yq3Y64i+awpwXqsGBYWkkqMtnbXgrMD+yj7rhW0kuEDxzJaYXGjEW5ogapKNMEKNMjibA==
Expand Down