2022-03-03 10:35:26 +01:00
|
|
|
import { DockerDatasource } from '../../../../modules/datasource/docker';
|
2021-06-02 12:06:16 +02:00
|
|
|
import getArgv from './__fixtures__/argv';
|
2020-03-05 21:57:24 +01:00
|
|
|
import * as cli from './cli';
|
2022-02-11 11:02:30 +01:00
|
|
|
import type { ParseConfigOptions } from './types';
|
2017-01-20 14:03:18 +01:00
|
|
|
|
2021-08-18 08:46:56 +03:00
|
|
|
describe('workers/global/config/parse/cli', () => {
|
2019-08-23 15:46:31 +02:00
|
|
|
let argv: string[];
|
2022-04-12 16:49:49 +02:00
|
|
|
|
2017-02-09 04:30:00 +00:00
|
|
|
beforeEach(() => {
|
|
|
|
argv = getArgv();
|
|
|
|
});
|
2022-04-12 16:49:49 +02:00
|
|
|
|
2017-01-20 14:03:18 +01:00
|
|
|
describe('.getCliName(definition)', () => {
|
|
|
|
it('generates CLI value', () => {
|
2022-02-11 11:02:30 +01:00
|
|
|
const option: ParseConfigOptions = {
|
2017-01-20 14:03:18 +01:00
|
|
|
name: 'oneTwoThree',
|
|
|
|
};
|
2021-11-08 13:16:58 +01:00
|
|
|
expect(cli.getCliName(option)).toBe('--one-two-three');
|
2017-01-20 14:03:18 +01:00
|
|
|
});
|
2022-04-12 16:49:49 +02:00
|
|
|
|
2017-01-31 12:19:06 +01:00
|
|
|
it('generates returns empty if CLI false', () => {
|
2022-02-11 11:02:30 +01:00
|
|
|
const option: ParseConfigOptions = {
|
2017-01-31 12:19:06 +01:00
|
|
|
name: 'oneTwoThree',
|
|
|
|
cli: false,
|
|
|
|
};
|
2021-11-08 13:16:58 +01:00
|
|
|
expect(cli.getCliName(option)).toBe('');
|
2017-01-31 12:19:06 +01:00
|
|
|
});
|
2017-01-20 14:03:18 +01:00
|
|
|
});
|
2022-04-12 16:49:49 +02:00
|
|
|
|
2017-01-20 14:03:18 +01:00
|
|
|
describe('.getConfig(argv)', () => {
|
|
|
|
it('returns empty argv', () => {
|
2019-09-25 11:40:58 +02:00
|
|
|
expect(cli.getConfig(argv)).toEqual({});
|
2017-01-20 14:03:18 +01:00
|
|
|
});
|
2022-04-12 16:49:49 +02:00
|
|
|
|
2017-01-20 14:03:18 +01:00
|
|
|
it('supports boolean no value', () => {
|
2023-06-12 21:33:40 +05:45
|
|
|
argv.push('--config-migration');
|
|
|
|
expect(cli.getConfig(argv)).toEqual({ configMigration: true });
|
2017-01-20 14:03:18 +01:00
|
|
|
argv = argv.slice(0, -1);
|
|
|
|
});
|
2022-04-12 16:49:49 +02:00
|
|
|
|
2017-01-20 14:03:18 +01:00
|
|
|
it('supports boolean space true', () => {
|
2023-06-12 21:33:40 +05:45
|
|
|
argv.push('--config-migration');
|
2017-01-20 14:03:18 +01:00
|
|
|
argv.push('true');
|
2023-06-12 21:33:40 +05:45
|
|
|
expect(cli.getConfig(argv)).toEqual({ configMigration: true });
|
2017-01-20 14:03:18 +01:00
|
|
|
});
|
2022-04-12 16:49:49 +02:00
|
|
|
|
2019-03-13 14:58:39 +01:00
|
|
|
it('throws exception for invalid boolean value', () => {
|
2023-06-12 21:33:40 +05:45
|
|
|
argv.push('--config-migration');
|
2019-03-13 14:58:39 +01:00
|
|
|
argv.push('badvalue');
|
2019-04-20 01:34:37 +05:30
|
|
|
expect(() => cli.getConfig(argv)).toThrow(
|
|
|
|
Error(
|
2023-11-07 12:50:29 -03:00
|
|
|
"Invalid boolean value: expected 'true' or 'false', but got 'badvalue'",
|
|
|
|
),
|
2019-03-13 14:58:39 +01:00
|
|
|
);
|
|
|
|
});
|
2022-04-12 16:49:49 +02:00
|
|
|
|
2017-01-20 14:03:18 +01:00
|
|
|
it('supports boolean space false', () => {
|
2023-06-12 21:33:40 +05:45
|
|
|
argv.push('--config-migration');
|
2017-01-20 14:03:18 +01:00
|
|
|
argv.push('false');
|
2023-06-12 21:33:40 +05:45
|
|
|
expect(cli.getConfig(argv)).toEqual({ configMigration: false });
|
2017-01-20 14:03:18 +01:00
|
|
|
});
|
2022-04-12 16:49:49 +02:00
|
|
|
|
2017-01-20 14:03:18 +01:00
|
|
|
it('supports boolean equals true', () => {
|
2023-06-12 21:33:40 +05:45
|
|
|
argv.push('--config-migration=true');
|
|
|
|
expect(cli.getConfig(argv)).toEqual({ configMigration: true });
|
2017-01-20 14:03:18 +01:00
|
|
|
});
|
2022-04-12 16:49:49 +02:00
|
|
|
|
2017-01-20 14:03:18 +01:00
|
|
|
it('supports boolean equals false', () => {
|
2023-06-12 21:33:40 +05:45
|
|
|
argv.push('--config-migration=false');
|
|
|
|
expect(cli.getConfig(argv)).toEqual({ configMigration: false });
|
2017-01-20 14:03:18 +01:00
|
|
|
});
|
2022-04-12 16:49:49 +02:00
|
|
|
|
2017-01-20 14:03:18 +01:00
|
|
|
it('supports list single', () => {
|
|
|
|
argv.push('--labels=a');
|
2019-09-25 11:40:58 +02:00
|
|
|
expect(cli.getConfig(argv)).toEqual({ labels: ['a'] });
|
2017-01-20 14:03:18 +01:00
|
|
|
});
|
2022-04-12 16:49:49 +02:00
|
|
|
|
2017-01-20 14:03:18 +01:00
|
|
|
it('supports list multiple', () => {
|
|
|
|
argv.push('--labels=a,b,c');
|
2019-09-25 11:40:58 +02:00
|
|
|
expect(cli.getConfig(argv)).toEqual({ labels: ['a', 'b', 'c'] });
|
2017-01-20 14:03:18 +01:00
|
|
|
});
|
2022-04-12 16:49:49 +02:00
|
|
|
|
2017-01-20 14:03:18 +01:00
|
|
|
it('supports string', () => {
|
|
|
|
argv.push('--token=a');
|
2019-09-25 11:40:58 +02:00
|
|
|
expect(cli.getConfig(argv)).toEqual({ token: 'a' });
|
2017-01-20 14:03:18 +01:00
|
|
|
});
|
2022-04-12 16:49:49 +02:00
|
|
|
|
2017-01-20 14:03:18 +01:00
|
|
|
it('supports repositories', () => {
|
|
|
|
argv.push('foo');
|
|
|
|
argv.push('bar');
|
2019-09-25 11:40:58 +02:00
|
|
|
expect(cli.getConfig(argv)).toEqual({ repositories: ['foo', 'bar'] });
|
2017-01-20 14:03:18 +01:00
|
|
|
});
|
2022-04-12 16:49:49 +02:00
|
|
|
|
2018-08-31 21:44:40 +02:00
|
|
|
it('parses json lists correctly', () => {
|
|
|
|
argv.push(
|
2023-11-07 12:50:29 -03:00
|
|
|
`--host-rules=[{"matchHost":"docker.io","hostType":"${DockerDatasource.id}","username":"user","password":"password"}]`,
|
2018-08-31 21:44:40 +02:00
|
|
|
);
|
2019-09-25 11:40:58 +02:00
|
|
|
expect(cli.getConfig(argv)).toEqual({
|
2018-09-12 12:16:17 +02:00
|
|
|
hostRules: [
|
2018-08-31 21:44:40 +02:00
|
|
|
{
|
2021-05-13 22:53:18 +02:00
|
|
|
matchHost: 'docker.io',
|
2022-02-13 22:13:13 +03:00
|
|
|
hostType: DockerDatasource.id,
|
2018-08-31 21:44:40 +02:00
|
|
|
username: 'user',
|
|
|
|
password: 'password',
|
|
|
|
},
|
|
|
|
],
|
|
|
|
});
|
|
|
|
});
|
2022-04-12 16:49:49 +02:00
|
|
|
|
2018-09-12 12:16:17 +02:00
|
|
|
it('parses [] correctly as empty list of hostRules', () => {
|
|
|
|
argv.push(`--host-rules=[]`);
|
2019-09-25 11:40:58 +02:00
|
|
|
expect(cli.getConfig(argv)).toEqual({
|
2018-09-12 12:16:17 +02:00
|
|
|
hostRules: [],
|
2018-08-31 21:44:40 +02:00
|
|
|
});
|
|
|
|
});
|
2022-04-12 16:49:49 +02:00
|
|
|
|
2018-09-12 12:16:17 +02:00
|
|
|
it('parses an empty string correctly as empty list of hostRules', () => {
|
|
|
|
argv.push(`--host-rules=`);
|
2019-09-25 11:40:58 +02:00
|
|
|
expect(cli.getConfig(argv)).toEqual({
|
2018-09-12 12:16:17 +02:00
|
|
|
hostRules: [],
|
|
|
|
});
|
|
|
|
});
|
2022-04-12 16:49:49 +02:00
|
|
|
|
2023-04-28 10:03:15 +05:30
|
|
|
it.each`
|
2021-10-11 09:36:07 +03:00
|
|
|
arg | config
|
|
|
|
${'--endpoints='} | ${{ hostRules: [] }}
|
|
|
|
${'--azure-auto-complete=false'} | ${{ platformAutomerge: false }}
|
|
|
|
${'--azure-auto-complete=true'} | ${{ platformAutomerge: true }}
|
|
|
|
${'--azure-auto-complete'} | ${{ platformAutomerge: true }}
|
|
|
|
${'--git-lab-automerge=false'} | ${{ platformAutomerge: false }}
|
|
|
|
${'--git-lab-automerge=true'} | ${{ platformAutomerge: true }}
|
|
|
|
${'--git-lab-automerge'} | ${{ platformAutomerge: true }}
|
2023-06-12 21:33:40 +05:45
|
|
|
${'--recreate-closed=false'} | ${{ recreateWhen: 'auto' }}
|
|
|
|
${'--recreate-closed=true'} | ${{ recreateWhen: 'always' }}
|
|
|
|
${'--recreate-closed'} | ${{ recreateWhen: 'always' }}
|
|
|
|
${'--recreate-when=auto'} | ${{ recreateWhen: 'auto' }}
|
|
|
|
${'--recreate-when=always'} | ${{ recreateWhen: 'always' }}
|
|
|
|
${'--recreate-when=never'} | ${{ recreateWhen: 'never' }}
|
2021-10-11 09:36:07 +03:00
|
|
|
`('"$arg" -> $config', ({ arg, config }) => {
|
|
|
|
argv.push(arg);
|
|
|
|
expect(cli.getConfig(argv)).toMatchObject(config);
|
2018-08-31 21:44:40 +02:00
|
|
|
});
|
2022-04-12 16:49:49 +02:00
|
|
|
|
2019-06-11 06:19:05 +02:00
|
|
|
it('parses json object correctly when empty', () => {
|
|
|
|
argv.push(`--onboarding-config=`);
|
2019-09-25 11:40:58 +02:00
|
|
|
expect(cli.getConfig(argv)).toEqual({
|
2019-06-11 06:19:05 +02:00
|
|
|
onboardingConfig: {},
|
|
|
|
});
|
|
|
|
});
|
2022-04-12 16:49:49 +02:00
|
|
|
|
2019-06-11 06:19:05 +02:00
|
|
|
it('parses json {} object correctly', () => {
|
|
|
|
argv.push(`--onboarding-config={}`);
|
2019-09-25 11:40:58 +02:00
|
|
|
expect(cli.getConfig(argv)).toEqual({
|
2019-06-11 06:19:05 +02:00
|
|
|
onboardingConfig: {},
|
|
|
|
});
|
|
|
|
});
|
2022-04-12 16:49:49 +02:00
|
|
|
|
2019-06-11 06:19:05 +02:00
|
|
|
it('parses json object correctly', () => {
|
2023-06-27 14:26:57 +02:00
|
|
|
argv.push(`--onboarding-config={"extends": ["config:recommended"]}`);
|
2019-09-25 11:40:58 +02:00
|
|
|
expect(cli.getConfig(argv)).toEqual({
|
2019-06-11 06:19:05 +02:00
|
|
|
onboardingConfig: {
|
2023-06-27 14:26:57 +02:00
|
|
|
extends: ['config:recommended'],
|
2019-06-11 06:19:05 +02:00
|
|
|
},
|
|
|
|
});
|
|
|
|
});
|
2022-04-12 16:49:49 +02:00
|
|
|
|
2019-06-11 06:19:05 +02:00
|
|
|
it('throws exception for invalid json object', () => {
|
|
|
|
argv.push('--onboarding-config=Hello_World');
|
|
|
|
expect(() => cli.getConfig(argv)).toThrow(
|
2023-11-07 12:50:29 -03:00
|
|
|
Error("Invalid JSON value: 'Hello_World'"),
|
2019-06-11 06:19:05 +02:00
|
|
|
);
|
|
|
|
});
|
2022-04-12 16:49:49 +02:00
|
|
|
|
2022-04-11 23:29:02 +03:00
|
|
|
it('dryRun boolean true', () => {
|
|
|
|
argv.push('--dry-run=true');
|
|
|
|
expect(cli.getConfig(argv)).toEqual({ dryRun: 'full' });
|
|
|
|
});
|
2022-04-12 16:49:49 +02:00
|
|
|
|
2022-04-11 23:29:02 +03:00
|
|
|
it('dryRun no value', () => {
|
|
|
|
argv.push('--dry-run');
|
|
|
|
expect(cli.getConfig(argv)).toEqual({ dryRun: 'full' });
|
|
|
|
});
|
2022-04-12 16:49:49 +02:00
|
|
|
|
2022-04-11 23:29:02 +03:00
|
|
|
it('dryRun boolean false', () => {
|
|
|
|
argv.push('--dry-run=false');
|
|
|
|
expect(cli.getConfig(argv)).toEqual({ dryRun: null });
|
|
|
|
});
|
2022-04-12 16:49:49 +02:00
|
|
|
|
2022-04-11 23:29:02 +03:00
|
|
|
it('dryRun null', () => {
|
|
|
|
argv.push('--dry-run=null');
|
|
|
|
expect(cli.getConfig(argv)).toEqual({ dryRun: null });
|
|
|
|
});
|
2022-05-18 13:21:51 +03:00
|
|
|
|
|
|
|
it('requireConfig boolean true', () => {
|
|
|
|
argv.push('--require-config=true');
|
|
|
|
expect(cli.getConfig(argv)).toEqual({ requireConfig: 'required' });
|
|
|
|
});
|
|
|
|
|
|
|
|
it('requireConfig no value', () => {
|
|
|
|
argv.push('--require-config');
|
|
|
|
expect(cli.getConfig(argv)).toEqual({ requireConfig: 'required' });
|
|
|
|
});
|
|
|
|
|
|
|
|
it('requireConfig boolean false', () => {
|
|
|
|
argv.push('--require-config=false');
|
|
|
|
expect(cli.getConfig(argv)).toEqual({ requireConfig: 'optional' });
|
|
|
|
});
|
2017-01-20 14:03:18 +01:00
|
|
|
});
|
|
|
|
});
|