11'use strict' ;
22const { isWindows } = require ( '../common' ) ;
3- const assert = require ( 'assert' ) ;
4- const url = require ( 'url' ) ;
53
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 ] ) {
810 assert . throws ( ( ) => url . fileURLToPath ( arg ) , {
911 code : 'ERR_INVALID_ARG_TYPE'
1012 } ) ;
1113 }
12- }
13-
14- // Input must be string or URL
15- testInvalidArgs ( null , undefined , 1 , { } , true ) ;
14+ } ) ;
1615
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+ } ) ;
2020} ) ;
2121
22- {
22+ test ( 'fileURLToPath with host' , ( ) => {
2323 const withHost = new URL ( 'file://host/a' ) ;
2424
2525 if ( isWindows ) {
@@ -29,9 +29,9 @@ assert.throws(() => url.fileURLToPath('https://a/b/c'), {
2929 code : 'ERR_INVALID_FILE_URL_HOST'
3030 } ) ;
3131 }
32- }
32+ } ) ;
3333
34- {
34+ test ( 'fileURLToPath with invalid path' , ( ) => {
3535 if ( isWindows ) {
3636 assert . throws ( ( ) => url . fileURLToPath ( 'file:///C:/a%2F/' ) , {
3737 code : 'ERR_INVALID_FILE_URL_PATH'
@@ -47,7 +47,7 @@ assert.throws(() => url.fileURLToPath('https://a/b/c'), {
4747 code : 'ERR_INVALID_FILE_URL_PATH'
4848 } ) ;
4949 }
50- }
50+ } ) ;
5151
5252const windowsTestCases = [
5353 // Lowercase ascii alpha
@@ -95,6 +95,7 @@ const windowsTestCases = [
9595 // UNC path (see https://docs.microsoft.com/en-us/archive/blogs/ie/file-uris-in-windows)
9696 { path : '\\\\nas\\My Docs\\File.doc' , fileURL : 'file://nas/My%20Docs/File.doc' } ,
9797] ;
98+
9899const posixTestCases = [
99100 // Lowercase ascii alpha
100101 { path : '/foo' , fileURL : 'file:///foo' } ,
@@ -140,29 +141,37 @@ const posixTestCases = [
140141 { path : '/🚀' , fileURL : 'file:///%F0%9F%9A%80' } ,
141142] ;
142143
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+ } ) ;
149153
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+ } ) ;
156162
157163const defaultTestCases = isWindows ? windowsTestCases : posixTestCases ;
158164
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+ } ) ;
162169
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+ } ) ;
0 commit comments