renovate/lib/config/cli.spec.ts

140 lines
4.5 KiB
TypeScript
Raw Normal View History

2020-05-01 16:03:48 +00:00
import * as datasourceDocker from '../datasource/docker';
2020-03-05 20:57:24 +00:00
import * as cli from './cli';
2020-03-01 06:42:52 +00:00
import getArgv from './config/__fixtures__/argv';
2020-03-05 20:57:24 +00:00
import { RenovateOptions } from './definitions';
describe('config/cli', () => {
2019-08-23 13:46:31 +00:00
let argv: string[];
beforeEach(() => {
argv = getArgv();
});
describe('.getCliName(definition)', () => {
it('generates CLI value', () => {
2019-08-23 13:46:31 +00:00
const option: Partial<RenovateOptions> = {
name: 'oneTwoThree',
};
expect(cli.getCliName(option)).toEqual('--one-two-three');
});
it('generates returns empty if CLI false', () => {
2019-08-23 13:46:31 +00:00
const option: Partial<RenovateOptions> = {
name: 'oneTwoThree',
cli: false,
};
expect(cli.getCliName(option)).toEqual('');
});
});
describe('.getConfig(argv)', () => {
it('returns empty argv', () => {
expect(cli.getConfig(argv)).toEqual({});
});
it('supports boolean no value', () => {
argv.push('--recreate-closed');
expect(cli.getConfig(argv)).toEqual({ recreateClosed: true });
argv = argv.slice(0, -1);
});
it('supports boolean space true', () => {
argv.push('--recreate-closed');
argv.push('true');
expect(cli.getConfig(argv)).toEqual({ recreateClosed: true });
});
it('throws exception for invalid boolean value', () => {
argv.push('--recreate-closed');
argv.push('badvalue');
expect(() => cli.getConfig(argv)).toThrow(
Error(
"Invalid boolean value: expected 'true' or 'false', but got 'badvalue'"
)
);
});
it('supports boolean space false', () => {
argv.push('--recreate-closed');
argv.push('false');
expect(cli.getConfig(argv)).toEqual({ recreateClosed: false });
});
it('supports boolean equals true', () => {
argv.push('--recreate-closed=true');
expect(cli.getConfig(argv)).toEqual({ recreateClosed: true });
});
it('supports boolean equals false', () => {
argv.push('--recreate-closed=false');
expect(cli.getConfig(argv)).toEqual({ recreateClosed: false });
});
it('supports list single', () => {
argv.push('--labels=a');
expect(cli.getConfig(argv)).toEqual({ labels: ['a'] });
});
it('supports list multiple', () => {
argv.push('--labels=a,b,c');
expect(cli.getConfig(argv)).toEqual({ labels: ['a', 'b', 'c'] });
});
it('supports string', () => {
argv.push('--token=a');
expect(cli.getConfig(argv)).toEqual({ token: 'a' });
});
it('supports repositories', () => {
argv.push('foo');
argv.push('bar');
expect(cli.getConfig(argv)).toEqual({ repositories: ['foo', 'bar'] });
});
it('parses json lists correctly', () => {
argv.push(
`--host-rules=[{"domainName":"docker.io","hostType":"${datasourceDocker.id}","username":"user","password":"password"}]`
);
expect(cli.getConfig(argv)).toEqual({
hostRules: [
{
domainName: 'docker.io',
hostType: datasourceDocker.id,
username: 'user',
password: 'password',
},
],
});
});
it('parses [] correctly as empty list of hostRules', () => {
argv.push(`--host-rules=[]`);
expect(cli.getConfig(argv)).toEqual({
hostRules: [],
});
});
it('parses an empty string correctly as empty list of hostRules', () => {
argv.push(`--host-rules=`);
expect(cli.getConfig(argv)).toEqual({
hostRules: [],
});
});
it('migrates --endpoints', () => {
argv.push(`--endpoints=`);
expect(cli.getConfig(argv)).toEqual({
hostRules: [],
});
});
it('parses json object correctly when empty', () => {
argv.push(`--onboarding-config=`);
expect(cli.getConfig(argv)).toEqual({
onboardingConfig: {},
});
});
it('parses json {} object correctly', () => {
argv.push(`--onboarding-config={}`);
expect(cli.getConfig(argv)).toEqual({
onboardingConfig: {},
});
});
it('parses json object correctly', () => {
argv.push(`--onboarding-config={"extends": ["config:base"]}`);
expect(cli.getConfig(argv)).toEqual({
onboardingConfig: {
extends: ['config:base'],
},
});
});
it('throws exception for invalid json object', () => {
argv.push('--onboarding-config=Hello_World');
expect(() => cli.getConfig(argv)).toThrow(
Error("Invalid JSON value: 'Hello_World'")
);
});
});
});