mirror of
https://github.com/nodejs/node.git
synced 2025-05-21 12:25:12 +00:00

- Add an accessor property `initialized `to FSEventWrap to check the state of the handle from the JS land - Introduce ERR_FS_WATCHER_ALREADY_STARTED so calling start() on a watcher that is already started will throw instead of doing nothing silently. - Introduce ERR_FS_WATCHER_NOT_STARTED so calling close() on a watcher that is already closed will throw instead of doing nothing silently. - Validate the filename passed to fs.watch() - Assert that the handle in the watcher are instances of FSEvent instead of relying on the illegal invocation error from the VM. - Add more assertions in FSEventWrap methods now that we check `initialized` and the filename in JS land before invoking the binding. - Use uvException instead of errornoException to create the errors with the error numbers from libuv to make them consistent with other errors in fs. TODO: - Improve fs.watchFile() the same way this patch improves fs.watch() - It seems possible to fire both rename and change event from libuv together now that we can check if the handle is closed via `initialized` in JS land. PR-URL: https://github.com/nodejs/node/pull/19089 Reviewed-By: James M Snell <jasnell@gmail.com> Reviewed-By: Ben Noordhuis <info@bnoordhuis.nl> Reviewed-By: Matteo Collina <matteo.collina@gmail.com> Reviewed-By: Ruben Bridgewater <ruben@bridgewater.de>
99 lines
2.8 KiB
JavaScript
99 lines
2.8 KiB
JavaScript
'use strict';
|
|
const common = require('../common');
|
|
|
|
// tests if `filename` is provided to watcher on supported platforms
|
|
|
|
const fs = require('fs');
|
|
const assert = require('assert');
|
|
const { join } = require('path');
|
|
|
|
class WatchTestCase {
|
|
constructor(shouldInclude, dirName, fileName, field) {
|
|
this.dirName = dirName;
|
|
this.fileName = fileName;
|
|
this.field = field;
|
|
this.shouldSkip = !shouldInclude;
|
|
}
|
|
get dirPath() { return join(tmpdir.path, this.dirName); }
|
|
get filePath() { return join(this.dirPath, this.fileName); }
|
|
}
|
|
|
|
const cases = [
|
|
// Watch on a directory should callback with a filename on supported systems
|
|
new WatchTestCase(
|
|
common.isLinux || common.isOSX || common.isWindows || common.isAIX,
|
|
'watch1',
|
|
'foo',
|
|
'filePath'
|
|
),
|
|
// Watch on a file should callback with a filename on supported systems
|
|
new WatchTestCase(
|
|
common.isLinux || common.isOSX || common.isWindows,
|
|
'watch2',
|
|
'bar',
|
|
'dirPath'
|
|
)
|
|
];
|
|
|
|
const tmpdir = require('../common/tmpdir');
|
|
tmpdir.refresh();
|
|
|
|
for (const testCase of cases) {
|
|
if (testCase.shouldSkip) continue;
|
|
fs.mkdirSync(testCase.dirPath);
|
|
// long content so it's actually flushed.
|
|
const content1 = Date.now() + testCase.fileName.toLowerCase().repeat(1e4);
|
|
fs.writeFileSync(testCase.filePath, content1);
|
|
|
|
let interval;
|
|
const watcher = fs.watch(testCase[testCase.field]);
|
|
watcher.on('error', (err) => {
|
|
if (interval) {
|
|
clearInterval(interval);
|
|
interval = null;
|
|
}
|
|
assert.fail(err);
|
|
});
|
|
watcher.on('change', common.mustCall(function(eventType, argFilename) {
|
|
if (interval) {
|
|
clearInterval(interval);
|
|
interval = null;
|
|
}
|
|
if (common.isOSX)
|
|
assert.strictEqual(['rename', 'change'].includes(eventType), true);
|
|
else
|
|
assert.strictEqual(eventType, 'change');
|
|
assert.strictEqual(argFilename, testCase.fileName);
|
|
|
|
common.expectsError(() => watcher.start(), {
|
|
code: 'ERR_FS_WATCHER_ALREADY_STARTED',
|
|
message: 'The watcher has already been started'
|
|
});
|
|
// end of test case
|
|
watcher.close();
|
|
common.expectsError(() => watcher.close(), {
|
|
code: 'ERR_FS_WATCHER_NOT_STARTED',
|
|
message: 'The watcher has not been started'
|
|
});
|
|
}));
|
|
|
|
// long content so it's actually flushed. toUpperCase so there's real change.
|
|
const content2 = Date.now() + testCase.fileName.toUpperCase().repeat(1e4);
|
|
interval = setInterval(() => {
|
|
fs.writeFileSync(testCase.filePath, '');
|
|
fs.writeFileSync(testCase.filePath, content2);
|
|
}, 100);
|
|
}
|
|
|
|
[false, 1, {}, [], null, undefined].forEach((i) => {
|
|
common.expectsError(
|
|
() => fs.watch(i, common.mustNotCall()),
|
|
{
|
|
code: 'ERR_INVALID_ARG_TYPE',
|
|
type: TypeError,
|
|
message: 'The "filename" argument must be one of ' +
|
|
'type string, Buffer, or URL'
|
|
}
|
|
);
|
|
});
|