Files
renovate/test/exec-util.ts
Michael Kriese 634d3b6084 refactor: use modern neotraverse API (#45809)
* build(deps): update dependency neotraverse to v1

| datasource | package     | from   | to    |
| ---------- | ----------- | ------ | ----- |
| npm        | neotraverse | 0.6.18 | 1.0.1 |

* refactor: use modern neotraverse API

---------

Co-authored-by: renovate[bot] <29139614+renovate[bot]@users.noreply.github.com>
2026-09-09 13:15:15 +00:00

102 lines
2.4 KiB
TypeScript

import is from '@sindresorhus/is';
import { map } from 'neotraverse';
import upath from 'upath';
import { rawExec as _exec } from '../lib/util/exec/common.ts';
import type {
CommandWithOptions,
RawExecOptions,
} from '../lib/util/exec/types.ts';
import { regEx } from '../lib/util/regex.ts';
export type ExecResult = { stdout: string; stderr: string } | Error;
export const exec = vi.mocked(_exec);
export interface ExecSnapshot {
cmd: string;
options?: RawExecOptions | null | undefined;
}
export type ExecSnapshots = ExecSnapshot[];
function execSnapshot(
cmd: string | CommandWithOptions,
options?: RawExecOptions,
): ExecSnapshot {
const snapshot = {
cmd,
options,
};
const cwd = upath.toUnix(process.cwd());
return map(snapshot, (ctx, val) => {
if (is.string(val)) {
const newVal = val
.replace(regEx(/\\(?<char>\w)/g), '/$<char>')
.replace(regEx(/^[A-Z]:\//), '/') // replace windows paths
.replace(regEx(/"[A-Z]:\//g), '"/') // replace windows paths
.replace(cwd, '/root/project');
ctx.update(newVal);
}
});
}
const defaultExecResult = { stdout: '', stderr: '' };
export function mockExecAll(
execResult: ExecResult = defaultExecResult,
): ExecSnapshots {
const snapshots: ExecSnapshots = [];
exec.mockImplementation((cmd, options) => {
snapshots.push(execSnapshot(cmd, options));
if (execResult instanceof Error) {
throw execResult;
}
return execResult as never;
});
return snapshots;
}
export function mockExecSequence(execResults: ExecResult[]): ExecSnapshots {
const snapshots: ExecSnapshots = [];
execResults.forEach((execResult) => {
exec.mockImplementationOnce((cmd, options) => {
snapshots.push(execSnapshot(cmd, options));
if (execResult instanceof Error) {
throw execResult;
}
return execResult as never;
});
});
return snapshots;
}
const basicEnvMock = {
CI: 'true',
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,
};