forked from renovatebot/renovate
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathexec-util.ts
99 lines (84 loc) · 2.36 KB
/
exec-util.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
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
import { exec as _exec } from 'child_process';
import is from '@sindresorhus/is';
import traverse from 'traverse';
import { toUnix } from 'upath';
import { ExecOptions } from '../lib/util/exec';
type CallOptions = ExecOptions | null | undefined;
export type ExecResult = { stdout: string; stderr: string } | Error;
export type ExecMock = jest.Mock<typeof _exec>;
export const exec: ExecMock = _exec as any;
interface ExecSnapshot {
cmd: string;
options?: ExecOptions | null | undefined;
}
export type ExecSnapshots = ExecSnapshot[];
export function execSnapshot(cmd: string, options?: CallOptions): ExecSnapshot {
const snapshot = {
cmd,
options,
};
const cwd = toUnix(process.cwd());
// eslint-disable-next-line array-callback-return
return traverse(snapshot).map(function fixup(v) {
if (is.string(v)) {
const val = v.replace(/\\(\w)/g, '/$1').replace(cwd, '/root/project');
this.update(val);
}
});
}
const defaultExecResult = { stdout: '', stderr: '' };
export function mockExecAll(
execFn: ExecMock,
execResult: ExecResult = defaultExecResult
): ExecSnapshots {
const snapshots: ExecSnapshots = [];
execFn.mockImplementation((cmd, options, callback) => {
snapshots.push(execSnapshot(cmd, options));
if (execResult instanceof Error) {
throw execResult;
}
callback(null, execResult);
return undefined;
});
return snapshots;
}
export function mockExecSequence(
execFn: ExecMock,
execResults: ExecResult[]
): ExecSnapshots {
const snapshots: ExecSnapshots = [];
execResults.forEach((execResult) => {
execFn.mockImplementationOnce((cmd, options, callback) => {
snapshots.push(execSnapshot(cmd, options));
if (execResult instanceof Error) {
throw execResult;
}
callback(null, execResult);
return undefined;
});
});
return snapshots;
}
const basicEnvMock = {
HTTP_PROXY: 'http://example.com',
HTTPS_PROXY: 'https://example.com',
NO_PROXY: 'localhost',
HOME: '/home/user',
PATH: '/tmp/path',
LANG: 'en_US.UTF-8',
LC_ALL: 'en_US',
};
const fullEnvMock = {
...basicEnvMock,
SELECTED_ENV_VAR: 'Can be selected',
FILTERED_ENV_VAR: 'Should be filtered',
};
const filteredEnvMock = {
...basicEnvMock,
SELECTED_ENV_VAR: fullEnvMock.SELECTED_ENV_VAR,
};
export const envMock = {
basic: basicEnvMock,
full: fullEnvMock,
filtered: filteredEnvMock,
};