installed pty
[VSoRC/.git] / node_modules / node-pty / src / windowsPtyAgent.test.ts
1 /**
2  * Copyright (c) 2017, Daniel Imms (MIT License).
3  * Copyright (c) 2018, Microsoft Corporation (MIT License).
4  */
5
6 import * as assert from 'assert';
7 import { argsToCommandLine } from './windowsPtyAgent';
8
9 function check(file: string, args: string | string[], expected: string): void {
10   assert.equal(argsToCommandLine(file, args), expected);
11 }
12
13 if (process.platform === 'win32') {
14   describe('argsToCommandLine', () => {
15     describe('Plain strings', () => {
16       it('doesn\'t quote plain string', () => {
17         check('asdf', [], 'asdf');
18       });
19       it('doesn\'t escape backslashes', () => {
20         check('\\asdf\\qwer\\', [], '\\asdf\\qwer\\');
21       });
22       it('doesn\'t escape multiple backslashes', () => {
23         check('asdf\\\\qwer', [], 'asdf\\\\qwer');
24       });
25       it('adds backslashes before quotes', () => {
26         check('"asdf"qwer"', [], '\\"asdf\\"qwer\\"');
27       });
28       it('escapes backslashes before quotes', () => {
29         check('asdf\\"qwer', [], 'asdf\\\\\\"qwer');
30       });
31     });
32
33     describe('Quoted strings', () => {
34       it('quotes string with spaces', () => {
35         check('asdf qwer', [], '"asdf qwer"');
36       });
37       it('quotes empty string', () => {
38         check('', [], '""');
39       });
40       it('quotes string with tabs', () => {
41         check('asdf\tqwer', [], '"asdf\tqwer"');
42       });
43       it('escapes only the last backslash', () => {
44         check('\\asdf \\qwer\\', [], '"\\asdf \\qwer\\\\"');
45       });
46       it('doesn\'t escape multiple backslashes', () => {
47         check('asdf \\\\qwer', [], '"asdf \\\\qwer"');
48       });
49       it('escapes backslashes before quotes', () => {
50         check('asdf \\"qwer', [], '"asdf \\\\\\"qwer"');
51       });
52       it('escapes multiple backslashes at the end', () => {
53         check('asdf qwer\\\\', [], '"asdf qwer\\\\\\\\"');
54       });
55     });
56
57     describe('Multiple arguments', () => {
58       it('joins arguments with spaces', () => {
59         check('asdf', ['qwer zxcv', '', '"'], 'asdf "qwer zxcv" "" \\"');
60       });
61       it('array argument all in quotes', () => {
62         check('asdf', ['"surounded by quotes"'], 'asdf \\"surounded by quotes\\"');
63       });
64       it('array argument quotes in the middle', () => {
65         check('asdf', ['quotes "in the" middle'], 'asdf "quotes \\"in the\\" middle"');
66       });
67       it('array argument quotes near start', () => {
68         check('asdf', ['"quotes" near start'], 'asdf "\\"quotes\\" near start"');
69       });
70       it('array argument quotes near end', () => {
71         check('asdf', ['quotes "near end"'], 'asdf "quotes \\"near end\\""');
72       });
73     });
74
75     describe('Args as CommandLine', () => {
76       it('should handle empty string', () => {
77         check('file', '', 'file');
78       });
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');
82       });
83     });
84
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\\"\'"');
88       });
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\\"');
91       });
92     });
93   });
94 }