|
1 | 1 | import assert from 'node:assert/strict' |
2 | 2 | import { spawnSync } from 'node:child_process' |
| 3 | +import path from 'node:path' |
3 | 4 | import { describe, it } from 'node:test' |
4 | 5 | import { fileURLToPath } from 'node:url' |
5 | 6 |
|
6 | | -const entryPath = fileURLToPath(new URL('../cli.js', import.meta.url)) |
| 7 | +// these aliases are defined in package.json |
| 8 | +const npms = ['npm8', 'npm10'] |
7 | 9 |
|
8 | | -/** |
9 | | - * Run relative to current file |
10 | | - * @param {object} param0 |
11 | | - * @param {string} param0.cwd |
12 | | - * @param {string[]} [param0.args] |
13 | | - * @param {import('node:child_process').ProcessEnvOptions['env'] | undefined} [param0.env] |
14 | | - * @returns {import('node:child_process').SpawnSyncReturns<string>} |
15 | | - */ |
16 | | -function spawnNPM ({ cwd, args = [], env }) { |
17 | | - const pwd = fileURLToPath(new URL(cwd, import.meta.url)) |
18 | | - return spawnSync(process.execPath, [entryPath, 'npm', ...args], { |
19 | | - cwd: pwd, |
20 | | - encoding: 'utf-8', |
21 | | - env: { |
22 | | - ...(env ?? process.env), |
23 | | - // make sure we don't borrow TTY from parent |
24 | | - SOCKET_SECURITY_TTY_IPC: undefined |
25 | | - }, |
26 | | - stdio: ['pipe', 'pipe', 'pipe'] |
27 | | - }) |
28 | | -} |
| 10 | +const cli = fileURLToPath(new URL('../cli.js', import.meta.url)) |
29 | 11 |
|
30 | | -describe('Socket npm wrapper', () => { |
31 | | - it('should bail on new typosquat', () => { |
32 | | - const ret = spawnNPM({ |
33 | | - cwd: './socket-npm-fixtures/lacking-typosquat', |
34 | | - args: ['i', 'bowserify'] |
| 12 | +for (const npm of npms) { |
| 13 | + const installDir = fileURLToPath(new URL(`./socket-npm-fixtures/${npm}`, import.meta.url)) |
| 14 | + spawnSync('npm', ['install'], { |
| 15 | + cwd: installDir, |
| 16 | + stdio: 'inherit' |
| 17 | + }) |
| 18 | + console.error(process.execPath) |
| 19 | + describe(`Socket npm wrapper for ${npm}`, () => { |
| 20 | + /** |
| 21 | + * Run relative to current file |
| 22 | + * @param {object} param0 |
| 23 | + * @param {string} param0.cwd |
| 24 | + * @param {string[]} [param0.args] |
| 25 | + * @param {import('node:child_process').ProcessEnvOptions['env'] | undefined} [param0.env] |
| 26 | + * @returns {import('node:child_process').SpawnSyncReturns<string>} |
| 27 | + */ |
| 28 | + function spawnNPM ({ cwd, args = [], env }) { |
| 29 | + const pwd = fileURLToPath(new URL(cwd, import.meta.url)) |
| 30 | + return spawnSync(process.execPath, [cli, 'npm', ...args], { |
| 31 | + cwd: pwd, |
| 32 | + encoding: 'utf-8', |
| 33 | + env: { |
| 34 | + ...(env ?? process.env), |
| 35 | + // make sure we don't borrow TTY from parent |
| 36 | + SOCKET_SECURITY_TTY_IPC: undefined, |
| 37 | + // @ts-ignore |
| 38 | + PATH: `${path.join(installDir, 'node_modules', '.bin')}:${process.env.PATH}` |
| 39 | + }, |
| 40 | + stdio: ['pipe', 'pipe', 'pipe'] |
| 41 | + }) |
| 42 | + } |
| 43 | + it('should bail on new typosquat', () => { |
| 44 | + const ret = spawnNPM({ |
| 45 | + cwd: fileURLToPath(new URL('./socket-npm-fixtures/lacking-typosquat', import.meta.url)), |
| 46 | + args: ['i', 'bowserify'] |
| 47 | + }) |
| 48 | + assert.equal(ret.status, 1) |
| 49 | + assert.match(ret.stderr, /Unable to prompt/) |
35 | 50 | }) |
36 | | - assert.equal(ret.status, 1) |
37 | | - assert.equal(/Unable to prompt/.test(ret.stderr), true) |
38 | 51 | }) |
39 | | -}) |
| 52 | +} |
0 commit comments