2 * Copyright (c) 2017, Daniel Imms (MIT License).
3 * Copyright (c) 2018, Microsoft Corporation (MIT License).
6 import * as assert from 'assert';
7 import { argsToCommandLine } from './windowsPtyAgent';
9 function check(file: string, args: string | string[], expected: string): void {
10 assert.equal(argsToCommandLine(file, args), expected);
13 if (process.platform === 'win32') {
14 describe('argsToCommandLine', () => {
15 describe('Plain strings', () => {
16 it('doesn\'t quote plain string', () => {
17 check('asdf', [], 'asdf');
19 it('doesn\'t escape backslashes', () => {
20 check('\\asdf\\qwer\\', [], '\\asdf\\qwer\\');
22 it('doesn\'t escape multiple backslashes', () => {
23 check('asdf\\\\qwer', [], 'asdf\\\\qwer');
25 it('adds backslashes before quotes', () => {
26 check('"asdf"qwer"', [], '\\"asdf\\"qwer\\"');
28 it('escapes backslashes before quotes', () => {
29 check('asdf\\"qwer', [], 'asdf\\\\\\"qwer');
33 describe('Quoted strings', () => {
34 it('quotes string with spaces', () => {
35 check('asdf qwer', [], '"asdf qwer"');
37 it('quotes empty string', () => {
40 it('quotes string with tabs', () => {
41 check('asdf\tqwer', [], '"asdf\tqwer"');
43 it('escapes only the last backslash', () => {
44 check('\\asdf \\qwer\\', [], '"\\asdf \\qwer\\\\"');
46 it('doesn\'t escape multiple backslashes', () => {
47 check('asdf \\\\qwer', [], '"asdf \\\\qwer"');
49 it('escapes backslashes before quotes', () => {
50 check('asdf \\"qwer', [], '"asdf \\\\\\"qwer"');
52 it('escapes multiple backslashes at the end', () => {
53 check('asdf qwer\\\\', [], '"asdf qwer\\\\\\\\"');
57 describe('Multiple arguments', () => {
58 it('joins arguments with spaces', () => {
59 check('asdf', ['qwer zxcv', '', '"'], 'asdf "qwer zxcv" "" \\"');
61 it('array argument all in quotes', () => {
62 check('asdf', ['"surounded by quotes"'], 'asdf \\"surounded by quotes\\"');
64 it('array argument quotes in the middle', () => {
65 check('asdf', ['quotes "in the" middle'], 'asdf "quotes \\"in the\\" middle"');
67 it('array argument quotes near start', () => {
68 check('asdf', ['"quotes" near start'], 'asdf "\\"quotes\\" near start"');
70 it('array argument quotes near end', () => {
71 check('asdf', ['quotes "near end"'], 'asdf "quotes \\"near end\\""');
75 describe('Args as CommandLine', () => {
76 it('should handle empty string', () => {
77 check('file', '', 'file');
79 it('should not change args', () => {
80 check('file', 'foo bar baz', 'file foo bar baz');
81 check('file', 'foo \\ba"r \baz', 'file foo \\ba"r \baz');
85 describe('Real-world cases', () => {
86 it('quotes within quotes', () => {
87 check('cmd.exe', ['/c', 'powershell -noexit -command \'Set-location \"C:\\user\"\''], 'cmd.exe /c "powershell -noexit -command \'Set-location \\\"C:\\user\\"\'"');
89 it('space within quotes', () => {
90 check('cmd.exe', ['/k', '"C:\\Users\\alros\\Desktop\\test script.bat"'], 'cmd.exe /k \\"C:\\Users\\alros\\Desktop\\test script.bat\\"');