Skip to content

Commit 4e04b9d

Browse files
committed
testing just execa
1 parent 6f272d6 commit 4e04b9d

File tree

4 files changed

+96
-4
lines changed

4 files changed

+96
-4
lines changed
Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
exports['plain execa.shell 1'] = {
2+
"stdout": "hello",
3+
"stderr": "",
4+
"code": 0,
5+
"failed": false,
6+
"killed": false,
7+
"signal": null,
8+
"cmd": "/bin/sh -c echo \"hello\"",
9+
"timedOut": false
10+
}
11+

package.json

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -69,14 +69,17 @@
6969
"dependency-check": "2.9.1",
7070
"deps-ok": "1.2.0",
7171
"dont-crack": "1.2.1",
72+
"execa": "0.7.0",
7273
"git-issues": "1.3.1",
7374
"github-post-release": "1.7.1",
7475
"license-checker": "12.0.0",
7576
"mocha": "3.4.2",
7677
"nsp": "2.6.3",
7778
"pre-git": "3.15.0",
79+
"prettier-standard": "6.0.0",
7880
"semantic-release": "6.3.6",
7981
"simple-commit-message": "3.1.0",
82+
"snap-shot": "2.17.0",
8083
"standard": "10.0.2"
8184
},
8285
"release": {
@@ -86,5 +89,10 @@
8689
"path": "dont-crack",
8790
"test-against": []
8891
}
92+
},
93+
"dependencies": {
94+
"check-more-types": "2.24.0",
95+
"debug": "2.6.8",
96+
"lazy-ass": "1.6.0"
8997
}
9098
}

src/index.js

Lines changed: 72 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,76 @@
11
'use strict'
22

3+
const cp = require('child_process')
4+
const debug = require('debug')('stub-spawn-once')
5+
const la = require('lazy-ass')
6+
const is = require('check-more-types')
37

4-
module.exports = true
8+
// save original spawn right away
9+
const oldSpawn = cp.spawn
510

11+
function spawnStub (file, args, options) {
12+
debug('in spawn stub', file, args)
13+
14+
const listeners = {}
15+
const on = (name, cb) => {
16+
if (!listeners[name]) {
17+
listeners[name] = []
18+
}
19+
listeners[name].push(cb)
20+
}
21+
22+
const kill = () => {}
23+
const child = {
24+
on,
25+
kill
26+
}
27+
28+
const exitCode = 0
29+
const exitSignal = null
30+
31+
setTimeout(() => {
32+
if (listeners.exit) {
33+
listeners.exit.forEach(cb => {
34+
console.log('calling exit code %d and signal %s', exitCode, exitSignal)
35+
cb(exitCode, exitSignal)
36+
})
37+
}
38+
}, 0)
39+
40+
return child
41+
}
42+
43+
// list of stubbed commands
44+
const commands = {}
45+
46+
function spawnDispatcher (file, args, options) {
47+
debug('spawnDispatcher', file, arguments)
48+
const command = file + ' ' + args.join(' ')
49+
const mock = commands[command]
50+
if (mock) {
51+
return spawnStub(file, args, options)
52+
} else {
53+
return oldSpawn(file, args, options)
54+
}
55+
}
56+
57+
function stubbed () {
58+
return cp.spawn === spawnDispatcher
59+
}
60+
61+
if (!stubbed()) {
62+
cp.spawn = spawnDispatcher
63+
} else {
64+
debug('child_process.spawn was already stubbed')
65+
}
66+
67+
function stubSpawnOnce (command, exitCode, stdout, stderr) {
68+
la(is.unemptyString(command), 'missing command to stub', command)
69+
commands[command] = {
70+
exitCode,
71+
stdout,
72+
stderr
73+
}
74+
}
75+
76+
module.exports = stubSpawnOnce

src/stub-spawn-once-spec.js

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,13 @@
11
'use strict'
22

3+
const execa = require('execa')
4+
const snapshot = require('snap-shot')
35

46
/* global describe, it */
5-
const stubSpawnOnce = require('.')
7+
// const stubSpawnOnce = require('.')
68

79
describe('stub-spawn-once', () => {
8-
it('write this test', () => {
9-
console.assert(stubSpawnOnce, 'should export something')
10+
it('plain execa.shell', () => {
11+
return snapshot(execa.shell('echo "hello"'))
1012
})
1113
})

0 commit comments

Comments
 (0)