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

Support dynamically detecting changes when .gitignore is updated #6962

Closed
Closed
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 @@ -143,6 +143,7 @@
- `[*]` Standardize file names ([#7316](https://github.com/facebook/jest/pull/7316), [#7266](https://github.com/facebook/jest/pull/7266), [#7238](https://github.com/facebook/jest/pull/7238), [#7314](https://github.com/facebook/jest/pull/7314), [#7467](https://github.com/facebook/jest/pull/7467), [#7464](https://github.com/facebook/jest/pull/7464))
- `[docs]` Add `testPathIgnorePatterns` in CLI documentation ([#7440](https://github.com/facebook/jest/pull/7440))
- `[docs]` Removed misleading text about `describe()` grouping together tests into a test suite ([#7434](https://github.com/facebook/jest/pull/7434))
- `[jest-haste-map]` Detect changes in `.gitignore` and `.hgignore` ([#6962](https://github.com/facebook/jest/pull/6962))

### Performance

Expand Down
32 changes: 28 additions & 4 deletions packages/jest-haste-map/src/__tests__/index.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -148,6 +148,9 @@ describe('HasteMap', () => {

mockEmitters = Object.create(null);
mockFs = object({
'/project/fruits/.gitignore': `
.git
`,
'/project/fruits/Banana.js': `
const Strawberry = require("Strawberry");
`,
Expand Down Expand Up @@ -272,6 +275,7 @@ describe('HasteMap', () => {
it('matches files against a pattern', () =>
new HasteMap(defaultConfig).build().then(({hasteFS}) => {
expect(hasteFS.matchFiles(/project\/fruits/)).toEqual([
'/project/fruits/.gitignore',
'/project/fruits/Banana.js',
'/project/fruits/Pear.js',
'/project/fruits/Strawberry.js',
Expand Down Expand Up @@ -337,6 +341,7 @@ describe('HasteMap', () => {

expect(data.files).toEqual(
createMap({
'fruits/.gitignore': ['.gitignore', 32, 1, [], null],
'fruits/Banana.js': ['Banana', 32, 1, ['Strawberry'], null],
'fruits/Pear.js': ['Pear', 32, 1, ['Banana', 'Strawberry'], null],
'fruits/Strawberry.js': ['Strawberry', 32, 1, [], null],
Expand All @@ -362,6 +367,7 @@ describe('HasteMap', () => {

expect(data.map).toEqual(
createMap({
".gitignore": {[H.GENERIC_PLATFORM]: ['fruits/.gitignore', H.MODULE]},
Banana: {[H.GENERIC_PLATFORM]: ['fruits/Banana.js', H.MODULE]},
Melon: {[H.GENERIC_PLATFORM]: ['vegetables/Melon.js', H.MODULE]},
Pear: {[H.GENERIC_PLATFORM]: ['fruits/Pear.js', H.MODULE]},
Expand Down Expand Up @@ -405,6 +411,7 @@ describe('HasteMap', () => {

// The node crawler returns "null" for the SHA-1.
data.files = createMap({
'fruits/.gitgignore': ['.gitignore', 32, 0, [], null],
'fruits/Banana.js': ['Banana', 32, 0, ['Strawberry'], null],
'fruits/Pear.js': ['Pear', 32, 0, ['Banana', 'Strawberry'], null],
'fruits/Strawberry.js': ['Strawberry', 32, 0, [], null],
Expand All @@ -427,6 +434,13 @@ describe('HasteMap', () => {

expect(data.files).toEqual(
createMap({
'fruits/.gitignore': [
'.gitignore',
32,
1,
[],
'1cf51edc93c6fa4d695a1fc0bcb3a5df95b731ff',
],
'fruits/Banana.js': [
'Banana',
32,
Expand Down Expand Up @@ -515,7 +529,7 @@ describe('HasteMap', () => {
expect(data.map.get('fbjs')).not.toBeDefined();

// cache file + 5 modules - the node_module
expect(fs.readFileSync.mock.calls.length).toBe(6);
expect(fs.readFileSync.mock.calls.length).toBe(7);
});
});

Expand Down Expand Up @@ -637,7 +651,7 @@ describe('HasteMap', () => {
.then(({__hasteMapForTest: initialData}) => {
// The first run should access the file system once for the (empty)
// cache file and five times for the files in the system.
expect(fs.readFileSync.mock.calls.length).toBe(6);
expect(fs.readFileSync.mock.calls.length).toBe(7);

fs.readFileSync.mockClear();

Expand Down Expand Up @@ -969,7 +983,7 @@ describe('HasteMap', () => {
return new HasteMap(defaultConfig)
.build()
.then(({__hasteMapForTest: data}) => {
expect(data.files.size).toBe(5);
expect(data.files.size).toBe(6);

// Ensure this file is not part of the file list.
expect(data.files.get('fruits/invalid/file.js')).toBe(undefined);
Expand All @@ -991,9 +1005,19 @@ describe('HasteMap', () => {
.then(({__hasteMapForTest: data}) => {
expect(jestWorker.mock.calls.length).toBe(1);

expect(mockWorker.mock.calls.length).toBe(5);
expect(mockWorker.mock.calls.length).toBe(6);

expect(mockWorker.mock.calls).toEqual([
[
{
computeDependencies: true,
computeSha1: false,
dependencyExtractor,
filePath: '/project/fruits/.gitignore',
hasteImplModulePath: undefined,
rootDir: '/project',
},
],
[
{
computeDependencies: true,
Expand Down
16 changes: 9 additions & 7 deletions packages/jest-haste-map/src/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -755,19 +755,20 @@ class HasteMap extends EventEmitter {
: os.platform() === 'darwin'
? sane.FSEventsWatcher
: sane.NodeWatcher;
const extensions = this._options.extensions;
const ignorePattern = this._options.ignorePattern;
const rootDir = this._options.rootDir;

const {extensions, ignorePattern, rootDir} = this._options;
const hiddenFiles = ['.gitignore', '.hgignore'];
let changeQueue = Promise.resolve();
let eventsQueue = [];
// We only need to copy the entire haste map once on every "frame".
let mustCopy = true;

const createWatcher = root => {
const watcher = new Watcher(root, {
dot: false,
glob: extensions.map(extension => '**/*.' + extension),
dot: true,
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Would this impact all dotfiles not just .gitignore and .hgignore?

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I don't think so because the line !suffixes.some(suffix => filePath.endsWith(suffix)) filters other dotfiles

glob: [
...extensions.map(extension => '**/*.' + extension),
...hiddenFiles.map(hiddenFile => '**/' + hiddenFile),
],
ignored: ignorePattern,
});

Expand Down Expand Up @@ -812,10 +813,11 @@ class HasteMap extends EventEmitter {
stat: ?fs.Stats,
) => {
filePath = path.join(root, normalizePathSep(filePath));
const suffixes = [...hiddenFiles, ...extensions];
if (
(stat && stat.isDirectory()) ||
this._ignore(filePath) ||
!extensions.some(extension => filePath.endsWith(extension))
!suffixes.some(suffix => filePath.endsWith(suffix))
) {
return;
}
Expand Down