|
| 1 | +import assert from 'node:assert/strict' |
| 2 | +import { spawnSync } from 'node:child_process' |
| 3 | +import { describe, it } from 'node:test' |
| 4 | +import { fileURLToPath } from 'node:url' |
| 5 | + |
| 6 | +const entryPath = fileURLToPath(new URL('../cli.js', import.meta.url)) |
| 7 | + |
| 8 | +/** |
| 9 | + * Run relative to current file |
| 10 | + * |
| 11 | + * @param {object} param0 |
| 12 | + * @param {string} param0.cwd |
| 13 | + * @param {string[]} [param0.args] |
| 14 | + * @param {import('node:child_process').ProcessEnvOptions['env'] | undefined} [param0.env] |
| 15 | + * @returns {import('node:child_process').SpawnSyncReturns<string>} |
| 16 | + */ |
| 17 | +function spawnNPM ({ cwd, args = [], env }) { |
| 18 | + const pwd = fileURLToPath(new URL(cwd, import.meta.url)) |
| 19 | + return spawnSync(process.execPath, [entryPath, 'npm', ...args], { |
| 20 | + cwd: pwd, |
| 21 | + encoding: 'utf-8', |
| 22 | + env: { |
| 23 | + ...(env ?? process.env), |
| 24 | + // make sure we don't borrow TTY from parent |
| 25 | + SOCKET_SECURITY_TTY_IPC: undefined |
| 26 | + }, |
| 27 | + stdio: ['pipe', 'pipe', 'pipe'] |
| 28 | + }) |
| 29 | +} |
| 30 | + |
| 31 | +describe('Socket npm wrapper', () => { |
| 32 | + it('should bail on new typosquat', () => { |
| 33 | + const ret = spawnNPM({ |
| 34 | + cwd: './socket-npm-fixtures/lacking-typosquat', |
| 35 | + args: ['i', 'bowserify'] |
| 36 | + }) |
| 37 | + assert.equal(ret.status, 1) |
| 38 | + assert.match(ret.stderr, /Unable to prompt/) |
| 39 | + }) |
| 40 | +}) |
0 commit comments