Skip to content

Commit b3c0f16

Browse files
committed
lib, path: allow file:// urls in path
1 parent 9dc4b22 commit b3c0f16

File tree

6 files changed

+23
-2
lines changed

6 files changed

+23
-2
lines changed

doc/api/path.md

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -104,8 +104,8 @@ path.win32.basename('C:\\foo.HTML', '.html');
104104
// Returns: 'foo.HTML'
105105
```
106106

107-
A [`TypeError`][] is thrown if `path` is not a string/URL or if `suffix` is given
108-
and is not a string.
107+
A [`TypeError`][] is thrown if `path` is not a string/URL or if `suffix` is
108+
given and is not a string.
109109

110110
## `path.delimiter`
111111

test/parallel/test-path-basename.js

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22
require('../common');
33
const assert = require('assert');
44
const path = require('path');
5+
const url = require('url');
56

67
assert.strictEqual(path.basename(__filename), 'test-path-basename.js');
78
assert.strictEqual(path.basename(__filename, '.js'), 'test-path-basename');
@@ -35,6 +36,10 @@ assert.strictEqual(path.basename('/a/b'), 'b');
3536
assert.strictEqual(path.basename('//a'), 'a');
3637
assert.strictEqual(path.basename('a', 'a'), '');
3738

39+
// URL inputs
40+
assert.strictEqual(path.basename(url.pathToFileURL(__filename)), 'test-path-basename.js');
41+
assert.strictEqual(path.basename(url.pathToFileURL(__filename), '.js'), 'test-path-basename');
42+
3843
// On Windows a backslash acts as a path separator.
3944
assert.strictEqual(path.win32.basename('\\dir\\basename.ext'), 'basename.ext');
4045
assert.strictEqual(path.win32.basename('\\basename.ext'), 'basename.ext');

test/parallel/test-path-dirname.js

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22
const common = require('../common');
33
const assert = require('assert');
44
const path = require('path');
5+
const url = require('url');
56

67
assert.strictEqual(path.dirname(__filename).substr(-13),
78
common.isWindows ? 'test\\parallel' : 'test/parallel');
@@ -15,6 +16,9 @@ assert.strictEqual(path.posix.dirname('////'), '/');
1516
assert.strictEqual(path.posix.dirname('//a'), '//');
1617
assert.strictEqual(path.posix.dirname('foo'), '.');
1718

19+
assert.strictEqual(path.posix.dirname(url.pathToFileURL('/a/b/')), '/a');
20+
assert.strictEqual(path.posix.dirname(url.pathToFileURL('/a/b')), '/a');
21+
1822
assert.strictEqual(path.win32.dirname('c:\\'), 'c:\\');
1923
assert.strictEqual(path.win32.dirname('c:\\foo'), 'c:\\');
2024
assert.strictEqual(path.win32.dirname('c:\\foo\\'), 'c:\\');
@@ -57,3 +61,6 @@ assert.strictEqual(path.win32.dirname(''), '.');
5761
assert.strictEqual(path.win32.dirname('/'), '/');
5862
assert.strictEqual(path.win32.dirname('////'), '/');
5963
assert.strictEqual(path.win32.dirname('foo'), '.');
64+
65+
assert.strictEqual(path.win32.dirname(url.pathToFileURL('c:\\')), 'c:\\');
66+
assert.strictEqual(path.win32.dirname(url.pathToFileURL('c:\\foo')), 'c:\\');

test/parallel/test-path-extname.js

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22
require('../common');
33
const assert = require('assert');
44
const path = require('path');
5+
const url = require('url');
56

67
const failures = [];
78
const slashRE = /\//g;
@@ -50,6 +51,7 @@ const testPaths = [
5051
['file//', ''],
5152
['file./', '.'],
5253
['file.//', '.'],
54+
[url.pathToFileURL('/file.ext'), '.ext'],
5355
];
5456

5557
for (const testPath of testPaths) {

test/parallel/test-path-isabsolute.js

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22
require('../common');
33
const assert = require('assert');
44
const path = require('path');
5+
const url = require('url');
56

67
assert.strictEqual(path.win32.isAbsolute('/'), true);
78
assert.strictEqual(path.win32.isAbsolute('//'), true);
@@ -26,3 +27,6 @@ assert.strictEqual(path.posix.isAbsolute('/home/foo'), true);
2627
assert.strictEqual(path.posix.isAbsolute('/home/foo/..'), true);
2728
assert.strictEqual(path.posix.isAbsolute('bar/'), false);
2829
assert.strictEqual(path.posix.isAbsolute('./baz'), false);
30+
31+
assert.strictEqual(path.posix.isAbsolute(url.pathToFileURL('bar/')), true);
32+
assert.strictEqual(path.posix.isAbsolute(url.pathToFileURL('/home/foo')), true);

test/parallel/test-path-makelong.js

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,7 @@ const common = require('../common');
2424
const fixtures = require('../common/fixtures');
2525
const assert = require('assert');
2626
const path = require('path');
27+
const url = require('url')
2728

2829
if (common.isWindows) {
2930
const file = fixtures.path('a.js');
@@ -58,6 +59,8 @@ assert.strictEqual(path.posix.toNamespacedPath(true), true);
5859
assert.strictEqual(path.posix.toNamespacedPath(1), 1);
5960
assert.strictEqual(path.posix.toNamespacedPath(), undefined);
6061
assert.strictEqual(path.posix.toNamespacedPath(emptyObj), emptyObj);
62+
assert.strictEqual(path.posix.toNamespacedPath(url.pathToFileURL('/foo/bar'), '/foo/bar'));
63+
6164
if (common.isWindows) {
6265
// These tests cause resolve() to insert the cwd, so we cannot test them from
6366
// non-Windows platforms (easily)

0 commit comments

Comments
 (0)