Skip to content

Commit 73a68bc

Browse files
committed
feat(code): allow default exit code 0, close #7
1 parent b7f4b66 commit 73a68bc

File tree

3 files changed

+34
-0
lines changed

3 files changed

+34
-0
lines changed

README.md

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -61,6 +61,14 @@ execa
6161
*/
6262
```
6363

64+
***hint** `exit code` argument is optional and you can omit it (then 0 will be
65+
returned)
66+
67+
```js
68+
const { stubSpawnShellOnce } = require('.')
69+
stubSpawnShellOnce('my command', 'hi there', 'error output string')
70+
```
71+
6472
[spawn]: http://devdocs.io/node/child_process#child_process_child_process_spawn_command_args_options
6573

6674
## Install

src/index.js

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -111,6 +111,14 @@ if (!stubbed()) {
111111

112112
function stubSpawnOnce (command, exitCode, stdout, stderr) {
113113
la(is.unemptyString(command), 'missing command to stub', command)
114+
115+
if (is.string(exitCode)) {
116+
debug('stub for %s without explicit exit code, assuming 0', command)
117+
stderr = stdout
118+
stdout = exitCode
119+
exitCode = 0
120+
}
121+
114122
commands[command] = {
115123
exitCode,
116124
stdout,
@@ -121,6 +129,14 @@ function stubSpawnOnce (command, exitCode, stdout, stderr) {
121129
// only provide the shell command like 'echo "hello"'
122130
function stubSpawnShellOnce (command, exitCode, stdout, stderr) {
123131
la(is.unemptyString(command), 'missing shell command to stub', command)
132+
133+
if (is.string(exitCode)) {
134+
debug('stub for %s without explicit exit code, assuming 0', command)
135+
stderr = stdout
136+
stdout = exitCode
137+
exitCode = 0
138+
}
139+
124140
const fullCommand = `/bin/sh -c ${command}`
125141
commands[fullCommand] = {
126142
exitCode,

src/stub-spawn-once-spec.js

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -65,6 +65,16 @@ describe('stub-spawn-once', () => {
6565
})
6666
})
6767

68+
it('stubs cp.exec output only', done => {
69+
const cmd = 'does not exist'
70+
stubExecOnce(cmd, 'foo')
71+
cp.exec(cmd, (code, stdout) => {
72+
la(code === 0, 'wrong code', code)
73+
la(stdout === 'foo', 'wrong stdout', stdout)
74+
done()
75+
})
76+
})
77+
6878
it('restores cp.exec after that', done => {
6979
const cmd = 'does not exist'
7080
stubExecOnce(cmd, 0, 'foo')

0 commit comments

Comments
 (0)