Skip to content

Commit

Permalink
Preserve file data when sha1s match
Browse files Browse the repository at this point in the history
  • Loading branch information
rubennorte committed Sep 17, 2018
1 parent 9822231 commit 5c8456a
Show file tree
Hide file tree
Showing 2 changed files with 31 additions and 14 deletions.
22 changes: 14 additions & 8 deletions packages/jest-haste-map/src/crawlers/__tests__/watchman.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -223,6 +223,8 @@ describe('watchman watch', () => {
});

test('resets the file object when watchman is restarted', () => {
const mockTomatoSha1 = '321f6b7e8bf7f29aab89c5e41a555b1b0baa41a9';

mockResponse = {
'list-capabilities': {
[undefined]: {
Expand All @@ -244,8 +246,9 @@ describe('watchman watch', () => {
name: 'fruits/banana.js',
},
{
'content.sha1hex': mockTomatoSha1,
exists: true,
mtime_ms: {toNumber: () => 31},
mtime_ms: {toNumber: () => 76},
name: 'fruits/tomato.js',
},
],
Expand All @@ -256,8 +259,10 @@ describe('watchman watch', () => {
'watch-project': WATCH_PROJECT_MOCK,
};

const mockMetadata = ['Banana', 41, 1, ['Raspberry'], null];
mockFiles.set(BANANA, mockMetadata);
const mockBananaMetadata = ['Banana', 41, 1, ['Raspberry'], null];
mockFiles.set(BANANA, mockBananaMetadata);
const mockTomatoMetadata = ['Tomato', 31, 1, [], mockTomatoSha1];
mockFiles.set(TOMATO, mockTomatoMetadata);

const clocks = createMap({
[ROOT_MOCK]: 'c:fake-clock:1',
Expand All @@ -284,17 +289,18 @@ describe('watchman watch', () => {
// /fruits/strawberry.js was removed from the file list.
expect(data.files).toEqual(
createMap({
[BANANA]: mockMetadata,
[BANANA]: mockBananaMetadata,
[KIWI]: ['', 42, 0, [], null],
[TOMATO]: mockFiles.get(TOMATO),
[TOMATO]: ['Tomato', 76, 1, [], mockTomatoSha1],
}),
);

// Even though the file list was reset, old file objects are still reused
// if no changes have been made.
expect(data.files.get(BANANA)).toBe(mockMetadata);
// if no changes have been made
expect(data.files.get(BANANA)).toBe(mockBananaMetadata);

expect(data.files.get(TOMATO)).toBe(mockFiles.get(TOMATO));
// Old file objects are not reused if they have a different mtime
expect(data.files.get(TOMATO)).not.toBe(mockTomatoMetadata);
});
});

Expand Down
23 changes: 17 additions & 6 deletions packages/jest-haste-map/src/crawlers/watchman.js
Original file line number Diff line number Diff line change
Expand Up @@ -169,16 +169,27 @@ module.exports = async function watchmanCrawl(
typeof fileData.mtime_ms === 'number'
? fileData.mtime_ms
: fileData.mtime_ms.toNumber();
let sha1hex = fileData['content.sha1hex'];
if (typeof sha1hex !== 'string' || sha1hex.length !== 40) {
sha1hex = null;
}

const existingFileData = data.files.get(name);
if (existingFileData && existingFileData[H.MTIME] === mtime) {
files.set(name, existingFileData);
} else if (
existingFileData &&
sha1hex &&
existingFileData[H.SHA1] === sha1hex
) {
files.set(name, [
existingFileData[0],
mtime,
existingFileData[2],
existingFileData[3],
existingFileData[4],
]);
} else {
let sha1hex = fileData['content.sha1hex'];

if (typeof sha1hex !== 'string' || sha1hex.length !== 40) {
sha1hex = null;
}

// See ../constants.js
files.set(name, ['', mtime, 0, [], sha1hex]);
}
Expand Down

0 comments on commit 5c8456a

Please sign in to comment.