From 6171aa91bc3856e69203104830183a94eb8e9375 Mon Sep 17 00:00:00 2001 From: Carlos Espa <43477095+Ceres6@users.noreply.github.com> Date: Wed, 6 Nov 2024 02:10:33 +0100 Subject: [PATCH] test: ignore unrelated events in FW watch tests Change assertions on `test-fs-watch-recursive-add-*` tests to only take into account change events that match the file. PR-URL: https://github.com/nodejs/node/pull/55605 Reviewed-By: Luigi Pinca Reviewed-By: Pietro Marchini Reviewed-By: Yagiz Nizipli Reviewed-By: James M Snell --- ...ecursive-add-file-to-existing-subfolder.js | 3 +- ...-watch-recursive-add-file-to-new-folder.js | 36 +++++++------------ ...st-fs-watch-recursive-add-file-with-url.js | 3 +- .../test-fs-watch-recursive-add-file.js | 3 +- .../test-fs-watch-recursive-add-folder.js | 3 +- 5 files changed, 16 insertions(+), 32 deletions(-) diff --git a/test/parallel/test-fs-watch-recursive-add-file-to-existing-subfolder.js b/test/parallel/test-fs-watch-recursive-add-file-to-existing-subfolder.js index 628ca4b2fdf805..511829fa385e52 100644 --- a/test/parallel/test-fs-watch-recursive-add-file-to-existing-subfolder.js +++ b/test/parallel/test-fs-watch-recursive-add-file-to-existing-subfolder.js @@ -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; } diff --git a/test/parallel/test-fs-watch-recursive-add-file-to-new-folder.js b/test/parallel/test-fs-watch-recursive-add-file-to-new-folder.js index 32a397821b8502..fcc49bb7464937 100644 --- a/test/parallel/test-fs-watch-recursive-add-file-to-new-folder.js +++ b/test/parallel/test-fs-watch-recursive-add-file-to-new-folder.js @@ -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'); diff --git a/test/parallel/test-fs-watch-recursive-add-file-with-url.js b/test/parallel/test-fs-watch-recursive-add-file-with-url.js index ee726961c41e9e..852c7088d59792 100644 --- a/test/parallel/test-fs-watch-recursive-add-file-with-url.js +++ b/test/parallel/test-fs-watch-recursive-add-file-with-url.js @@ -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; } diff --git a/test/parallel/test-fs-watch-recursive-add-file.js b/test/parallel/test-fs-watch-recursive-add-file.js index 27b933871cb403..e8724102c89ff8 100644 --- a/test/parallel/test-fs-watch-recursive-add-file.js +++ b/test/parallel/test-fs-watch-recursive-add-file.js @@ -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; } diff --git a/test/parallel/test-fs-watch-recursive-add-folder.js b/test/parallel/test-fs-watch-recursive-add-folder.js index 1851a7850f66ff..1a6671de2f3617 100644 --- a/test/parallel/test-fs-watch-recursive-add-folder.js +++ b/test/parallel/test-fs-watch-recursive-add-folder.js @@ -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; }