Files
renovate/lib/util/git/config.spec.ts
Thomas Andersen 86890d2142 refactor(git): extract runtime config appending (#45165)
* refactor(git): centralize runtime config environment

Preserve caller-supplied and inherited GIT_CONFIG_KEY_n and GIT_CONFIG_VALUE_n entries when appending runtime Git configuration.

Co-authored-by: Jamie Tanna <jamie.tanna@mend.io>

* refactor(git): extract runtime config appending

* style(git): format runtime config tests

* test(git): remove superseded environment cases

* Apply suggestion from `@RahulGautamSingh`

Co-authored-by: RahulGautamSingh <rahultesnik@gmail.com>

* test(git): align invalid config count warning

---------

Co-authored-by: MrVampy <4302946+MrVampy@users.noreply.github.com>
Co-authored-by: Jamie Tanna <jamie.tanna@mend.io>
Co-authored-by: Michael Kriese <michael.kriese@gmx.de>
Co-authored-by: RahulGautamSingh <rahultesnik@gmail.com>
2026-09-14 06:42:38 +00:00

114 lines
3.3 KiB
TypeScript

import { logger } from '~test/util.ts';
import { GlobalConfig } from '../../config/global.ts';
import {
addGitConfigEnvironmentVariables,
setNoVerify,
simpleGitConfig,
} from './config.ts';
describe('util/git/config', () => {
beforeEach(() => {
GlobalConfig.reset();
});
it('uses "close" events, ignores "exit" events from child processes', () => {
expect(simpleGitConfig()).toEqual({
completion: { onClose: true, onExit: false },
config: ['core.quotePath=false'],
unsafe: {
allowUnsafeAskPass: true,
allowUnsafeSshCommand: true,
allowUnsafeConfigEnvCount: true,
},
});
});
it('uses timeout value from GlobalConfig', () => {
GlobalConfig.set({ gitTimeout: 50000 });
expect(simpleGitConfig()).toEqual({
completion: { onClose: true, onExit: false },
timeout: {
block: 50000,
},
config: ['core.quotePath=false'],
unsafe: {
allowUnsafeAskPass: true,
allowUnsafeSshCommand: true,
allowUnsafeConfigEnvCount: true,
},
});
});
it('throws', () => {
// @ts-expect-error -- testing invalid input
expect(() => setNoVerify(1)).toThrow(
'config error: gitNoVerify should be an array of strings',
);
});
it('appends entries without modifying the supplied environment', () => {
const environmentVariables = {
GIT_CONFIG_COUNT: '1',
GIT_CONFIG_KEY_0: 'existing-key',
GIT_CONFIG_VALUE_0: 'existing-value',
RANDOM_VARIABLE: 'random',
};
expect(
addGitConfigEnvironmentVariables(environmentVariables, [
{ key: 'first-key', value: 'first-value' },
{ key: 'second-key', value: 'second-value' },
]),
).toStrictEqual({
GIT_CONFIG_COUNT: '3',
GIT_CONFIG_KEY_0: 'existing-key',
GIT_CONFIG_KEY_1: 'first-key',
GIT_CONFIG_KEY_2: 'second-key',
GIT_CONFIG_VALUE_0: 'existing-value',
GIT_CONFIG_VALUE_1: 'first-value',
GIT_CONFIG_VALUE_2: 'second-value',
RANDOM_VARIABLE: 'random',
});
expect(environmentVariables).toStrictEqual({
GIT_CONFIG_COUNT: '1',
GIT_CONFIG_KEY_0: 'existing-key',
GIT_CONFIG_VALUE_0: 'existing-value',
RANDOM_VARIABLE: 'random',
});
});
it('supports repeated additions', () => {
const firstEnvironment = addGitConfigEnvironmentVariables({}, [
{ key: 'first-key', value: 'first-value' },
]);
expect(
addGitConfigEnvironmentVariables(firstEnvironment, [
{ key: 'second-key', value: 'second-value' },
]),
).toStrictEqual({
GIT_CONFIG_COUNT: '2',
GIT_CONFIG_KEY_0: 'first-key',
GIT_CONFIG_KEY_1: 'second-key',
GIT_CONFIG_VALUE_0: 'first-value',
GIT_CONFIG_VALUE_1: 'second-value',
});
});
it('ignores an invalid Git configuration count', () => {
expect(
addGitConfigEnvironmentVariables({ GIT_CONFIG_COUNT: 'invalid' }, [
{ key: 'new-key', value: 'new-value' },
]),
).toStrictEqual({
GIT_CONFIG_COUNT: '1',
GIT_CONFIG_KEY_0: 'new-key',
GIT_CONFIG_VALUE_0: 'new-value',
});
expect(logger.logger.warn).toHaveBeenCalledWith(
{ GIT_CONFIG_COUNT: 'invalid' },
`Found GIT_CONFIG_COUNT env variable, but could not parse the value to an integer. Ignoring it.`,
);
});
});