forked from zowe/zowe-cli
-
Notifications
You must be signed in to change notification settings - Fork 0
/
child_process.ts
61 lines (52 loc) · 1.91 KB
/
child_process.ts
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
/*
* This program and the accompanying materials are made available under the terms of the
* Eclipse Public License v2.0 which accompanies this distribution, and is available at
* https://www.eclipse.org/legal/epl-v20.html
*
* SPDX-License-Identifier: EPL-2.0
*
* Copyright Contributors to the Zowe Project.
*
*/
'use strict';
const child_process: any = jest.genMockFromModule("child_process");
const useRealFunction: string = "useRealFunction";
let execSyncOutput: string = useRealFunction;
const spawnSyncOutput = {
"stdout": useRealFunction
};
// tests can set their own output value for execSync
function setExecSyncOutput(outputVal: string) {
execSyncOutput = outputVal;
}
// tests can set their own output value for spawnSync
function setSpawnSyncOutput(outputVal: string) {
spawnSyncOutput.stdout = outputVal;
}
// Mock function to use for child_process.execSync
function execSyncMock(cmd: string, options: Object) {
if (execSyncOutput === useRealFunction) {
// run the real execSync
const execSyncReal = jest.requireActual("child_process").execSync;
return execSyncReal(cmd, options);
} else {
// return the output that the test set with setExecSyncOutput().
return execSyncOutput;
}
}
// Mock function to use for child_process.spawnSync
function spawnSyncMock(cmd: string, args: [string], options: Object) {
if (spawnSyncOutput.stdout === useRealFunction) {
// run the real spawnSync
const spawnSyncReal = jest.requireActual("child_process").spawnSync;
return spawnSyncReal(cmd, args, options);
} else {
// return the output that the test set with setSpawnSyncOutput().
return spawnSyncOutput;
}
}
child_process.setExecSyncOutput = setExecSyncOutput;
child_process.execSync = execSyncMock;
child_process.setSpawnSyncOutput = setSpawnSyncOutput;
child_process.spawnSync = spawnSyncMock;
module.exports = child_process;