|
4 | 4 | * @see https://github.com/nodejs/node/blob/v23.2.0/test/parallel/test-url-pathtofileurl.js |
5 | 5 | */ |
6 | 6 |
|
| 7 | +import DRIVE from '#fixtures/drive' |
| 8 | +import { sepWindows } from '#internal/constants' |
| 9 | +import process from '#internal/process' |
7 | 10 | import testSubject from '#lib/path-to-file-url' |
| 11 | +import sep from '#lib/sep' |
8 | 12 | import toPosix from '#lib/to-posix' |
| 13 | +import cwdWindows from '#tests/utils/cwd-windows' |
9 | 14 | import { codes, isNodeError, type NodeError } from '@flex-development/errnode' |
10 | 15 | import { pathToFileURL } from 'node:url' |
11 | 16 |
|
12 | 17 | describe('unit:lib/pathToFileURL', () => { |
13 | 18 | describe('posix', () => { |
14 | 19 | it.each<Parameters<typeof testSubject>>([ |
15 | | - ['/FOO'], |
16 | | - ['/dir/'], |
17 | | - ['/dir/foo'], |
18 | | - ['/foo bar'], |
19 | | - ['/foo#bar'], |
20 | | - ['/foo%bar'], |
21 | | - ['/foo&bar'], |
22 | | - ['/foo'], |
23 | | - ['/foo.mjs'], |
24 | | - ['/foo:bar'], |
25 | | - ['/foo;bar'], |
26 | | - ['/foo=bar'], |
27 | | - ['/foo?bar'], |
28 | | - ['/foo\bbar'], |
29 | | - ['/foo\nbar'], |
30 | | - ['/foo\rbar'], |
31 | | - ['/foo\tbar'], |
32 | | - ['/fóóbàr'], |
33 | | - ['/€'], |
34 | | - ['/🚀'] |
35 | | - ])('should return `path` as `file:` URL (%#)', path => { |
| 20 | + [sep + 'FOO'], |
| 21 | + [sep + 'dir/'], |
| 22 | + [sep + 'dir/foo'], |
| 23 | + [sep + 'foo bar'], |
| 24 | + [sep + 'foo#bar'], |
| 25 | + [sep + 'foo%bar'], |
| 26 | + [sep + 'foo&bar'], |
| 27 | + [sep + 'foo'], |
| 28 | + [sep + 'foo.mjs'], |
| 29 | + [sep + 'foo:bar'] |
| 30 | + ])('should return `path` as `file:` URL (%j)', path => { |
36 | 31 | // Act |
37 | 32 | const result = testSubject(path) |
38 | 33 |
|
39 | 34 | // Expect |
40 | 35 | expect(result).to.eql(pathToFileURL(path, { windows: false })) |
41 | 36 | }) |
| 37 | + |
| 38 | + it.each<Parameters<typeof testSubject>>([ |
| 39 | + [sep + 'foo;bar'], |
| 40 | + [sep + 'foo=bar'], |
| 41 | + [sep + 'foo?bar'], |
| 42 | + [sep + 'foo\bbar'], |
| 43 | + [sep + 'foo\nbar'], |
| 44 | + [sep + 'foo\rbar'], |
| 45 | + [sep + 'foo\tbar'], |
| 46 | + [sep + 'fóóbàr'], |
| 47 | + [sep + '€'], |
| 48 | + [sep + '🚀'] |
| 49 | + ])('should return `path` as `file:` URL string (%j)', path => { |
| 50 | + // Act |
| 51 | + const result = testSubject(path, { string: true }) |
| 52 | + |
| 53 | + // Expect |
| 54 | + expect(result).to.eql(String(pathToFileURL(path, { windows: false }))) |
| 55 | + }) |
42 | 56 | }) |
43 | 57 |
|
44 | 58 | describe('windows', () => { |
45 | | - it.each<Parameters<typeof testSubject>>([ |
46 | | - ['C:\\FOO'], |
47 | | - ['C:\\dir\\'], |
48 | | - ['C:\\dir\\foo'], |
49 | | - ['C:\\foo bar'], |
50 | | - ['C:\\foo#bar'], |
51 | | - ['C:\\foo%bar'], |
52 | | - ['C:\\foo&bar'], |
53 | | - ['C:\\foo'], |
54 | | - ['C:\\foo.mjs'], |
55 | | - ['C:\\foo:bar'], |
56 | | - ['C:\\foo;bar'], |
57 | | - ['C:\\foo=bar'], |
58 | | - ['C:\\foo?bar'], |
59 | | - ['C:\\foo\\bar'], |
60 | | - ['C:\\foo\bbar'], |
61 | | - ['C:\\foo\nbar'], |
62 | | - ['C:\\foo\rbar'], |
63 | | - ['C:\\foo\tbar'], |
64 | | - ['C:\\fóóbàr'], |
65 | | - ['C:\\€'], |
66 | | - ['C:\\🚀'], |
67 | | - ['\\\\?\\C:\\path\\to\\file.txt'], |
68 | | - ['\\\\?\\UNC\\server\\share\\folder\\file.txt'], |
69 | | - ['\\\\nas\\My Docs\\File.doc'], |
70 | | - ['\\\\nas\\share\\path.txt'] |
71 | | - ])('should return `path` as `file:` URL (%#)', path => { |
72 | | - // Arrange |
73 | | - const windows: boolean = true |
74 | | - const url: URL = pathToFileURL(path, { windows }) |
| 59 | + let windows: true |
| 60 | + |
| 61 | + beforeAll(() => { |
| 62 | + windows = true |
| 63 | + }) |
75 | 64 |
|
| 65 | + beforeEach(() => { |
| 66 | + vi.spyOn(process, 'cwd').mockImplementation(cwdWindows) |
| 67 | + }) |
| 68 | + |
| 69 | + it.each<Parameters<typeof testSubject>>([ |
| 70 | + [DRIVE + sepWindows + 'FOO'], |
| 71 | + [DRIVE + sepWindows + 'dir\\'], |
| 72 | + [DRIVE + sepWindows + 'dir\\foo'], |
| 73 | + [DRIVE + sepWindows + 'foo bar'], |
| 74 | + [DRIVE + sepWindows + 'foo#bar'], |
| 75 | + [DRIVE + sepWindows + 'foo%bar'], |
| 76 | + [DRIVE + sepWindows + 'foo&bar'], |
| 77 | + [DRIVE + sepWindows + 'foo'], |
| 78 | + [DRIVE + sepWindows + 'foo.mjs'], |
| 79 | + [DRIVE + sepWindows + 'foo:bar'], |
| 80 | + [DRIVE + sepWindows + 'foo;bar'], |
| 81 | + [DRIVE + sepWindows + 'foo=bar'], |
| 82 | + [DRIVE + sepWindows + 'foo?bar'] |
| 83 | + ])('should return `path` as `file:` URL (%j)', path => { |
76 | 84 | // Act |
77 | 85 | const result = testSubject(path, { windows }) |
78 | 86 |
|
79 | 87 | // Expect |
80 | | - expect(result.href).to.eq(toPosix(url.href)) |
81 | | - expect(result.pathname).to.eq(toPosix(url.pathname)) |
| 88 | + expect(result).to.eql(toPosix(pathToFileURL(path, { windows }))) |
| 89 | + }) |
| 90 | + |
| 91 | + it.each<Parameters<typeof testSubject>>([ |
| 92 | + [DRIVE + sepWindows + '\\foo\\bar'], |
| 93 | + [DRIVE + sepWindows + '\\foo\bbar'], |
| 94 | + [DRIVE + sepWindows + '\\foo\nbar'], |
| 95 | + [DRIVE + sepWindows + '\\foo\rbar'], |
| 96 | + [DRIVE + sepWindows + '\\foo\tbar'], |
| 97 | + [DRIVE + sepWindows + '\\fóóbàr'], |
| 98 | + [DRIVE + sepWindows + '\\€'], |
| 99 | + [DRIVE + sepWindows + '\\🚀'], |
| 100 | + [sepWindows.repeat(2) + '?\\C:\\path\\to\\file.txt'], |
| 101 | + [sepWindows.repeat(2) + '?\\UNC\\server\\share\\folder\\file.txt'], |
| 102 | + [sepWindows.repeat(2) + 'nas\\My Docs\\File.doc'], |
| 103 | + [sepWindows.repeat(2) + 'nas\\share\\path.txt'] |
| 104 | + ])('should return `path` as `file:` URL string (%j)', path => { |
| 105 | + // Act |
| 106 | + const result = testSubject(path, { string: true, windows }) |
| 107 | + |
| 108 | + // Expect |
| 109 | + expect(result).to.eq(toPosix(String(pathToFileURL(path, { windows })))) |
82 | 110 | }) |
83 | 111 |
|
84 | 112 | it.each<Parameters<typeof testSubject>>([ |
85 | 113 | ['\\\\host'], |
86 | 114 | ['\\\\\\no-server'] |
87 | | - ])('should throw if UNC path hostname is invalid (%#)', path => { |
| 115 | + ])('should throw if UNC path hostname is invalid (%j)', path => { |
88 | 116 | // Arrange |
89 | 117 | let error!: NodeError |
90 | 118 |
|
|
0 commit comments