Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
microsoft
GitHub Repository: microsoft/vscode
Path: blob/main/src/vs/platform/environment/test/node/argv.test.ts
5222 views
1
/*---------------------------------------------------------------------------------------------
2
* Copyright (c) Microsoft Corporation. All rights reserved.
3
* Licensed under the MIT License. See License.txt in the project root for license information.
4
*--------------------------------------------------------------------------------------------*/
5
6
import assert from 'assert';
7
import { ensureNoDisposablesAreLeakedInTestSuite } from '../../../../base/test/common/utils.js';
8
import { formatOptions, Option, OptionDescriptions, Subcommand, parseArgs, ErrorReporter } from '../../node/argv.js';
9
import { addArg } from '../../node/argvHelper.js';
10
11
function o(description: string, type: 'boolean' | 'string' | 'string[]' = 'string'): Option<any> {
12
return {
13
description, type
14
};
15
}
16
function c(description: string, options: OptionDescriptions<any>): Subcommand<any> {
17
return {
18
description, type: 'subcommand', options
19
};
20
}
21
22
suite('formatOptions', () => {
23
24
test('Text should display small columns correctly', () => {
25
assert.deepStrictEqual(
26
formatOptions({
27
'add': o('bar')
28
}, 80),
29
[' --add bar']
30
);
31
assert.deepStrictEqual(
32
formatOptions({
33
'add': o('bar'),
34
'wait': o('ba'),
35
'trace': o('b')
36
}, 80),
37
[
38
' --add bar',
39
' --wait ba',
40
' --trace b'
41
]);
42
});
43
44
test('Text should wrap', () => {
45
assert.deepStrictEqual(
46
formatOptions({
47
// eslint-disable-next-line local/code-no-any-casts
48
'add': o((<any>'bar ').repeat(9))
49
}, 40),
50
[
51
' --add bar bar bar bar bar bar',
52
' bar bar bar'
53
]);
54
});
55
56
test('Text should revert to the condensed view when the terminal is too narrow', () => {
57
assert.deepStrictEqual(
58
formatOptions({
59
// eslint-disable-next-line local/code-no-any-casts
60
'add': o((<any>'bar ').repeat(9))
61
}, 30),
62
[
63
' --add',
64
' bar bar bar bar bar bar bar bar bar '
65
]);
66
});
67
68
test('addArg', () => {
69
assert.deepStrictEqual(addArg([], 'foo'), ['foo']);
70
assert.deepStrictEqual(addArg([], 'foo', 'bar'), ['foo', 'bar']);
71
assert.deepStrictEqual(addArg(['foo'], 'bar'), ['foo', 'bar']);
72
assert.deepStrictEqual(addArg(['--wait'], 'bar'), ['--wait', 'bar']);
73
assert.deepStrictEqual(addArg(['--wait', '--', '--foo'], 'bar'), ['--wait', 'bar', '--', '--foo']);
74
assert.deepStrictEqual(addArg(['--', '--foo'], 'bar'), ['bar', '--', '--foo']);
75
});
76
77
test('subcommands', () => {
78
assert.deepStrictEqual(
79
formatOptions({
80
'testcmd': c('A test command', { add: o('A test command option') })
81
}, 30),
82
[
83
' --testcmd',
84
' A test command'
85
]);
86
});
87
88
ensureNoDisposablesAreLeakedInTestSuite();
89
});
90
91
suite('parseArgs', () => {
92
function newErrorReporter(result: string[] = [], command = ''): ErrorReporter & { result: string[] } {
93
const commandPrefix = command ? command + '-' : '';
94
return {
95
onDeprecatedOption: (deprecatedId) => result.push(`${commandPrefix}onDeprecatedOption ${deprecatedId}`),
96
onUnknownOption: (id) => result.push(`${commandPrefix}onUnknownOption ${id}`),
97
onEmptyValue: (id) => result.push(`${commandPrefix}onEmptyValue ${id}`),
98
onMultipleValues: (id, usedValue) => result.push(`${commandPrefix}onMultipleValues ${id} ${usedValue}`),
99
getSubcommandReporter: (c) => newErrorReporter(result, commandPrefix + c),
100
result
101
};
102
}
103
104
function assertParse<T>(options: OptionDescriptions<T>, input: string[], expected: T, expectedErrors: string[]) {
105
const errorReporter = newErrorReporter();
106
assert.deepStrictEqual(parseArgs(input, options, errorReporter), expected);
107
assert.deepStrictEqual(errorReporter.result, expectedErrors);
108
}
109
110
test('subcommands', () => {
111
112
interface TestArgs1 {
113
testcmd?: {
114
testArg?: string;
115
_: string[];
116
};
117
_: string[];
118
}
119
120
const options1 = {
121
'testcmd': c('A test command', {
122
testArg: o('A test command option'),
123
_: { type: 'string[]' }
124
}),
125
_: { type: 'string[]' }
126
} as OptionDescriptions<TestArgs1>;
127
assertParse(
128
options1,
129
['testcmd', '--testArg=foo'],
130
{ testcmd: { testArg: 'foo', '_': [] }, '_': [] },
131
[]
132
);
133
assertParse(
134
options1,
135
['testcmd', '--testArg=foo', '--testX'],
136
{ testcmd: { testArg: 'foo', '_': [] }, '_': [] },
137
['testcmd-onUnknownOption testX']
138
);
139
140
assertParse(
141
options1,
142
['--testArg=foo', 'testcmd', '--testX'],
143
{ testcmd: { testArg: 'foo', '_': [] }, '_': [] },
144
['testcmd-onUnknownOption testX']
145
);
146
147
assertParse(
148
options1,
149
['--testArg=foo', 'testcmd'],
150
{ testcmd: { testArg: 'foo', '_': [] }, '_': [] },
151
[]
152
);
153
154
assertParse(
155
options1,
156
['--testArg', 'foo', 'testcmd'],
157
{ testcmd: { testArg: 'foo', '_': [] }, '_': [] },
158
[]
159
);
160
161
interface TestArgs2 {
162
testcmd?: {
163
testArg?: string;
164
testX?: boolean;
165
_: string[];
166
};
167
testX?: boolean;
168
_: string[];
169
}
170
171
const options2 = {
172
'testcmd': c('A test command', {
173
testArg: o('A test command option')
174
}),
175
testX: { type: 'boolean', global: true, description: '' },
176
_: { type: 'string[]' }
177
} as OptionDescriptions<TestArgs2>;
178
assertParse(
179
options2,
180
['testcmd', '--testArg=foo', '--testX'],
181
{ testcmd: { testArg: 'foo', testX: true, '_': [] }, '_': [] },
182
[]
183
);
184
});
185
186
ensureNoDisposablesAreLeakedInTestSuite();
187
});
188
189