mirror of
https://github.com/renovatebot/renovate.git
synced 2026-10-25 23:11:31 +00:00
fbedd41e82
* 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.
119 lines
3.6 KiB
TypeScript
119 lines
3.6 KiB
TypeScript
import { GlobalConfig } from '../../config/global.ts';
|
|
import { setCustomEnv, setUserEnv } from '../env.ts';
|
|
import { exec as _exec } from '../exec/index.ts';
|
|
import { add, clear } from '../host-rules.ts';
|
|
import { withGitEnvironment } from './exec.ts';
|
|
|
|
vi.mock('../exec/index.ts');
|
|
|
|
const exec = vi.mocked(_exec);
|
|
|
|
describe('util/git/exec', () => {
|
|
beforeEach(() => {
|
|
GlobalConfig.reset();
|
|
setCustomEnv({});
|
|
setUserEnv({});
|
|
clear();
|
|
});
|
|
|
|
it('appends authentication to approved runtime Git configuration', async () => {
|
|
exec.mockResolvedValue({ stdout: '', stderr: '' });
|
|
const gitExec = withGitEnvironment();
|
|
setCustomEnv({
|
|
GIT_CONFIG_COUNT: '1',
|
|
GIT_CONFIG_KEY_0: 'existing-key',
|
|
GIT_CONFIG_VALUE_0: 'existing-value',
|
|
});
|
|
add({
|
|
hostType: 'github',
|
|
matchHost: 'api.github.com',
|
|
token: 'token123',
|
|
});
|
|
|
|
await gitExec('command', {
|
|
extraEnv: { EXTRA_VARIABLE: 'extra' },
|
|
env: { FORCED_VARIABLE: 'forced' },
|
|
docker: {
|
|
envVars: ['EXISTING_DOCKER_VARIABLE', 'GIT_CONFIG_COUNT'],
|
|
},
|
|
});
|
|
|
|
expect(exec).toHaveBeenCalledExactlyOnceWith('command', {
|
|
extraEnv: { EXTRA_VARIABLE: 'extra' },
|
|
env: {
|
|
FORCED_VARIABLE: 'forced',
|
|
GIT_CONFIG_COUNT: '4',
|
|
GIT_CONFIG_KEY_0: 'existing-key',
|
|
GIT_CONFIG_KEY_1: 'url.https://ssh:token123@github.com/.insteadOf',
|
|
GIT_CONFIG_KEY_2: 'url.https://git:token123@github.com/.insteadOf',
|
|
GIT_CONFIG_KEY_3: 'url.https://token123@github.com/.insteadOf',
|
|
GIT_CONFIG_VALUE_0: 'existing-value',
|
|
GIT_CONFIG_VALUE_1: 'ssh://git@github.com/',
|
|
GIT_CONFIG_VALUE_2: 'git@github.com:',
|
|
GIT_CONFIG_VALUE_3: 'https://github.com/',
|
|
},
|
|
docker: {
|
|
envVars: [
|
|
'EXISTING_DOCKER_VARIABLE',
|
|
'GIT_CONFIG_COUNT',
|
|
'GIT_CONFIG_KEY_0',
|
|
'GIT_CONFIG_VALUE_0',
|
|
'GIT_CONFIG_KEY_1',
|
|
'GIT_CONFIG_VALUE_1',
|
|
'GIT_CONFIG_KEY_2',
|
|
'GIT_CONFIG_VALUE_2',
|
|
'GIT_CONFIG_KEY_3',
|
|
'GIT_CONFIG_VALUE_3',
|
|
],
|
|
},
|
|
});
|
|
});
|
|
|
|
it('does not import unapproved runtime Git configuration', async () => {
|
|
exec.mockResolvedValue({ stdout: '', stderr: '' });
|
|
const gitExec = withGitEnvironment();
|
|
vi.stubEnv('GIT_CONFIG_COUNT', '1');
|
|
vi.stubEnv('GIT_CONFIG_KEY_0', 'process-key');
|
|
vi.stubEnv('GIT_CONFIG_VALUE_0', 'process-value');
|
|
add({
|
|
hostType: 'github',
|
|
matchHost: 'api.github.com',
|
|
token: 'token123',
|
|
});
|
|
|
|
await gitExec('command');
|
|
|
|
expect(exec).toHaveBeenCalledExactlyOnceWith('command', {
|
|
env: {
|
|
GIT_CONFIG_COUNT: '3',
|
|
GIT_CONFIG_KEY_0: 'url.https://ssh:token123@github.com/.insteadOf',
|
|
GIT_CONFIG_KEY_1: 'url.https://git:token123@github.com/.insteadOf',
|
|
GIT_CONFIG_KEY_2: 'url.https://token123@github.com/.insteadOf',
|
|
GIT_CONFIG_VALUE_0: 'ssh://git@github.com/',
|
|
GIT_CONFIG_VALUE_1: 'git@github.com:',
|
|
GIT_CONFIG_VALUE_2: 'https://github.com/',
|
|
},
|
|
});
|
|
});
|
|
|
|
it('forces approved runtime Git configuration without authentication', async () => {
|
|
exec.mockResolvedValue({ stdout: '', stderr: '' });
|
|
const gitExec = withGitEnvironment();
|
|
setCustomEnv({
|
|
GIT_CONFIG_COUNT: '1',
|
|
GIT_CONFIG_KEY_0: 'existing-key',
|
|
GIT_CONFIG_VALUE_0: 'existing-value',
|
|
});
|
|
|
|
await gitExec('command');
|
|
|
|
expect(exec).toHaveBeenCalledExactlyOnceWith('command', {
|
|
env: {
|
|
GIT_CONFIG_COUNT: '1',
|
|
GIT_CONFIG_KEY_0: 'existing-key',
|
|
GIT_CONFIG_VALUE_0: 'existing-value',
|
|
},
|
|
});
|
|
});
|
|
});
|