Skip to content

Commit

Permalink
feat(jest-haste-map): Enable crawling for symlink test files
Browse files Browse the repository at this point in the history
  • Loading branch information
mrmeku committed Jan 2, 2020
1 parent 9419034 commit 43f5092
Show file tree
Hide file tree
Showing 5 changed files with 90 additions and 8 deletions.
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@
- `[jest-environment-node]` Implement `compileFunction` ([#9140](https://github.com/facebook/jest/pull/9140))
- `[@jest/fake-timers]` Add Lolex as implementation of fake timers ([#8897](https://github.com/facebook/jest/pull/8897))
- `[jest-get-type]` Add `BigInt` support. ([#8382](https://github.com/facebook/jest/pull/8382))
- `[jest-haste-map]` Enable crawling for symlinked test files ([#9350](https://github.com/facebook/jest/issues/9350))
- `[jest-matcher-utils]` Add `BigInt` support to `ensureNumbers` `ensureActualIsNumber`, `ensureExpectedIsNumber` ([#8382](https://github.com/facebook/jest/pull/8382))
- `[jest-reporters]` Export utils for path formatting ([#9162](https://github.com/facebook/jest/pull/9162))
- `[jest-runner]` Warn if a worker had to be force exited ([#8206](https://github.com/facebook/jest/pull/8206))
Expand Down
19 changes: 19 additions & 0 deletions e2e/Utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -90,6 +90,25 @@ export const writeFiles = (
});
};

export const writeSymlinks = (
directory: string,
symlinks: {[existingFile: string]: string},
) => {
createDirectory(directory);
Object.keys(symlinks).forEach(fileOrPath => {
const symLinkPath = symlinks[fileOrPath];
const dirname = path.dirname(symLinkPath);

if (dirname !== '/') {
createDirectory(path.join(directory, dirname));
}
fs.symlinkSync(
path.resolve(directory, ...fileOrPath.split('/')),
path.resolve(directory, ...symLinkPath.split('/')),
);
});
};

const NUMBER_OF_TESTS_TO_FORCE_USING_WORKERS = 25;
/**
* Forces Jest to use workers by generating many test files to run.
Expand Down
36 changes: 36 additions & 0 deletions e2e/__tests__/crawlSymlinks.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
import {tmpdir} from 'os';
import * as path from 'path';
import * as fs from 'fs';
import {writeFiles, writeSymlinks} from '../Utils';
import runJest from '../runJest';
const DIR = path.resolve(tmpdir(), 'crawl-symlinks-test');

test('Node crawler picks up symlinked files', () => {
if (fs.existsSync(DIR)) {
fs.rmdirSync(DIR, {recursive: true});
}

writeFiles(DIR, {
'package.json': `
{
"jest": {
"testMatch": ["<rootDir>/test-files/test.js"]
}
}
`,
'symlinked-files/test.js': `
test('1+1', () => {
expect(1).toBe(1);
});
`,
});

writeSymlinks(DIR, {
'symlinked-files/test.js': 'test-files/test.js',
});

const {stdout, stderr, exitCode} = runJest(DIR, ['--no-watchman']);
expect(stderr).toContain('Test Suites: 1 passed, 1 total');
expect(stdout).toEqual('');
expect(exitCode).toEqual(0);
});
5 changes: 5 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 @@ -118,8 +118,13 @@ describe('node crawler', () => {
expect(childProcess.spawn).lastCalledWith('find', [
'/project/fruits',
'/project/vegtables',
'(',
'-type',
'f',
'-o',
'-type',
'l',
')',
'(',
'-iname',
'*.js',
Expand Down
37 changes: 29 additions & 8 deletions packages/jest-haste-map/src/crawlers/node.ts
Original file line number Diff line number Diff line change
Expand Up @@ -48,13 +48,32 @@ function find(
fs.lstat(file, (err, stat) => {
activeCalls--;

if (!err && stat && !stat.isSymbolicLink()) {
if (stat.isDirectory()) {
search(file);
if (!err && stat) {
if (stat.isSymbolicLink()) {
activeCalls++;
fs.stat(file, (err, stat) => {
activeCalls--;
if (!err && stat) {
if (stat.isFile()) {
const ext = path.extname(file).substr(1);
if (extensions.indexOf(ext) !== -1) {
result.push([file, stat.mtime.getTime(), stat.size]);
}
}
}

if (activeCalls === 0) {
callback(result);
}
});
} else {
const ext = path.extname(file).substr(1);
if (extensions.indexOf(ext) !== -1) {
result.push([file, stat.mtime.getTime(), stat.size]);
if (stat.isDirectory()) {
search(file);
} else {
const ext = path.extname(file).substr(1);
if (extensions.indexOf(ext) !== -1) {
result.push([file, stat.mtime.getTime(), stat.size]);
}
}
}
}
Expand Down Expand Up @@ -84,7 +103,8 @@ function findNative(
callback: Callback,
): void {
const args = Array.from(roots);
args.push('-type', 'f');
args.push('(', '-type', 'f', '-o', '-type', 'l', ')');

if (extensions.length) {
args.push('(');
}
Expand Down Expand Up @@ -121,7 +141,8 @@ function findNative(
} else {
lines.forEach(path => {
fs.stat(path, (err, stat) => {
if (!err && stat) {
// Filter out symlinks that describe directories
if (!err && stat && !stat.isDirectory()) {
result.push([path, stat.mtime.getTime(), stat.size]);
}
if (--count === 0) {
Expand Down

0 comments on commit 43f5092

Please sign in to comment.