mirror of
https://github.com/renovatebot/renovate.git
synced 2026-10-26 07:20:39 +00:00
7436b80cda
ci(linter): add renovate/no-redundant-mock-reset rule Vitest sets mockReset: true globally in vitest.config.mts, which resets all mocks before each test. This makes vi.resetAllMocks() / vi.clearAllMocks() and per-mock .mockReset() / .mockClear() calls inside beforeEach/afterEach hooks redundant. Add a custom oxlint rule (enabled for **/*.spec.ts and test/**) flagging such calls, and remove the 40 redundant calls it found. restoreAllMocks/mockRestore are not flagged because restoreMocks is not enabled globally. Two sites keep an inline disable with a reason: nested beforeEach hooks in filter-checks.spec.ts and onboarding/pr/index.spec.ts intentionally call mockReset() to discard once-values queued by an outer beforeEach. Requested by maintainer review feedback (review comment 3052324877). Co-authored-by: Claude Fable 5 <noreply@anthropic.com>
103 lines
2.5 KiB
TypeScript
103 lines
2.5 KiB
TypeScript
import is from '@sindresorhus/is';
|
|
import traverse from 'neotraverse/legacy';
|
|
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());
|
|
|
|
// traverse binds `this.update()` to the callback, requires regular function
|
|
// eslint-disable-next-line prefer-arrow-callback
|
|
return traverse(snapshot).map(function fixup(v) {
|
|
if (is.string(v)) {
|
|
const val = v
|
|
.replace(regEx(/\\(\w)/g), '/$1')
|
|
.replace(regEx(/^[A-Z]:\//), '/') // replace windows paths
|
|
.replace(regEx(/"[A-Z]:\//g), '"/') // replace windows paths
|
|
.replace(cwd, '/root/project');
|
|
this.update(val);
|
|
}
|
|
});
|
|
}
|
|
|
|
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 = {
|
|
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,
|
|
};
|