From bbdcd0513be1a9c3309cd8c963b8bb48a8caaf3e Mon Sep 17 00:00:00 2001 From: Antoine du Hamel Date: Mon, 13 Dec 2021 15:45:09 +0100 Subject: [PATCH] fs: accept URL as argument for `fs.rm` and `fs.rmSync` MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit PR-URL: https://github.com/nodejs/node/pull/41132 Reviewed-By: Michaël Zasso Reviewed-By: James M Snell Reviewed-By: Gerhard Stöbich Reviewed-By: Ruben Bridgewater Reviewed-By: Mohammed Keyvanzadeh Reviewed-By: Matteo Collina --- doc/api/fs.md | 10 ++++++++++ lib/fs.js | 2 ++ test/parallel/test-fs-rm.js | 26 ++++++++++++++++++++++++++ 3 files changed, 38 insertions(+) diff --git a/doc/api/fs.md b/doc/api/fs.md index fb4a625b863240..c8fa23cf7c4228 100644 --- a/doc/api/fs.md +++ b/doc/api/fs.md @@ -3580,6 +3580,11 @@ with options `{ recursive: true, force: true }`. * `path` {string|Buffer|URL} @@ -5328,6 +5333,11 @@ with options `{ recursive: true, force: true }`. * `path` {string|Buffer|URL} diff --git a/lib/fs.js b/lib/fs.js index abdc2f0c39c11b..9884e85d5845ee 100644 --- a/lib/fs.js +++ b/lib/fs.js @@ -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(); diff --git a/test/parallel/test-fs-rm.js b/test/parallel/test-fs-rm.js index 3fdfc8426248ac..5b30599189dec4 100644 --- a/test/parallel/test-fs-rm.js +++ b/test/parallel/test-fs-rm.js @@ -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.