Files
renovate/tools/lint/rules/prefer-stub-env.ts
Sebastian Poxhofer fbedd41e82 test: replace env operation with vi.stubEnv() (#45490)
* test: replace manual process.env mutation with vi.stubEnv

Enable Vitest's `unstubEnvs` so env stubs are reverted automatically
before every test, and convert the manual `process.env.X = ...` /
`delete process.env.X` pairs in specs to `vi.stubEnv()`.

This removes the hand-written `afterEach` cleanup blocks and the
`const OLD_ENV = process.env` / `process.env = { ...OLD_ENV }`
save-restore dance, which silently leaked state between tests when a
test set a variable without a matching delete.

Left as-is (incompatible with `unstubEnvs`, which unstubs before each
test, not after):
- module top-level env writes (cocoapods, gomod, post-update, pip-compile
  and bundler artifacts specs)
- `beforeAll` env writes -- the two in `util/http` moved to `beforeEach`

* test: stop asserting on ambient environment variables

Three specs asserted on values that the surrounding environment can
override, so they only passed on a machine that happened not to set them:

- `util/exec/env`: `getChildProcessEnv()` forwards every entry of
  `basicEnvVars`, but the spec only seeded a subset. Any other forwarded
  variable that was set ambiently (e.g. a lowercase `http_proxy`) leaked
  into the `toEqual` assertion. Clear all of them first.
- `util/git/index`: `process.env.GIT_SSH_COMMAND` deliberately overrides
  the default that the two env-inheritance tests assert on. Clear it.
- `logger/pretty-stdout`: the colorized level strings are built at import
  time, so the `beforeEach` setting `FORCE_COLOR` could never affect them
  -- it was dead code, and the test silently assumed a terminal without
  colour support. Derive the expectation the same way the module does.

* test(datasource/nuget): use a throwaway cache dir for nupkg downloads

The `determine source URL from nupkg` tests do not mock the filesystem,
so they really wrote downloaded packages into a fixed `/tmp/cache` path.
That fails wherever the system tmpdir is not writable, and two checkouts
running the suite at once would share the same directory.

Use `tmp.dir({ unsafeCleanup: true })` and clean it up afterwards, which
is the pattern the other specs that touch disk already use.

* feat(lint): add renovate/prefer-stub-env rule

Enforces in specs what the migration to `vi.stubEnv` established:

- assigning to or deleting `process.env.X` inside `beforeEach`, `afterEach`,
  `it` or `test` is reported, with a fixer that rewrites it to `vi.stubEnv()`
- module scope, `describe` bodies and `beforeAll` / `afterAll` are left alone:
  `unstubEnvs` reverts stubs *before* each test, so a stub created there is
  gone by the time the first test runs, and those must keep assigning directly
- replacing the `process.env` object is reported everywhere and never fixed.
  `vi.stubEnv()` captures the original object when the worker starts, so after
  a reassignment its deletes silently target the old object

The rule found 28 further violations in specs the earlier sweep missed, since
that one was anchored on `delete process.env`. Twelve were auto-fixed; the
rest replaced `process.env` wholesale and are now `clearEnv()` from test/util.

`util/exec/index.spec.ts` opts out with a comment: it asserts on the whole
child env, and `vi.stubEnv()` cannot express that because it maps `PROD`,
`DEV` and `SSR` onto '' instead of deleting them.
2026-08-28 09:26:20 +00:00

175 lines
5.9 KiB
TypeScript

import type { ESTree } from '@oxlint/plugins';
import { defineRule } from '@oxlint/plugins';
/**
* Names of the vitest suite functions that establish the innermost calling
* context. Walking up from a `process.env` mutation, the first of these found
* decides whether the mutation happens per-test (where `vi.stubEnv` works) or
* once per file (where it does not).
*/
const CONTEXT_NAMES = new Set([
'beforeEach',
'afterEach',
'beforeAll',
'afterAll',
'it',
'test',
'describe',
]);
/**
* Contexts that run per test. `unstubEnvs` reverts stubs *before* each test,
* so only stubs created from here survive into the test that needs them.
*
* Module scope, `describe` bodies and `beforeAll` / `afterAll` are absent on
* purpose: a `vi.stubEnv()` call there is undone before the first test runs,
* so those have to keep assigning to `process.env` directly.
*/
const PER_TEST_CONTEXTS = new Set(['beforeEach', 'afterEach', 'it', 'test']);
/**
* Resolve the name a suite function is called by, so that `it.each(...)(...)`
* and `it.only(...)` are recognised the same way as a plain `it(...)`.
*/
function getCalleeName(callee: ESTree.Node): string | null {
switch (callee.type) {
case 'Identifier':
return callee.name;
// `it.only`, `test.each`, ...
case 'MemberExpression':
return callee.object.type === 'Identifier' ? callee.object.name : null;
// `it.each([...])(...)` calls the result of `it.each([...])`
case 'CallExpression':
return getCalleeName(callee.callee);
default:
return null;
}
}
/** Find the name of the innermost enclosing suite function call. */
function getEnclosingContextName(node: ESTree.Node): string | null {
for (
let current: ESTree.Node | null = node.parent;
current;
current = current.parent
) {
if (current.type === 'CallExpression') {
const name = getCalleeName(current.callee);
if (name !== null && CONTEXT_NAMES.has(name)) {
return name;
}
}
}
return null;
}
/** Whether `node` is the `process.env` member expression. */
function isProcessEnv(node: ESTree.Node): boolean {
return (
node.type === 'MemberExpression' &&
!node.computed &&
node.object.type === 'Identifier' &&
node.object.name === 'process' &&
node.property.type === 'Identifier' &&
node.property.name === 'env'
);
}
/** Whether `node` reads a single variable off `process.env`. */
function isProcessEnvMember(
node: ESTree.Node,
): node is ESTree.MemberExpression {
return node.type === 'MemberExpression' && isProcessEnv(node.object);
}
export default defineRule({
meta: {
type: 'suggestion',
fixable: 'code',
messages: {
preferStubEnv:
'Use `vi.stubEnv({{key}}, ...)` instead of assigning to `process.env` inside `{{context}}`. `unstubEnvs` is enabled in vitest.config.mts, so stubs are reverted before every test and need no manual cleanup.',
preferStubEnvDelete:
'Use `vi.stubEnv({{key}}, undefined)` instead of `delete process.env` inside `{{context}}`. `unstubEnvs` is enabled in vitest.config.mts, so stubs are reverted before every test and need no manual cleanup.',
noProcessEnvReassign:
'Do not replace the `process.env` object. `vi.stubEnv()` captures the original object when the worker starts, so after a reassignment its deletes silently target the old object and leave the variable set. Stub the individual variables instead.',
},
},
createOnce(context) {
/**
* Source text of the key to pass to `vi.stubEnv`: the literal property
* name quoted, or the computed expression verbatim.
*/
function keyText(member: ESTree.MemberExpression): string {
if (member.computed) {
return context.sourceCode.getText(member.property);
}
return member.property.type === 'Identifier'
? `'${member.property.name}'`
: context.sourceCode.getText(member.property);
}
return {
AssignmentExpression(node) {
// `process.env = { ...OLD_ENV }` breaks `vi.stubEnv` outright, so it is
// reported wherever it appears, not just inside a per-test context.
if (isProcessEnv(node.left)) {
context.report({ node, messageId: 'noProcessEnvReassign' });
return;
}
if (!isProcessEnvMember(node.left)) {
return;
}
const contextName = getEnclosingContextName(node);
if (contextName === null || !PER_TEST_CONTEXTS.has(contextName)) {
return;
}
const key = keyText(node.left);
context.report({
node,
messageId: 'preferStubEnv',
data: { key, context: contextName },
// Only a plain `=` maps onto a stub, and only as a statement: as an
// expression the assignment yields the assigned value whereas
// `vi.stubEnv()` yields the `vi` utils object.
fix:
node.operator === '=' && node.parent.type === 'ExpressionStatement'
? (fixer) =>
fixer.replaceText(
node,
`vi.stubEnv(${key}, ${context.sourceCode.getText(node.right)})`,
)
: undefined,
});
},
UnaryExpression(node) {
if (node.operator !== 'delete' || !isProcessEnvMember(node.argument)) {
return;
}
const contextName = getEnclosingContextName(node);
if (contextName === null || !PER_TEST_CONTEXTS.has(contextName)) {
return;
}
const key = keyText(node.argument);
context.report({
node,
messageId: 'preferStubEnvDelete',
data: { key, context: contextName },
// `delete` yields a boolean, so only replace it as a statement.
fix:
node.parent.type === 'ExpressionStatement'
? (fixer) =>
fixer.replaceText(node, `vi.stubEnv(${key}, undefined)`)
: undefined,
});
},
};
},
});