-
Notifications
You must be signed in to change notification settings - Fork 693
metro-file-map: Watch directories before listing them in fallback (Linux + Windows) watcher #1907
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
+151
−4
Open
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
137 changes: 137 additions & 0 deletions
packages/metro-file-map/src/watchers/__tests__/FallbackWatcher-test.js
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,137 @@ | ||
| /** | ||
| * Copyright (c) Meta Platforms, Inc. and affiliates. | ||
| * | ||
| * This source code is licensed under the MIT license found in the | ||
| * LICENSE file in the root directory of this source tree. | ||
| * | ||
| * @flow strict-local | ||
| * @format | ||
| * @oncall react_native | ||
| */ | ||
|
|
||
| import FallbackWatcher from '../FallbackWatcher'; | ||
| import {createTempWatchRoot} from './helpers'; | ||
| import fs from 'node:fs'; | ||
| import {join} from 'node:path'; | ||
|
|
||
| jest.useRealTimers(); | ||
| jest.setTimeout(10 * 1000); | ||
|
|
||
| const {mkdir, rm, writeFile} = fs.promises; | ||
|
|
||
| describe('FallbackWatcher', () => { | ||
| let watchRoot: string; | ||
| let watcher: ?FallbackWatcher; | ||
| let calls: Array<string>; | ||
| let watchFailure: ?{code: string, path: string}; | ||
|
|
||
| const indexOfCall = (op: 'watch' | 'readdir', dir: string) => | ||
| calls.indexOf(`${op}:${dir}`); | ||
|
|
||
| const expectWatchedBeforeListed = (dir: string) => { | ||
| expect(indexOfCall('watch', dir)).toBeGreaterThanOrEqual(0); | ||
| expect(indexOfCall('watch', dir)).toBeLessThan(indexOfCall('readdir', dir)); | ||
| }; | ||
|
|
||
| beforeEach(async () => { | ||
| watchRoot = await createTempWatchRoot('Fallback', false); | ||
| calls = []; | ||
| watchFailure = null; | ||
|
|
||
| const {watch} = fs; | ||
| jest.spyOn(fs, 'watch').mockImplementation((dir, ...args) => { | ||
| calls.push(`watch:${String(dir)}`); | ||
| const failure = watchFailure; | ||
| if (failure != null && dir === failure.path) { | ||
| const error = new Error(`Cannot watch path '${String(dir)}'.`); | ||
| // $FlowFixMe[prop-missing] code | ||
| error.code = failure.code; | ||
| throw error; | ||
| } | ||
| return watch(dir, ...args); | ||
| }); | ||
| const {readdir} = fs.promises; | ||
| // $FlowFixMe[incompatible-call] - variadic passthrough | ||
| jest.spyOn(fs.promises, 'readdir').mockImplementation((dir, ...args) => { | ||
| calls.push(`readdir:${String(dir)}`); | ||
| return readdir(dir, ...args); | ||
| }); | ||
|
|
||
| watcher = new FallbackWatcher(watchRoot, { | ||
| dot: true, | ||
| globs: [], | ||
| ignored: null, | ||
| watchmanDeferStates: [], | ||
| }); | ||
| }); | ||
|
|
||
| afterEach(async () => { | ||
| await watcher?.stopWatching(); | ||
| jest.restoreAllMocks(); | ||
| await rm(watchRoot, {recursive: true}); | ||
| }); | ||
|
|
||
| // A file written into a directory after it has been listed but before it is | ||
| // watched is reported by neither the listing nor any subsequent event, and is | ||
| // missed until the next full crawl. This is how installing a package against | ||
| // a running server loses files: https://github.com/expo/expo/issues/48950 | ||
| describe('watches each directory before listing it', () => { | ||
| test('during the initial crawl', async () => { | ||
| await mkdir(join(watchRoot, 'a', 'b'), {recursive: true}); | ||
|
|
||
| await watcher?.startWatching(); | ||
|
|
||
| for (const dir of ['', 'a', join('a', 'b')]) { | ||
| expectWatchedBeforeListed(join(watchRoot, dir)); | ||
| } | ||
| }); | ||
|
|
||
| test('for a directory created while watching', async () => { | ||
| await watcher?.startWatching(); | ||
| calls = []; | ||
|
|
||
| const nested = join(watchRoot, 'new', 'nested'); | ||
| await mkdir(nested, {recursive: true}); | ||
| await writeFile(join(nested, 'file.js'), ''); | ||
| await waitFor(() => indexOfCall('readdir', nested) >= 0); | ||
|
|
||
| for (const dir of [join(watchRoot, 'new'), nested]) { | ||
| expectWatchedBeforeListed(dir); | ||
| } | ||
| }); | ||
| }); | ||
|
|
||
| // A watch we cannot establish - one directory over the inotify limit, say - | ||
| // must not cost us the files under it, which would silently truncate the | ||
| // file map. | ||
| test.each([ | ||
| ['ENOSPC', 1], | ||
| ['ENOENT', 0], | ||
| ])( | ||
| 'crawls past a directory it cannot watch (%s)', | ||
| async (code, expectedErrors) => { | ||
| await mkdir(join(watchRoot, 'a', 'b'), {recursive: true}); | ||
| watchFailure = {code, path: join(watchRoot, 'a')}; | ||
| const errors: Array<Error> = []; | ||
| watcher?.onError(error => { | ||
| errors.push(error); | ||
| }); | ||
|
|
||
| await expect(watcher?.startWatching()).resolves.toBeUndefined(); | ||
|
|
||
| expect(errors).toHaveLength(expectedErrors); | ||
| for (const dir of ['a', join('a', 'b')]) { | ||
| expect( | ||
| indexOfCall('readdir', join(watchRoot, dir)), | ||
| ).toBeGreaterThanOrEqual(0); | ||
| } | ||
| }, | ||
| ); | ||
| }); | ||
|
|
||
| async function waitFor(predicate: () => boolean): Promise<void> { | ||
| const deadline = Date.now() + 5000; | ||
| while (!predicate() && Date.now() < deadline) { | ||
| await new Promise(resolve => setTimeout(resolve, 20)); | ||
| } | ||
| } |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.