mirror of
https://github.com/renovatebot/renovate.git
synced 2026-10-26 23:39:13 +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.
164 lines
5.6 KiB
TypeScript
164 lines
5.6 KiB
TypeScript
import { isArray, isString } from '@sindresorhus/is';
|
|
import type { Response, SimpleGit } from 'simple-git';
|
|
import { mock } from 'vitest-mock-extended';
|
|
import { clearEnv } from '~test/util.ts';
|
|
import { GlobalConfig } from '../../../config/global.ts';
|
|
import * as git from '../../../util/git/index.ts';
|
|
import * as hostRules from '../../../util/host-rules.ts';
|
|
import { extractPackageFile } from './index.ts';
|
|
|
|
const createSimpleGit = vi.mocked(git.createSimpleGit);
|
|
|
|
const gitMock = mock<SimpleGit>();
|
|
|
|
describe('modules/manager/git-submodules/extract', () => {
|
|
beforeEach(async () => {
|
|
const { simpleGit } =
|
|
await vi.importActual<typeof import('simple-git')>('simple-git');
|
|
GlobalConfig.set({ localDir: `${import.meta.dirname}/__fixtures__` });
|
|
// clear host rules
|
|
hostRules.clear();
|
|
clearEnv();
|
|
|
|
createSimpleGit.mockImplementation((...args: any[]) => {
|
|
const git = simpleGit(...args);
|
|
|
|
gitMock.subModule.mockResolvedValue(
|
|
'4b825dc642cb6eb9a060e54bf8d69288fbee4904',
|
|
);
|
|
|
|
gitMock.raw.mockImplementation((options) => {
|
|
if (
|
|
(isString(options) || isArray(options, isString)) &&
|
|
options.includes('remote.origin.url')
|
|
) {
|
|
return Promise.resolve(
|
|
'https://github.com/renovatebot/renovate.git',
|
|
) as Response<string>;
|
|
}
|
|
return git.raw(options);
|
|
});
|
|
return gitMock;
|
|
});
|
|
});
|
|
|
|
describe('extractPackageFile()', () => {
|
|
it('empty submodule returns null', async () => {
|
|
expect(await extractPackageFile('', '.gitmodules.1', {})).toBeNull();
|
|
});
|
|
|
|
it('currentValue is unset when no branch is specified', async () => {
|
|
const res = await extractPackageFile('', '.gitmodules.2', {});
|
|
expect(res?.deps).toHaveLength(1);
|
|
expect(res?.deps[0].currentValue).toBeUndefined();
|
|
});
|
|
|
|
it('given branch is used when branch is specified', async () => {
|
|
const res = await extractPackageFile('', '.gitmodules.3', {});
|
|
expect(res?.deps).toHaveLength(1);
|
|
expect(res?.deps[0].currentValue).toBe('staging');
|
|
});
|
|
|
|
it('submodule packageName is constructed from relative path', async () => {
|
|
const res = await extractPackageFile('', '.gitmodules.4', {});
|
|
expect(res?.deps).toHaveLength(2);
|
|
expect(res?.deps[0].packageName).toBe(
|
|
'https://github.com/PowerShell/PowerShell-Docs',
|
|
);
|
|
expect(res?.deps[1].packageName).toBe(
|
|
'https://github.com/PowerShell/PowerShell-Docs-2',
|
|
);
|
|
});
|
|
|
|
describe('submodule sourceUrl is determined from packageName', () => {
|
|
it('when using SSH clone URL', async () => {
|
|
const res = await extractPackageFile('', '.gitmodules.3', {});
|
|
expect(res?.deps).toHaveLength(1);
|
|
expect(res?.deps[0].sourceUrl).toBe(
|
|
'https://github.com/PowerShell/PowerShell-Docs',
|
|
);
|
|
});
|
|
it('when using a relative path', async () => {
|
|
const res = await extractPackageFile('', '.gitmodules.4', {});
|
|
expect(res?.deps).toHaveLength(2);
|
|
expect(res?.deps[0].sourceUrl).toBe(
|
|
'https://github.com/PowerShell/PowerShell-Docs',
|
|
);
|
|
expect(res?.deps[1].sourceUrl).toBe(
|
|
'https://github.com/PowerShell/PowerShell-Docs-2',
|
|
);
|
|
});
|
|
});
|
|
|
|
it('fallback to current branch if special value is detected', async () => {
|
|
gitMock.branch.mockResolvedValueOnce({
|
|
all: ['staging', 'main'],
|
|
branches: {
|
|
staging: {
|
|
current: true,
|
|
name: 'staging',
|
|
commit: '9eeb873',
|
|
label: 'staging branch',
|
|
linkedWorkTree: false,
|
|
},
|
|
main: {
|
|
current: false,
|
|
name: 'main',
|
|
commit: 'e14c7e1',
|
|
label: 'main branch',
|
|
linkedWorkTree: false,
|
|
},
|
|
},
|
|
current: 'staging',
|
|
detached: false,
|
|
});
|
|
|
|
const res = await extractPackageFile('', '.gitmodules.7', {});
|
|
expect(res).toEqual({
|
|
datasource: 'git-refs',
|
|
deps: [
|
|
{
|
|
currentDigest: '4b825dc642cb6eb9a060e54bf8d69288fbee4904',
|
|
currentValue: 'staging',
|
|
depName: 'PowerShell-Docs',
|
|
packageName: 'https://github.com/PowerShell/PowerShell-Docs',
|
|
sourceUrl: 'https://github.com/PowerShell/PowerShell-Docs',
|
|
},
|
|
],
|
|
});
|
|
});
|
|
|
|
it('given semver version is extracted from branch and versioning is set to semver', async () => {
|
|
const res = await extractPackageFile('', '.gitmodules.8', {});
|
|
expect(res).toEqual({
|
|
datasource: 'git-refs',
|
|
deps: [
|
|
{
|
|
currentDigest: '4b825dc642cb6eb9a060e54bf8d69288fbee4904',
|
|
currentValue: 'v0.0.1',
|
|
depName: 'deps/renovate1',
|
|
packageName: 'https://github.com/renovatebot/renovate.git',
|
|
sourceUrl: 'https://github.com/renovatebot/renovate',
|
|
versioning: 'semver',
|
|
},
|
|
{
|
|
currentDigest: '4b825dc642cb6eb9a060e54bf8d69288fbee4904',
|
|
currentValue: '0.0.1',
|
|
depName: 'deps/renovate2',
|
|
packageName: 'https://github.com/renovatebot/renovate.git',
|
|
sourceUrl: 'https://github.com/renovatebot/renovate',
|
|
versioning: 'semver',
|
|
},
|
|
{
|
|
currentDigest: '4b825dc642cb6eb9a060e54bf8d69288fbee4904',
|
|
currentValue: 'not-a-semver',
|
|
packageName: 'https://github.com/renovatebot/renovate.git',
|
|
sourceUrl: 'https://github.com/renovatebot/renovate',
|
|
depName: 'deps/renovate3',
|
|
},
|
|
],
|
|
});
|
|
});
|
|
});
|
|
});
|