Skip to content

Commit

Permalink
feat(presets): Restrict internal presets validation (#8382)
Browse files Browse the repository at this point in the history
  • Loading branch information
zharinov committed Feb 16, 2021
1 parent 32440f2 commit 64f93ec
Show file tree
Hide file tree
Showing 2 changed files with 34 additions and 4 deletions.
6 changes: 5 additions & 1 deletion lib/config/presets/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ import * as gitlab from './gitlab';
import * as internal from './internal';
import * as local from './local';
import * as npm from './npm';
import { PRESET_DEP_NOT_FOUND } from './util';

const presetSources: Record<string, PresetApi> = {
github,
Expand Down Expand Up @@ -151,6 +152,9 @@ export async function getPreset(
presetName,
baseConfig,
});
if (!presetConfig) {
throw new Error(PRESET_DEP_NOT_FOUND);
}
logger.trace({ presetConfig }, `Found preset ${preset}`);
if (params) {
const argMapping = {};
Expand Down Expand Up @@ -227,7 +231,7 @@ export async function resolveConfigPresets(
throw err;
}
const error = new Error(CONFIG_VALIDATION);
if (err.message === 'dep not found') {
if (err.message === PRESET_DEP_NOT_FOUND) {
error.validationError = `Cannot find preset's package (${preset})`;
} else if (err.message === 'preset renovate-config not found') {
error.validationError = `Preset package is missing a renovate-config entry (${preset})`;
Expand Down
32 changes: 29 additions & 3 deletions lib/config/presets/internal/index.spec.ts
Original file line number Diff line number Diff line change
@@ -1,20 +1,46 @@
import { mocked } from '../../../../test/util';
import { CONFIG_VALIDATION } from '../../../constants/error-messages';
import { massageConfig } from '../../massage';
import { validateConfig } from '../../validation';
import { resolveConfigPresets } from '../index';
import * as _npm from '../npm';
import * as internal from '.';

jest.mock('./npm');
jest.mock('../../../datasource/npm');

const npm = mocked(_npm);
npm.getPreset = jest.fn((_) => null);

const ignoredPresets = ['default:group', 'default:timezone'];

describe('config/presets/internal', () => {
it('fails for undefined internal preset', async () => {
const preset = 'foo:bar';
const presetConfig = { extends: [preset] };
await expect(resolveConfigPresets(presetConfig)).rejects.toThrow(
CONFIG_VALIDATION
);
});

for (const [groupName, groupPresets] of Object.entries(internal.groups)) {
for (const [presetName, presetConfig] of Object.entries(groupPresets)) {
const preset = `${groupName}:${presetName}`;
if (presetName !== 'description' && !ignoredPresets.includes(preset)) {
it(`${preset} validates`, async () => {
const res = await validateConfig(massageConfig(presetConfig), true);
expect(res.errors).toHaveLength(0);
expect(res.warnings).toHaveLength(0);
try {
const config = await resolveConfigPresets(
massageConfig(presetConfig)
);
const res = await validateConfig(config, true);
expect(res.errors).toHaveLength(0);
expect(res.warnings).toHaveLength(0);
} catch (err) {
if (err.validationError) {
throw new Error(err.validationError);
}
throw err;
}
});
}
}
Expand Down

0 comments on commit 64f93ec

Please sign in to comment.