From 625d668b2d6baf0f0eef795b059268ea7e100089 Mon Sep 17 00:00:00 2001 From: mrmeku Date: Wed, 1 Jan 2020 13:34:44 -0700 Subject: [PATCH] feat(jest-haste-map): Enable crawling for symlink test files --- CHANGELOG.md | 1 + e2e/Utils.ts | 19 +++++++++++ e2e/__tests__/crawlSymlinks.test.ts | 34 +++++++++++++++++++ .../src/crawlers/__tests__/node.test.js | 5 +++ packages/jest-haste-map/src/crawlers/node.ts | 6 ++-- 5 files changed, 63 insertions(+), 2 deletions(-) create mode 100644 e2e/__tests__/crawlSymlinks.test.ts diff --git a/CHANGELOG.md b/CHANGELOG.md index 1a386a795440..821fecb28ca5 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -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)) diff --git a/e2e/Utils.ts b/e2e/Utils.ts index a98e2e6bca39..f294fbb09138 100644 --- a/e2e/Utils.ts +++ b/e2e/Utils.ts @@ -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. diff --git a/e2e/__tests__/crawlSymlinks.test.ts b/e2e/__tests__/crawlSymlinks.test.ts new file mode 100644 index 000000000000..78bebbced7e4 --- /dev/null +++ b/e2e/__tests__/crawlSymlinks.test.ts @@ -0,0 +1,34 @@ +import runJest from '../runJest'; +import {tmpdir} from 'os'; +import * as path from 'path'; +import {writeFiles, writeSymlinks} from '../Utils'; +import * as fs from 'fs'; +const DIR = path.resolve(tmpdir(), 'crawl-symlinks-test'); + +test('Node crawler picks up symlinked files', () => { + fs.rmdirSync(DIR, {recursive: true}); + + writeFiles(DIR, { + 'package.json': ` + { + "jest": { + "testMatch": ["/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); +}); diff --git a/packages/jest-haste-map/src/crawlers/__tests__/node.test.js b/packages/jest-haste-map/src/crawlers/__tests__/node.test.js index 69d3c3d6ca73..97e7094bdebf 100644 --- a/packages/jest-haste-map/src/crawlers/__tests__/node.test.js +++ b/packages/jest-haste-map/src/crawlers/__tests__/node.test.js @@ -118,8 +118,13 @@ describe('node crawler', () => { expect(childProcess.spawn).lastCalledWith('find', [ '/project/fruits', '/project/vegtables', + '(', '-type', 'f', + '-o', + '-type', + 'l', + ')', '(', '-iname', '*.js', diff --git a/packages/jest-haste-map/src/crawlers/node.ts b/packages/jest-haste-map/src/crawlers/node.ts index 6e6b6b5d4c9d..108ecfbcda49 100644 --- a/packages/jest-haste-map/src/crawlers/node.ts +++ b/packages/jest-haste-map/src/crawlers/node.ts @@ -84,7 +84,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('('); } @@ -121,7 +122,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) {