Skip to content

Commit

Permalink
fix: check childProcess was created
Browse files Browse the repository at this point in the history
  • Loading branch information
Kikobeats committed Dec 29, 2023
1 parent 7631190 commit 8761380
Show file tree
Hide file tree
Showing 2 changed files with 49 additions and 17 deletions.
4 changes: 2 additions & 2 deletions src/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
const { spawn } = require('child_process')
const { EOL } = require('os')

const EVENT_EMITTER_PROPS = Object.getOwnPropertyNames(require('events').EventEmitter.prototype).filter(name => !name.startsWith('_'))
const EE_PROPS = Object.getOwnPropertyNames(require('events').EventEmitter.prototype).filter(name => !name.startsWith('_'))

const eos = (stream, listener, buffer = []) => stream[listener].on('data', data => buffer.push(data)) && buffer

Expand Down Expand Up @@ -45,7 +45,7 @@ const extend = defaults => (input, args, options) => {
})

const subprocess = Object.assign(promise, childProcess)
EVENT_EMITTER_PROPS.forEach(name => (subprocess[name] = childProcess[name].bind(childProcess)))
if (childProcess) EE_PROPS.forEach(name => (subprocess[name] = childProcess[name].bind(childProcess)))
return subprocess
}

Expand Down
62 changes: 47 additions & 15 deletions test/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -26,25 +26,45 @@ test.serial('run a command', async t => {
const result = await $('echo hello world')
t.is(result.stdout, 'hello world')
t.is(result.spawnfile, SHELL)
t.deepEqual(result.spawnargs, isWindows ? [SHELL, '/d', '/s', '/c', '"echo hello world"'] : [SHELL, '-c', 'echo hello world'])
t.deepEqual(
result.spawnargs,
isWindows
? [SHELL, '/d', '/s', '/c', '"echo hello world"']
: [SHELL, '-c', 'echo hello world']
)
}
{
const result = await $('echo $0', { argv0: 'hello world' })
t.is(result.stdout, isWindows ? '$0' : 'hello world')
t.is(result.spawnfile, SHELL)
t.deepEqual(result.spawnargs, isWindows ? ['hello world', '/d', '/s', '/c', '"echo $0"'] : ['hello world', '-c', 'echo $0'])
t.deepEqual(
result.spawnargs,
isWindows
? ['hello world', '/d', '/s', '/c', '"echo $0"']
: ['hello world', '-c', 'echo $0']
)
}
{
const result = await $('echo', ['hello world'])
t.is(result.stdout, 'hello world')
t.is(result.spawnfile, SHELL)
t.deepEqual(result.spawnargs, isWindows ? [SHELL, '/d', '/s', '/c', '"echo hello world"'] : [SHELL, '-c', 'echo hello world'])
t.deepEqual(
result.spawnargs,
isWindows
? [SHELL, '/d', '/s', '/c', '"echo hello world"']
: [SHELL, '-c', 'echo hello world']
)
}
{
const result = await $('echo', ['hello $0'], { argv0: 'world' })
t.is(result.stdout, isWindows ? 'hello $0' : 'hello world')
t.is(result.spawnfile, SHELL)
t.deepEqual(result.spawnargs, isWindows ? ['world', '/d', '/s', '/c', '"echo hello $0"'] : ['world', '-c', 'echo hello $0'])
t.deepEqual(
result.spawnargs,
isWindows
? ['world', '/d', '/s', '/c', '"echo hello $0"']
: ['world', '-c', 'echo hello $0']
)
}
})

Expand Down Expand Up @@ -80,7 +100,9 @@ test('output is child_process', async t => {
})

test('$.json', async t => {
const { stdout } = await require('..').json('curl https://geolocation.microlink.io')
const { stdout } = await require('..').json(
'curl https://geolocation.microlink.io'
)
t.true(!!stdout.ip.address)
})

Expand All @@ -104,7 +126,10 @@ test('piping subprocess', async t => {
})

test('passing timeout', async t => {
const result = await $('sleep 3 && echo OK', { timeout: 1, killSignal: 'SIGKILL' }).catch(err => err)
const result = await $('sleep 3 && echo OK', {
timeout: 1,
killSignal: 'SIGKILL'
}).catch(err => err)
t.is(result.killed, true)
t.is(result.signalCode, 'SIGKILL')
})
Expand All @@ -115,15 +140,22 @@ test('event emitter properties are availables', async t => {
const result = await subprocess
t.is(result.stdout, '1234567890')
t.is(result.exitCode, 0)

;[
'constructor', 'setMaxListeners',
'getMaxListeners', 'emit',
'addListener', 'on',
'prependListener', 'once',
'prependOnceListener', 'removeListener',
'off', 'removeAllListeners',
'listeners', 'rawListeners',
'listenerCount', 'eventNames'
'constructor',
'setMaxListeners',
'getMaxListeners',
'emit',
'addListener',
'on',
'prependListener',
'once',
'prependOnceListener',
'removeListener',
'off',
'removeAllListeners',
'listeners',
'rawListeners',
'listenerCount',
'eventNames'
].forEach(name => t.truthy(subprocess[name]))
})

0 comments on commit 8761380

Please sign in to comment.