From 42f465972a846fa5b69c4a5beef1fb238db3c40c 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 628ca4b2fdf..511829fa385 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 32a397821b8..fcc49bb7464 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); + watcher.close(); + watcherClosed = true; + } +}); - 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)); -} - -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 ee726961c41..852c7088d59 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 27b933871cb..e8724102c89 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 1851a7850f6..1a6671de2f3 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; }