|
1 | 1 | 'use strict'; |
2 | 2 | const { isWindows } = require('../common'); |
3 | | -const assert = require('assert'); |
4 | | -const url = require('url'); |
5 | 3 |
|
6 | | -function testInvalidArgs(...args) { |
7 | | - for (const arg of args) { |
| 4 | +const { test } = require('node:test'); |
| 5 | +const assert = require('node:assert'); |
| 6 | +const url = require('node:url'); |
| 7 | + |
| 8 | +test('invalid arguments', () => { |
| 9 | + for (const arg of [null, undefined, 1, {}, true]) { |
8 | 10 | assert.throws(() => url.fileURLToPath(arg), { |
9 | 11 | code: 'ERR_INVALID_ARG_TYPE' |
10 | 12 | }); |
11 | 13 | } |
12 | | -} |
13 | | - |
14 | | -// Input must be string or URL |
15 | | -testInvalidArgs(null, undefined, 1, {}, true); |
| 14 | +}); |
16 | 15 |
|
17 | | -// Input must be a file URL |
18 | | -assert.throws(() => url.fileURLToPath('https://a/b/c'), { |
19 | | - code: 'ERR_INVALID_URL_SCHEME' |
| 16 | +test('input must be a file URL', () => { |
| 17 | + assert.throws(() => url.fileURLToPath('https://a/b/c'), { |
| 18 | + code: 'ERR_INVALID_URL_SCHEME' |
| 19 | + }); |
20 | 20 | }); |
21 | 21 |
|
22 | | -{ |
| 22 | +test('fileURLToPath with host', () => { |
23 | 23 | const withHost = new URL('file://host/a'); |
24 | 24 |
|
25 | 25 | if (isWindows) { |
@@ -29,9 +29,9 @@ assert.throws(() => url.fileURLToPath('https://a/b/c'), { |
29 | 29 | code: 'ERR_INVALID_FILE_URL_HOST' |
30 | 30 | }); |
31 | 31 | } |
32 | | -} |
| 32 | +}); |
33 | 33 |
|
34 | | -{ |
| 34 | +test('fileURLToPath with invalid path', () => { |
35 | 35 | if (isWindows) { |
36 | 36 | assert.throws(() => url.fileURLToPath('file:///C:/a%2F/'), { |
37 | 37 | code: 'ERR_INVALID_FILE_URL_PATH' |
@@ -47,7 +47,7 @@ assert.throws(() => url.fileURLToPath('https://a/b/c'), { |
47 | 47 | code: 'ERR_INVALID_FILE_URL_PATH' |
48 | 48 | }); |
49 | 49 | } |
50 | | -} |
| 50 | +}); |
51 | 51 |
|
52 | 52 | const windowsTestCases = [ |
53 | 53 | // Lowercase ascii alpha |
@@ -95,6 +95,7 @@ const windowsTestCases = [ |
95 | 95 | // UNC path (see https://docs.microsoft.com/en-us/archive/blogs/ie/file-uris-in-windows) |
96 | 96 | { path: '\\\\nas\\My Docs\\File.doc', fileURL: 'file://nas/My%20Docs/File.doc' }, |
97 | 97 | ]; |
| 98 | + |
98 | 99 | const posixTestCases = [ |
99 | 100 | // Lowercase ascii alpha |
100 | 101 | { path: '/foo', fileURL: 'file:///foo' }, |
@@ -140,29 +141,37 @@ const posixTestCases = [ |
140 | 141 | { path: '/🚀', fileURL: 'file:///%F0%9F%9A%80' }, |
141 | 142 | ]; |
142 | 143 |
|
143 | | -for (const { path, fileURL } of windowsTestCases) { |
144 | | - const fromString = url.fileURLToPath(fileURL, { windows: true }); |
145 | | - assert.strictEqual(fromString, path); |
146 | | - const fromURL = url.fileURLToPath(new URL(fileURL), { windows: true }); |
147 | | - assert.strictEqual(fromURL, path); |
148 | | -} |
| 144 | +test('fileURLToPath with windows path', { skip: !isWindows }, () => { |
| 145 | + |
| 146 | + for (const { path, fileURL } of windowsTestCases) { |
| 147 | + const fromString = url.fileURLToPath(fileURL, { windows: true }); |
| 148 | + assert.strictEqual(fromString, path); |
| 149 | + const fromURL = url.fileURLToPath(new URL(fileURL), { windows: true }); |
| 150 | + assert.strictEqual(fromURL, path); |
| 151 | + } |
| 152 | +}); |
149 | 153 |
|
150 | | -for (const { path, fileURL } of posixTestCases) { |
151 | | - const fromString = url.fileURLToPath(fileURL, { windows: false }); |
152 | | - assert.strictEqual(fromString, path); |
153 | | - const fromURL = url.fileURLToPath(new URL(fileURL), { windows: false }); |
154 | | - assert.strictEqual(fromURL, path); |
155 | | -} |
| 154 | +test('fileURLToPath with posix path', { skip: isWindows }, () => { |
| 155 | + for (const { path, fileURL } of posixTestCases) { |
| 156 | + const fromString = url.fileURLToPath(fileURL, { windows: false }); |
| 157 | + assert.strictEqual(fromString, path); |
| 158 | + const fromURL = url.fileURLToPath(new URL(fileURL), { windows: false }); |
| 159 | + assert.strictEqual(fromURL, path); |
| 160 | + } |
| 161 | +}); |
156 | 162 |
|
157 | 163 | const defaultTestCases = isWindows ? windowsTestCases : posixTestCases; |
158 | 164 |
|
159 | | -// Test when `options` is null |
160 | | -const whenNullActual = url.fileURLToPath(new URL(defaultTestCases[0].fileURL), null); |
161 | | -assert.strictEqual(whenNullActual, defaultTestCases[0].path); |
| 165 | +test('options is null', () => { |
| 166 | + const whenNullActual = url.fileURLToPath(new URL(defaultTestCases[0].fileURL), null); |
| 167 | + assert.strictEqual(whenNullActual, defaultTestCases[0].path); |
| 168 | +}); |
162 | 169 |
|
163 | | -for (const { path, fileURL } of defaultTestCases) { |
164 | | - const fromString = url.fileURLToPath(fileURL); |
165 | | - assert.strictEqual(fromString, path); |
166 | | - const fromURL = url.fileURLToPath(new URL(fileURL)); |
167 | | - assert.strictEqual(fromURL, path); |
168 | | -} |
| 170 | +test('defaultTestCases', () => { |
| 171 | + for (const { path, fileURL } of defaultTestCases) { |
| 172 | + const fromString = url.fileURLToPath(fileURL); |
| 173 | + assert.strictEqual(fromString, path); |
| 174 | + const fromURL = url.fileURLToPath(new URL(fileURL)); |
| 175 | + assert.strictEqual(fromURL, path); |
| 176 | + } |
| 177 | +}); |