-
Notifications
You must be signed in to change notification settings - Fork 60
/
cliArgs.test.ts
195 lines (171 loc) · 5.83 KB
/
cliArgs.test.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
import { ConfigOptions } from './ConfigOptions';
import { getOptionsFromCliArgs } from './cliArgs';
describe('getOptionsFromCliArgs', () => {
it('should merge config and cli options', () => {
const configOptions: ConfigOptions = {
accessToken: 'myAccessToken',
all: false,
fork: true,
gitHostname: 'github.com',
githubApiBaseUrlV3: 'https://api.github.com',
githubApiBaseUrlV4: 'https://api.github.com/graphql',
maxNumber: 10,
multipleBranches: true,
multipleCommits: false,
noVerify: true,
prTitle: 'myPrTitle',
sourcePRLabels: [],
targetBranchChoices: [],
targetPRLabels: [],
upstream: 'elastic/kibana',
username: 'sqren',
};
const argv = [
'--branch',
'6.0',
'--branch',
'6.1',
'--upstream',
'backport-org/backport-demo',
'--all',
'--username',
'sqren',
];
const res = getOptionsFromCliArgs(configOptions, argv);
expect(res).toEqual({
accessToken: 'myAccessToken',
all: true,
assignees: [],
ci: false,
dryRun: false,
fork: true,
gitHostname: 'github.com',
githubApiBaseUrlV3: 'https://api.github.com',
githubApiBaseUrlV4: 'https://api.github.com/graphql',
maxNumber: 10,
multipleBranches: true,
multipleCommits: false,
noVerify: true,
prTitle: 'myPrTitle',
resetAuthor: false,
sha: undefined,
sourcePRLabels: [],
targetBranchChoices: [],
targetBranches: ['6.0', '6.1'],
targetPRLabels: [],
upstream: 'backport-org/backport-demo',
username: 'sqren',
verbose: false,
});
});
it('should accept both camel-case and dashed-case and convert them to camel cased', () => {
const configOptions: ConfigOptions = {};
const argv = [
'--access-token',
'my access token',
'--githubApiBaseUrlV3',
'my api hostname',
];
const res = getOptionsFromCliArgs(configOptions, argv);
expect(res.accessToken).toEqual('my access token');
expect('access-token' in res).toEqual(false);
expect(res.githubApiBaseUrlV3).toEqual('my api hostname');
expect('api-hostname' in res).toEqual(false);
});
describe('pullNumber', () => {
it('should accept `--pr` alias but only return the full representation (`pullNumber`)', () => {
const configOptions: ConfigOptions = {};
const argv = ['--pr', '1337'];
const res = getOptionsFromCliArgs(configOptions, argv);
expect(res.pullNumber).toEqual(1337);
//@ts-expect-error
expect(res.pr).toBe(undefined);
});
});
describe('autoAssign', () => {
it('should set assignees to current user if `autoAssign` is true', () => {
const configOptions: ConfigOptions = { username: 'sqren' };
const argv = ['--auto-assign'];
const res = getOptionsFromCliArgs(configOptions, argv);
expect(res.assignees).toEqual(['sqren']);
});
});
describe('assignees', () => {
it('should set assignees', () => {
const configOptions: ConfigOptions = { username: 'sqren' };
const argv = ['--assignees', 'john'];
const res = getOptionsFromCliArgs(configOptions, argv);
expect(res.assignees).toEqual(['john']);
});
});
describe('multipleBranches', () => {
it('should be settable', () => {
const configOptions: ConfigOptions = { multipleBranches: false };
const argv = [] as const;
const res = getOptionsFromCliArgs(configOptions, argv);
expect(res.multipleBranches).toBe(false);
});
it('should respect `multiple` option', () => {
const configOptions: ConfigOptions = { multipleBranches: false };
const argv = ['--multiple'];
const res = getOptionsFromCliArgs(configOptions, argv);
expect(res.multipleBranches).toBe(true);
});
});
describe('noVerify', () => {
it('should be settable', () => {
const configOptions: ConfigOptions = { noVerify: false };
const argv = [] as const;
const res = getOptionsFromCliArgs(configOptions, argv);
expect(res.noVerify).toBe(false);
});
it('should be flipped by `verify`', () => {
const configOptions: ConfigOptions = { noVerify: false };
const argv = ['--verify'];
const res = getOptionsFromCliArgs(configOptions, argv);
expect(res.noVerify).toBe(true);
});
});
describe('mainline', () => {
it('should default to 1', () => {
const configOptions: ConfigOptions = {};
const argv = ['--mainline'];
const res = getOptionsFromCliArgs(configOptions, argv);
expect(res.mainline).toEqual(1);
});
it('should accept numbers', () => {
const configOptions: ConfigOptions = {};
const argv = ['--mainline', '2'];
const res = getOptionsFromCliArgs(configOptions, argv);
expect(res.mainline).toEqual(2);
});
});
describe('targetBranches', () => {
it('should not coerce 6.0 to 6', () => {
const configOptions: ConfigOptions = {};
const argv = ['-b', '6.0'];
const res = getOptionsFromCliArgs(configOptions, argv);
expect(res.targetBranches).toEqual(['6.0']);
});
});
describe('targetBranchChoices', () => {
it('should support objects', () => {
const configOptions: ConfigOptions = {
targetBranchChoices: [{ name: '7.x', checked: false }],
};
const argv = [] as const;
const res = getOptionsFromCliArgs(configOptions, argv);
expect(res.targetBranchChoices).toEqual([
{ name: '7.x', checked: false },
]);
});
it('should convert primitive values to objects', () => {
const configOptions: ConfigOptions = {};
const argv = ['--target-branch-choices', '8.x'];
const res = getOptionsFromCliArgs(configOptions, argv);
expect(res.targetBranchChoices).toEqual([
{ name: '8.x', checked: false },
]);
});
});
});