|
| 1 | +import '../common/index.mjs'; |
| 2 | +import { spawnSyncAndAssert, spawnSyncAndExit } from '../common/child_process.js'; |
| 3 | +import * as fixtures from '../common/fixtures.mjs'; |
| 4 | + |
| 5 | +const map = fixtures.path('package-map/traversal/map.json'); |
| 6 | +const cwd = fixtures.path('package-map/traversal/app'); |
| 7 | + |
| 8 | +function runESM(specifier) { |
| 9 | + return [ |
| 10 | + '--no-warnings', |
| 11 | + '--experimental-package-map', map, |
| 12 | + '--input-type=module', |
| 13 | + '--eval', `import x from '${specifier}'; console.log(x);`, |
| 14 | + ]; |
| 15 | +} |
| 16 | + |
| 17 | +function runCJS(specifier) { |
| 18 | + return [ |
| 19 | + '--no-warnings', |
| 20 | + '--experimental-package-map', map, |
| 21 | + '--eval', `console.log(require('${specifier}'));`, |
| 22 | + ]; |
| 23 | +} |
| 24 | + |
| 25 | +for (const specifier of [ |
| 26 | + 'dep/../secret.mjs', |
| 27 | + 'dep/%2e%2e/secret.mjs', |
| 28 | + 'dep/sub/../../secret.mjs', |
| 29 | +]) { |
| 30 | + spawnSyncAndExit(process.execPath, runESM(specifier), { cwd }, { |
| 31 | + status: 1, |
| 32 | + signal: null, |
| 33 | + stderr: /ERR_INVALID_MODULE_SPECIFIER/, |
| 34 | + }); |
| 35 | +} |
| 36 | + |
| 37 | +for (const specifier of [ |
| 38 | + 'dep/sub/file.mjs', |
| 39 | + 'dep/./sub/file.mjs', |
| 40 | + 'dep/sub/../sub/file.mjs', |
| 41 | +]) { |
| 42 | + spawnSyncAndAssert(process.execPath, runESM(specifier), { cwd }, { |
| 43 | + stdout: /dep-sub/, |
| 44 | + trim: true, |
| 45 | + }); |
| 46 | +} |
| 47 | + |
| 48 | +for (const specifier of [ |
| 49 | + 'dep/../secret.cjs', |
| 50 | + 'dep/%2e%2e/secret.cjs', |
| 51 | + 'dep/sub/../../secret.cjs', |
| 52 | +]) { |
| 53 | + spawnSyncAndExit(process.execPath, runCJS(specifier), { cwd }, { |
| 54 | + status: 1, |
| 55 | + signal: null, |
| 56 | + stderr: /ERR_INVALID_MODULE_SPECIFIER/, |
| 57 | + }); |
| 58 | +} |
| 59 | + |
| 60 | +for (const specifier of [ |
| 61 | + 'dep/sub/file.cjs', |
| 62 | + 'dep/./sub/file.cjs', |
| 63 | + 'dep/sub/../sub/file.cjs', |
| 64 | +]) { |
| 65 | + spawnSyncAndAssert(process.execPath, runCJS(specifier), { cwd }, { |
| 66 | + stdout: /dep-sub-cjs/, |
| 67 | + trim: true, |
| 68 | + }); |
| 69 | +} |
0 commit comments