0
0
mirror of https://github.com/nodejs/node.git synced 2024-12-01 16:10:02 +01:00
nodejs/test/parallel/test-fs-watch.js
Alec Larson 5cc948b77a fs: add 'close' event to FSWatcher
PR-URL: https://github.com/nodejs/node/pull/19900
Reviewed-By: Colin Ihrig <cjihrig@gmail.com>
Reviewed-By: Richard Lau <riclau@uk.ibm.com>
Reviewed-By: Matteo Collina <matteo.collina@gmail.com>
Reviewed-By: Joyee Cheung <joyeec9h3@gmail.com>
Reviewed-By: Luigi Pinca <luigipinca@gmail.com>
2018-04-16 14:23:11 +02:00

94 lines
2.7 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('close', common.mustCall());
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);
watcher.start(); // Starting a started watcher should be a noop
// End of test case
watcher.close();
watcher.close(); // Closing a closed watcher should be a noop
}));
// 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((input) => {
common.expectsError(
() => fs.watch(input, common.mustNotCall()),
{
code: 'ERR_INVALID_ARG_TYPE',
type: TypeError,
message: 'The "filename" argument must be one of type string, Buffer, ' +
`or URL. Received type ${typeof input}`
}
);
});