2022-12-20 14:25:48 +00:00
|
|
|
'use strict';
|
|
|
|
|
|
|
|
const common = require('../common');
|
|
|
|
|
|
|
|
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 tmpdir = require('../common/tmpdir');
|
|
|
|
const testDir = tmpdir.path;
|
|
|
|
tmpdir.refresh();
|
|
|
|
|
2024-01-25 08:53:21 +00:00
|
|
|
// Add a file to subfolder of a watching folder
|
2022-12-20 14:25:48 +00:00
|
|
|
|
2024-01-25 08:53:21 +00:00
|
|
|
const rootDirectory = fs.mkdtempSync(testDir + path.sep);
|
|
|
|
const testDirectory = path.join(rootDirectory, 'test-4');
|
|
|
|
fs.mkdirSync(testDirectory);
|
2022-12-20 14:25:48 +00:00
|
|
|
|
2024-01-25 08:53:21 +00:00
|
|
|
const file = 'folder-5';
|
|
|
|
const filePath = path.join(testDirectory, file);
|
|
|
|
fs.mkdirSync(filePath);
|
2022-12-20 14:25:48 +00:00
|
|
|
|
2024-01-25 08:53:21 +00:00
|
|
|
const subfolderPath = path.join(filePath, 'subfolder-6');
|
|
|
|
fs.mkdirSync(subfolderPath);
|
2022-12-20 14:25:48 +00:00
|
|
|
|
2024-01-25 08:53:21 +00:00
|
|
|
const childrenFile = 'file-7.txt';
|
|
|
|
const childrenAbsolutePath = path.join(subfolderPath, childrenFile);
|
|
|
|
const relativePath = path.join(file, path.basename(subfolderPath), childrenFile);
|
2022-12-20 14:25:48 +00:00
|
|
|
|
2024-01-25 08:53:21 +00:00
|
|
|
const watcher = fs.watch(testDirectory, { recursive: true });
|
|
|
|
let watcherClosed = false;
|
|
|
|
watcher.on('change', function(event, filename) {
|
|
|
|
if (filename === relativePath) {
|
2024-11-06 01:10:33 +00:00
|
|
|
assert.strictEqual(event, 'rename');
|
2024-01-25 08:53:21 +00:00
|
|
|
watcher.close();
|
|
|
|
watcherClosed = true;
|
|
|
|
}
|
|
|
|
});
|
2022-12-20 14:25:48 +00:00
|
|
|
|
2024-02-23 02:00:42 +00:00
|
|
|
// Do the write with a delay to ensure that the OS is ready to notify us.
|
|
|
|
setTimeout(() => {
|
|
|
|
fs.writeFileSync(childrenAbsolutePath, 'world');
|
|
|
|
}, common.platformTimeout(200));
|
2022-12-20 14:25:48 +00:00
|
|
|
|
2024-01-25 08:53:21 +00:00
|
|
|
process.once('exit', function() {
|
|
|
|
assert(watcherClosed, 'watcher Object was not closed');
|
|
|
|
});
|