renovate/test/exec-util.ts

107 lines
2.5 KiB
TypeScript
Raw Normal View History

import is from '@sindresorhus/is';
import traverse from 'traverse';
import upath from 'upath';
import { rawExec as _exec } from '../lib/util/exec/common';
2022-07-22 07:42:30 +00:00
import type { RawExecOptions } from '../lib/util/exec/types';
import { regEx } from '../lib/util/regex';
2022-07-22 07:42:30 +00:00
import { mockedFunction } from './util';
2022-07-22 07:42:30 +00:00
jest.mock('../lib/util/exec/common');
feat(util/exec): use spawn instead of exec (#16414) * refactor(util): use spawn instead of exec - Use child_process.spawn instead of child_process.exec * refactor(util): use spawn instead of exec - Use child_process.spawn instead of child_process.exec * refactor(util): use spawn instead of exec - Use child_process.spawn instead of child_process.exec * refactor(util): use spawn instead of exec - Use child_process.spawn instead of child_process.exec * refactor(util): use spawn instead of exec - init spawn-util * refactor(util): use spawn instead of exec - spawn-util * refactor(util): use spawn instead of exec - init index-spawn.spec.ts * refactor(util): use spawn instead of exec - fixed various tests * refactor(util): use spawn instead of exec - fix all artifacts.spec.ts * refactor(util): use spawn instead of exec - fix all artifacts.spec.ts * refactor(util): use spawn instead of exec - fix npm post update imports * refactor(util): use spawn instead of exec - revert renaming to minimize PR diff * refactor(util): use spawn instead of exec - revert renaming to minimize PR diff * refactor(util): use spawn instead of exec - revert renaming to minimize PR diff * refactor(util): use spawn instead of exec - revert renaming to minimize PR diff * refactor(util): use spawn instead of exec - revert renaming to minimize PR diff - destroy stdio when terminating child process * refactor(util): use spawn instead of exec - delete and revert dev related changes * refactor(util): use spawn instead of exec - fix support for windows * refactor(util): use spawn instead of exec - handle SIGSTOP and such - add test coverage * refactor(util): use spawn instead of exec - now converts to strings when resolving/rejecting * refactor(util): use spawn instead of exec - logs improvements - force shell (exec like) - fix tests * refactor(util): use spawn instead of exec - strongly type listeners * refactor(util): use spawn instead of exec - create helper mock for spawn * refactor(util): use spawn instead of exec - cr changes * Update lib/util/exec/common.ts Co-authored-by: Sergei Zharinov <zharinov@users.noreply.github.com> * refactor(util): use spawn instead of exec - documentation * refactor(util): use spawn instead of exec - revert unnecessary formatting * refactor(util): use spawn instead of exec * refactor(util): use spawn instead of exec - added ExecError class * refactor(util): use spawn instead of exec - exec-error.ts restructure * refactor(util): use spawn instead of exec * Apply suggestions from code review Co-authored-by: Sergei Zharinov <zharinov@users.noreply.github.com> * refactor(util): use spawn instead of exec * refactor(util): use spawn instead of exec * refactor(util): use spawn instead of exec - deprecated RawExecOptions.encoding property * refactor(util): use spawn instead of exec * refactor(util): use spawn instead of exec * refactor(util): use spawn instead of exec Co-authored-by: Sergei Zharinov <zharinov@users.noreply.github.com>
2022-07-22 15:43:28 +00:00
// TODO: rename #16653
export type ExecResult = { stdout: string; stderr: string } | Error;
2022-07-22 07:42:30 +00:00
export const exec = mockedFunction(_exec);
2022-07-22 07:42:30 +00:00
export interface ExecSnapshot {
cmd: string;
2022-07-22 07:42:30 +00:00
options?: RawExecOptions | null | undefined;
}
feat(util/exec): use spawn instead of exec (#16414) * refactor(util): use spawn instead of exec - Use child_process.spawn instead of child_process.exec * refactor(util): use spawn instead of exec - Use child_process.spawn instead of child_process.exec * refactor(util): use spawn instead of exec - Use child_process.spawn instead of child_process.exec * refactor(util): use spawn instead of exec - Use child_process.spawn instead of child_process.exec * refactor(util): use spawn instead of exec - init spawn-util * refactor(util): use spawn instead of exec - spawn-util * refactor(util): use spawn instead of exec - init index-spawn.spec.ts * refactor(util): use spawn instead of exec - fixed various tests * refactor(util): use spawn instead of exec - fix all artifacts.spec.ts * refactor(util): use spawn instead of exec - fix all artifacts.spec.ts * refactor(util): use spawn instead of exec - fix npm post update imports * refactor(util): use spawn instead of exec - revert renaming to minimize PR diff * refactor(util): use spawn instead of exec - revert renaming to minimize PR diff * refactor(util): use spawn instead of exec - revert renaming to minimize PR diff * refactor(util): use spawn instead of exec - revert renaming to minimize PR diff * refactor(util): use spawn instead of exec - revert renaming to minimize PR diff - destroy stdio when terminating child process * refactor(util): use spawn instead of exec - delete and revert dev related changes * refactor(util): use spawn instead of exec - fix support for windows * refactor(util): use spawn instead of exec - handle SIGSTOP and such - add test coverage * refactor(util): use spawn instead of exec - now converts to strings when resolving/rejecting * refactor(util): use spawn instead of exec - logs improvements - force shell (exec like) - fix tests * refactor(util): use spawn instead of exec - strongly type listeners * refactor(util): use spawn instead of exec - create helper mock for spawn * refactor(util): use spawn instead of exec - cr changes * Update lib/util/exec/common.ts Co-authored-by: Sergei Zharinov <zharinov@users.noreply.github.com> * refactor(util): use spawn instead of exec - documentation * refactor(util): use spawn instead of exec - revert unnecessary formatting * refactor(util): use spawn instead of exec * refactor(util): use spawn instead of exec - added ExecError class * refactor(util): use spawn instead of exec - exec-error.ts restructure * refactor(util): use spawn instead of exec * Apply suggestions from code review Co-authored-by: Sergei Zharinov <zharinov@users.noreply.github.com> * refactor(util): use spawn instead of exec * refactor(util): use spawn instead of exec * refactor(util): use spawn instead of exec - deprecated RawExecOptions.encoding property * refactor(util): use spawn instead of exec * refactor(util): use spawn instead of exec * refactor(util): use spawn instead of exec Co-authored-by: Sergei Zharinov <zharinov@users.noreply.github.com>
2022-07-22 15:43:28 +00:00
// TODO: rename #16653
export type ExecSnapshots = ExecSnapshot[];
feat(util/exec): use spawn instead of exec (#16414) * refactor(util): use spawn instead of exec - Use child_process.spawn instead of child_process.exec * refactor(util): use spawn instead of exec - Use child_process.spawn instead of child_process.exec * refactor(util): use spawn instead of exec - Use child_process.spawn instead of child_process.exec * refactor(util): use spawn instead of exec - Use child_process.spawn instead of child_process.exec * refactor(util): use spawn instead of exec - init spawn-util * refactor(util): use spawn instead of exec - spawn-util * refactor(util): use spawn instead of exec - init index-spawn.spec.ts * refactor(util): use spawn instead of exec - fixed various tests * refactor(util): use spawn instead of exec - fix all artifacts.spec.ts * refactor(util): use spawn instead of exec - fix all artifacts.spec.ts * refactor(util): use spawn instead of exec - fix npm post update imports * refactor(util): use spawn instead of exec - revert renaming to minimize PR diff * refactor(util): use spawn instead of exec - revert renaming to minimize PR diff * refactor(util): use spawn instead of exec - revert renaming to minimize PR diff * refactor(util): use spawn instead of exec - revert renaming to minimize PR diff * refactor(util): use spawn instead of exec - revert renaming to minimize PR diff - destroy stdio when terminating child process * refactor(util): use spawn instead of exec - delete and revert dev related changes * refactor(util): use spawn instead of exec - fix support for windows * refactor(util): use spawn instead of exec - handle SIGSTOP and such - add test coverage * refactor(util): use spawn instead of exec - now converts to strings when resolving/rejecting * refactor(util): use spawn instead of exec - logs improvements - force shell (exec like) - fix tests * refactor(util): use spawn instead of exec - strongly type listeners * refactor(util): use spawn instead of exec - create helper mock for spawn * refactor(util): use spawn instead of exec - cr changes * Update lib/util/exec/common.ts Co-authored-by: Sergei Zharinov <zharinov@users.noreply.github.com> * refactor(util): use spawn instead of exec - documentation * refactor(util): use spawn instead of exec - revert unnecessary formatting * refactor(util): use spawn instead of exec * refactor(util): use spawn instead of exec - added ExecError class * refactor(util): use spawn instead of exec - exec-error.ts restructure * refactor(util): use spawn instead of exec * Apply suggestions from code review Co-authored-by: Sergei Zharinov <zharinov@users.noreply.github.com> * refactor(util): use spawn instead of exec * refactor(util): use spawn instead of exec * refactor(util): use spawn instead of exec - deprecated RawExecOptions.encoding property * refactor(util): use spawn instead of exec * refactor(util): use spawn instead of exec * refactor(util): use spawn instead of exec Co-authored-by: Sergei Zharinov <zharinov@users.noreply.github.com>
2022-07-22 15:43:28 +00:00
// TODO: rename #16653
2022-07-22 07:42:30 +00:00
function execSnapshot(cmd: string, options?: RawExecOptions): ExecSnapshot {
const snapshot = {
cmd,
options,
};
const cwd = upath.toUnix(process.cwd());
return traverse(snapshot).map(function fixup(v) {
if (is.string(v)) {
const val = v
.replace(regEx(/\\(\w)/g), '/$1')
.replace(cwd, '/root/project');
this.update(val);
}
});
}
const defaultExecResult = { stdout: '', stderr: '' };
feat(util/exec): use spawn instead of exec (#16414) * refactor(util): use spawn instead of exec - Use child_process.spawn instead of child_process.exec * refactor(util): use spawn instead of exec - Use child_process.spawn instead of child_process.exec * refactor(util): use spawn instead of exec - Use child_process.spawn instead of child_process.exec * refactor(util): use spawn instead of exec - Use child_process.spawn instead of child_process.exec * refactor(util): use spawn instead of exec - init spawn-util * refactor(util): use spawn instead of exec - spawn-util * refactor(util): use spawn instead of exec - init index-spawn.spec.ts * refactor(util): use spawn instead of exec - fixed various tests * refactor(util): use spawn instead of exec - fix all artifacts.spec.ts * refactor(util): use spawn instead of exec - fix all artifacts.spec.ts * refactor(util): use spawn instead of exec - fix npm post update imports * refactor(util): use spawn instead of exec - revert renaming to minimize PR diff * refactor(util): use spawn instead of exec - revert renaming to minimize PR diff * refactor(util): use spawn instead of exec - revert renaming to minimize PR diff * refactor(util): use spawn instead of exec - revert renaming to minimize PR diff * refactor(util): use spawn instead of exec - revert renaming to minimize PR diff - destroy stdio when terminating child process * refactor(util): use spawn instead of exec - delete and revert dev related changes * refactor(util): use spawn instead of exec - fix support for windows * refactor(util): use spawn instead of exec - handle SIGSTOP and such - add test coverage * refactor(util): use spawn instead of exec - now converts to strings when resolving/rejecting * refactor(util): use spawn instead of exec - logs improvements - force shell (exec like) - fix tests * refactor(util): use spawn instead of exec - strongly type listeners * refactor(util): use spawn instead of exec - create helper mock for spawn * refactor(util): use spawn instead of exec - cr changes * Update lib/util/exec/common.ts Co-authored-by: Sergei Zharinov <zharinov@users.noreply.github.com> * refactor(util): use spawn instead of exec - documentation * refactor(util): use spawn instead of exec - revert unnecessary formatting * refactor(util): use spawn instead of exec * refactor(util): use spawn instead of exec - added ExecError class * refactor(util): use spawn instead of exec - exec-error.ts restructure * refactor(util): use spawn instead of exec * Apply suggestions from code review Co-authored-by: Sergei Zharinov <zharinov@users.noreply.github.com> * refactor(util): use spawn instead of exec * refactor(util): use spawn instead of exec * refactor(util): use spawn instead of exec - deprecated RawExecOptions.encoding property * refactor(util): use spawn instead of exec * refactor(util): use spawn instead of exec * refactor(util): use spawn instead of exec Co-authored-by: Sergei Zharinov <zharinov@users.noreply.github.com>
2022-07-22 15:43:28 +00:00
// TODO: rename #16653
export function mockExecAll(
execResult: ExecResult = defaultExecResult
): ExecSnapshots {
const snapshots: ExecSnapshots = [];
2022-07-22 07:42:30 +00:00
exec.mockImplementation((cmd, options) => {
snapshots.push(execSnapshot(cmd, options));
if (execResult instanceof Error) {
throw execResult;
}
return execResult as never;
});
return snapshots;
}
feat(util/exec): use spawn instead of exec (#16414) * refactor(util): use spawn instead of exec - Use child_process.spawn instead of child_process.exec * refactor(util): use spawn instead of exec - Use child_process.spawn instead of child_process.exec * refactor(util): use spawn instead of exec - Use child_process.spawn instead of child_process.exec * refactor(util): use spawn instead of exec - Use child_process.spawn instead of child_process.exec * refactor(util): use spawn instead of exec - init spawn-util * refactor(util): use spawn instead of exec - spawn-util * refactor(util): use spawn instead of exec - init index-spawn.spec.ts * refactor(util): use spawn instead of exec - fixed various tests * refactor(util): use spawn instead of exec - fix all artifacts.spec.ts * refactor(util): use spawn instead of exec - fix all artifacts.spec.ts * refactor(util): use spawn instead of exec - fix npm post update imports * refactor(util): use spawn instead of exec - revert renaming to minimize PR diff * refactor(util): use spawn instead of exec - revert renaming to minimize PR diff * refactor(util): use spawn instead of exec - revert renaming to minimize PR diff * refactor(util): use spawn instead of exec - revert renaming to minimize PR diff * refactor(util): use spawn instead of exec - revert renaming to minimize PR diff - destroy stdio when terminating child process * refactor(util): use spawn instead of exec - delete and revert dev related changes * refactor(util): use spawn instead of exec - fix support for windows * refactor(util): use spawn instead of exec - handle SIGSTOP and such - add test coverage * refactor(util): use spawn instead of exec - now converts to strings when resolving/rejecting * refactor(util): use spawn instead of exec - logs improvements - force shell (exec like) - fix tests * refactor(util): use spawn instead of exec - strongly type listeners * refactor(util): use spawn instead of exec - create helper mock for spawn * refactor(util): use spawn instead of exec - cr changes * Update lib/util/exec/common.ts Co-authored-by: Sergei Zharinov <zharinov@users.noreply.github.com> * refactor(util): use spawn instead of exec - documentation * refactor(util): use spawn instead of exec - revert unnecessary formatting * refactor(util): use spawn instead of exec * refactor(util): use spawn instead of exec - added ExecError class * refactor(util): use spawn instead of exec - exec-error.ts restructure * refactor(util): use spawn instead of exec * Apply suggestions from code review Co-authored-by: Sergei Zharinov <zharinov@users.noreply.github.com> * refactor(util): use spawn instead of exec * refactor(util): use spawn instead of exec * refactor(util): use spawn instead of exec - deprecated RawExecOptions.encoding property * refactor(util): use spawn instead of exec * refactor(util): use spawn instead of exec * refactor(util): use spawn instead of exec Co-authored-by: Sergei Zharinov <zharinov@users.noreply.github.com>
2022-07-22 15:43:28 +00:00
// TODO: rename #16653
2022-07-22 07:42:30 +00:00
export function mockExecSequence(execResults: ExecResult[]): ExecSnapshots {
const snapshots: ExecSnapshots = [];
execResults.forEach((execResult) => {
2022-07-22 07:42:30 +00:00
exec.mockImplementationOnce((cmd, options) => {
snapshots.push(execSnapshot(cmd, options));
if (execResult instanceof Error) {
throw execResult;
}
return execResult as never;
});
});
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,
};
// reset exec mock, otherwise there can be some left over from previous test
beforeEach(() => {
// maybe not mocked
exec.mockReset?.();
});