mirror of
https://github.com/nodejs/node.git
synced 2026-11-26 21:58:26 -05:00
fs: accept URL as argument for fs.rm and fs.rmSync
PR-URL: https://github.com/nodejs/node/pull/41132 Reviewed-By: Michaël Zasso <targos@protonmail.com> Reviewed-By: James M Snell <jasnell@gmail.com> Reviewed-By: Gerhard Stöbich <deb2001-github@yahoo.de> Reviewed-By: Ruben Bridgewater <ruben@bridgewater.de> Reviewed-By: Mohammed Keyvanzadeh <mohammadkeyvanzade94@gmail.com> Reviewed-By: Matteo Collina <matteo.collina@gmail.com>
This commit is contained in:
1 parent
db410e7d3e
commit
bbdcd0513b
3 files changed
+38
No files matched your search
@@ -3580,6 +3580,11 @@ with options `{ recursive: true, force: true }`.
|
||||
|
||||
<!-- YAML
|
||||
added: v14.14.0
|
||||
changes:
|
||||
- version: REPLACEME
|
||||
pr-url: https://github.com/nodejs/node/pull/41132
|
||||
description: The `path` parameter can be a WHATWG `URL` object using `file:`
|
||||
protocol.
|
||||
-->
|
||||
|
||||
* `path` {string|Buffer|URL}
|
||||
@@ -5328,6 +5333,11 @@ with options `{ recursive: true, force: true }`.
|
||||
|
||||
<!-- YAML
|
||||
added: v14.14.0
|
||||
changes:
|
||||
- version: REPLACEME
|
||||
pr-url: https://github.com/nodejs/node/pull/41132
|
||||
description: The `path` parameter can be a WHATWG `URL` object using `file:`
|
||||
protocol.
|
||||
-->
|
||||
|
||||
* `path` {string|Buffer|URL}
|
||||
|
||||
@@ -1185,6 +1185,7 @@ function rm(path, options, callback) {
|
||||
callback = options;
|
||||
options = undefined;
|
||||
}
|
||||
path = getValidatedPath(path);
|
||||
|
||||
validateRmOptions(path, options, false, (err, options) => {
|
||||
if (err) {
|
||||
@@ -1208,6 +1209,7 @@ function rm(path, options, callback) {
|
||||
* @returns {void}
|
||||
*/
|
||||
function rmSync(path, options) {
|
||||
path = getValidatedPath(path);
|
||||
options = validateRmOptionsSync(path, options, false);
|
||||
|
||||
lazyLoadRimraf();
|
||||
|
||||
@@ -5,6 +5,7 @@ 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');
|
||||
@@ -97,6 +98,11 @@ function removeAsync(dir) {
|
||||
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);
|
||||
@@ -156,6 +162,16 @@ function removeAsync(dir) {
|
||||
fs.rmSync(filePath, { force: true });
|
||||
}
|
||||
|
||||
// Should accept URL
|
||||
const fileURL = pathToFileURL(path.join(tmpdir.path, 'rm-file.txt'));
|
||||
fs.writeFileSync(fileURL, '');
|
||||
|
||||
try {
|
||||
fs.rmSync(fileURL, { recursive: true });
|
||||
} finally {
|
||||
fs.rmSync(fileURL, { force: true });
|
||||
}
|
||||
|
||||
// Recursive removal should succeed.
|
||||
fs.rmSync(dir, { recursive: true });
|
||||
|
||||
@@ -202,6 +218,16 @@ function removeAsync(dir) {
|
||||
} finally {
|
||||
fs.rmSync(filePath, { force: true });
|
||||
}
|
||||
|
||||
// Should accept URL
|
||||
const fileURL = pathToFileURL(path.join(tmpdir.path, 'rm-promises-file.txt'));
|
||||
fs.writeFileSync(fileURL, '');
|
||||
|
||||
try {
|
||||
await fs.promises.rm(fileURL, { recursive: true });
|
||||
} finally {
|
||||
fs.rmSync(fileURL, { force: true });
|
||||
}
|
||||
})().then(common.mustCall());
|
||||
|
||||
// Test input validation.
|
||||
|
||||
Reference in new issue
Block a user