From 3a9a2601aa9a32b90c0a2389d44c61bbaa9dbafd 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 Backport-PR-URL: #41752 --- doc/api/fs.md | 10 ++++++++++ lib/fs.js | 2 ++ test/parallel/test-fs-rm.js | 27 +++++++++++++++++++++++++++ 3 files changed, 39 insertions(+) diff --git a/doc/api/fs.md b/doc/api/fs.md index f94cb794a14f63..81bacd32995095 100644 --- a/doc/api/fs.md +++ b/doc/api/fs.md @@ -3518,6 +3518,11 @@ with options `{ recursive: true, force: true }`. * `path` {string|Buffer|URL} @@ -5261,6 +5266,11 @@ with options `{ recursive: true, force: true }`. * `path` {string|Buffer|URL} diff --git a/lib/fs.js b/lib/fs.js index 9ec725ffebdf58..c1cc7aecaa49e4 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 5bb5d2de553eee..b1ef509a4e6ebf 100644 --- a/test/parallel/test-fs-rm.js +++ b/test/parallel/test-fs-rm.js @@ -5,6 +5,8 @@ const tmpdir = require('../common/tmpdir'); const assert = require('assert'); const fs = require('fs'); const path = require('path'); +const { pathToFileURL } = require('url'); + const { validateRmOptionsSync } = require('internal/fs/utils'); tmpdir.refresh(); @@ -95,6 +97,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); @@ -154,6 +161,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 }); @@ -200,6 +217,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.