forked from angular/angular-cli
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathexec-stub.js
71 lines (62 loc) · 1.63 KB
/
exec-stub.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
'use strict';
var child_process = require('child_process');
var sinon = require('sinon');
class ExecStub {
constructor() {
this.execOrig = child_process.exec;
this.stub = sinon.stub(child_process, 'exec', this.execStubFunc.bind(this));
this.stack = [];
this.failed = false;
}
execStubFunc(cmd) {
let resp;
// console.log('####running', cmd);
if (this.failed) {
resp = this.failedExec('ExecStub - in fail mode');
return resp.apply(null, arguments);
}
if (this.stack.length === 0) {
this.failed = true;
resp = this.failedExec('ExecStub - expected stack size exceeded');
return resp.apply(null, arguments);
}
let item = this.stack.shift();
// console.log('####expected', item.cmd);
if (cmd !== item.cmd) {
this.failed = true;
resp = this.failedExec(`ExecStub - unexpected command: ${cmd}`);
return resp.apply(null, arguments);
}
return item.resp.apply(null, arguments);
}
hasFailed() {
return this.failed;
}
hasEmptyStack() {
return this.stack.length === 0;
}
restore() {
this.stub.restore();
return this;
}
addExecSuccess(cmd, sdout) {
sdout = sdout || '';
this.stack.push({
cmd,
resp: (cmd, opt, cb) => (cb ? cb : opt)(null, sdout, null)
});
return this;
}
addExecError(cmd, stderr) {
stderr = stderr || '';
this.stack.push({
cmd,
resp: (cmd, opt, cb) => (cb ? cb : opt)(new Error(stderr), null, stderr)
});
return this;
}
failedExec(reason) {
return (cmd, opt, cb) => (cb ? cb : opt)(new Error(reason), null, reason)
}
}
module.exports = ExecStub;