Skip to content

Commit b7f4b66

Browse files
committed
feat(exec): add alias stubExecOnce, close #6
1 parent 72710cc commit b7f4b66

File tree

3 files changed

+44
-1
lines changed

3 files changed

+44
-1
lines changed

README.md

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -131,6 +131,13 @@ cp.exec('echo "hello"', (code, out, errors) => {
131131
})
132132
```
133133

134+
You can use alias `stubExecOnce` to `stubSpawnOnce`
135+
136+
```js
137+
const {stubExecOnce} = require('stub-spawn-once')
138+
stubExecOnce('echo "hi"', "bye")
139+
```
140+
134141
[execFile]: https://nodejs.org/api/child_process.html#child_process_child_process_execfile_file_args_options_callback
135142

136143
### Small print

src/index.js

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -129,4 +129,9 @@ function stubSpawnShellOnce (command, exitCode, stdout, stderr) {
129129
}
130130
}
131131

132-
module.exports = { stubSpawnOnce, stubSpawnShellOnce }
132+
module.exports = {
133+
stubSpawnOnce,
134+
stubSpawnShellOnce,
135+
stubExecOnce: stubSpawnOnce,
136+
stubExecFileOnce: stubSpawnOnce
137+
}

src/stub-spawn-once-spec.js

Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -48,6 +48,37 @@ describe('stub-spawn-once', () => {
4848
})
4949
})
5050

51+
describe('stubExecOnce', () => {
52+
const { stubExecOnce } = require('..')
53+
54+
it('is a function', () => {
55+
la(is.fn(stubExecOnce))
56+
})
57+
58+
it('stubs cp.exec', done => {
59+
const cmd = 'does not exist'
60+
stubExecOnce(cmd, 0, 'foo')
61+
cp.exec(cmd, (code, stdout) => {
62+
la(code === 0, 'wrong code', code)
63+
la(stdout === 'foo', 'wrong stdout', stdout)
64+
done()
65+
})
66+
})
67+
68+
it('restores cp.exec after that', done => {
69+
const cmd = 'does not exist'
70+
stubExecOnce(cmd, 0, 'foo')
71+
cp.exec(cmd, (code, stdout) => {
72+
la(code === 0, 'wrong code', code)
73+
la(stdout === 'foo', 'wrong stdout', stdout)
74+
cp.exec(cmd, (err, out) => {
75+
la(is.error(err), 'hmm, command succeeded', err, out)
76+
done()
77+
})
78+
})
79+
})
80+
})
81+
5182
describe('stubSpawnOnce', () => {
5283
it('is a function', () => {
5384
la(is.fn(stubSpawnOnce))

0 commit comments

Comments
 (0)