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.
96 lines
3.0 KiB
TypeScript
96 lines
3.0 KiB
TypeScript
import { logger } from '~test/util.ts';
|
|
import {
|
|
isCrossOriginPaginationAllowed,
|
|
resolvePaginationUrl,
|
|
} from './util.ts';
|
|
|
|
describe('modules/datasource/util', () => {
|
|
describe('isCrossOriginPaginationAllowed', () => {
|
|
it('returns false when the flag is unset', () => {
|
|
expect(isCrossOriginPaginationAllowed('docker')).toBe(false);
|
|
expect(isCrossOriginPaginationAllowed('nuget')).toBe(false);
|
|
});
|
|
|
|
it('returns true only for the opted-in datasource', () => {
|
|
vi.stubEnv('RENOVATE_X_DOCKER_PAGINATION_ALLOW_CROSS_ORIGIN', 'true');
|
|
expect(isCrossOriginPaginationAllowed('docker')).toBe(true);
|
|
// per-datasource: docker's flag must not opt nuget in
|
|
expect(isCrossOriginPaginationAllowed('nuget')).toBe(false);
|
|
});
|
|
|
|
it('reads a separate flag for nuget', () => {
|
|
vi.stubEnv('RENOVATE_X_NUGET_PAGINATION_ALLOW_CROSS_ORIGIN', 'true');
|
|
expect(isCrossOriginPaginationAllowed('nuget')).toBe(true);
|
|
expect(isCrossOriginPaginationAllowed('docker')).toBe(false);
|
|
});
|
|
|
|
it('returns false for a datasource without a flag', () => {
|
|
expect(isCrossOriginPaginationAllowed('npm')).toBe(false);
|
|
});
|
|
});
|
|
|
|
describe('resolvePaginationUrl', () => {
|
|
it('resolves a same-origin link when cross-origin is not allowed', () => {
|
|
expect(
|
|
resolvePaginationUrl(
|
|
'https://reg.example.com/v2/foo?n=10',
|
|
'https://reg.example.com/v2/foo?n=10&last=z',
|
|
false,
|
|
),
|
|
).toBe('https://reg.example.com/v2/foo?n=10&last=z');
|
|
});
|
|
|
|
it('drops a cross-origin link when cross-origin is not allowed', () => {
|
|
expect(
|
|
resolvePaginationUrl(
|
|
'https://reg.example.com/v2/foo',
|
|
'https://attacker.example.com/v2/foo',
|
|
false,
|
|
),
|
|
).toBeNull();
|
|
});
|
|
|
|
it('follows a cross-origin link when allowed', () => {
|
|
expect(
|
|
resolvePaginationUrl(
|
|
'https://reg.example.com/v2/foo',
|
|
'https://cdn.example.com/v2/foo?page=2',
|
|
true,
|
|
),
|
|
).toBe('https://cdn.example.com/v2/foo?page=2');
|
|
});
|
|
|
|
it('logs a warning when following a cross-origin link when allowed', () => {
|
|
resolvePaginationUrl(
|
|
'https://reg.example.com/v2/foo',
|
|
'https://cdn.example.com/v2/foo?page=2',
|
|
true,
|
|
);
|
|
|
|
expect(logger.logger.once.warn).toHaveBeenCalledWith(
|
|
{
|
|
baseUrl: 'https://reg.example.com/v2/foo',
|
|
nextUrl: 'https://cdn.example.com/v2/foo?page=2',
|
|
},
|
|
'Following cross-origin pagination link',
|
|
);
|
|
});
|
|
|
|
it('resolves a relative link when cross-origin is allowed', () => {
|
|
expect(
|
|
resolvePaginationUrl(
|
|
'https://reg.example.com/v2/foo',
|
|
'/v2/foo?page=2',
|
|
true,
|
|
),
|
|
).toBe('https://reg.example.com/v2/foo?page=2');
|
|
});
|
|
|
|
it('returns null for an invalid link when cross-origin is allowed', () => {
|
|
expect(
|
|
resolvePaginationUrl('https://reg.example.com/v2/', 'http://', true),
|
|
).toBeNull();
|
|
});
|
|
});
|
|
});
|