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-enoent.js
Daniel Bayley cff7da7749
src,test: further cleanup references to osx
PR-URL: https://github.com/nodejs/node/pull/53820
Reviewed-By: Antoine du Hamel <duhamelantoine1995@gmail.com>
Reviewed-By: Michaël Zasso <targos@protonmail.com>
Reviewed-By: Luigi Pinca <luigipinca@gmail.com>
Reviewed-By: Rafael Gonzaga <rafael.nunu@hotmail.com>
2024-07-15 20:32:26 +00:00

73 lines
2.1 KiB
JavaScript

// Flags: --expose-internals
'use strict';
// This verifies the error thrown by fs.watch.
const common = require('../common');
if (common.isIBMi)
common.skip('IBMi does not support `fs.watch()`');
const assert = require('assert');
const fs = require('fs');
const tmpdir = require('../common/tmpdir');
const nonexistentFile = tmpdir.resolve('non-existent');
const { internalBinding } = require('internal/test/binding');
const {
UV_ENODEV,
UV_ENOENT
} = internalBinding('uv');
tmpdir.refresh();
{
const validateError = (err) => {
assert.strictEqual(err.path, nonexistentFile);
assert.strictEqual(err.filename, nonexistentFile);
assert.ok(err.syscall === 'watch' || err.syscall === 'stat');
if (err.code === 'ENOENT') {
assert.ok(err.message.startsWith('ENOENT: no such file or directory'));
assert.strictEqual(err.errno, UV_ENOENT);
assert.strictEqual(err.code, 'ENOENT');
} else { // AIX
assert.strictEqual(
err.message,
`ENODEV: no such device, watch '${nonexistentFile}'`);
assert.strictEqual(err.errno, UV_ENODEV);
assert.strictEqual(err.code, 'ENODEV');
}
return true;
};
assert.throws(
() => fs.watch(nonexistentFile, common.mustNotCall()),
validateError
);
}
{
if (common.isMacOS || common.isWindows) {
const file = tmpdir.resolve('file-to-watch');
fs.writeFileSync(file, 'test');
const watcher = fs.watch(file, common.mustNotCall());
const validateError = (err) => {
assert.strictEqual(err.path, nonexistentFile);
assert.strictEqual(err.filename, nonexistentFile);
assert.strictEqual(
err.message,
`ENOENT: no such file or directory, watch '${nonexistentFile}'`);
assert.strictEqual(err.errno, UV_ENOENT);
assert.strictEqual(err.code, 'ENOENT');
assert.strictEqual(err.syscall, 'watch');
fs.unlinkSync(file);
return true;
};
watcher.on('error', common.mustCall(validateError));
// Simulate the invocation from the binding
watcher._handle.onchange(UV_ENOENT, 'ENOENT', nonexistentFile);
}
}