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
3296 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
'add': o((<any>'bar ').repeat(9))
48
}, 40),
49
[
50
' --add bar bar bar bar bar bar',
51
' bar bar bar'
52
]);
53
});
54
55
test('Text should revert to the condensed view when the terminal is too narrow', () => {
56
assert.deepStrictEqual(
57
formatOptions({
58
'add': o((<any>'bar ').repeat(9))
59
}, 30),
60
[
61
' --add',
62
' bar bar bar bar bar bar bar bar bar '
63
]);
64
});
65
66
test('addArg', () => {
67
assert.deepStrictEqual(addArg([], 'foo'), ['foo']);
68
assert.deepStrictEqual(addArg([], 'foo', 'bar'), ['foo', 'bar']);
69
assert.deepStrictEqual(addArg(['foo'], 'bar'), ['foo', 'bar']);
70
assert.deepStrictEqual(addArg(['--wait'], 'bar'), ['--wait', 'bar']);
71
assert.deepStrictEqual(addArg(['--wait', '--', '--foo'], 'bar'), ['--wait', 'bar', '--', '--foo']);
72
assert.deepStrictEqual(addArg(['--', '--foo'], 'bar'), ['bar', '--', '--foo']);
73
});
74
75
test('subcommands', () => {
76
assert.deepStrictEqual(
77
formatOptions({
78
'testcmd': c('A test command', { add: o('A test command option') })
79
}, 30),
80
[
81
' --testcmd',
82
' A test command'
83
]);
84
});
85
86
ensureNoDisposablesAreLeakedInTestSuite();
87
});
88
89
suite('parseArgs', () => {
90
function newErrorReporter(result: string[] = [], command = ''): ErrorReporter & { result: string[] } {
91
const commandPrefix = command ? command + '-' : '';
92
return {
93
onDeprecatedOption: (deprecatedId) => result.push(`${commandPrefix}onDeprecatedOption ${deprecatedId}`),
94
onUnknownOption: (id) => result.push(`${commandPrefix}onUnknownOption ${id}`),
95
onEmptyValue: (id) => result.push(`${commandPrefix}onEmptyValue ${id}`),
96
onMultipleValues: (id, usedValue) => result.push(`${commandPrefix}onMultipleValues ${id} ${usedValue}`),
97
getSubcommandReporter: (c) => newErrorReporter(result, commandPrefix + c),
98
result
99
};
100
}
101
102
function assertParse<T>(options: OptionDescriptions<T>, input: string[], expected: T, expectedErrors: string[]) {
103
const errorReporter = newErrorReporter();
104
assert.deepStrictEqual(parseArgs(input, options, errorReporter), expected);
105
assert.deepStrictEqual(errorReporter.result, expectedErrors);
106
}
107
108
test('subcommands', () => {
109
110
interface TestArgs1 {
111
testcmd?: {
112
testArg?: string;
113
_: string[];
114
};
115
_: string[];
116
}
117
118
const options1 = {
119
'testcmd': c('A test command', {
120
testArg: o('A test command option'),
121
_: { type: 'string[]' }
122
}),
123
_: { type: 'string[]' }
124
} as OptionDescriptions<TestArgs1>;
125
assertParse(
126
options1,
127
['testcmd', '--testArg=foo'],
128
{ testcmd: { testArg: 'foo', '_': [] }, '_': [] },
129
[]
130
);
131
assertParse(
132
options1,
133
['testcmd', '--testArg=foo', '--testX'],
134
{ testcmd: { testArg: 'foo', '_': [] }, '_': [] },
135
['testcmd-onUnknownOption testX']
136
);
137
138
assertParse(
139
options1,
140
['--testArg=foo', 'testcmd', '--testX'],
141
{ testcmd: { testArg: 'foo', '_': [] }, '_': [] },
142
['testcmd-onUnknownOption testX']
143
);
144
145
assertParse(
146
options1,
147
['--testArg=foo', 'testcmd'],
148
{ testcmd: { testArg: 'foo', '_': [] }, '_': [] },
149
[]
150
);
151
152
assertParse(
153
options1,
154
['--testArg', 'foo', 'testcmd'],
155
{ testcmd: { testArg: 'foo', '_': [] }, '_': [] },
156
[]
157
);
158
159
interface TestArgs2 {
160
testcmd?: {
161
testArg?: string;
162
testX?: boolean;
163
_: string[];
164
};
165
testX?: boolean;
166
_: string[];
167
}
168
169
const options2 = {
170
'testcmd': c('A test command', {
171
testArg: o('A test command option')
172
}),
173
testX: { type: 'boolean', global: true, description: '' },
174
_: { type: 'string[]' }
175
} as OptionDescriptions<TestArgs2>;
176
assertParse(
177
options2,
178
['testcmd', '--testArg=foo', '--testX'],
179
{ testcmd: { testArg: 'foo', testX: true, '_': [] }, '_': [] },
180
[]
181
);
182
});
183
184
ensureNoDisposablesAreLeakedInTestSuite();
185
});
186
187