Skip to content

Commit

Permalink
Optimize haste map tracking of deleted files with Watchman. (#8056)
Browse files Browse the repository at this point in the history
* Optimize haste map tracking of deprecated files with Watchman.

* Fix issue with not-fresh Watchman.

* Update change log.

* deprecatedFiles -> removedFiles

* deprecated file -> removed file in comments/test descriptions.

* Resolve lint error.
  • Loading branch information
scotthovestadt authored and SimenB committed Mar 6, 2019
1 parent 3e32813 commit 547dcea
Show file tree
Hide file tree
Showing 7 changed files with 204 additions and 92 deletions.
2 changes: 2 additions & 0 deletions CHANGELOG.md
Expand Up @@ -94,6 +94,8 @@

### Performance

- `[jest-haste-map]` Optimize haste map tracking of deleted files with Watchman. ([#8056](https://github.com/facebook/jest/pull/8056))

## 24.1.0

### Features
Expand Down
31 changes: 24 additions & 7 deletions packages/jest-haste-map/src/__tests__/index.test.js
Expand Up @@ -40,6 +40,7 @@ jest.mock('../crawlers/watchman', () =>

const {data, ignore, rootDir, roots, computeSha1} = options;
const list = mockChangedFiles || mockFs;
const removedFiles = new Map();

data.clocks = mockClocks;

Expand All @@ -51,12 +52,19 @@ jest.mock('../crawlers/watchman', () =>

data.files.set(relativeFilePath, ['', 32, 42, 0, [], hash]);
} else {
data.files.delete(relativeFilePath);
const fileData = data.files.get(relativeFilePath);
if (fileData) {
removedFiles.set(relativeFilePath, fileData);
data.files.delete(relativeFilePath);
}
}
}
}

return Promise.resolve(data);
return Promise.resolve({
hasteMap: data,
removedFiles,
});
}),
);

Expand Down Expand Up @@ -416,7 +424,10 @@ describe('HasteMap', () => {
'vegetables/Melon.js': ['Melon', 32, 42, 0, [], null],
});

return Promise.resolve(data);
return Promise.resolve({
hasteMap: data,
removedFiles: new Map(),
});
});

const hasteMap = new HasteMap({
Expand Down Expand Up @@ -543,7 +554,7 @@ describe('HasteMap', () => {
...defaultConfig,
})
.build()
.catch(({__hasteMapForTest: data}) => {
.catch(() => {
expect(console.error.mock.calls[0][0]).toMatchSnapshot();
});
});
Expand Down Expand Up @@ -979,7 +990,7 @@ describe('HasteMap', () => {
mockImpl(options).then(() => {
const {data} = options;
data.files.set('fruits/invalid/file.js', ['', 34, 44, 0, []]);
return data;
return {hasteMap: data, removedFiles: new Map()};
}),
);
return new HasteMap(defaultConfig)
Expand Down Expand Up @@ -1077,7 +1088,10 @@ describe('HasteMap', () => {
data.files = createMap({
'fruits/Banana.js': ['', 32, 42, 0, [], null],
});
return Promise.resolve(data);
return Promise.resolve({
hasteMap: data,
removedFiles: new Map(),
});
});

return new HasteMap(defaultConfig)
Expand Down Expand Up @@ -1108,7 +1122,10 @@ describe('HasteMap', () => {
data.files = createMap({
'fruits/Banana.js': ['', 32, 42, 0, [], null],
});
return Promise.resolve(data);
return Promise.resolve({
hasteMap: data,
removedFiles: new Map(),
});
});

return new HasteMap(defaultConfig)
Expand Down
70 changes: 56 additions & 14 deletions packages/jest-haste-map/src/crawlers/__tests__/node.test.js
Expand Up @@ -114,7 +114,7 @@ describe('node crawler', () => {
ignore: pearMatcher,
rootDir,
roots: ['/project/fruits', '/project/vegtables'],
}).then(data => {
}).then(({hasteMap, removedFiles}) => {
expect(childProcess.spawn).lastCalledWith('find', [
'/project/fruits',
'/project/vegtables',
Expand All @@ -129,15 +129,17 @@ describe('node crawler', () => {
')',
]);

expect(data.files).not.toBe(null);
expect(hasteMap.files).not.toBe(null);

expect(data.files).toEqual(
expect(hasteMap.files).toEqual(
createMap({
'fruits/strawberry.js': ['', 32, 42, 0, [], null],
'fruits/tomato.js': ['', 33, 42, 0, [], null],
'vegetables/melon.json': ['', 34, 42, 0, [], null],
}),
);

expect(removedFiles).toEqual(new Map());
});

return promise;
Expand All @@ -161,16 +163,52 @@ describe('node crawler', () => {
ignore: pearMatcher,
rootDir,
roots: ['/project/fruits'],
}).then(data => {
expect(data.files).toEqual(
}).then(({hasteMap, removedFiles}) => {
expect(hasteMap.files).toEqual(
createMap({
'fruits/strawberry.js': ['', 32, 42, 0, [], null],
'fruits/tomato.js': tomato,
}),
);

// Make sure it is the *same* unchanged object.
expect(data.files.get('fruits/tomato.js')).toBe(tomato);
expect(hasteMap.files.get('fruits/tomato.js')).toBe(tomato);

expect(removedFiles).toEqual(new Map());
});
});

it('returns removed files', () => {
process.platform = 'linux';

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

// In this test sample, previouslyExisted was present before and will not be
// when crawling this directory.
const files = createMap({
'fruits/previouslyExisted.js': ['', 30, 40, 1, [], null],
'fruits/strawberry.js': ['', 33, 42, 0, [], null],
'fruits/tomato.js': ['', 32, 42, 0, [], null],
});

return nodeCrawl({
data: {files},
extensions: ['js'],
ignore: pearMatcher,
rootDir,
roots: ['/project/fruits'],
}).then(({hasteMap, removedFiles}) => {
expect(hasteMap.files).toEqual(
createMap({
'fruits/strawberry.js': ['', 32, 42, 0, [], null],
'fruits/tomato.js': ['', 33, 42, 0, [], null],
}),
);
expect(removedFiles).toEqual(
createMap({
'fruits/previouslyExisted.js': ['', 30, 40, 1, [], null],
}),
);
});
});

Expand All @@ -187,13 +225,14 @@ describe('node crawler', () => {
ignore: pearMatcher,
rootDir,
roots: ['/project/fruits'],
}).then(data => {
expect(data.files).toEqual(
}).then(({hasteMap, removedFiles}) => {
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());
});
});

Expand All @@ -210,13 +249,14 @@ describe('node crawler', () => {
ignore: pearMatcher,
rootDir,
roots: ['/project/fruits'],
}).then(data => {
expect(data.files).toEqual(
}).then(({hasteMap, removedFiles}) => {
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());
});
});

Expand All @@ -233,8 +273,9 @@ describe('node crawler', () => {
ignore: pearMatcher,
rootDir,
roots: [],
}).then(data => {
expect(data.files).toEqual(new Map());
}).then(({hasteMap, removedFiles}) => {
expect(hasteMap.files).toEqual(new Map());
expect(removedFiles).toEqual(new Map());
});
});

Expand All @@ -250,8 +291,9 @@ describe('node crawler', () => {
ignore: pearMatcher,
rootDir,
roots: ['/error'],
}).then(data => {
expect(data.files).toEqual(new Map());
}).then(({hasteMap, removedFiles}) => {
expect(hasteMap.files).toEqual(new Map());
expect(removedFiles).toEqual(new Map());
});
});
});

0 comments on commit 547dcea

Please sign in to comment.