From 1856fb2c5186a96c0cd3de1298ec1e3e46ca56c9 Mon Sep 17 00:00:00 2001 From: Giorgi Rostomashvili Date: Thu, 11 Jun 2020 11:12:28 +0200 Subject: [PATCH 1/4] watchman crawler now includes dotfiles --- CHANGELOG.md | 1 + jest.config.js | 1 + .../src/__tests__/includes_dotfiles.test.ts | 48 +++++++++++++++++++ .../__tests__/test_dotfiles_root/.eslintrc.js | 6 +++ .../src/__tests__/test_dotfiles_root/index.js | 6 +++ .../jest-haste-map/src/crawlers/watchman.ts | 2 +- 6 files changed, 63 insertions(+), 1 deletion(-) create mode 100644 packages/jest-haste-map/src/__tests__/includes_dotfiles.test.ts create mode 100644 packages/jest-haste-map/src/__tests__/test_dotfiles_root/.eslintrc.js create mode 100644 packages/jest-haste-map/src/__tests__/test_dotfiles_root/index.js diff --git a/CHANGELOG.md b/CHANGELOG.md index 3bc2c8d7bb91..b1d771f52228 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -79,6 +79,7 @@ - `[jest-haste-map]` Stop reporting files as changed when they are only accessed ([#7347](https://github.com/facebook/jest/pull/7347)) - `[jest-resolve]` Show relative path from root dir for `module not found` errors ([#9963](https://github.com/facebook/jest/pull/9963)) - `[jest-runtime]` Fix absolute path moduleNameMapper + jest.mock bug ([#8727](https://github.com/facebook/jest/pull/8727)) +- `[jest-haste-map]` Watchman crawler now includes dotfiles ([#10075](https://github.com/facebook/jest/pull/10075)) ### Chore & Maintenance diff --git a/jest.config.js b/jest.config.js index 18ad91a82b6d..cb4e3eb6ae0b 100644 --- a/jest.config.js +++ b/jest.config.js @@ -49,6 +49,7 @@ module.exports = { '/packages/jest-cli/src/init/__tests__/fixtures/', '/packages/jest-haste-map/src/__tests__/haste_impl.js', '/packages/jest-haste-map/src/__tests__/dependencyExtractor.js', + '/packages/jest-haste-map/src/__tests__/test_dotfiles_root', '/packages/jest-resolve-dependencies/src/__tests__/__fixtures__/', '/packages/jest-runtime/src/__tests__/defaultResolver.js', '/packages/jest-runtime/src/__tests__/module_dir/', diff --git a/packages/jest-haste-map/src/__tests__/includes_dotfiles.test.ts b/packages/jest-haste-map/src/__tests__/includes_dotfiles.test.ts new file mode 100644 index 000000000000..990a8d7bf849 --- /dev/null +++ b/packages/jest-haste-map/src/__tests__/includes_dotfiles.test.ts @@ -0,0 +1,48 @@ +/** + * Copyright (c) Facebook, Inc. and its affiliates. All Rights Reserved. + * + * This source code is licensed under the MIT license found in the + * LICENSE file in the root directory of this source tree. + */ + +import path from 'path'; +import HasteMap from '../index'; + +const rootDir = path.join(__dirname, './test_dotfiles_root'); + +const commonOptions = { + extensions: ['js'], + maxWorkers: 1, + platforms: [], + resetCache: true, + retainAllFiles: true, + rootDir, + roots: [rootDir], +}; + +test('watchman crawler and node crawler both include dotfiles', async () => { + const hasteMapWithWatchman = new HasteMap({ + ...commonOptions, + name: 'withWatchman', + useWatchman: true, + }); + + const hasteMapWithNode = new HasteMap({ + ...commonOptions, + name: 'withNode', + useWatchman: false, + }); + + const [builtHasteMapWithWatchman, builtHasteMapWithNode] = await Promise.all([ + hasteMapWithWatchman.build(), + hasteMapWithNode.build(), + ]); + + expect( + builtHasteMapWithWatchman.hasteFS.matchFiles('.eslintrc.js'), + ).toHaveLength(1); + + expect(builtHasteMapWithWatchman.hasteFS.getAllFiles().sort()).toEqual( + builtHasteMapWithNode.hasteFS.getAllFiles().sort(), + ); +}); diff --git a/packages/jest-haste-map/src/__tests__/test_dotfiles_root/.eslintrc.js b/packages/jest-haste-map/src/__tests__/test_dotfiles_root/.eslintrc.js new file mode 100644 index 000000000000..b6861b5fb248 --- /dev/null +++ b/packages/jest-haste-map/src/__tests__/test_dotfiles_root/.eslintrc.js @@ -0,0 +1,6 @@ +/** + * Copyright (c) Facebook, Inc. and its affiliates. All Rights Reserved. + * + * This source code is licensed under the MIT license found in the + * LICENSE file in the root directory of this source tree. + */ diff --git a/packages/jest-haste-map/src/__tests__/test_dotfiles_root/index.js b/packages/jest-haste-map/src/__tests__/test_dotfiles_root/index.js new file mode 100644 index 000000000000..b6861b5fb248 --- /dev/null +++ b/packages/jest-haste-map/src/__tests__/test_dotfiles_root/index.js @@ -0,0 +1,6 @@ +/** + * Copyright (c) Facebook, Inc. and its affiliates. All Rights Reserved. + * + * This source code is licensed under the MIT license found in the + * LICENSE file in the root directory of this source tree. + */ diff --git a/packages/jest-haste-map/src/crawlers/watchman.ts b/packages/jest-haste-map/src/crawlers/watchman.ts index 2ee8a5a88fda..5d47d33f8842 100644 --- a/packages/jest-haste-map/src/crawlers/watchman.ts +++ b/packages/jest-haste-map/src/crawlers/watchman.ts @@ -126,7 +126,7 @@ export = async function watchmanCrawl( ? // Use the `since` generator if we have a clock available {expression, fields, since: clocks.get(relativeRoot)} : // Otherwise use the `glob` filter - {expression, fields, glob}; + {expression, fields, glob, glob_includedotfiles: true}; const response = await cmd('query', root, query); From ec929558efc7cab17bfaedaff4a4927092b69483 Mon Sep 17 00:00:00 2001 From: Giorgi Rostomashvili Date: Thu, 11 Jun 2020 11:12:50 +0200 Subject: [PATCH 2/4] haste-map now ignores vcs directories --- packages/jest-haste-map/package.json | 1 + .../src/__tests__/index.test.js | 39 +++++++++++++++++++ packages/jest-haste-map/src/index.ts | 32 +++++++++++---- packages/jest-haste-map/tsconfig.json | 1 + yarn.lock | 3 +- 5 files changed, 68 insertions(+), 8 deletions(-) diff --git a/packages/jest-haste-map/package.json b/packages/jest-haste-map/package.json index 76e34ab3ea23..22df1b8907ca 100644 --- a/packages/jest-haste-map/package.json +++ b/packages/jest-haste-map/package.json @@ -16,6 +16,7 @@ "anymatch": "^3.0.3", "fb-watchman": "^2.0.0", "graceful-fs": "^4.2.4", + "jest-regex-util": "^26.0.0", "jest-serializer": "^26.1.0", "jest-util": "^26.1.0", "jest-worker": "^26.1.0", diff --git a/packages/jest-haste-map/src/__tests__/index.test.js b/packages/jest-haste-map/src/__tests__/index.test.js index 5d67e670dcde..1135360eb698 100644 --- a/packages/jest-haste-map/src/__tests__/index.test.js +++ b/packages/jest-haste-map/src/__tests__/index.test.js @@ -294,6 +294,45 @@ describe('HasteMap', () => { }); }); + it('ignores vcs directories without ignore pattern', () => { + mockFs['/project/fruits/.git/fruit-history.js'] = ` + // test + `; + return new HasteMap(defaultConfig).build().then(({hasteFS}) => { + expect(hasteFS.matchFiles('.git')).toEqual([]); + }); + }); + + it('ignores vcs directories with ignore pattern regex', () => { + const config = {...defaultConfig, ignorePattern: /Kiwi/}; + mockFs['/project/fruits/Kiwi.js'] = ` + // Kiwi! + `; + + mockFs['/project/fruits/.git/fruit-history.js'] = ` + // test + `; + return new HasteMap(config).build().then(({hasteFS}) => { + expect(hasteFS.matchFiles(/Kiwi/)).toEqual([]); + expect(hasteFS.matchFiles('.git')).toEqual([]); + }); + }); + + it('ignores vcs directories with ignore pattern function', () => { + const config = {...defaultConfig, ignorePattern: f => /Kiwi/.test(f)}; + mockFs['/project/fruits/Kiwi.js'] = ` + // Kiwi! + `; + + mockFs['/project/fruits/.git/fruit-history.js'] = ` + // test + `; + return new HasteMap(config).build().then(({hasteFS}) => { + expect(hasteFS.matchFiles(/Kiwi/)).toEqual([]); + expect(hasteFS.matchFiles('.git')).toEqual([]); + }); + }); + it('builds a haste map on a fresh cache', () => { // Include these files in the map mockFs['/project/fruits/node_modules/react/React.js'] = ` diff --git a/packages/jest-haste-map/src/index.ts b/packages/jest-haste-map/src/index.ts index 572164a4bbf2..8de73059e305 100644 --- a/packages/jest-haste-map/src/index.ts +++ b/packages/jest-haste-map/src/index.ts @@ -15,6 +15,7 @@ import {NodeWatcher, Watcher as SaneWatcher} from 'sane'; import type {Config} from '@jest/types'; import serializer from 'jest-serializer'; import Worker from 'jest-worker'; +import {escapePathForRegex} from 'jest-regex-util'; import {getSha1, worker} from './worker'; import getMockName from './getMockName'; import getPlatformExtension from './lib/getPlatformExtension'; @@ -113,6 +114,9 @@ const CHANGE_INTERVAL = 30; const MAX_WAIT_TIME = 240000; const NODE_MODULES = path.sep + 'node_modules' + path.sep; const PACKAGE_JSON = path.sep + 'package.json'; +const VCS_DIRECTORIES = ['.git', '.hg'] + .map(vcs => escapePathForRegex(path.sep + vcs + path.sep)) + .join('|'); // TypeScript doesn't like us importing from outside `rootDir`, but it doesn't // understand `require`. @@ -233,7 +237,6 @@ class HasteMap extends EventEmitter { extensions: options.extensions, forceNodeFilesystemAPI: !!options.forceNodeFilesystemAPI, hasteImplModulePath: options.hasteImplModulePath, - ignorePattern: options.ignorePattern, maxWorkers: options.maxWorkers, mocksPattern: options.mocksPattern ? new RegExp(options.mocksPattern) @@ -250,11 +253,26 @@ class HasteMap extends EventEmitter { watch: !!options.watch, }; this._console = options.console || global.console; - if (options.ignorePattern && !(options.ignorePattern instanceof RegExp)) { - this._console.warn( - 'jest-haste-map: the `ignorePattern` options as a function is being ' + - 'deprecated. Provide a RegExp instead. See https://github.com/facebook/jest/pull/4063.', - ); + + if (options.ignorePattern) { + if (options.ignorePattern instanceof RegExp) { + this._options.ignorePattern = new RegExp( + options.ignorePattern.source.concat('|' + VCS_DIRECTORIES), + options.ignorePattern.flags, + ); + } else { + const ignorePattern = options.ignorePattern; + const vcsIgnoreRegExp = new RegExp(VCS_DIRECTORIES); + this._options.ignorePattern = (filePath: string) => + vcsIgnoreRegExp.test(filePath) || ignorePattern(filePath); + + this._console.warn( + 'jest-haste-map: the `ignorePattern` options as a function is being ' + + 'deprecated. Provide a RegExp instead. See https://github.com/facebook/jest/pull/4063.', + ); + } + } else { + this._options.ignorePattern = new RegExp(VCS_DIRECTORIES); } const rootDirHash = createHash('md5').update(options.rootDir).digest('hex'); @@ -790,7 +808,7 @@ class HasteMap extends EventEmitter { const createWatcher = (root: Config.Path): Promise => { // @ts-expect-error: TODO how? "Cannot use 'new' with an expression whose type lacks a call or construct signature." const watcher = new Watcher(root, { - dot: false, + dot: true, glob: extensions.map(extension => '**/*.' + extension), ignored: ignorePattern, }); diff --git a/packages/jest-haste-map/tsconfig.json b/packages/jest-haste-map/tsconfig.json index 8290de687869..f945d5e82cc1 100644 --- a/packages/jest-haste-map/tsconfig.json +++ b/packages/jest-haste-map/tsconfig.json @@ -7,6 +7,7 @@ "references": [ {"path": "../jest-worker"}, {"path": "../jest-serializer"}, + {"path": "../jest-regex-util"}, {"path": "../jest-util"}, {"path": "../jest-types"} ] diff --git a/yarn.lock b/yarn.lock index 42b35e08e785..0e2c65f6a462 100644 --- a/yarn.lock +++ b/yarn.lock @@ -3,7 +3,7 @@ __metadata: version: 4 - cacheKey: 5 + cacheKey: 6 "@angular/common@npm:^10.0.2": version: 10.0.2 @@ -11213,6 +11213,7 @@ fsevents@^1.2.7: fb-watchman: ^2.0.0 fsevents: ^2.1.2 graceful-fs: ^4.2.4 + jest-regex-util: ^26.0.0 jest-serializer: ^26.1.0 jest-util: ^26.1.0 jest-worker: ^26.1.0 From 11d88c4a5f7806ada8313659ab488f9f6a7fbb23 Mon Sep 17 00:00:00 2001 From: Simen Bekkhus Date: Thu, 23 Jul 2020 08:56:33 +0200 Subject: [PATCH 3/4] move changelog entry --- CHANGELOG.md | 2 +- packages/jest-haste-map/tsconfig.json | 6 +++--- 2 files changed, 4 insertions(+), 4 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index 2cfda11e6bb1..1c3b57e67af0 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -2,6 +2,7 @@ ### Features +- `[jest-haste-map]` Watchman crawler now includes dotfiles ([#10075](https://github.com/facebook/jest/pull/10075)) - `[jest-worker]` Added support for workers to send custom messages to parent in jest-worker ([#10293](https://github.com/facebook/jest/pull/10293)) - `[pretty-format]` Added support for serializing custom elements (web components) ([#10217](https://github.com/facebook/jest/pull/10237)) @@ -80,7 +81,6 @@ - `[jest-haste-map]` Stop reporting files as changed when they are only accessed ([#7347](https://github.com/facebook/jest/pull/7347)) - `[jest-resolve]` Show relative path from root dir for `module not found` errors ([#9963](https://github.com/facebook/jest/pull/9963)) - `[jest-runtime]` Fix absolute path moduleNameMapper + jest.mock bug ([#8727](https://github.com/facebook/jest/pull/8727)) -- `[jest-haste-map]` Watchman crawler now includes dotfiles ([#10075](https://github.com/facebook/jest/pull/10075)) ### Chore & Maintenance diff --git a/packages/jest-haste-map/tsconfig.json b/packages/jest-haste-map/tsconfig.json index f945d5e82cc1..9bec851aaddb 100644 --- a/packages/jest-haste-map/tsconfig.json +++ b/packages/jest-haste-map/tsconfig.json @@ -5,10 +5,10 @@ "outDir": "build" }, "references": [ - {"path": "../jest-worker"}, - {"path": "../jest-serializer"}, {"path": "../jest-regex-util"}, + {"path": "../jest-serializer"}, + {"path": "../jest-types"}, {"path": "../jest-util"}, - {"path": "../jest-types"} + {"path": "../jest-worker"} ] } From a2c348a8716d2b01511396046de767d5d42cd15d Mon Sep 17 00:00:00 2001 From: Giorgi Rostomashvili Date: Tue, 28 Jul 2020 09:11:33 +0200 Subject: [PATCH 4/4] fix testPathIgnorePattern for test_dotfiles_root --- jest.config.js | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/jest.config.js b/jest.config.js index cb4e3eb6ae0b..b746ebfce9d5 100644 --- a/jest.config.js +++ b/jest.config.js @@ -49,7 +49,7 @@ module.exports = { '/packages/jest-cli/src/init/__tests__/fixtures/', '/packages/jest-haste-map/src/__tests__/haste_impl.js', '/packages/jest-haste-map/src/__tests__/dependencyExtractor.js', - '/packages/jest-haste-map/src/__tests__/test_dotfiles_root', + '/packages/jest-haste-map/src/__tests__/test_dotfiles_root/', '/packages/jest-resolve-dependencies/src/__tests__/__fixtures__/', '/packages/jest-runtime/src/__tests__/defaultResolver.js', '/packages/jest-runtime/src/__tests__/module_dir/',