test: fix test-watch-mode
Refs: https://github.com/nodejs/node/issues/44898 PR-URL: https://github.com/nodejs/node/pull/45585 Reviewed-By: Moshe Atlow <moshe@atlow.co.il> Reviewed-By: Erick Wendel <erick.workspace@gmail.com> Reviewed-By: Rich Trott <rtrott@gmail.com>
This commit is contained in:
parent
2cad517854
commit
89502401bf
@ -15,11 +15,16 @@ if (common.isIBMi)
|
|||||||
common.skip('IBMi does not support `fs.watch()`');
|
common.skip('IBMi does not support `fs.watch()`');
|
||||||
|
|
||||||
const supportsRecursive = common.isOSX || common.isWindows;
|
const supportsRecursive = common.isOSX || common.isWindows;
|
||||||
|
let disableRestart = false;
|
||||||
|
|
||||||
function restart(file) {
|
function restart(file) {
|
||||||
// To avoid flakiness, we save the file repeatedly until test is done
|
// To avoid flakiness, we save the file repeatedly until test is done
|
||||||
writeFileSync(file, readFileSync(file));
|
writeFileSync(file, readFileSync(file));
|
||||||
const timer = setInterval(() => writeFileSync(file, readFileSync(file)), 1000);
|
const timer = setInterval(() => {
|
||||||
|
if (!disableRestart) {
|
||||||
|
writeFileSync(file, readFileSync(file));
|
||||||
|
}
|
||||||
|
}, common.platformTimeout(1000));
|
||||||
return () => clearInterval(timer);
|
return () => clearInterval(timer);
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -38,11 +43,15 @@ async function spawnWithRestarts({
|
|||||||
let stdout = '';
|
let stdout = '';
|
||||||
let cancelRestarts;
|
let cancelRestarts;
|
||||||
|
|
||||||
|
disableRestart = true;
|
||||||
const child = spawn(execPath, ['--watch', '--no-warnings', ...args], { encoding: 'utf8' });
|
const child = spawn(execPath, ['--watch', '--no-warnings', ...args], { encoding: 'utf8' });
|
||||||
child.stderr.on('data', (data) => {
|
child.stderr.on('data', (data) => {
|
||||||
stderr += data;
|
stderr += data;
|
||||||
});
|
});
|
||||||
child.stdout.on('data', async (data) => {
|
child.stdout.on('data', async (data) => {
|
||||||
|
if (data.toString().includes('Restarting')) {
|
||||||
|
disableRestart = true;
|
||||||
|
}
|
||||||
stdout += data;
|
stdout += data;
|
||||||
const restartsCount = stdout.match(new RegExp(`Restarting ${printedArgs.replace(/\\/g, '\\\\')}`, 'g'))?.length ?? 0;
|
const restartsCount = stdout.match(new RegExp(`Restarting ${printedArgs.replace(/\\/g, '\\\\')}`, 'g'))?.length ?? 0;
|
||||||
if (restarts === 0 || !isReady(data.toString())) {
|
if (restarts === 0 || !isReady(data.toString())) {
|
||||||
@ -54,6 +63,9 @@ async function spawnWithRestarts({
|
|||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
cancelRestarts ??= restart(watchedFile);
|
cancelRestarts ??= restart(watchedFile);
|
||||||
|
if (isReady(data.toString())) {
|
||||||
|
disableRestart = false;
|
||||||
|
}
|
||||||
});
|
});
|
||||||
|
|
||||||
await once(child, 'exit');
|
await once(child, 'exit');
|
||||||
@ -97,9 +109,9 @@ async function failWriteSucceed({ file, watchedFile }) {
|
|||||||
|
|
||||||
tmpdir.refresh();
|
tmpdir.refresh();
|
||||||
|
|
||||||
// Warning: this suite can run safely with concurrency: true
|
// Warning: this suite cannot run safely with concurrency: true
|
||||||
// only if tests do not watch/depend on the same files
|
// because of the disableRestart flag used for controlling restarts
|
||||||
describe('watch mode', { concurrency: true, timeout: 60_000 }, () => {
|
describe('watch mode', { concurrency: false, timeout: 60_000 }, () => {
|
||||||
it('should watch changes to a file - event loop ended', async () => {
|
it('should watch changes to a file - event loop ended', async () => {
|
||||||
const file = createTmpFile();
|
const file = createTmpFile();
|
||||||
const { stderr, stdout } = await spawnWithRestarts({ file });
|
const { stderr, stdout } = await spawnWithRestarts({ file });
|
||||||
|
Loading…
x
Reference in New Issue
Block a user