|
| 1 | +const t = require('tap') |
| 2 | + |
| 3 | +const fileURLToPath = require('../../../lib/common/file-url-to-path/polyfill.js') |
| 4 | + |
| 5 | +// these two errors are the only shared code, everything else has platform |
| 6 | +// specific tests and assertions below |
| 7 | +t.test('invalid input throws ERR_INVALID_ARG_TYPE', async (t) => { |
| 8 | + t.throws(() => fileURLToPath({}), { |
| 9 | + code: 'ERR_INVALID_ARG_TYPE', |
| 10 | + message: /must be one of type string or an instance of URL/, |
| 11 | + }, 'got the right error') |
| 12 | +}) |
| 13 | + |
| 14 | +t.test('invalid protocol throws ERR_INVALID_URL_SCHEME', async (t) => { |
| 15 | + t.throws(() => fileURLToPath('https://npmjs.com'), { |
| 16 | + code: 'ERR_INVALID_URL_SCHEME', |
| 17 | + message: /URL must be of scheme file/, |
| 18 | + }, 'got the right error') |
| 19 | +}) |
| 20 | + |
| 21 | +t.test('posix', async (t) => { |
| 22 | + let fileURLToPath |
| 23 | + t.before(() => { |
| 24 | + t.context.platform = Object.getOwnPropertyDescriptor(process, 'platform') |
| 25 | + Object.defineProperty(process, 'platform', { |
| 26 | + ...t.context.platform, |
| 27 | + value: 'linux', |
| 28 | + }) |
| 29 | + fileURLToPath = t.mock('../../../lib/common/file-url-to-path/polyfill.js') |
| 30 | + }) |
| 31 | + |
| 32 | + t.teardown(() => { |
| 33 | + Object.defineProperty(process, 'platform', t.context.platform) |
| 34 | + }) |
| 35 | + |
| 36 | + t.test('can convert a file url to a path', async (t) => { |
| 37 | + const url = new URL('file:///some/path') |
| 38 | + const result = fileURLToPath(url) |
| 39 | + t.type(result, 'string', 'result is a string') |
| 40 | + t.equal(result, '/some/path', 'got the right path') |
| 41 | + }) |
| 42 | + |
| 43 | + t.test('allows string urls', async (t) => { |
| 44 | + const url = 'file:///some/path' |
| 45 | + const result = fileURLToPath(url) |
| 46 | + t.type(result, 'string', 'result is a string') |
| 47 | + t.equal(result, '/some/path', 'got the right path') |
| 48 | + }) |
| 49 | + |
| 50 | + t.test('allows url encoded characters', async (t) => { |
| 51 | + const url = 'file:///some%20path' |
| 52 | + const result = fileURLToPath(url) |
| 53 | + t.type(result, 'string', 'result is a string') |
| 54 | + t.equal(result, '/some path', 'got the right path') |
| 55 | + }) |
| 56 | + |
| 57 | + t.test('url encoded / throws ERR_INVALID_FILE_URL_PATH', async (t) => { |
| 58 | + t.throws(() => fileURLToPath('file:///some%2fpath'), { |
| 59 | + code: 'ERR_INVALID_FILE_URL_PATH', |
| 60 | + message: /must not include encoded/, |
| 61 | + }, '%2f encoded / throws') |
| 62 | + |
| 63 | + t.throws(() => fileURLToPath('file:///some%2Fpath'), { |
| 64 | + code: 'ERR_INVALID_FILE_URL_PATH', |
| 65 | + message: /must not include encoded/, |
| 66 | + }, '%2F encoded / throws') |
| 67 | + }) |
| 68 | + |
| 69 | + t.test('urls with a hostname throw ERR_INVALID_FILE_URL_HOST', async (t) => { |
| 70 | + t.throws(() => fileURLToPath('file://host/some/path'), { |
| 71 | + code: 'ERR_INVALID_FILE_URL_HOST', |
| 72 | + message: /host must be "localhost" or empty/, |
| 73 | + }, 'hostname present throws') |
| 74 | + }) |
| 75 | +}) |
| 76 | + |
| 77 | +t.test('windows', async (t) => { |
| 78 | + // t.mock instead of require so we flush the cache first |
| 79 | + let fileURLToPath |
| 80 | + t.before(() => { |
| 81 | + t.context.platform = Object.getOwnPropertyDescriptor(process, 'platform') |
| 82 | + Object.defineProperty(process, 'platform', { |
| 83 | + ...t.context.platform, |
| 84 | + value: 'win32', |
| 85 | + }) |
| 86 | + fileURLToPath = t.mock('../../../lib/common/file-url-to-path/polyfill.js') |
| 87 | + }) |
| 88 | + |
| 89 | + t.teardown(() => { |
| 90 | + Object.defineProperty(process, 'platform', t.context.platform) |
| 91 | + }) |
| 92 | + |
| 93 | + t.test('can convert a file url to a path', async (t) => { |
| 94 | + const url = new URL('file://C:\\some\\path') |
| 95 | + const result = fileURLToPath(url) |
| 96 | + t.type(result, 'string', 'result is a string') |
| 97 | + t.equal(result, 'C:\\some\\path', 'got the right path') |
| 98 | + }) |
| 99 | + |
| 100 | + t.test('allows string urls', async (t) => { |
| 101 | + const url = 'file://C:\\some\\path' |
| 102 | + const result = fileURLToPath(url) |
| 103 | + t.type(result, 'string', 'result is a string') |
| 104 | + t.equal(result, 'C:\\some\\path', 'got the right path') |
| 105 | + }) |
| 106 | + |
| 107 | + t.test('allows hostnames', async (t) => { |
| 108 | + const url = 'file://host/some/path' |
| 109 | + const result = fileURLToPath(url) |
| 110 | + t.type(result, 'string', 'result is a string') |
| 111 | + t.equal(result, '\\\\host\\some\\path', 'got the right path') |
| 112 | + }) |
| 113 | + |
| 114 | + t.test('allows url encoded characters', async (t) => { |
| 115 | + const url = 'file://host/some%20path' |
| 116 | + const result = fileURLToPath(url) |
| 117 | + t.type(result, 'string', 'result is a string') |
| 118 | + t.equal(result, '\\\\host\\some path', 'got the right path') |
| 119 | + }) |
| 120 | + |
| 121 | + t.test('non-absolute path throws ERR_INVALID_FILE_URL_PATH', async (t) => { |
| 122 | + t.throws(() => fileURLToPath('file://\\some\\path'), { |
| 123 | + code: 'ERR_INVALID_FILE_URL_PATH', |
| 124 | + message: /must be absolute/, |
| 125 | + }, 'path with no drive letter threw') |
| 126 | + }) |
| 127 | + |
| 128 | + t.test('encoded \\ or / characters throw ERR_INVALID_FILE_URL_PATH', async (t) => { |
| 129 | + t.throws(() => fileURLToPath('file://c:/some%5cpath'), { |
| 130 | + code: 'ERR_INVALID_FILE_URL_PATH', |
| 131 | + message: /must not include encoded/, |
| 132 | + }, '%5c encoded \\ threw') |
| 133 | + t.throws(() => fileURLToPath('file://c:/some%5Cpath'), { |
| 134 | + code: 'ERR_INVALID_FILE_URL_PATH', |
| 135 | + message: /must not include encoded/, |
| 136 | + }, '%5C encoded \\ threw') |
| 137 | + t.throws(() => fileURLToPath('file://c:/some%2fpath'), { |
| 138 | + code: 'ERR_INVALID_FILE_URL_PATH', |
| 139 | + message: /must not include encoded/, |
| 140 | + }, '%2f encoded / threw') |
| 141 | + t.throws(() => fileURLToPath('file://c:/some%2Fpath'), { |
| 142 | + code: 'ERR_INVALID_FILE_URL_PATH', |
| 143 | + message: /must not include encoded/, |
| 144 | + }, '%2F encoded / threw') |
| 145 | + }) |
| 146 | +}) |
0 commit comments