Path: blob/main/src/vs/platform/environment/test/node/argv.test.ts
3296 views
/*---------------------------------------------------------------------------------------------1* Copyright (c) Microsoft Corporation. All rights reserved.2* Licensed under the MIT License. See License.txt in the project root for license information.3*--------------------------------------------------------------------------------------------*/45import assert from 'assert';6import { ensureNoDisposablesAreLeakedInTestSuite } from '../../../../base/test/common/utils.js';7import { formatOptions, Option, OptionDescriptions, Subcommand, parseArgs, ErrorReporter } from '../../node/argv.js';8import { addArg } from '../../node/argvHelper.js';910function o(description: string, type: 'boolean' | 'string' | 'string[]' = 'string'): Option<any> {11return {12description, type13};14}15function c(description: string, options: OptionDescriptions<any>): Subcommand<any> {16return {17description, type: 'subcommand', options18};19}2021suite('formatOptions', () => {2223test('Text should display small columns correctly', () => {24assert.deepStrictEqual(25formatOptions({26'add': o('bar')27}, 80),28[' --add bar']29);30assert.deepStrictEqual(31formatOptions({32'add': o('bar'),33'wait': o('ba'),34'trace': o('b')35}, 80),36[37' --add bar',38' --wait ba',39' --trace b'40]);41});4243test('Text should wrap', () => {44assert.deepStrictEqual(45formatOptions({46'add': o((<any>'bar ').repeat(9))47}, 40),48[49' --add bar bar bar bar bar bar',50' bar bar bar'51]);52});5354test('Text should revert to the condensed view when the terminal is too narrow', () => {55assert.deepStrictEqual(56formatOptions({57'add': o((<any>'bar ').repeat(9))58}, 30),59[60' --add',61' bar bar bar bar bar bar bar bar bar '62]);63});6465test('addArg', () => {66assert.deepStrictEqual(addArg([], 'foo'), ['foo']);67assert.deepStrictEqual(addArg([], 'foo', 'bar'), ['foo', 'bar']);68assert.deepStrictEqual(addArg(['foo'], 'bar'), ['foo', 'bar']);69assert.deepStrictEqual(addArg(['--wait'], 'bar'), ['--wait', 'bar']);70assert.deepStrictEqual(addArg(['--wait', '--', '--foo'], 'bar'), ['--wait', 'bar', '--', '--foo']);71assert.deepStrictEqual(addArg(['--', '--foo'], 'bar'), ['bar', '--', '--foo']);72});7374test('subcommands', () => {75assert.deepStrictEqual(76formatOptions({77'testcmd': c('A test command', { add: o('A test command option') })78}, 30),79[80' --testcmd',81' A test command'82]);83});8485ensureNoDisposablesAreLeakedInTestSuite();86});8788suite('parseArgs', () => {89function newErrorReporter(result: string[] = [], command = ''): ErrorReporter & { result: string[] } {90const commandPrefix = command ? command + '-' : '';91return {92onDeprecatedOption: (deprecatedId) => result.push(`${commandPrefix}onDeprecatedOption ${deprecatedId}`),93onUnknownOption: (id) => result.push(`${commandPrefix}onUnknownOption ${id}`),94onEmptyValue: (id) => result.push(`${commandPrefix}onEmptyValue ${id}`),95onMultipleValues: (id, usedValue) => result.push(`${commandPrefix}onMultipleValues ${id} ${usedValue}`),96getSubcommandReporter: (c) => newErrorReporter(result, commandPrefix + c),97result98};99}100101function assertParse<T>(options: OptionDescriptions<T>, input: string[], expected: T, expectedErrors: string[]) {102const errorReporter = newErrorReporter();103assert.deepStrictEqual(parseArgs(input, options, errorReporter), expected);104assert.deepStrictEqual(errorReporter.result, expectedErrors);105}106107test('subcommands', () => {108109interface TestArgs1 {110testcmd?: {111testArg?: string;112_: string[];113};114_: string[];115}116117const options1 = {118'testcmd': c('A test command', {119testArg: o('A test command option'),120_: { type: 'string[]' }121}),122_: { type: 'string[]' }123} as OptionDescriptions<TestArgs1>;124assertParse(125options1,126['testcmd', '--testArg=foo'],127{ testcmd: { testArg: 'foo', '_': [] }, '_': [] },128[]129);130assertParse(131options1,132['testcmd', '--testArg=foo', '--testX'],133{ testcmd: { testArg: 'foo', '_': [] }, '_': [] },134['testcmd-onUnknownOption testX']135);136137assertParse(138options1,139['--testArg=foo', 'testcmd', '--testX'],140{ testcmd: { testArg: 'foo', '_': [] }, '_': [] },141['testcmd-onUnknownOption testX']142);143144assertParse(145options1,146['--testArg=foo', 'testcmd'],147{ testcmd: { testArg: 'foo', '_': [] }, '_': [] },148[]149);150151assertParse(152options1,153['--testArg', 'foo', 'testcmd'],154{ testcmd: { testArg: 'foo', '_': [] }, '_': [] },155[]156);157158interface TestArgs2 {159testcmd?: {160testArg?: string;161testX?: boolean;162_: string[];163};164testX?: boolean;165_: string[];166}167168const options2 = {169'testcmd': c('A test command', {170testArg: o('A test command option')171}),172testX: { type: 'boolean', global: true, description: '' },173_: { type: 'string[]' }174} as OptionDescriptions<TestArgs2>;175assertParse(176options2,177['testcmd', '--testArg=foo', '--testX'],178{ testcmd: { testArg: 'foo', testX: true, '_': [] }, '_': [] },179[]180);181});182183ensureNoDisposablesAreLeakedInTestSuite();184});185186187