Skip to content

Commit

Permalink
test: ignore unrelated events in FW watch tests
Browse files Browse the repository at this point in the history
Change assertions on `test-fs-watch-recursive-add-*` tests to only take
into account change events that match the file.

PR-URL: nodejs#55605
Reviewed-By: Luigi Pinca <[email protected]>
Reviewed-By: Pietro Marchini <[email protected]>
Reviewed-By: Yagiz Nizipli <[email protected]>
Reviewed-By: James M Snell <[email protected]>
  • Loading branch information
Ceres6 authored and tpoisseau committed Nov 21, 2024
1 parent 2d0f7ab commit 6171aa9
Show file tree
Hide file tree
Showing 5 changed files with 16 additions and 32 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -40,9 +40,8 @@ const relativePath = path.join(file, path.basename(subfolderPath), childrenFile)
const watcher = fs.watch(testDirectory, { recursive: true });
let watcherClosed = false;
watcher.on('change', function(event, filename) {
assert.strictEqual(event, 'rename');

if (filename === relativePath) {
assert.strictEqual(event, 'rename');
watcher.close();
watcherClosed = true;
}
Expand Down
36 changes: 12 additions & 24 deletions test/parallel/test-fs-watch-recursive-add-file-to-new-folder.js
Original file line number Diff line number Diff line change
Expand Up @@ -33,32 +33,20 @@ const childrenAbsolutePath = path.join(filePath, childrenFile);
const childrenRelativePath = path.join(path.basename(filePath), childrenFile);
let watcherClosed = false;

function doWatch() {
const watcher = fs.watch(testDirectory, { recursive: true });
watcher.on('change', function(event, filename) {
const watcher = fs.watch(testDirectory, { recursive: true });
watcher.on('change', function(event, filename) {
if (filename === childrenRelativePath) {
assert.strictEqual(event, 'rename');
assert.ok(filename === path.basename(filePath) || filename === childrenRelativePath);

if (filename === childrenRelativePath) {
watcher.close();
watcherClosed = true;
}
});

// Do the write with a delay to ensure that the OS is ready to notify us.
setTimeout(() => {
fs.mkdirSync(filePath);
fs.writeFileSync(childrenAbsolutePath, 'world');
}, common.platformTimeout(200));
}
watcher.close();
watcherClosed = true;
}
});

if (common.isMacOS) {
// On macOS delay watcher start to avoid leaking previous events.
// Refs: https://github.com/libuv/libuv/pull/4503
setTimeout(doWatch, common.platformTimeout(100));
} else {
doWatch();
}
// Do the write with a delay to ensure that the OS is ready to notify us.
setTimeout(() => {
fs.mkdirSync(filePath);
fs.writeFileSync(childrenAbsolutePath, 'world');
}, common.platformTimeout(200));

process.once('exit', function() {
assert(watcherClosed, 'watcher Object was not closed');
Expand Down
3 changes: 1 addition & 2 deletions test/parallel/test-fs-watch-recursive-add-file-with-url.js
Original file line number Diff line number Diff line change
Expand Up @@ -35,9 +35,8 @@ tmpdir.refresh();
const watcher = fs.watch(url, { recursive: true });
let watcherClosed = false;
watcher.on('change', function(event, filename) {
assert.strictEqual(event, 'rename');

if (filename === path.basename(filePath)) {
assert.strictEqual(event, 'rename');
watcher.close();
watcherClosed = true;
}
Expand Down
3 changes: 1 addition & 2 deletions test/parallel/test-fs-watch-recursive-add-file.js
Original file line number Diff line number Diff line change
Expand Up @@ -31,9 +31,8 @@ const testFile = path.join(testDirectory, 'file-1.txt');
const watcher = fs.watch(testDirectory, { recursive: true });
let watcherClosed = false;
watcher.on('change', function(event, filename) {
assert.strictEqual(event, 'rename');

if (filename === path.basename(testFile)) {
assert.strictEqual(event, 'rename');
watcher.close();
watcherClosed = true;
}
Expand Down
3 changes: 1 addition & 2 deletions test/parallel/test-fs-watch-recursive-add-folder.js
Original file line number Diff line number Diff line change
Expand Up @@ -33,9 +33,8 @@ tmpdir.refresh();
const watcher = fs.watch(testDirectory, { recursive: true });
let watcherClosed = false;
watcher.on('change', function(event, filename) {
assert.strictEqual(event, 'rename');

if (filename === path.basename(testFile)) {
assert.strictEqual(event, 'rename');
watcher.close();
watcherClosed = true;
}
Expand Down

0 comments on commit 6171aa9

Please sign in to comment.