Skip to content
Snippets Groups Projects
Unverified Commit d2257e15 authored by Max Daniline's avatar Max Daniline Committed by GitHub
Browse files

fix(config): replace default constants with default factories (#22587)

parent 40507c23
No related branches found
Tags mysql-0.1.1
No related merge requests found
import { getDefault } from './defaults';
import type { RenovateOptions } from './types';
describe('config/defaults', () => {
describe('getDefault()', () => {
it('returns new instances of arrays when called repeatedly', () => {
const option: RenovateOptions = {
type: 'array',
description: 'thing',
name: 'thing',
};
const array1 = getDefault(option);
const array2 = getDefault(option);
// Equal values, different objects
expect(array2).toEqual(array2);
expect(array1).not.toBe(array2);
});
it('returns true for boolean values', () => {
const option: RenovateOptions = {
type: 'boolean',
description: 'thing',
name: 'thing',
};
const val = getDefault(option);
expect(val).toBe(true);
});
['string', 'object', 'integer'].forEach((type: string) => {
it(`returns null for ${type} values`, () => {
const option: RenovateOptions = {
type: type as 'string' | 'object' | 'integer',
description: 'thing',
name: 'thing',
};
const val = getDefault(option);
expect(val).toBeNull();
});
});
});
});
import { getOptions } from './options';
import type { AllConfig, RenovateOptions } from './types';
const defaultValues = {
boolean: true,
array: [],
string: null,
object: null,
integer: null,
// Use functions instead of direct values to avoid introducing global references.
// In particular, we want a new array instance every time we request a default array
// instead of sharing a single instance - mutation of this value could cause serious problems.
// See https://github.com/mend/renovate-on-prem/issues/290 for an example
const defaultValueFactories = {
boolean: () => true,
array: () => [],
string: () => null,
object: () => null,
integer: () => null,
} as const;
export function getDefault(option: RenovateOptions): any {
return option.default === undefined
? defaultValues[option.type]
? defaultValueFactories[option.type]()
: option.default;
}
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment