-
-
Notifications
You must be signed in to change notification settings - Fork 35.5k
Expand file tree
/
Copy pathtest-watch-mode-files_watcher.mjs
More file actions
254 lines (219 loc) Β· 9.26 KB
/
test-watch-mode-files_watcher.mjs
File metadata and controls
254 lines (219 loc) Β· 9.26 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
// Flags: --expose-internals
import * as common from '../common/index.mjs';
import * as fixtures from '../common/fixtures.mjs';
import tmpdir from '../common/tmpdir.js';
import path from 'node:path';
import assert from 'node:assert';
import process from 'node:process';
import { describe, it, beforeEach, afterEach } from 'node:test';
import { writeFileSync, mkdirSync, appendFileSync } from 'node:fs';
import { createInterface } from 'node:readline';
import { setTimeout } from 'node:timers/promises';
import { once } from 'node:events';
import { spawn } from 'node:child_process';
import watcher from 'internal/watch_mode/files_watcher';
if (common.isIBMi)
common.skip('IBMi does not support `fs.watch()`');
const supportsRecursiveWatching = common.isMacOS || common.isWindows;
const { FilesWatcher } = watcher;
tmpdir.refresh();
describe('watch mode file watcher', () => {
let watcher;
let changesCount;
beforeEach(() => {
changesCount = 0;
watcher = new FilesWatcher({ debounce: 100 });
watcher.on('changed', () => changesCount++);
});
afterEach(() => watcher.clear());
let counter = 0;
function writeAndWaitForChanges(watcher, file) {
return new Promise((resolve) => {
const interval = setInterval(() => writeFileSync(file, `write ${counter++}`), 100);
watcher.once('changed', () => {
clearInterval(interval);
resolve();
});
});
}
it('should watch changed files', async () => {
const file = tmpdir.resolve('file1');
writeFileSync(file, 'written');
watcher.filterFile(file);
await writeAndWaitForChanges(watcher, file);
assert.strictEqual(changesCount, 1);
});
it('should watch changed files with same prefix path string', async () => {
mkdirSync(tmpdir.resolve('subdir'));
mkdirSync(tmpdir.resolve('sub'));
const file1 = tmpdir.resolve('subdir', 'file1.mjs');
const file2 = tmpdir.resolve('sub', 'file2.mjs');
writeFileSync(file2, 'export const hello = () => { return "hello world"; };');
writeFileSync(file1, 'import { hello } from "../sub/file2.mjs"; console.log(hello());');
const child = spawn(process.execPath,
['--watch', file1],
{ stdio: ['ignore', 'pipe', 'ignore'] });
let completeCount = 0;
for await (const line of createInterface(child.stdout)) {
if (!line.startsWith('Completed running')) {
continue;
}
completeCount++;
if (completeCount === 1) {
appendFileSync(file1, '\n // append 1');
}
// The file is reloaded due to file watching
if (completeCount === 2) {
child.kill();
}
}
});
it('should debounce changes', async () => {
const file = tmpdir.resolve('file2');
writeFileSync(file, 'written');
watcher.filterFile(file);
await writeAndWaitForChanges(watcher, file);
writeFileSync(file, '1');
writeFileSync(file, '2');
writeFileSync(file, '3');
writeFileSync(file, '4');
await setTimeout(200); // debounce * 2
writeFileSync(file, '5');
const changed = once(watcher, 'changed');
writeFileSync(file, 'after');
await changed;
// Unfortunately testing that changesCount === 2 is flaky
assert.ok(changesCount < 5);
});
it('should debounce changes on multiple files', async () => {
const files = [];
for (let i = 0; i < 10; i++) {
const file = tmpdir.resolve(`file-debounced-${i}`);
writeFileSync(file, 'written');
watcher.filterFile(file);
files.push(file);
}
files.forEach((file) => writeFileSync(file, '1'));
files.forEach((file) => writeFileSync(file, '2'));
files.forEach((file) => writeFileSync(file, '3'));
files.forEach((file) => writeFileSync(file, '4'));
await setTimeout(200); // debounce * 2
files.forEach((file) => writeFileSync(file, '5'));
const changed = once(watcher, 'changed');
files.forEach((file) => writeFileSync(file, 'after'));
await changed;
// Unfortunately testing that changesCount === 2 is flaky
assert.ok(changesCount < 5);
});
it('should ignore files in watched directory if they are not filtered',
{ skip: !supportsRecursiveWatching }, async () => {
watcher.on('changed', common.mustNotCall());
watcher.watchPath(tmpdir.path);
writeFileSync(tmpdir.resolve('file3'), '1');
// Wait for this long to make sure changes are not triggered
await setTimeout(1000);
});
it('should allow clearing filters', async () => {
const file = tmpdir.resolve('file4');
writeFileSync(file, 'written');
watcher.filterFile(file);
await writeAndWaitForChanges(watcher, file);
writeFileSync(file, '1');
assert.strictEqual(changesCount, 1);
watcher.clearFileFilters();
writeFileSync(file, '2');
// Wait for this long to make sure changes are triggered only once
await setTimeout(1000);
assert.strictEqual(changesCount, 1);
});
it('should watch all files in watched path when in "all" mode',
{ skip: !supportsRecursiveWatching }, async () => {
watcher = new FilesWatcher({ debounce: 100, mode: 'all' });
watcher.on('changed', () => changesCount++);
const file = tmpdir.resolve('file5');
watcher.watchPath(tmpdir.path);
const changed = once(watcher, 'changed');
await setTimeout(common.platformTimeout(100)); // avoid throttling
writeFileSync(file, 'changed');
await changed;
assert.strictEqual(changesCount, 1);
});
// Regression test for https://github.com/nodejs/node/issues/61906
// When --watch-path is used (mode: 'all'), filterFile() is called for
// --env-file entries. It must watch only that specific file, not the
// entire parent directory, so that touching an unrelated file in the
// same directory does not trigger a restart.
it('filterFile in "all" mode should not trigger on unrelated files',
{ skip: !supportsRecursiveWatching }, async () => {
watcher = new FilesWatcher({ debounce: 100, mode: 'all' });
watcher.on('changed', common.mustNotCall(
'unexpected restart triggered by unrelated file change'));
const envFile = tmpdir.resolve('env-no-trigger.env');
const unrelated = tmpdir.resolve('env-unrelated.txt');
writeFileSync(envFile, 'FOO=bar');
writeFileSync(unrelated, 'initial');
watcher.filterFile(envFile);
await setTimeout(common.platformTimeout(100)); // avoid throttling
writeFileSync(unrelated, 'changed');
// Wait long enough to confirm no restart was triggered
await setTimeout(1000);
});
it('filterFile in "all" mode should trigger when the watched file changes',
{ skip: !supportsRecursiveWatching }, async () => {
watcher = new FilesWatcher({ debounce: 100, mode: 'all' });
watcher.on('changed', () => changesCount++);
const envFile = tmpdir.resolve('env-trigger.env');
writeFileSync(envFile, 'FOO=bar');
watcher.filterFile(envFile);
const changed = once(watcher, 'changed');
await setTimeout(common.platformTimeout(100)); // avoid throttling
writeFileSync(envFile, 'FOO=newvalue');
await changed;
assert.strictEqual(changesCount, 1);
});
it('should ruse existing watcher if it exists',
{ skip: !supportsRecursiveWatching }, () => {
assert.deepStrictEqual(watcher.watchedPaths, []);
watcher.watchPath(tmpdir.path);
assert.deepStrictEqual(watcher.watchedPaths, [tmpdir.path]);
watcher.watchPath(tmpdir.path);
assert.deepStrictEqual(watcher.watchedPaths, [tmpdir.path]);
});
it('should ruse existing watcher of a parent directory',
{ skip: !supportsRecursiveWatching }, () => {
assert.deepStrictEqual(watcher.watchedPaths, []);
watcher.watchPath(tmpdir.path);
assert.deepStrictEqual(watcher.watchedPaths, [tmpdir.path]);
watcher.watchPath(tmpdir.resolve('subdirectory'));
assert.deepStrictEqual(watcher.watchedPaths, [tmpdir.path]);
});
it('should remove existing watcher if adding a parent directory watcher',
{ skip: !supportsRecursiveWatching }, () => {
assert.deepStrictEqual(watcher.watchedPaths, []);
const subdirectory = tmpdir.resolve('subdirectory');
mkdirSync(subdirectory);
watcher.watchPath(subdirectory);
assert.deepStrictEqual(watcher.watchedPaths, [subdirectory]);
watcher.watchPath(tmpdir.path);
assert.deepStrictEqual(watcher.watchedPaths, [tmpdir.path]);
});
it('should clear all watchers when calling clear',
{ skip: !supportsRecursiveWatching }, () => {
assert.deepStrictEqual(watcher.watchedPaths, []);
watcher.watchPath(tmpdir.path);
assert.deepStrictEqual(watcher.watchedPaths, [tmpdir.path]);
watcher.clear();
assert.deepStrictEqual(watcher.watchedPaths, []);
});
it('should watch files from subprocess IPC events', async () => {
const file = fixtures.path('watch-mode/ipc.js');
const child = spawn(process.execPath, [file], { stdio: ['pipe', 'pipe', 'pipe', 'ipc'], encoding: 'utf8' });
watcher.watchChildProcessModules(child);
await once(child, 'exit');
let expected = [file, tmpdir.resolve('file')];
if (supportsRecursiveWatching) {
expected = expected.map((file) => path.dirname(file));
}
assert.deepStrictEqual(watcher.watchedPaths, expected);
});
});