mirror of
https://github.com/nodejs/node.git
synced 2024-12-01 16:10:02 +01:00
219932a9f7
covert lib/fs.js over to using lib/internal/errors.js i have not addressed the cases that use errnoException(), for reasons described in GH-12926 - throw the ERR_INVALID_CALLBACK error when the the callback is invalid - replace the ['object', 'string'] with ['string', 'object'] in the error constructor call, to better match the previous err msg in the getOptions() function - add error ERR_VALUE_OUT_OF_RANGE in lib/internal/errors.js, this error is thrown when a numeric value is out of range - document the ERR_VALUE_OUT_OF_RANGE err in errors.md - correct the expected args, in the error thrown in the function fs._toUnixTimestamp() to ['Date', 'time in seconds'] (lib/fs.js) - update the listener error type in the fs.watchFile() function, from Error to TypeError (lib/fs.js) - update errors from ERR_INVALID_OPT_VALUE to ERR_INVALID_ARG_TYPE in the functions fs.ReadStream() and fs.WriteStream(), for the cases of range errors use the new error: ERR_VALUE_OUT_OF_RANGE (lib/fs.js) PR-URL: https://github.com/nodejs/node/pull/15043 Refs: https://github.com/nodejs/node/issues/11273 Reviewed-By: Refael Ackermann <refack@gmail.com> Reviewed-By: James M Snell <jasnell@gmail.com> Reviewed-By: Michael Dawson <michael_dawson@ca.ibm.com>
98 lines
3.2 KiB
JavaScript
98 lines
3.2 KiB
JavaScript
'use strict';
|
|
const common = require('../common');
|
|
|
|
const assert = require('assert');
|
|
const fs = require('fs');
|
|
const path = require('path');
|
|
|
|
// Basic usage tests.
|
|
common.expectsError(
|
|
() => {
|
|
fs.watchFile('./some-file');
|
|
},
|
|
{
|
|
code: 'ERR_INVALID_ARG_TYPE',
|
|
type: TypeError
|
|
});
|
|
|
|
common.expectsError(
|
|
() => {
|
|
fs.watchFile('./another-file', {}, 'bad listener');
|
|
},
|
|
{
|
|
code: 'ERR_INVALID_ARG_TYPE',
|
|
type: TypeError
|
|
});
|
|
|
|
assert.throws(function() {
|
|
fs.watchFile(new Object(), common.mustNotCall());
|
|
}, common.expectsError({ code: 'ERR_INVALID_ARG_TYPE', type: TypeError }));
|
|
|
|
const enoentFile = path.join(common.tmpDir, 'non-existent-file');
|
|
const expectedStatObject = new fs.Stats(
|
|
0, // dev
|
|
0, // mode
|
|
0, // nlink
|
|
0, // uid
|
|
0, // gid
|
|
0, // rdev
|
|
common.isWindows ? undefined : 0, // blksize
|
|
0, // ino
|
|
0, // size
|
|
common.isWindows ? undefined : 0, // blocks
|
|
Date.UTC(1970, 0, 1, 0, 0, 0), // atime
|
|
Date.UTC(1970, 0, 1, 0, 0, 0), // mtime
|
|
Date.UTC(1970, 0, 1, 0, 0, 0), // ctime
|
|
Date.UTC(1970, 0, 1, 0, 0, 0) // birthtime
|
|
);
|
|
|
|
common.refreshTmpDir();
|
|
|
|
// If the file initially didn't exist, and gets created at a later point of
|
|
// time, the callback should be invoked again with proper values in stat object
|
|
let fileExists = false;
|
|
|
|
fs.watchFile(enoentFile, { interval: 0 }, common.mustCall(function(curr, prev) {
|
|
if (!fileExists) {
|
|
// If the file does not exist, all the fields should be zero and the date
|
|
// fields should be UNIX EPOCH time
|
|
assert.deepStrictEqual(curr, expectedStatObject);
|
|
assert.deepStrictEqual(prev, expectedStatObject);
|
|
// Create the file now, so that the callback will be called back once the
|
|
// event loop notices it.
|
|
fs.closeSync(fs.openSync(enoentFile, 'w'));
|
|
fileExists = true;
|
|
} else {
|
|
// If the ino (inode) value is greater than zero, it means that the file is
|
|
// present in the filesystem and it has a valid inode number.
|
|
assert(curr.ino > 0);
|
|
// As the file just got created, previous ino value should be lesser than
|
|
// or equal to zero (non-existent file).
|
|
assert(prev.ino <= 0);
|
|
// Stop watching the file
|
|
fs.unwatchFile(enoentFile);
|
|
}
|
|
}, 2));
|
|
|
|
// Watch events should callback with a filename on supported systems.
|
|
// Omitting AIX. It works but not reliably.
|
|
if (common.isLinux || common.isOSX || common.isWindows) {
|
|
const dir = path.join(common.tmpDir, 'watch');
|
|
|
|
fs.mkdir(dir, common.mustCall(function(err) {
|
|
if (err) assert.fail(err);
|
|
|
|
fs.watch(dir, common.mustCall(function(eventType, filename) {
|
|
clearInterval(interval);
|
|
this._handle.close();
|
|
assert.strictEqual(filename, 'foo.txt');
|
|
}));
|
|
|
|
const interval = setInterval(() => {
|
|
fs.writeFile(path.join(dir, 'foo.txt'), 'foo', common.mustCall((err) => {
|
|
if (err) assert.fail(err);
|
|
}));
|
|
}, 1);
|
|
}));
|
|
}
|