Skip to content

Commit

Permalink
Check find binary supports the -iname parameter
Browse files Browse the repository at this point in the history
The `-iname` parameter is a non-POSIX extension and may not be supported
by a POSIX compliant find binary. Add a check that the parameter is
supported when determining whether to use the native find binary.
  • Loading branch information
richardlau committed Jul 23, 2020
1 parent 23dce6b commit 7505a40
Show file tree
Hide file tree
Showing 3 changed files with 48 additions and 1 deletion.
1 change: 1 addition & 0 deletions CHANGELOG.md
Expand Up @@ -10,6 +10,7 @@
- `[expect]` Match symbols and bigints in `any()` ([#10223](https://github.com/facebook/jest/pull/10223))
- `[jest-changed-files]` Use `git diff` instead of `git log` for `--changedSince` ([#10155](https://github.com/facebook/jest/pull/10155))
- `[jest-console]` Add missing console.timeLog for compatability with Node ([#10209](https://github.com/facebook/jest/pull/10209))
- `[jest-haste-map] `Check find binary supports the `-iname` parameter ([#10308](https://github.com/facebook/jest/pull/10308))
- `[jest-snapshot]` Strip added indentation for inline error snapshots ([#10217](https://github.com/facebook/jest/pull/10217))

### Chore & Maintenance
Expand Down
39 changes: 39 additions & 0 deletions packages/jest-haste-map/src/crawlers/__tests__/node.test.js
Expand Up @@ -14,6 +14,11 @@ jest.mock('child_process', () => ({
spawn: jest.fn((cmd, args) => {
let closeCallback;
return {
on: jest.fn().mockImplementation((event, callback) => {
if (event === 'exit') {
callback(mockSpawnExit, null);
}
}),
stdout: {
on: jest.fn().mockImplementation((event, callback) => {
if (event === 'data') {
Expand Down Expand Up @@ -131,6 +136,7 @@ const createMap = obj => new Map(Object.keys(obj).map(key => [key, obj[key]]));

const rootDir = '/project';
let mockResponse;
let mockSpawnExit;
let nodeCrawl;
let childProcess;

Expand All @@ -148,6 +154,8 @@ describe('node crawler', () => {
'/project/fruits/strawberry.js',
'/project/fruits/tomato.js',
].join('\n');

mockSpawnExit = 0;
});

it('crawls for files based on patterns', () => {
Expand Down Expand Up @@ -293,6 +301,37 @@ describe('node crawler', () => {
});
});

it('uses node fs APIs on Unix based OS with incompatible find binary', () => {
process.platform = 'linux';
mockResponse = '';
mockSpawnExit = 1;
childProcess = require('child_process');
const which = require('which');
which.mockReturnValueOnce(Promise.resolve('/mypath/find'));

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

return nodeCrawl({
data: {
files: new Map(),
},
extensions: ['js'],
ignore: pearMatcher,
rootDir,
roots: ['/project/fruits'],
}).then(({hasteMap, removedFiles}) => {
expect(childProcess.spawn).lastCalledWith('find', ['.', '-iname', "''"]);
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 on Unix based OS without find binary', () => {
process.platform = 'linux';
const which = require('which');
Expand Down
9 changes: 8 additions & 1 deletion packages/jest-haste-map/src/crawlers/node.ts
Expand Up @@ -31,7 +31,14 @@ async function hasNativeFindSupport(

try {
await which('find');
return true;
return await new Promise(resolve => {
// Check the find binary supports the non-POSIX -iname parameter.
const args = ['.', '-iname', "''"];
const child = spawn('find', args);
child.on('exit', code => {
resolve(code == 0);
});
});
} catch {
return false;
}
Expand Down

0 comments on commit 7505a40

Please sign in to comment.