mirror of
https://github.com/renovatebot/renovate.git
synced 2025-01-12 06:56:24 +00:00
refactor(presets): Reorder code and fix mocks (#9421)
This commit is contained in:
parent
36f8d1df0e
commit
612be5e449
3 changed files with 88 additions and 87 deletions
|
@ -11,7 +11,11 @@ Array [
|
|||
]
|
||||
`;
|
||||
|
||||
exports[`config/presets/local/index getPreset() forwards to custom bitbucket-server 2`] = `undefined`;
|
||||
exports[`config/presets/local/index getPreset() forwards to custom bitbucket-server 2`] = `
|
||||
Object {
|
||||
"resolved": "preset",
|
||||
}
|
||||
`;
|
||||
|
||||
exports[`config/presets/local/index getPreset() forwards to custom gitea 1`] = `
|
||||
Array [
|
||||
|
@ -24,7 +28,11 @@ Array [
|
|||
]
|
||||
`;
|
||||
|
||||
exports[`config/presets/local/index getPreset() forwards to custom gitea 2`] = `undefined`;
|
||||
exports[`config/presets/local/index getPreset() forwards to custom gitea 2`] = `
|
||||
Object {
|
||||
"resolved": "preset",
|
||||
}
|
||||
`;
|
||||
|
||||
exports[`config/presets/local/index getPreset() forwards to custom github 1`] = `
|
||||
Array [
|
||||
|
@ -71,7 +79,11 @@ Array [
|
|||
]
|
||||
`;
|
||||
|
||||
exports[`config/presets/local/index getPreset() forwards to gitea 2`] = `undefined`;
|
||||
exports[`config/presets/local/index getPreset() forwards to gitea 2`] = `
|
||||
Object {
|
||||
"resolved": "preset",
|
||||
}
|
||||
`;
|
||||
|
||||
exports[`config/presets/local/index getPreset() forwards to github 1`] = `
|
||||
Array [
|
||||
|
|
|
@ -5,21 +5,24 @@ import * as _github from '../github';
|
|||
import * as _gitlab from '../gitlab';
|
||||
import * as local from '.';
|
||||
|
||||
jest.mock('../gitlab');
|
||||
jest.mock('../github');
|
||||
jest.mock('../gitea');
|
||||
jest.mock('../bitbucket-server');
|
||||
jest.mock('../gitea');
|
||||
jest.mock('../github');
|
||||
jest.mock('../gitlab');
|
||||
|
||||
const gitlab = mocked(_gitlab);
|
||||
const github = mocked(_github);
|
||||
const gitea = mocked(_gitea);
|
||||
const bitbucketServer = mocked(_bitbucketServer);
|
||||
const gitea = mocked(_gitea);
|
||||
const github = mocked(_github);
|
||||
const gitlab = mocked(_gitlab);
|
||||
|
||||
describe(getName(__filename), () => {
|
||||
beforeEach(() => {
|
||||
jest.resetAllMocks();
|
||||
gitlab.getPresetFromEndpoint.mockResolvedValueOnce({ resolved: 'preset' });
|
||||
github.getPresetFromEndpoint.mockResolvedValueOnce({ resolved: 'preset' });
|
||||
const preset = { resolved: 'preset' };
|
||||
bitbucketServer.getPresetFromEndpoint.mockResolvedValueOnce(preset);
|
||||
gitea.getPresetFromEndpoint.mockResolvedValueOnce(preset);
|
||||
github.getPresetFromEndpoint.mockResolvedValueOnce(preset);
|
||||
gitlab.getPresetFromEndpoint.mockResolvedValueOnce(preset);
|
||||
});
|
||||
describe('getPreset()', () => {
|
||||
it('throws for unsupported platform', async () => {
|
||||
|
@ -44,50 +47,19 @@ describe(getName(__filename), () => {
|
|||
});
|
||||
}).rejects.toThrow();
|
||||
});
|
||||
it('forwards to gitlab', async () => {
|
||||
const content = await local.getPreset({
|
||||
packageName: 'some/repo',
|
||||
presetName: 'default',
|
||||
baseConfig: {
|
||||
platform: 'GitLab',
|
||||
},
|
||||
});
|
||||
expect(gitlab.getPresetFromEndpoint.mock.calls).toMatchSnapshot();
|
||||
expect(content).toMatchSnapshot();
|
||||
});
|
||||
it('forwards to custom gitlab', async () => {
|
||||
const content = await local.getPreset({
|
||||
packageName: 'some/repo',
|
||||
presetName: 'default',
|
||||
baseConfig: {
|
||||
platform: 'gitlab',
|
||||
endpoint: 'https://gitlab.example.com/api/v4',
|
||||
},
|
||||
});
|
||||
expect(gitlab.getPresetFromEndpoint.mock.calls).toMatchSnapshot();
|
||||
expect(content).toMatchSnapshot();
|
||||
});
|
||||
|
||||
it('forwards to github', async () => {
|
||||
const content = await local.getPreset({
|
||||
packageName: 'some/repo',
|
||||
baseConfig: {
|
||||
platform: 'github',
|
||||
},
|
||||
});
|
||||
expect(github.getPresetFromEndpoint.mock.calls).toMatchSnapshot();
|
||||
expect(content).toMatchSnapshot();
|
||||
});
|
||||
it('forwards to custom github', async () => {
|
||||
it('forwards to custom bitbucket-server', async () => {
|
||||
const content = await local.getPreset({
|
||||
packageName: 'some/repo',
|
||||
presetName: 'default',
|
||||
baseConfig: {
|
||||
platform: 'github',
|
||||
endpoint: 'https://api.github.example.com',
|
||||
platform: 'bitbucket-server',
|
||||
endpoint: 'https://git.example.com',
|
||||
},
|
||||
});
|
||||
expect(github.getPresetFromEndpoint.mock.calls).toMatchSnapshot();
|
||||
expect(
|
||||
bitbucketServer.getPresetFromEndpoint.mock.calls
|
||||
).toMatchSnapshot();
|
||||
expect(content).toMatchSnapshot();
|
||||
});
|
||||
|
||||
|
@ -114,18 +86,50 @@ describe(getName(__filename), () => {
|
|||
expect(content).toMatchSnapshot();
|
||||
});
|
||||
|
||||
it('forwards to custom bitbucket-server', async () => {
|
||||
it('forwards to github', async () => {
|
||||
const content = await local.getPreset({
|
||||
packageName: 'some/repo',
|
||||
baseConfig: {
|
||||
platform: 'github',
|
||||
},
|
||||
});
|
||||
expect(github.getPresetFromEndpoint.mock.calls).toMatchSnapshot();
|
||||
expect(content).toMatchSnapshot();
|
||||
});
|
||||
it('forwards to custom github', async () => {
|
||||
const content = await local.getPreset({
|
||||
packageName: 'some/repo',
|
||||
presetName: 'default',
|
||||
baseConfig: {
|
||||
platform: 'bitbucket-server',
|
||||
endpoint: 'https://git.example.com',
|
||||
platform: 'github',
|
||||
endpoint: 'https://api.github.example.com',
|
||||
},
|
||||
});
|
||||
expect(
|
||||
bitbucketServer.getPresetFromEndpoint.mock.calls
|
||||
).toMatchSnapshot();
|
||||
expect(github.getPresetFromEndpoint.mock.calls).toMatchSnapshot();
|
||||
expect(content).toMatchSnapshot();
|
||||
});
|
||||
|
||||
it('forwards to gitlab', async () => {
|
||||
const content = await local.getPreset({
|
||||
packageName: 'some/repo',
|
||||
presetName: 'default',
|
||||
baseConfig: {
|
||||
platform: 'GitLab',
|
||||
},
|
||||
});
|
||||
expect(gitlab.getPresetFromEndpoint.mock.calls).toMatchSnapshot();
|
||||
expect(content).toMatchSnapshot();
|
||||
});
|
||||
it('forwards to custom gitlab', async () => {
|
||||
const content = await local.getPreset({
|
||||
packageName: 'some/repo',
|
||||
presetName: 'default',
|
||||
baseConfig: {
|
||||
platform: 'gitlab',
|
||||
endpoint: 'https://gitlab.example.com/api/v4',
|
||||
},
|
||||
});
|
||||
expect(gitlab.getPresetFromEndpoint.mock.calls).toMatchSnapshot();
|
||||
expect(content).toMatchSnapshot();
|
||||
});
|
||||
});
|
||||
|
|
|
@ -10,6 +10,13 @@ import * as github from '../github';
|
|||
import * as gitlab from '../gitlab';
|
||||
import type { Preset, PresetConfig } from '../types';
|
||||
|
||||
const resolvers = {
|
||||
[PLATFORM_TYPE_BITBUCKET_SERVER]: bitbucketServer,
|
||||
[PLATFORM_TYPE_GITEA]: gitea,
|
||||
[PLATFORM_TYPE_GITHUB]: github,
|
||||
[PLATFORM_TYPE_GITLAB]: gitlab,
|
||||
};
|
||||
|
||||
export function getPreset({
|
||||
packageName: pkgName,
|
||||
presetName = 'default',
|
||||
|
@ -20,38 +27,16 @@ export function getPreset({
|
|||
if (!platform) {
|
||||
throw new Error(`Missing platform config for local preset.`);
|
||||
}
|
||||
switch (platform.toLowerCase()) {
|
||||
case PLATFORM_TYPE_GITLAB:
|
||||
return gitlab.getPresetFromEndpoint(
|
||||
pkgName,
|
||||
presetName,
|
||||
presetPath,
|
||||
endpoint
|
||||
);
|
||||
case PLATFORM_TYPE_GITHUB:
|
||||
return github.getPresetFromEndpoint(
|
||||
pkgName,
|
||||
presetName,
|
||||
presetPath,
|
||||
endpoint
|
||||
);
|
||||
case PLATFORM_TYPE_BITBUCKET_SERVER:
|
||||
return bitbucketServer.getPresetFromEndpoint(
|
||||
pkgName,
|
||||
presetName,
|
||||
presetPath,
|
||||
endpoint
|
||||
);
|
||||
case PLATFORM_TYPE_GITEA:
|
||||
return gitea.getPresetFromEndpoint(
|
||||
pkgName,
|
||||
presetName,
|
||||
presetPath,
|
||||
endpoint
|
||||
);
|
||||
default:
|
||||
throw new Error(
|
||||
`Unsupported platform '${baseConfig.platform}' for local preset.`
|
||||
);
|
||||
const resolver = resolvers[platform.toLowerCase()];
|
||||
if (!resolver) {
|
||||
throw new Error(
|
||||
`Unsupported platform '${baseConfig.platform}' for local preset.`
|
||||
);
|
||||
}
|
||||
return resolver.getPresetFromEndpoint(
|
||||
pkgName,
|
||||
presetName,
|
||||
presetPath,
|
||||
endpoint
|
||||
);
|
||||
}
|
||||
|
|
Loading…
Reference in a new issue