Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 5 additions & 0 deletions lib/run-script-pkg.js
Original file line number Diff line number Diff line change
Expand Up @@ -67,6 +67,11 @@ const runScriptPkg = async options => {
stdioString,
})

if (options.replaceProcess && stdio === 'inherit' && promiseSpawn.canExecve()) {
inputEnd()
return promiseSpawn.execve(spawnShell, spawnArgs, spawnOpts)
}

const p = promiseSpawn(spawnShell, spawnArgs, spawnOpts, {
event,
script: cmd,
Expand Down
70 changes: 70 additions & 0 deletions test/replace-process.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,70 @@
const t = require('tap')
const spawk = require('spawk')
const realPromiseSpawn = require('@npmcli/promise-spawn')

const emptyDir = t.testdir({})

const mockRunScript = (t, { canExecve }) => {
const calls = []
const promiseSpawn = Object.assign((...args) => realPromiseSpawn(...args), {
canExecve: () => canExecve,
execve: async (...args) => {
calls.push(args)
},
})
const runScript = t.mock('../lib/run-script.js', {
'../lib/run-script-pkg.js': t.mock('../lib/run-script-pkg.js', {
'@npmcli/promise-spawn': promiseSpawn,
}),
})
return { runScript, calls }
}

const options = (overrides) => ({
event: 'npx',
path: emptyDir,
scriptShell: 'sh',
stdio: 'inherit',
cmd: 'bar',
args: ['baz qux'],
replaceProcess: true,
pkg: { scripts: {} },
...overrides,
})

t.test('replaces the process when asked to and possible', async t => {
const { runScript, calls } = mockRunScript(t, { canExecve: true })
await runScript(options())
t.equal(calls.length, 1)
const [cmd, args, opts] = calls[0]
t.equal(cmd, 'bar')
t.strictSame(args, ['baz qux'])
t.equal(opts.shell, 'sh')
t.equal(opts.cwd, emptyDir)
t.equal(opts.env.npm_lifecycle_event, 'npx')
t.equal(opts.env.npm_lifecycle_script, 'bar')
})

t.test('spawns when execve is not available', async t => {
const { runScript, calls } = mockRunScript(t, { canExecve: false })
spawk.spawn('sh', a => a.includes("bar 'baz qux'"))
await runScript(options())
t.strictSame(calls, [])
t.ok(spawk.done())
})

t.test('spawns when stdio is not inherited', async t => {
const { runScript, calls } = mockRunScript(t, { canExecve: true })
spawk.spawn('sh', a => a.includes("bar 'baz qux'"))
await runScript(options({ stdio: 'pipe' }))
t.strictSame(calls, [])
t.ok(spawk.done())
})

t.test('spawns when not asked to replace the process', async t => {
const { runScript, calls } = mockRunScript(t, { canExecve: true })
spawk.spawn('sh', a => a.includes("bar 'baz qux'"))
await runScript(options({ replaceProcess: false }))
t.strictSame(calls, [])
t.ok(spawk.done())
})