-
Notifications
You must be signed in to change notification settings - Fork 92
Expand file tree
/
Copy pathgetCliOptions.test.ts
More file actions
219 lines (183 loc) · 8.46 KB
/
getCliOptions.test.ts
File metadata and controls
219 lines (183 loc) · 8.46 KB
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
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
import { afterEach, describe, expect, it, jest } from '@jest/globals';
import { getCliOptions } from '../../options/getCliOptions';
import { findProjectRoot, getDefaultRemoteBranch } from 'workspace-tools';
jest.mock('workspace-tools', () => {
return {
getDefaultRemoteBranch: jest.fn((options: { branch?: string }) => `origin/${options.branch || 'main'}`),
findProjectRoot: jest.fn(() => 'fake-root'),
};
});
//
// These tests cover a mix of built-in parser behavior, provided options, and custom overrides.
// The parser is commander, and these tests document the expected behavior from the beachball
// "end user" perspective.
//
describe('getCliOptions', () => {
// This is the same mocked value as above (can't be shared in a const because jest.mock() is
// not allowed to access the surrounding context)
const projectRoot = 'fake-root';
const mockFindProjectRoot = findProjectRoot as jest.MockedFunction<typeof findProjectRoot>;
const defaults = { command: 'change', path: projectRoot };
/** test wrapper for `getCliOptions` which adds common args */
function getCliOptionsTest(args: string[], cwd?: string) {
return getCliOptions({
argv: ['node', 'beachball', ...args],
cwd: cwd || projectRoot,
});
}
afterEach(() => {
jest.clearAllMocks();
});
// start by making sure nothing went wrong with the mock
it('uses fake project root', () => {
expect(findProjectRoot(process.cwd())).toEqual(projectRoot);
});
it('parses no args (adds path to result)', () => {
const options = getCliOptionsTest([]);
expect(options).toEqual(defaults);
});
it('parses command', () => {
const options = getCliOptionsTest(['check']);
expect(options).toEqual({ ...defaults, command: 'check' });
});
it('parses options', () => {
// use a basic option of each value type (except arrays, tested later)
const options = getCliOptionsTest(['--type', 'patch', '--access=public', '--fetch', '--depth', '1']);
expect(options).toEqual({ ...defaults, type: 'patch', access: 'public', fetch: true, depth: 1 });
});
it('parses command and options', () => {
const options = getCliOptionsTest(['publish', '--tag', 'foo']);
expect(options).toEqual({ ...defaults, command: 'publish', tag: 'foo' });
});
it('parses array options with multiple values', () => {
const options = getCliOptionsTest(['--scope', 'foo', 'bar']);
expect(options).toEqual({ ...defaults, scope: ['foo', 'bar'] });
});
it('parses array option specified multiple times', () => {
const options = getCliOptionsTest(['--scope', 'foo', '--scope', 'bar']);
expect(options).toEqual({ ...defaults, scope: ['foo', 'bar'] });
});
// documenting that this is not currently supported (could change in the future if desired)
it('does not parse values with commas as separate array entries', () => {
const options = getCliOptionsTest(['--scope', 'a,b', '--scope=c,d']);
expect(options).toEqual({ ...defaults, scope: ['a,b', 'c,d'] });
});
it('uses last value if non-array option is specified multiple times', () => {
const options = getCliOptionsTest(['--tag', 'foo', '--tag', 'baz']);
expect(options).toEqual({ ...defaults, tag: 'baz' });
});
it('parses negated boolean option', () => {
const options = getCliOptionsTest(['--no-fetch']);
expect(options).toEqual({ ...defaults, fetch: false });
});
it('parses negated boolean options with --no-X syntax', () => {
const options = getCliOptionsTest(['--no-fetch', '--no-yes']);
expect(options).toEqual({ ...defaults, fetch: false, yes: false });
});
it('throws on invalid numeric value', () => {
expect(() => getCliOptionsTest(['--depth', 'foo'])).toThrow();
});
it('converts hyphenated options to camel case', () => {
const options = getCliOptionsTest(['--git-tags', '--dependent-change-type', 'patch']);
expect(options).toEqual({ ...defaults, gitTags: true, dependentChangeType: 'patch' });
});
it('requires hyphenated form for multi-word options', () => {
const options = getCliOptionsTest([
'--git-tags',
'--dependent-change-type',
'patch',
'--disallowed-change-types',
'major',
'minor',
]);
expect(options).toEqual({
...defaults,
gitTags: true,
dependentChangeType: 'patch',
disallowedChangeTypes: ['major', 'minor'],
});
});
it('rejects camelCase form of multi-word options', () => {
expect(() => getCliOptionsTest(['--gitTags'])).toThrow();
expect(() => getCliOptionsTest(['--dependentChangeType', 'patch'])).toThrow();
expect(() => getCliOptionsTest(['--disallowedChangeTypes', 'major'])).toThrow();
});
it('parses short option aliases', () => {
const options = getCliOptionsTest(['publish', '-t', 'test', '-r', 'http://whatever', '-y']);
expect(options).toEqual({ ...defaults, command: 'publish', tag: 'test', registry: 'http://whatever', yes: true });
});
it('parses long option aliases', () => {
const options = getCliOptionsTest(['--config', 'path/to/config.json', '--force', '--since', 'main']);
expect(options).toEqual({ ...defaults, configPath: 'path/to/config.json', forceVersions: true, fromRef: 'main' });
});
it('for canary command, adds canary tag and ignores regular tag', () => {
const options = getCliOptionsTest(['canary', '--tag', 'bar']);
expect(options).toEqual({ ...defaults, command: 'canary', tag: 'canary' });
});
it('for canary command, uses canaryName as tag and ignores regular tag', () => {
const options = getCliOptionsTest(['canary', '--canary-name', 'foo', '--tag', 'bar']);
expect(options).toEqual({ ...defaults, command: 'canary', canaryName: 'foo', tag: 'foo' });
});
it('does not set tag to canaryName for non-canary command', () => {
const options = getCliOptionsTest(['publish', '--canary-name', 'foo', '--tag', 'bar']);
expect(options).toEqual({ ...defaults, command: 'publish', canaryName: 'foo', tag: 'bar' });
});
it('falls back to given cwd as path if findProjectRoot fails', () => {
mockFindProjectRoot.mockImplementationOnce(() => {
throw new Error('nope');
});
const options = getCliOptionsTest([], 'somewhere');
expect(options).toEqual({ ...defaults, path: 'somewhere' });
});
it('uses provided branch if it contains a slash', () => {
const options = getCliOptionsTest(['--branch', 'someremote/foo']);
expect(options).toEqual({ ...defaults, branch: 'someremote/foo' });
// this is mocked at the top of the file
// eslint-disable-next-line etc/no-deprecated
expect(getDefaultRemoteBranch).not.toHaveBeenCalled();
});
it('adds default remote to branch without slash', () => {
const options = getCliOptionsTest(['--branch', 'foo']);
expect(options).toEqual({ ...defaults, branch: 'origin/foo' });
// eslint-disable-next-line etc/no-deprecated
expect(getDefaultRemoteBranch).toHaveBeenCalledWith({ branch: 'foo', verbose: undefined, cwd: projectRoot });
});
it('throws on unknown string options', () => {
expect(() => getCliOptionsTest(['--foo', 'bar'])).toThrow();
});
it('throws on unknown boolean flags', () => {
expect(() => getCliOptionsTest(['--foo'])).toThrow();
});
it('throws on unknown negated boolean flags', () => {
expect(() => getCliOptionsTest(['--no-bar'])).toThrow();
});
it('throws on unknown option with value', () => {
expect(() => getCliOptionsTest(['--foo', 'true'])).toThrow();
});
it('throws on unknown option specified multiple times', () => {
expect(() => getCliOptionsTest(['--foo', 'bar', '--foo', 'baz'])).toThrow();
});
it('throws on unknown option followed by positional', () => {
expect(() => getCliOptionsTest(['--foo', 'bar', 'baz'])).toThrow();
});
describe('config command', () => {
it('parses config get with setting name', () => {
const options = getCliOptionsTest(['config', 'get', 'branch']);
expect(options).toEqual({ ...defaults, command: 'config', _extraPositionalArgs: ['get', 'branch'] });
});
it('parses config get with setting name and options', () => {
const options = getCliOptionsTest(['config', 'get', 'tag', '--package', 'my-pkg']);
expect(options).toEqual({
...defaults,
command: 'config',
_extraPositionalArgs: ['get', 'tag'],
package: ['my-pkg'],
});
});
it('still throws for non-config command with extra positional args', () => {
expect(() => getCliOptionsTest(['check', 'extra'])).toThrow(
'Only one positional argument (the command) is allowed'
);
});
});
});