X-Git-Url: https://git.josue.xyz/?p=VSoRC%2F.git;a=blobdiff_plain;f=node_modules%2Fnode-pty%2Fsrc%2FwindowsPtyAgent.test.ts;fp=node_modules%2Fnode-pty%2Fsrc%2FwindowsPtyAgent.test.ts;h=dc2104b34fd396722a9e30821ff0b7dea18b4295;hp=0000000000000000000000000000000000000000;hb=e79e4a5a87f3e84f7c1777f10a954453a69bf540;hpb=4339da12467b75fb8b6ca831f4bf0081c485ed2c diff --git a/node_modules/node-pty/src/windowsPtyAgent.test.ts b/node_modules/node-pty/src/windowsPtyAgent.test.ts new file mode 100644 index 0000000..dc2104b --- /dev/null +++ b/node_modules/node-pty/src/windowsPtyAgent.test.ts @@ -0,0 +1,94 @@ +/** + * Copyright (c) 2017, Daniel Imms (MIT License). + * Copyright (c) 2018, Microsoft Corporation (MIT License). + */ + +import * as assert from 'assert'; +import { argsToCommandLine } from './windowsPtyAgent'; + +function check(file: string, args: string | string[], expected: string): void { + assert.equal(argsToCommandLine(file, args), expected); +} + +if (process.platform === 'win32') { + describe('argsToCommandLine', () => { + describe('Plain strings', () => { + it('doesn\'t quote plain string', () => { + check('asdf', [], 'asdf'); + }); + it('doesn\'t escape backslashes', () => { + check('\\asdf\\qwer\\', [], '\\asdf\\qwer\\'); + }); + it('doesn\'t escape multiple backslashes', () => { + check('asdf\\\\qwer', [], 'asdf\\\\qwer'); + }); + it('adds backslashes before quotes', () => { + check('"asdf"qwer"', [], '\\"asdf\\"qwer\\"'); + }); + it('escapes backslashes before quotes', () => { + check('asdf\\"qwer', [], 'asdf\\\\\\"qwer'); + }); + }); + + describe('Quoted strings', () => { + it('quotes string with spaces', () => { + check('asdf qwer', [], '"asdf qwer"'); + }); + it('quotes empty string', () => { + check('', [], '""'); + }); + it('quotes string with tabs', () => { + check('asdf\tqwer', [], '"asdf\tqwer"'); + }); + it('escapes only the last backslash', () => { + check('\\asdf \\qwer\\', [], '"\\asdf \\qwer\\\\"'); + }); + it('doesn\'t escape multiple backslashes', () => { + check('asdf \\\\qwer', [], '"asdf \\\\qwer"'); + }); + it('escapes backslashes before quotes', () => { + check('asdf \\"qwer', [], '"asdf \\\\\\"qwer"'); + }); + it('escapes multiple backslashes at the end', () => { + check('asdf qwer\\\\', [], '"asdf qwer\\\\\\\\"'); + }); + }); + + describe('Multiple arguments', () => { + it('joins arguments with spaces', () => { + check('asdf', ['qwer zxcv', '', '"'], 'asdf "qwer zxcv" "" \\"'); + }); + it('array argument all in quotes', () => { + check('asdf', ['"surounded by quotes"'], 'asdf \\"surounded by quotes\\"'); + }); + it('array argument quotes in the middle', () => { + check('asdf', ['quotes "in the" middle'], 'asdf "quotes \\"in the\\" middle"'); + }); + it('array argument quotes near start', () => { + check('asdf', ['"quotes" near start'], 'asdf "\\"quotes\\" near start"'); + }); + it('array argument quotes near end', () => { + check('asdf', ['quotes "near end"'], 'asdf "quotes \\"near end\\""'); + }); + }); + + describe('Args as CommandLine', () => { + it('should handle empty string', () => { + check('file', '', 'file'); + }); + it('should not change args', () => { + check('file', 'foo bar baz', 'file foo bar baz'); + check('file', 'foo \\ba"r \baz', 'file foo \\ba"r \baz'); + }); + }); + + describe('Real-world cases', () => { + it('quotes within quotes', () => { + check('cmd.exe', ['/c', 'powershell -noexit -command \'Set-location \"C:\\user\"\''], 'cmd.exe /c "powershell -noexit -command \'Set-location \\\"C:\\user\\"\'"'); + }); + it('space within quotes', () => { + check('cmd.exe', ['/k', '"C:\\Users\\alros\\Desktop\\test script.bat"'], 'cmd.exe /k \\"C:\\Users\\alros\\Desktop\\test script.bat\\"'); + }); + }); + }); +}