0
0
mirror of https://github.com/nodejs/node.git synced 2024-11-21 21:19:50 +01:00
nodejs/test/parallel/test-fs-watch-recursive-add-file-with-url.js
Carlos Espa 42f465972a
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 <luigipinca@gmail.com>
Reviewed-By: Pietro Marchini <pietro.marchini94@gmail.com>
Reviewed-By: Yagiz Nizipli <yagiz@nizipli.com>
Reviewed-By: James M Snell <jasnell@gmail.com>
2024-11-06 02:10:33 +01:00

52 lines
1.5 KiB
JavaScript

'use strict';
const common = require('../common');
const { setTimeout } = require('timers/promises');
if (common.isIBMi)
common.skip('IBMi does not support `fs.watch()`');
// fs-watch on folders have limited capability in AIX.
// The testcase makes use of folder watching, and causes
// hang. This behavior is documented. Skip this for AIX.
if (common.isAIX)
common.skip('folder watch capability is limited in AIX.');
const assert = require('assert');
const path = require('path');
const fs = require('fs');
const { pathToFileURL } = require('url');
const tmpdir = require('../common/tmpdir');
const testDir = tmpdir.path;
tmpdir.refresh();
(async () => {
// Add a file to already watching folder, and use URL as the path
const rootDirectory = fs.mkdtempSync(testDir + path.sep);
const testDirectory = path.join(rootDirectory, 'test-5');
fs.mkdirSync(testDirectory);
const filePath = path.join(testDirectory, 'file-8.txt');
const url = pathToFileURL(testDirectory);
const watcher = fs.watch(url, { recursive: true });
let watcherClosed = false;
watcher.on('change', function(event, filename) {
if (filename === path.basename(filePath)) {
assert.strictEqual(event, 'rename');
watcher.close();
watcherClosed = true;
}
});
await setTimeout(common.platformTimeout(100));
fs.writeFileSync(filePath, 'world');
process.on('exit', function() {
assert(watcherClosed, 'watcher Object was not closed');
});
})().then(common.mustCall());