Skip to content

Commit 59d4d95

Browse files
committed
feat(shell): separate command for stubbing shell
1 parent d3d7ccc commit 59d4d95

File tree

3 files changed

+46
-8
lines changed

3 files changed

+46
-8
lines changed

__snapshots__/stub-spawn-once-spec.js.snap-shot

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -20,3 +20,14 @@ exports['mocks command with exit code, stdout and stderr 1'] = {
2020
"timedOut": false
2121
}
2222

23+
exports['mocks echo hello 1'] = {
24+
"stdout": "foo",
25+
"stderr": "bar",
26+
"code": 0,
27+
"failed": false,
28+
"killed": false,
29+
"signal": null,
30+
"cmd": "/bin/sh -c echo \"hello\"",
31+
"timedOut": false
32+
}
33+

src/index.js

Lines changed: 12 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -96,4 +96,15 @@ function stubSpawnOnce (command, exitCode, stdout, stderr) {
9696
}
9797
}
9898

99-
module.exports = { stubSpawnOnce }
99+
// only provide the shell command like 'echo "hello"'
100+
function stubSpawnShellOnce (command, exitCode, stdout, stderr) {
101+
la(is.unemptyString(command), 'missing shell command to stub', command)
102+
const fullCommand = `/bin/sh -c ${command}`
103+
commands[fullCommand] = {
104+
exitCode,
105+
stdout,
106+
stderr
107+
}
108+
}
109+
110+
module.exports = { stubSpawnOnce, stubSpawnShellOnce }

src/stub-spawn-once-spec.js

Lines changed: 23 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -14,14 +14,30 @@ describe('stub-spawn-once', () => {
1414
return snapshot(execa.shell('echo "hello"'))
1515
})
1616

17-
it('is a function', () => {
18-
la(is.fn(stubSpawnOnce))
17+
describe('stubSpawnOnce', () => {
18+
it('is a function', () => {
19+
la(is.fn(stubSpawnOnce))
20+
})
21+
22+
it('mocks command with exit code, stdout and stderr', () => {
23+
const cmd = 'echo "hello"'
24+
// this is command for execa.shell
25+
stubSpawnOnce(`/bin/sh -c ${cmd}`, 0, 'foo', 'bar')
26+
return snapshot(execa.shell(cmd))
27+
})
1928
})
2029

21-
it('mocks command with exit code, stdout and stderr', () => {
22-
const cmd = 'echo "hello"'
23-
// this is command for execa.shell
24-
stubSpawnOnce(`/bin/sh -c ${cmd}`, 0, 'foo', 'bar')
25-
return snapshot(execa.shell(cmd))
30+
describe('stubSpawnShellOnce', () => {
31+
const { stubSpawnShellOnce } = require('.')
32+
33+
it('is a function', () => {
34+
la(is.fn(stubSpawnShellOnce))
35+
})
36+
37+
it('mocks echo hello', () => {
38+
const cmd = 'echo "hello"'
39+
stubSpawnShellOnce(cmd, 0, 'foo', 'bar')
40+
return snapshot(execa.shell(cmd))
41+
})
2642
})
2743
})

0 commit comments

Comments
 (0)