2020-05-15 05:30:36 -06:00
|
|
|
import is from '@sindresorhus/is';
|
2024-08-05 03:49:05 -06:00
|
|
|
import traverse from 'neotraverse/legacy';
|
2021-12-22 01:37:47 -07:00
|
|
|
import upath from 'upath';
|
2022-07-11 03:41:24 -06:00
|
|
|
import { rawExec as _exec } from '../lib/util/exec/common';
|
2022-07-22 01:42:30 -06:00
|
|
|
import type { RawExecOptions } from '../lib/util/exec/types';
|
2021-10-27 08:37:11 -06:00
|
|
|
import { regEx } from '../lib/util/regex';
|
2022-07-22 01:42:30 -06:00
|
|
|
import { mockedFunction } from './util';
|
2020-01-02 08:30:40 -07:00
|
|
|
|
2022-07-22 01:42:30 -06:00
|
|
|
jest.mock('../lib/util/exec/common');
|
2020-01-02 08:30:40 -07:00
|
|
|
|
|
|
|
export type ExecResult = { stdout: string; stderr: string } | Error;
|
|
|
|
|
2022-07-22 01:42:30 -06:00
|
|
|
export const exec = mockedFunction(_exec);
|
2020-07-18 02:03:05 -06:00
|
|
|
|
2022-07-22 01:42:30 -06:00
|
|
|
export interface ExecSnapshot {
|
2020-01-02 08:30:40 -07:00
|
|
|
cmd: string;
|
2022-07-22 01:42:30 -06:00
|
|
|
options?: RawExecOptions | null | undefined;
|
2020-01-02 08:30:40 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
export type ExecSnapshots = ExecSnapshot[];
|
|
|
|
|
2022-07-22 01:42:30 -06:00
|
|
|
function execSnapshot(cmd: string, options?: RawExecOptions): ExecSnapshot {
|
2020-01-02 08:30:40 -07:00
|
|
|
const snapshot = {
|
|
|
|
cmd,
|
|
|
|
options,
|
|
|
|
};
|
|
|
|
|
2021-12-22 01:37:47 -07:00
|
|
|
const cwd = upath.toUnix(process.cwd());
|
2020-01-02 08:30:40 -07:00
|
|
|
|
2020-05-15 05:30:36 -06:00
|
|
|
return traverse(snapshot).map(function fixup(v) {
|
|
|
|
if (is.string(v)) {
|
2021-10-27 08:37:11 -06:00
|
|
|
const val = v
|
|
|
|
.replace(regEx(/\\(\w)/g), '/$1')
|
2024-02-29 08:04:58 -07:00
|
|
|
.replace(regEx(/^[A-Z]:\//), '/') // replace windows paths
|
|
|
|
.replace(regEx(/"[A-Z]:\//g), '"/') // replace windows paths
|
2021-12-28 23:26:13 -07:00
|
|
|
.replace(cwd, '/root/project');
|
2020-05-15 05:30:36 -06:00
|
|
|
this.update(val);
|
|
|
|
}
|
|
|
|
});
|
2020-01-02 08:30:40 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
const defaultExecResult = { stdout: '', stderr: '' };
|
|
|
|
|
|
|
|
export function mockExecAll(
|
2023-11-07 08:50:29 -07:00
|
|
|
execResult: ExecResult = defaultExecResult,
|
2020-01-02 08:30:40 -07:00
|
|
|
): ExecSnapshots {
|
2020-05-07 02:23:45 -06:00
|
|
|
const snapshots: ExecSnapshots = [];
|
2022-07-22 01:42:30 -06:00
|
|
|
exec.mockImplementation((cmd, options) => {
|
2020-01-02 08:30:40 -07:00
|
|
|
snapshots.push(execSnapshot(cmd, options));
|
|
|
|
if (execResult instanceof Error) {
|
|
|
|
throw execResult;
|
|
|
|
}
|
2022-07-11 03:41:24 -06:00
|
|
|
return execResult as never;
|
2020-01-02 08:30:40 -07:00
|
|
|
});
|
|
|
|
return snapshots;
|
|
|
|
}
|
|
|
|
|
2022-07-22 01:42:30 -06:00
|
|
|
export function mockExecSequence(execResults: ExecResult[]): ExecSnapshots {
|
2020-05-07 02:23:45 -06:00
|
|
|
const snapshots: ExecSnapshots = [];
|
2020-04-12 10:09:36 -06:00
|
|
|
execResults.forEach((execResult) => {
|
2022-07-22 01:42:30 -06:00
|
|
|
exec.mockImplementationOnce((cmd, options) => {
|
2020-01-02 08:30:40 -07:00
|
|
|
snapshots.push(execSnapshot(cmd, options));
|
|
|
|
if (execResult instanceof Error) {
|
|
|
|
throw execResult;
|
|
|
|
}
|
2022-07-11 03:41:24 -06:00
|
|
|
return execResult as never;
|
2020-01-02 08:30:40 -07:00
|
|
|
});
|
|
|
|
});
|
|
|
|
return snapshots;
|
|
|
|
}
|
2020-01-10 07:18:41 -07:00
|
|
|
|
|
|
|
const basicEnvMock = {
|
|
|
|
HTTP_PROXY: 'http://example.com',
|
|
|
|
HTTPS_PROXY: 'https://example.com',
|
|
|
|
NO_PROXY: 'localhost',
|
|
|
|
HOME: '/home/user',
|
|
|
|
PATH: '/tmp/path',
|
2020-01-28 22:29:53 -07:00
|
|
|
LANG: 'en_US.UTF-8',
|
|
|
|
LC_ALL: 'en_US',
|
2020-01-10 07:18:41 -07:00
|
|
|
};
|
|
|
|
|
|
|
|
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,
|
|
|
|
};
|
2022-05-04 04:28:37 -06:00
|
|
|
|
|
|
|
// reset exec mock, otherwise there can be some left over from previous test
|
|
|
|
beforeEach(() => {
|
|
|
|
// maybe not mocked
|
|
|
|
exec.mockReset?.();
|
|
|
|
});
|