|
1 | 1 | import * as PATH from 'path' |
2 | 2 | import t from 'tap' |
3 | | -import { fileURLToPath } from 'url' |
4 | 3 | import { inspect } from 'util' |
5 | 4 |
|
6 | | -if (!process.env.__TESTING_RIMRAF_PLATFORM__) { |
7 | | - const fake = process.platform === 'win32' ? 'posix' : 'win32' |
8 | | - t.spawn( |
9 | | - process.execPath, |
10 | | - [...process.execArgv, fileURLToPath(import.meta.url)], |
11 | | - { |
12 | | - name: fake, |
13 | | - env: { |
14 | | - ...process.env, |
15 | | - __TESTING_RIMRAF_PLATFORM__: fake, |
16 | | - }, |
17 | | - }, |
18 | | - ) |
19 | | -} |
| 5 | +for (const platform of ['win32', 'posix'] as const) { |
| 6 | + t.test(platform, async t => { |
| 7 | + t.intercept(process, 'platform', { value: platform }) |
| 8 | + const path = PATH[platform] || PATH |
| 9 | + const { default: pathArg } = (await t.mockImport('../src/path-arg.js', { |
| 10 | + path, |
| 11 | + })) as typeof import('../src/path-arg.js') |
20 | 12 |
|
21 | | -const platform = process.env.__TESTING_RIMRAF_PLATFORM__ || process.platform |
22 | | -const path = PATH[platform as 'win32' | 'posix'] || PATH |
23 | | -const { default: pathArg } = (await t.mockImport('../dist/esm/path-arg.js', { |
24 | | - path, |
25 | | -})) as typeof import('../dist/esm/path-arg.js') |
| 13 | + t.equal(pathArg('a/b/c'), path.resolve('a/b/c')) |
| 14 | + t.throws( |
| 15 | + () => pathArg('a\0b'), |
| 16 | + Error('path must be a string without null bytes'), |
| 17 | + ) |
| 18 | + if (platform === 'win32') { |
| 19 | + const badPaths = [ |
| 20 | + 'c:\\a\\b:c', |
| 21 | + 'c:\\a\\b*c', |
| 22 | + 'c:\\a\\b?c', |
| 23 | + 'c:\\a\\b<c', |
| 24 | + 'c:\\a\\b>c', |
| 25 | + 'c:\\a\\b|c', |
| 26 | + 'c:\\a\\b"c', |
| 27 | + ] |
| 28 | + for (const path of badPaths) { |
| 29 | + const er = Object.assign(new Error('Illegal characters in path'), { |
| 30 | + path, |
| 31 | + code: 'EINVAL', |
| 32 | + }) |
| 33 | + t.throws(() => pathArg(path), er) |
| 34 | + } |
| 35 | + } |
26 | 36 |
|
27 | | -const { resolve } = path |
| 37 | + t.throws(() => pathArg('/'), { code: 'ERR_PRESERVE_ROOT' }) |
28 | 38 |
|
29 | | -t.equal(pathArg('a/b/c'), resolve('a/b/c')) |
30 | | -t.throws( |
31 | | - () => pathArg('a\0b'), |
32 | | - Error('path must be a string without null bytes'), |
33 | | -) |
34 | | -if (platform === 'win32') { |
35 | | - const badPaths = [ |
36 | | - 'c:\\a\\b:c', |
37 | | - 'c:\\a\\b*c', |
38 | | - 'c:\\a\\b?c', |
39 | | - 'c:\\a\\b<c', |
40 | | - 'c:\\a\\b>c', |
41 | | - 'c:\\a\\b|c', |
42 | | - 'c:\\a\\b"c', |
43 | | - ] |
44 | | - for (const path of badPaths) { |
45 | | - const er = Object.assign(new Error('Illegal characters in path'), { |
46 | | - path, |
47 | | - code: 'EINVAL', |
| 39 | + t.throws(() => pathArg('/', { preserveRoot: undefined }), { |
| 40 | + code: 'ERR_PRESERVE_ROOT', |
48 | 41 | }) |
49 | | - t.throws(() => pathArg(path), er) |
50 | | - } |
51 | | -} |
52 | | - |
53 | | -t.throws(() => pathArg('/'), { code: 'ERR_PRESERVE_ROOT' }) |
54 | | - |
55 | | -t.throws(() => pathArg('/', { preserveRoot: undefined }), { |
56 | | - code: 'ERR_PRESERVE_ROOT', |
57 | | -}) |
58 | | -t.equal(pathArg('/', { preserveRoot: false }), resolve('/')) |
| 42 | + t.equal(pathArg('/', { preserveRoot: false }), path.resolve('/')) |
59 | 43 |
|
60 | | -//@ts-expect-error |
61 | | -t.throws(() => pathArg({}), { |
62 | | - code: 'ERR_INVALID_ARG_TYPE', |
63 | | - path: {}, |
64 | | - message: |
65 | | - 'The "path" argument must be of type string. ' + |
66 | | - 'Received an instance of Object', |
67 | | - name: 'TypeError', |
68 | | -}) |
69 | | -//@ts-expect-error |
70 | | -t.throws(() => pathArg([]), { |
71 | | - code: 'ERR_INVALID_ARG_TYPE', |
72 | | - path: [], |
73 | | - message: |
74 | | - 'The "path" argument must be of type string. ' + |
75 | | - 'Received an instance of Array', |
76 | | - name: 'TypeError', |
77 | | -}) |
78 | | -//@ts-expect-error |
79 | | -t.throws(() => pathArg(Object.create(null) as {}), { |
80 | | - code: 'ERR_INVALID_ARG_TYPE', |
81 | | - path: Object.create(null), |
82 | | - message: |
83 | | - 'The "path" argument must be of type string. ' + |
84 | | - `Received ${inspect(Object.create(null))}`, |
85 | | - name: 'TypeError', |
86 | | -}) |
87 | | -//@ts-expect-error |
88 | | -t.throws(() => pathArg(true), { |
89 | | - code: 'ERR_INVALID_ARG_TYPE', |
90 | | - path: true, |
91 | | - message: |
92 | | - 'The "path" argument must be of type string. ' + |
93 | | - `Received type boolean true`, |
94 | | - name: 'TypeError', |
95 | | -}) |
| 44 | + //@ts-expect-error |
| 45 | + t.throws(() => pathArg({}), { |
| 46 | + code: 'ERR_INVALID_ARG_TYPE', |
| 47 | + path: {}, |
| 48 | + message: |
| 49 | + 'The "path" argument must be of type string. ' + |
| 50 | + 'Received an instance of Object', |
| 51 | + name: 'TypeError', |
| 52 | + }) |
| 53 | + //@ts-expect-error |
| 54 | + t.throws(() => pathArg([]), { |
| 55 | + code: 'ERR_INVALID_ARG_TYPE', |
| 56 | + path: [], |
| 57 | + message: |
| 58 | + 'The "path" argument must be of type string. ' + |
| 59 | + 'Received an instance of Array', |
| 60 | + name: 'TypeError', |
| 61 | + }) |
| 62 | + //@ts-expect-error |
| 63 | + t.throws(() => pathArg(Object.create(null) as object), { |
| 64 | + code: 'ERR_INVALID_ARG_TYPE', |
| 65 | + path: Object.create(null) as object, |
| 66 | + message: |
| 67 | + 'The "path" argument must be of type string. ' + |
| 68 | + `Received ${inspect(Object.create(null))}`, |
| 69 | + name: 'TypeError', |
| 70 | + }) |
| 71 | + //@ts-expect-error |
| 72 | + t.throws(() => pathArg(true), { |
| 73 | + code: 'ERR_INVALID_ARG_TYPE', |
| 74 | + path: true, |
| 75 | + message: |
| 76 | + 'The "path" argument must be of type string. ' + |
| 77 | + `Received type boolean true`, |
| 78 | + name: 'TypeError', |
| 79 | + }) |
| 80 | + }) |
| 81 | +} |
0 commit comments