Files
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

157 lines
4.4 KiB
TypeScript

import { GlobalConfig } from '../../config/global.ts';
import {
basicEnvVars,
getChildProcessEnv,
hardcodedProcessEnv,
} from './env.ts';
describe('util/exec/env', () => {
const envVars = [
'CI',
'HTTP_PROXY',
'HTTPS_PROXY',
'NO_PROXY',
'HOME',
'PATH',
'LC_ALL',
'LANG',
'DOCKER_HOST',
'GIT_SSL_CAPATH',
'GIT_SSL_CAINFO',
'SSL_CERT_FILE',
'URL_REPLACE_1_FROM',
'URL_REPLACE_1_TO',
'PROGRAMFILES',
'PROGRAMFILES(X86)',
'APPDATA',
'LOCALAPPDATA',
];
beforeEach(() => {
// Clear any ambient value first, so that a forwarded variable which is set
// in the surrounding environment cannot leak into the assertions below.
basicEnvVars.forEach((env) => {
vi.stubEnv(env, undefined);
});
envVars.forEach((env) => {
vi.stubEnv(env, env);
});
});
it('returns default environment variables', () => {
expect(getChildProcessEnv()).toEqual({
DOCKER_HOST: 'DOCKER_HOST',
GIT_SSL_CAPATH: 'GIT_SSL_CAPATH',
GIT_SSL_CAINFO: 'GIT_SSL_CAINFO',
HOME: 'HOME',
HTTPS_PROXY: 'HTTPS_PROXY',
HTTP_PROXY: 'HTTP_PROXY',
LANG: 'LANG',
LC_ALL: 'LC_ALL',
NO_PROXY: 'NO_PROXY',
PATH: 'PATH',
SSL_CERT_FILE: 'SSL_CERT_FILE',
URL_REPLACE_1_FROM: 'URL_REPLACE_1_FROM',
URL_REPLACE_1_TO: 'URL_REPLACE_1_TO',
PROGRAMFILES: 'PROGRAMFILES',
'PROGRAMFILES(X86)': 'PROGRAMFILES(X86)',
APPDATA: 'APPDATA',
LOCALAPPDATA: 'LOCALAPPDATA',
CI: 'true',
});
});
it('always sets static values for CI', () => {
expect(getChildProcessEnv()).toMatchObject({
CI: 'true',
});
});
it('static environment variables override the process environment variables', () => {
vi.stubEnv('CI', 'false');
expect(getChildProcessEnv()).toMatchObject({
CI: 'true',
});
vi.stubEnv('CI', undefined);
});
it('returns environment variable only if defined', () => {
vi.stubEnv('PATH', undefined);
expect(getChildProcessEnv()).not.toHaveProperty('PATH');
});
it('returns custom environment variables if passed and defined', () => {
vi.stubEnv('FOOBAR', 'FOOBAR');
expect(getChildProcessEnv(['FOOBAR'])).toMatchObject({
DOCKER_HOST: 'DOCKER_HOST',
FOOBAR: 'FOOBAR',
HOME: 'HOME',
HTTPS_PROXY: 'HTTPS_PROXY',
HTTP_PROXY: 'HTTP_PROXY',
LANG: 'LANG',
LC_ALL: 'LC_ALL',
NO_PROXY: 'NO_PROXY',
PATH: 'PATH',
});
vi.stubEnv('LANG', undefined);
});
describe('getChildProcessEnv when exposeAllEnv=true', () => {
it('returns process.env if exposeAllEnv=true', () => {
GlobalConfig.set({ exposeAllEnv: true });
expect(getChildProcessEnv()).toMatchObject({
...process.env,
CI: 'true',
});
});
it('static environment variables override the process environment variables', () => {
GlobalConfig.set({ exposeAllEnv: true });
vi.stubEnv('CI', 'false');
expect(getChildProcessEnv()).toMatchObject({
CI: 'true',
});
});
});
describe('basicEnvVars and hardcodedProcessEnv should not have any overlap', () => {
describe('basicEnvVars does not include any environment variables in hardcodedProcessEnv', () => {
for (const env of Object.keys(hardcodedProcessEnv)) {
it(`${env} is not in basicEnvVars`, () => {
expect(basicEnvVars).not.toContain(env);
});
}
});
describe('hardcodedProcessEnv does not include any environment variables in basicEnvVars', () => {
for (const env of basicEnvVars) {
it(`${env} is not in hardcodedProcessEnv`, () => {
expect(hardcodedProcessEnv).not.toContainKey(env);
});
}
});
});
describe('basicEnvVars and hardcodedProcessEnv should not have any overlap', () => {
describe('basicEnvVars does not include any environment variables in hardcodedProcessEnv', () => {
for (const env of Object.keys(hardcodedProcessEnv)) {
it(`${env} is not in basicEnvVars`, () => {
expect(basicEnvVars).not.toContain(env);
});
}
});
describe('hardcodedProcessEnv does not include any environment variables in basicEnvVars', () => {
for (const env of basicEnvVars) {
it(`${env} is not in hardcodedProcessEnv`, () => {
expect(hardcodedProcessEnv).not.toContainKey(env);
});
}
});
});
});