From 34c17417927b1b056fb8c3451ce4df5e3f35c8c9 Mon Sep 17 00:00:00 2001 From: Livia Medeiros Date: Wed, 6 Sep 2023 06:07:21 +0900 Subject: [PATCH] test: add test for urlstrings usage in `node:fs` PR-URL: https://github.com/nodejs/node/pull/49471 Reviewed-By: Antoine du Hamel Reviewed-By: Luigi Pinca --- test/parallel/test-fs-whatwg-url.js | 24 ++++++++++++++++++++++++ 1 file changed, 24 insertions(+) diff --git a/test/parallel/test-fs-whatwg-url.js b/test/parallel/test-fs-whatwg-url.js index fcd3b7e98a5dd4..7401ed7e76ecd1 100644 --- a/test/parallel/test-fs-whatwg-url.js +++ b/test/parallel/test-fs-whatwg-url.js @@ -4,6 +4,8 @@ const common = require('../common'); const fixtures = require('../common/fixtures'); const assert = require('assert'); const fs = require('fs'); +const tmpdir = require('../common/tmpdir'); +tmpdir.refresh(); const url = fixtures.fileURL('a.js'); @@ -80,3 +82,25 @@ if (common.isWindows) { } ); } + +// Test that strings are interpreted as paths and not as URL +// Can't use process.chdir in Workers +// Please avoid testing fs.rmdir('file:') or using it as cleanup +if (common.isMainThread && !common.isWindows) { + const oldCwd = process.cwd(); + process.chdir(tmpdir.path); + + for (let slashCount = 0; slashCount < 9; slashCount++) { + const slashes = '/'.repeat(slashCount); + + const dirname = `file:${slashes}thisDirectoryWasMadeByFailingNodeJSTestSorry/subdir`; + fs.mkdirSync(dirname, { recursive: true }); + fs.writeFileSync(`${dirname}/file`, `test failed with ${slashCount} slashes`); + + const expected = fs.readFileSync(tmpdir.resolve(dirname, 'file')); + const actual = fs.readFileSync(`${dirname}/file`); + assert.deepStrictEqual(actual, expected); + } + + process.chdir(oldCwd); +}