Compare commits

...

2 Commits

Author SHA1 Message Date
Don Isaac
51fc28a8c1 test(node/fs): add test-fs-rm.js 2025-01-27 14:00:36 -08:00
Don Isaac
81cfe2ca2a fix(node/fs): better validation in fs.Dir 2025-01-27 14:00:36 -08:00
4 changed files with 721 additions and 39 deletions

View File

@@ -89,9 +89,22 @@ export default {
validateObject: validateObject,
validateLinkHeaderValue: validateLinkHeaderValue,
checkIsHttpToken: checkIsHttpToken,
/** `(value, name, min = NumberMIN_SAFE_INTEGER, max = NumberMAX_SAFE_INTEGER)` */
/**
* @param value the value that should be an int
* @paran name the name of the parameter. Used when creating error codes
* @param min minimum value, inclusive. Defaults to {@link Number.MIN_SAFE_INTEGER}.
* @param max maximum value, inclusive. Defaults to {@link Number.MAX_SAFE_INTEGER}.
*
* @throws if `value` is not an int
* @throws if `value` is outside `[min, max]`
*/
validateInteger: $newCppFunction("NodeValidator.cpp", "jsFunction_validateInteger", 0),
/** `(value, name, min = undefined, max)` */
/**
* @param value the value that should be an int
* @paran name the name of the parameter. Used when creating error codes
* @param min minimum value, exclusive. Defaults to {@link Number.MIN_SAFE_INTEGER}.
* @param max maximum value, exclusive. Defaults to {@link Number.MAX_SAFE_INTEGER}.
*/
validateNumber: $newCppFunction("NodeValidator.cpp", "jsFunction_validateNumber", 0),
/** `(value, name)` */
validateString: $newCppFunction("NodeValidator.cpp", "jsFunction_validateString", 0),

View File

@@ -1,8 +1,9 @@
// Hardcoded module "node:fs"
import type { Stats as StatsType } from "fs";
import type { Stats as StatsType, Dirent as DirentType, PathLike } from "fs";
const EventEmitter = require("node:events");
const promises = require("node:fs/promises");
const types = require("node:util/types");
const { validateString, validateFunction, validateInteger } = require("internal/validators");
const isDate = types.isDate;
@@ -599,12 +600,15 @@ var access = function access(path, mode, callback) {
return new FSWatcher(path, options, listener);
},
opendir = function opendir(path, options, callback) {
// TODO: validatePath
// validateString(path, "path");
if (typeof options === "function") {
callback = options;
options = undefined;
}
validateFunction(callback, "callback");
const result = new Dir(1, path, options);
if (callback) callback(null, result);
callback(null, result);
};
const { defineCustomPromisifyArgs } = require("internal/promisify");
@@ -700,7 +704,7 @@ function encodeRealpathResult(result, encoding) {
}
let assertEncodingForWindows: any = undefined;
const realpathSync: any =
const realpathSync =
process.platform !== "win32"
? fs.realpathSync.bind(fs)
: function realpathSync(p, options) {
@@ -1009,6 +1013,8 @@ function _toUnixTimestamp(time: any, name = "time") {
}
function opendirSync(path, options) {
// TODO: validatePath
// validateString(path, "path");
return new Dir(1, path, options);
}
@@ -1018,13 +1024,14 @@ class Dir {
* {@link close} or {@link closeSync}.
*/
#handle: number;
#path;
#path: PathLike;
#options;
#entries: any[] | null = null;
#entries: DirentType[] | null = null;
constructor(handle, path, options) {
constructor(handle, path: PathLike, options) {
if ($isUndefinedOrNull(handle)) throw $ERR_MISSING_ARGS("handle");
this.#handle = handle;
validateInteger(handle, "handle", 0);
this.#handle = $toLength(handle);
this.#path = path;
this.#options = options;
}
@@ -1040,10 +1047,11 @@ class Dir {
return entries.shift() ?? null;
}
read(cb?): any {
read(cb?: (err: Error | null, entry: DirentType) => void): any {
if (this.#handle < 0) throw $ERR_DIR_CLOSED();
if (cb) {
if (!$isUndefinedOrNull(cb)) {
validateFunction(cb, "callback");
return this.read().then(entry => cb(null, entry));
}
@@ -1063,7 +1071,9 @@ class Dir {
close(cb?: () => void) {
const handle = this.#handle;
if (cb) {
if (handle < 0) throw $ERR_DIR_CLOSED();
if (!$isUndefinedOrNull(cb)) {
validateFunction(cb, "callback");
process.nextTick(cb);
}
if (handle > 2) fs.closeSync(handle);
@@ -1072,6 +1082,7 @@ class Dir {
closeSync() {
const handle = this.#handle;
if (handle < 0) throw $ERR_DIR_CLOSED();
if (handle > 2) fs.closeSync(handle);
this.#handle = -1;
}

View File

@@ -1,34 +1,119 @@
import { describe, beforeAll, afterAll, it, expect } from "bun:test";
import { describe, beforeAll, afterAll, beforeEach, afterEach, it, expect } from "bun:test";
import fs from "node:fs";
import os from "node:os";
import path from "node:path";
describe("given a directory that exists", () => {
let dirname: string;
function noop() {}
describe("fs.opendir", () => {
// TODO: validatePath
// it.each([1, 0, null, undefined, function foo() {}, Symbol.for("foo")])(
// "throws if the path is not a string: %p",
// (path: any) => {
// expect(() => fs.opendir(path, noop)).toThrow(/The "path" argument must be of type string/);
// },
// );
beforeAll(() => {
const name = "dir-sync.test." + String(Math.random() * 100).substring(0, 6);
dirname = path.join(os.tmpdir(), name);
fs.mkdirSync(dirname);
});
afterAll(() => {
fs.rmdirSync(dirname, { recursive: true });
});
it("can be opened/closed synchronously", () => {
const dir = fs.opendirSync(dirname);
expect(dir).toBeDefined();
expect(dir).toBeInstanceOf(fs.Dir);
expect(dir.closeSync()).toBeUndefined();
expect(() => dir.readSync()).toThrow("Directory handle was closed");
});
it("can be opened/closed asynchronously", async () => {
const dir = await fs.promises.opendir(dirname);
expect(dir).toBeDefined();
expect(dir).toBeInstanceOf(fs.Dir);
expect(await dir.close()).toBeUndefined();
expect(() => dir.read()).toThrow("Directory handle was closed");
it("throws if callback is not provided", () => {
expect(() => fs.opendir("foo")).toThrow(/The "callback" argument must be of type function/);
});
});
describe("fs.Dir", () => {
describe("given an empty temp directory", () => {
let dirname: string;
beforeAll(() => {
const name = "dir-sync.test." + String(Math.random() * 100).substring(0, 6);
dirname = path.join(os.tmpdir(), name);
fs.mkdirSync(dirname);
});
afterAll(() => {
fs.rmdirSync(dirname, { recursive: true });
});
describe("when an empty directory is opened", () => {
let dir: fs.Dir;
beforeEach(() => {
dir = fs.opendirSync(dirname);
});
afterEach(() => {
try {
dir.closeSync();
} catch {
/* suppress */
}
});
it("returns a Dir instance", () => {
expect(dir).toBeDefined();
expect(dir).toBeInstanceOf(fs.Dir);
});
describe("reading from the directory", () => {
it.each([0, 1, false, "foo", {}])("throws if passed a non-function callback (%p)", badCb => {
expect(() => dir.read(badCb)).toThrow(/The "callback" argument must be of type function/);
});
it("it can be read synchronously, even though no entries exist", () => {
for (let i = 0; i < 5; i++) {
const actual = dir.readSync();
expect(actual).toBeNull();
}
});
it("can be read asynchronously, even though no entries exist", async () => {
const actual = await dir.read();
expect(actual).toBeNull();
});
it("can be read asynchronously with callbacks, even though no entries exist", async () => {
const actual = await new Promise((resolve, reject) => {
dir.read((err, ent) => {
if (err) reject(err);
else resolve(ent);
});
});
expect(actual).toBeNull();
});
}); // </reading from the directory>
it("can be closed asynchronously", async () => {
const actual = await dir.close();
expect(actual).toBeUndefined();
});
it("can be closed asynchronously with callbacks", async () => {
const actual = await new Promise<void>((resolve, reject) => {
dir.close(err => {
if (err) reject(err);
else resolve();
});
});
expect(actual).toBeUndefined();
});
it("can be closed synchronously", () => {
expect(dir.closeSync()).toBeUndefined();
});
describe("when closed", () => {
beforeEach(async () => {
await dir.close();
});
it('attempts to close again will throw "Directory handle was closed"', () => {
expect(() => dir.closeSync()).toThrow("Directory handle was closed");
expect(() => dir.close()).toThrow("Directory handle was closed");
});
it("attempts to read will throw", () => {
expect(() => dir.readSync()).toThrow("Directory handle was closed");
expect(() => dir.read()).toThrow("Directory handle was closed");
});
}); // </when closed>
}); // </when an empty directory is opened>
}); // </given an empty temp directory>
}); // </fs.Dir>

View File

@@ -0,0 +1,573 @@
// Flags: --expose-internals
'use strict';
const common = require('../common');
const tmpdir = require('../common/tmpdir');
const assert = require('assert');
const fs = require('fs');
const path = require('path');
const { pathToFileURL } = require('url');
const { execSync } = require('child_process');
// const { validateRmOptionsSync } = require('internal/fs/utils');
// Bun does not have a validateRmdirOptions function
// Instead, we can just remove a temp file.
function validateRmOptionsSync(path, options) {
fs.writeFileSync(path, '');
fs.rmSync(path, options);
}
tmpdir.refresh();
let count = 0;
const nextDirPath = (name = 'rm') =>
tmpdir.resolve(`${name}-${count++}`);
const isGitPresent = (() => {
try { execSync('git --version'); return true; } catch { return false; }
})();
function gitInit(gitDirectory) {
fs.mkdirSync(gitDirectory);
execSync('git init', common.mustNotMutateObjectDeep({ cwd: gitDirectory }));
}
function makeNonEmptyDirectory(depth, files, folders, dirname, createSymLinks) {
fs.mkdirSync(dirname, common.mustNotMutateObjectDeep({ recursive: true }));
fs.writeFileSync(path.join(dirname, 'text.txt'), 'hello', 'utf8');
const options = common.mustNotMutateObjectDeep({ flag: 'wx' });
for (let f = files; f > 0; f--) {
fs.writeFileSync(path.join(dirname, `f-${depth}-${f}`), '', options);
}
if (createSymLinks) {
// Valid symlink
fs.symlinkSync(
`f-${depth}-1`,
path.join(dirname, `link-${depth}-good`),
'file'
);
// Invalid symlink
fs.symlinkSync(
'does-not-exist',
path.join(dirname, `link-${depth}-bad`),
'file'
);
// Symlinks that form a loop
[['a', 'b'], ['b', 'a']].forEach(([x, y]) => {
fs.symlinkSync(
`link-${depth}-loop-${x}`,
path.join(dirname, `link-${depth}-loop-${y}`),
'file'
);
});
}
// File with a name that looks like a glob
fs.writeFileSync(path.join(dirname, '[a-z0-9].txt'), '', options);
depth--;
if (depth <= 0) {
return;
}
for (let f = folders; f > 0; f--) {
fs.mkdirSync(
path.join(dirname, `folder-${depth}-${f}`),
{ recursive: true }
);
makeNonEmptyDirectory(
depth,
files,
folders,
path.join(dirname, `d-${depth}-${f}`),
createSymLinks
);
}
}
function removeAsync(dir) {
// Removal should fail without the recursive option.
fs.rm(dir, common.mustCall((err) => {
assert.strictEqual(err.syscall, 'rm');
// Removal should fail without the recursive option set to true.
fs.rm(dir, common.mustNotMutateObjectDeep({ recursive: false }), common.mustCall((err) => {
assert.strictEqual(err.syscall, 'rm');
// Recursive removal should succeed.
fs.rm(dir, common.mustNotMutateObjectDeep({ recursive: true }), common.mustSucceed(() => {
// Attempted removal should fail now because the directory is gone.
fs.rm(dir, common.mustCall((err) => {
// DIFFERENCE: we don't lstat directories before removing them, so the
// failure will occur at the rm syscall.
// assert.strictEqual(err.syscall, 'lstat');
assert.strictEqual(err.syscall, 'rm');
}));
}));
}));
}));
}
// Test the asynchronous version
{
// Create a 4-level folder hierarchy including symlinks
let dir = nextDirPath();
makeNonEmptyDirectory(4, 10, 2, dir, true);
removeAsync(dir);
// Create a 2-level folder hierarchy without symlinks
dir = nextDirPath();
makeNonEmptyDirectory(2, 10, 2, dir, false);
removeAsync(dir);
// Same test using URL instead of a path
dir = nextDirPath();
makeNonEmptyDirectory(2, 10, 2, dir, false);
removeAsync(pathToFileURL(dir));
// Create a flat folder including symlinks
dir = nextDirPath();
makeNonEmptyDirectory(1, 10, 2, dir, true);
removeAsync(dir);
// Should fail if target does not exist
fs.rm(
tmpdir.resolve('noexist.txt'),
common.mustNotMutateObjectDeep({ recursive: true }),
common.mustCall((err) => {
assert.strictEqual(err.code, 'ENOENT');
})
);
// Should delete a file
const filePath = tmpdir.resolve('rm-async-file.txt');
fs.writeFileSync(filePath, '');
fs.rm(filePath, common.mustNotMutateObjectDeep({ recursive: true }), common.mustCall((err) => {
try {
assert.strictEqual(err, null);
assert.strictEqual(fs.existsSync(filePath), false);
} finally {
fs.rmSync(filePath, common.mustNotMutateObjectDeep({ force: true }));
}
}));
// Should delete a valid symlink
const linkTarget = tmpdir.resolve('link-target-async.txt');
fs.writeFileSync(linkTarget, '');
const validLink = tmpdir.resolve('valid-link-async');
fs.symlinkSync(linkTarget, validLink);
fs.rm(validLink, common.mustNotMutateObjectDeep({ recursive: true }), common.mustCall((err) => {
try {
assert.strictEqual(err, null);
assert.strictEqual(fs.existsSync(validLink), false);
} finally {
fs.rmSync(linkTarget, common.mustNotMutateObjectDeep({ force: true }));
fs.rmSync(validLink, common.mustNotMutateObjectDeep({ force: true }));
}
}));
// Should delete an invalid symlink
const invalidLink = tmpdir.resolve('invalid-link-async');
fs.symlinkSync('definitely-does-not-exist-async', invalidLink);
fs.rm(invalidLink, common.mustNotMutateObjectDeep({ recursive: true }), common.mustCall((err) => {
try {
assert.strictEqual(err, null);
assert.strictEqual(fs.existsSync(invalidLink), false);
} finally {
fs.rmSync(invalidLink, common.mustNotMutateObjectDeep({ force: true }));
}
}));
// Should delete a symlink that is part of a loop
const loopLinkA = tmpdir.resolve('loop-link-async-a');
const loopLinkB = tmpdir.resolve('loop-link-async-b');
fs.symlinkSync(loopLinkA, loopLinkB);
fs.symlinkSync(loopLinkB, loopLinkA);
fs.rm(loopLinkA, common.mustNotMutateObjectDeep({ recursive: true }), common.mustCall((err) => {
try {
assert.strictEqual(err, null);
assert.strictEqual(fs.existsSync(loopLinkA), false);
} finally {
fs.rmSync(loopLinkA, common.mustNotMutateObjectDeep({ force: true }));
fs.rmSync(loopLinkB, common.mustNotMutateObjectDeep({ force: true }));
}
}));
}
// Removing a .git directory should not throw an EPERM.
// Refs: https://github.com/isaacs/rimraf/issues/21.
if (isGitPresent) {
const gitDirectory = nextDirPath();
gitInit(gitDirectory);
fs.rm(gitDirectory, common.mustNotMutateObjectDeep({ recursive: true }), common.mustSucceed(() => {
assert.strictEqual(fs.existsSync(gitDirectory), false);
}));
}
// Test the synchronous version.
{
const dir = nextDirPath();
makeNonEmptyDirectory(4, 10, 2, dir, true);
// Removal should fail without the recursive option set to true.
assert.throws(() => {
fs.rmSync(dir);
}, { syscall: 'rm' });
assert.throws(() => {
fs.rmSync(dir, common.mustNotMutateObjectDeep({ recursive: false }));
}, { syscall: 'rm' });
// Should fail if target does not exist
assert.throws(() => {
fs.rmSync(tmpdir.resolve('noexist.txt'), common.mustNotMutateObjectDeep({ recursive: true }));
}, {
code: 'ENOENT',
name: 'Error',
// DIFFERENCE: we don't lstat entries before removing them.
// message: /^ENOENT: no such file or directory, lstat/
message: /^ENOENT: no such file or directory, /
});
// Should delete a file
const filePath = tmpdir.resolve('rm-file.txt');
fs.writeFileSync(filePath, '');
try {
fs.rmSync(filePath, common.mustNotMutateObjectDeep({ recursive: true }));
assert.strictEqual(fs.existsSync(filePath), false);
} finally {
fs.rmSync(filePath, common.mustNotMutateObjectDeep({ force: true }));
}
// Should delete a valid symlink
const linkTarget = tmpdir.resolve('link-target.txt');
fs.writeFileSync(linkTarget, '');
const validLink = tmpdir.resolve('valid-link');
fs.symlinkSync(linkTarget, validLink);
try {
fs.rmSync(validLink);
assert.strictEqual(fs.existsSync(validLink), false);
} finally {
fs.rmSync(linkTarget, common.mustNotMutateObjectDeep({ force: true }));
fs.rmSync(validLink, common.mustNotMutateObjectDeep({ force: true }));
}
// Should delete an invalid symlink
const invalidLink = tmpdir.resolve('invalid-link');
fs.symlinkSync('definitely-does-not-exist', invalidLink);
try {
fs.rmSync(invalidLink);
assert.strictEqual(fs.existsSync(invalidLink), false);
} finally {
fs.rmSync(invalidLink, common.mustNotMutateObjectDeep({ force: true }));
}
// Should delete a symlink that is part of a loop
const loopLinkA = tmpdir.resolve('loop-link-a');
const loopLinkB = tmpdir.resolve('loop-link-b');
fs.symlinkSync(loopLinkA, loopLinkB);
fs.symlinkSync(loopLinkB, loopLinkA);
try {
fs.rmSync(loopLinkA);
assert.strictEqual(fs.existsSync(loopLinkA), false);
} finally {
fs.rmSync(loopLinkA, common.mustNotMutateObjectDeep({ force: true }));
fs.rmSync(loopLinkB, common.mustNotMutateObjectDeep({ force: true }));
}
// Should accept URL
const fileURL = tmpdir.fileURL('rm-file.txt');
fs.writeFileSync(fileURL, '');
try {
fs.rmSync(fileURL, common.mustNotMutateObjectDeep({ recursive: true }));
assert.strictEqual(fs.existsSync(fileURL), false);
} finally {
fs.rmSync(fileURL, common.mustNotMutateObjectDeep({ force: true }));
}
// Recursive removal should succeed.
fs.rmSync(dir, { recursive: true });
assert.strictEqual(fs.existsSync(dir), false);
// Attempted removal should fail now because the directory is gone.
// DIFFERENCE: we don't lstat entries before removing them.
// assert.throws(() => fs.rmSync(dir), { syscall: 'lstat' });
assert.throws(() => fs.rmSync(dir), { syscall: 'rm' });
}
// Removing a .git directory should not throw an EPERM.
// Refs: https://github.com/isaacs/rimraf/issues/21.
if (isGitPresent) {
const gitDirectory = nextDirPath();
gitInit(gitDirectory);
fs.rmSync(gitDirectory, common.mustNotMutateObjectDeep({ recursive: true }));
assert.strictEqual(fs.existsSync(gitDirectory), false);
}
// Test the Promises based version.
(async () => {
const dir = nextDirPath();
makeNonEmptyDirectory(4, 10, 2, dir, true);
// Removal should fail without the recursive option set to true.
await assert.rejects(fs.promises.rm(dir), { syscall: 'rm' });
await assert.rejects(fs.promises.rm(dir, common.mustNotMutateObjectDeep({ recursive: false })), {
syscall: 'rm'
});
// Recursive removal should succeed.
await fs.promises.rm(dir, common.mustNotMutateObjectDeep({ recursive: true }));
assert.strictEqual(fs.existsSync(dir), false);
// Attempted removal should fail now because the directory is gone.
// DIFFERENCE: we don't lstat entries before removing them.
// await assert.rejects(fs.promises.rm(dir), { syscall: 'lstat' });
await assert.rejects(fs.promises.rm(dir), { syscall: 'rm' });
// Should fail if target does not exist
await assert.rejects(fs.promises.rm(
tmpdir.resolve('noexist.txt'),
{ recursive: true }
), {
code: 'ENOENT',
name: 'Error',
// DIFFERENCE: we don't lstat entries before removing them.
// message: /^ENOENT: no such file or directory, lstat/
message: /^ENOENT: no such file or directory/
});
// Should not fail if target does not exist and force option is true
await fs.promises.rm(tmpdir.resolve('noexist.txt'), common.mustNotMutateObjectDeep({ force: true }));
// Should delete file
const filePath = tmpdir.resolve('rm-promises-file.txt');
fs.writeFileSync(filePath, '');
try {
await fs.promises.rm(filePath, common.mustNotMutateObjectDeep({ recursive: true }));
assert.strictEqual(fs.existsSync(filePath), false);
} finally {
fs.rmSync(filePath, common.mustNotMutateObjectDeep({ force: true }));
}
// Should delete a valid symlink
const linkTarget = tmpdir.resolve('link-target-prom.txt');
fs.writeFileSync(linkTarget, '');
const validLink = tmpdir.resolve('valid-link-prom');
fs.symlinkSync(linkTarget, validLink);
try {
await fs.promises.rm(validLink);
assert.strictEqual(fs.existsSync(validLink), false);
} finally {
fs.rmSync(linkTarget, common.mustNotMutateObjectDeep({ force: true }));
fs.rmSync(validLink, common.mustNotMutateObjectDeep({ force: true }));
}
// Should delete an invalid symlink
const invalidLink = tmpdir.resolve('invalid-link-prom');
fs.symlinkSync('definitely-does-not-exist-prom', invalidLink);
try {
await fs.promises.rm(invalidLink);
assert.strictEqual(fs.existsSync(invalidLink), false);
} finally {
fs.rmSync(invalidLink, common.mustNotMutateObjectDeep({ force: true }));
}
// Should delete a symlink that is part of a loop
const loopLinkA = tmpdir.resolve('loop-link-prom-a');
const loopLinkB = tmpdir.resolve('loop-link-prom-b');
fs.symlinkSync(loopLinkA, loopLinkB);
fs.symlinkSync(loopLinkB, loopLinkA);
try {
await fs.promises.rm(loopLinkA);
assert.strictEqual(fs.existsSync(loopLinkA), false);
} finally {
fs.rmSync(loopLinkA, common.mustNotMutateObjectDeep({ force: true }));
fs.rmSync(loopLinkB, common.mustNotMutateObjectDeep({ force: true }));
}
// Should accept URL
const fileURL = tmpdir.fileURL('rm-promises-file.txt');
fs.writeFileSync(fileURL, '');
try {
await fs.promises.rm(fileURL, common.mustNotMutateObjectDeep({ recursive: true }));
assert.strictEqual(fs.existsSync(fileURL), false);
} finally {
fs.rmSync(fileURL, common.mustNotMutateObjectDeep({ force: true }));
}
})().then(common.mustCall());
// Removing a .git directory should not throw an EPERM.
// Refs: https://github.com/isaacs/rimraf/issues/21.
if (isGitPresent) {
(async () => {
const gitDirectory = nextDirPath();
gitInit(gitDirectory);
await fs.promises.rm(gitDirectory, common.mustNotMutateObjectDeep({ recursive: true }));
assert.strictEqual(fs.existsSync(gitDirectory), false);
})().then(common.mustCall());
}
// Test input validation.
{
const dir = nextDirPath();
makeNonEmptyDirectory(4, 10, 2, dir, true);
const filePath = (tmpdir.resolve('rm-args-file.txt'));
fs.writeFileSync(filePath, '');
// const defaults = {
// retryDelay: 100,
// maxRetries: 0,
// recursive: false,
// force: false
// };
// const modified = {
// retryDelay: 953,
// maxRetries: 5,
// recursive: true,
// force: false
// };
// assert.deepStrictEqual(validateRmOptionsSync(filePath), defaults);
// assert.deepStrictEqual(validateRmOptionsSync(filePath, {}), defaults);
// assert.deepStrictEqual(validateRmOptionsSync(filePath, modified), modified);
// assert.deepStrictEqual(validateRmOptionsSync(filePath, {
// maxRetries: 99
// }), {
// retryDelay: 100,
// maxRetries: 99,
// recursive: false,
// force: false
// });
[null, 'foo', 5, NaN].forEach((bad) => {
assert.throws(() => {
validateRmOptionsSync(filePath, bad);
}, {
code: 'ERR_INVALID_ARG_TYPE',
name: 'TypeError',
message: /^The "options" argument must be of type object\./
});
});
[/* undefined, */ null, 'foo', Infinity, function() {}].forEach((bad) => {
assert.throws(() => {
validateRmOptionsSync(filePath, { recursive: bad });
}, {
code: 'ERR_INVALID_ARG_TYPE',
name: 'TypeError',
message: /^The "options\.recursive" property must be of type boolean\./
});
});
[/* undefined , */ null, 'foo', Infinity, function() {}].forEach((bad) => {
assert.throws(() => {
validateRmOptionsSync(filePath, { force: bad });
}, {
code: 'ERR_INVALID_ARG_TYPE',
name: 'TypeError',
message: /^The "options\.force" property must be of type boolean\./
});
});
assert.throws(() => {
validateRmOptionsSync(filePath, { retryDelay: -1 });
}, {
code: 'ERR_OUT_OF_RANGE',
name: 'RangeError',
message: /^The value of "options\.retryDelay" is out of range\./
});
assert.throws(() => {
validateRmOptionsSync(filePath, { maxRetries: -1 });
}, {
code: 'ERR_OUT_OF_RANGE',
name: 'RangeError',
message: /^The value of "options\.maxRetries" is out of range\./
});
}
{
// IBMi has a different access permission mechanism
// This test should not be run as `root`
if (!common.isIBMi && (common.isWindows || process.getuid() !== 0)) {
function makeDirectoryReadOnly(dir, mode) {
let accessErrorCode = 'EACCES';
if (common.isWindows) {
accessErrorCode = 'EPERM';
execSync(`icacls ${dir} /deny "everyone:(OI)(CI)(DE,DC)"`);
} else {
fs.chmodSync(dir, mode);
}
return accessErrorCode;
}
function makeDirectoryWritable(dir) {
if (fs.existsSync(dir)) {
if (common.isWindows) {
execSync(`icacls ${dir} /remove:d "everyone"`);
} else {
fs.chmodSync(dir, 0o777);
}
}
}
{
// Check that deleting a file that cannot be accessed using rmsync throws
// https://github.com/nodejs/node/issues/38683
const dirname = nextDirPath();
const filePath = path.join(dirname, 'text.txt');
try {
fs.mkdirSync(dirname, common.mustNotMutateObjectDeep({ recursive: true }));
fs.writeFileSync(filePath, 'hello');
const code = makeDirectoryReadOnly(dirname, 0o444);
assert.throws(() => {
fs.rmSync(filePath, common.mustNotMutateObjectDeep({ force: true }));
}, {
code,
name: 'Error',
});
} finally {
makeDirectoryWritable(dirname);
}
}
{
// Check endless recursion.
// https://github.com/nodejs/node/issues/34580
const dirname = nextDirPath();
fs.mkdirSync(dirname, common.mustNotMutateObjectDeep({ recursive: true }));
const root = fs.mkdtempSync(path.join(dirname, 'fs-'));
const middle = path.join(root, 'middle');
fs.mkdirSync(middle);
fs.mkdirSync(path.join(middle, 'leaf')); // Make `middle` non-empty
try {
const code = makeDirectoryReadOnly(middle, 0o555);
try {
assert.throws(() => {
fs.rmSync(root, common.mustNotMutateObjectDeep({ recursive: true }));
}, {
code,
name: 'Error',
});
} catch (err) {
// Only fail the test if the folder was not deleted.
// as in some cases rmSync successfully deletes read-only folders.
if (fs.existsSync(root)) {
throw err;
}
}
} finally {
makeDirectoryWritable(middle);
}
}
}
}