|
| 1 | +import { expect } from 'chai'; |
| 2 | +import * as shlex from '@cmt/shlex'; |
| 3 | + |
| 4 | +function splitWin(str: string): string[] { |
| 5 | + return [...shlex.split(str, { mode: 'windows' })]; |
| 6 | +} |
| 7 | + |
| 8 | +function splitUnix(str: string): string[] { |
| 9 | + return [...shlex.split(str, { mode: 'posix' })]; |
| 10 | +} |
| 11 | + |
| 12 | +suite('shlex testing (backend)', () => { |
| 13 | + suite('Windows shell splitting', () => { |
| 14 | + test('Basic token splitting', () => { |
| 15 | + const pairs: [string, string[]][] = [ |
| 16 | + ['foo', ['foo']], |
| 17 | + ['foo bar', ['foo', 'bar']], |
| 18 | + ['', []], |
| 19 | + ['""', ['""']], |
| 20 | + [' Something ', ['Something']], |
| 21 | + ['foo bar', ['foo', 'bar']], |
| 22 | + ['"C:\\Program Files" something', ['"C:\\Program Files"', 'something']], |
| 23 | + ['foo "" bar', ['foo', '""', 'bar']], |
| 24 | + [`\"'fo o'\" bar`, [`\"'fo o'\"`, 'bar']], |
| 25 | + [`'quote arg'`, [`'quote`, `arg'`]], |
| 26 | + ['" fail"', ['" fail"']] |
| 27 | + ]; |
| 28 | + |
| 29 | + for (const [cmd, expected] of pairs) { |
| 30 | + expect(splitWin(cmd)).to.deep.equal(expected, `Bad parse for string: ${cmd}`); |
| 31 | + } |
| 32 | + }); |
| 33 | + |
| 34 | + test('Windows mode: backslash only escapes backslash, not quotes', () => { |
| 35 | + // -DAWESOME=\"\\\"'fo o' bar\\\"\" should preserve all escapes |
| 36 | + const cmd = `-DAWESOME=\"\\\"'fo o' bar\\\"\"`; |
| 37 | + expect(splitWin(cmd)).to.deep.equal([`-DAWESOME=\"\\\"'fo o' bar\\\"\"`]); |
| 38 | + }); |
| 39 | + }); |
| 40 | + |
| 41 | + suite('Posix shell splitting', () => { |
| 42 | + test('Basic token splitting', () => { |
| 43 | + const pairs: [string, string[]][] = [ |
| 44 | + ['foo', ['foo']], |
| 45 | + ['foo bar', ['foo', 'bar']], |
| 46 | + ['', []], |
| 47 | + ['""', ['""']], |
| 48 | + [`''`, [`''`]], |
| 49 | + [' Something ', ['Something']], |
| 50 | + ['foo bar', ['foo', 'bar']], |
| 51 | + ['"C:\\Program Files" something', ['"C:\\Program Files"', 'something']], |
| 52 | + ['foo "" bar', ['foo', '""', 'bar']], |
| 53 | + [`"fo o" bar`, [`"fo o"`, 'bar']], |
| 54 | + [`'quote arg'`, [`'quote arg'`]], |
| 55 | + ['" fail"', ['" fail"']] |
| 56 | + ]; |
| 57 | + |
| 58 | + for (const [cmd, expected] of pairs) { |
| 59 | + expect(splitUnix(cmd)).to.deep.equal(expected, `Bad parse for string: ${cmd}`); |
| 60 | + } |
| 61 | + }); |
| 62 | + }); |
| 63 | + |
| 64 | + suite('Posix escape handling outside quotes', () => { |
| 65 | + test('\\\\ -> single backslash', () => { |
| 66 | + expect(splitUnix('foo\\\\bar')).to.deep.equal(['foo\\bar']); |
| 67 | + }); |
| 68 | + |
| 69 | + test('\\" -> literal quote', () => { |
| 70 | + expect(splitUnix('-DFOO=\\"bar\\"')).to.deep.equal(['-DFOO="bar"']); |
| 71 | + }); |
| 72 | + |
| 73 | + test('\\n (not newline char) -> n', () => { |
| 74 | + expect(splitUnix('foo\\nbar')).to.deep.equal(['foonbar']); |
| 75 | + }); |
| 76 | + |
| 77 | + test('Escaped space keeps token together', () => { |
| 78 | + expect(splitUnix('foo\\ bar')).to.deep.equal(['foo bar']); |
| 79 | + }); |
| 80 | + |
| 81 | + test('Line continuation (actual newline) - backslash+newline consumed', () => { |
| 82 | + expect(splitUnix('foo\\\nbar')).to.deep.equal(['foobar']); |
| 83 | + }); |
| 84 | + |
| 85 | + test('Compile command with escaped quotes (issue #4896)', () => { |
| 86 | + expect(splitUnix('-DIMGUI_USER_CONFIG=\\"frontends/sdl/imgui/sa2_imconfig.h\\"')) |
| 87 | + .to.deep.equal(['-DIMGUI_USER_CONFIG="frontends/sdl/imgui/sa2_imconfig.h"']); |
| 88 | + }); |
| 89 | + }); |
| 90 | + |
| 91 | + suite('Posix escape handling inside double quotes', () => { |
| 92 | + test('Inside double quotes: \\\\ -> \\', () => { |
| 93 | + expect(splitUnix('"foo\\\\bar"')).to.deep.equal(['"foo\\bar"']); |
| 94 | + }); |
| 95 | + |
| 96 | + test('Inside double quotes: \\" -> "', () => { |
| 97 | + expect(splitUnix('"foo\\"bar"')).to.deep.equal(['"foo"bar"']); |
| 98 | + }); |
| 99 | + |
| 100 | + test('Inside double quotes: \\n (not escapable) -> \\n preserved', () => { |
| 101 | + expect(splitUnix('"foo\\nbar"')).to.deep.equal(['"foo\\nbar"']); |
| 102 | + }); |
| 103 | + |
| 104 | + test('Inside double quotes: \\$ -> $', () => { |
| 105 | + expect(splitUnix('"foo\\$bar"')).to.deep.equal(['"foo$bar"']); |
| 106 | + }); |
| 107 | + |
| 108 | + test('Inside double quotes: \\` -> `', () => { |
| 109 | + expect(splitUnix('"foo\\`bar"')).to.deep.equal(['"foo`bar"']); |
| 110 | + }); |
| 111 | + }); |
| 112 | + |
| 113 | + suite('Posix escape handling inside single quotes', () => { |
| 114 | + test('Inside single quotes: backslash has no special meaning', () => { |
| 115 | + expect(splitUnix(`'foo\\bar'`)).to.deep.equal([`'foo\\bar'`]); |
| 116 | + }); |
| 117 | + |
| 118 | + test('Inside single quotes: \\" stays as \\"', () => { |
| 119 | + expect(splitUnix(`'foo\\"bar'`)).to.deep.equal([`'foo\\"bar'`]); |
| 120 | + }); |
| 121 | + |
| 122 | + test('Inside single quotes: \\\\ stays as \\\\', () => { |
| 123 | + expect(splitUnix(`'foo\\\\bar'`)).to.deep.equal([`'foo\\\\bar'`]); |
| 124 | + }); |
| 125 | + }); |
| 126 | + |
| 127 | + // Cross-platform regression tests (macOS uses POSIX mode) |
| 128 | + suite('Cross-platform: mixed quotes (macOS/Linux POSIX)', () => { |
| 129 | + test('Double quote inside single quotes is literal', () => { |
| 130 | + // 'a"b' -> single token with literal " |
| 131 | + expect(splitUnix(`'a"b'`)).to.deep.equal([`'a"b'`]); |
| 132 | + }); |
| 133 | + |
| 134 | + test('Single quote inside double quotes is literal', () => { |
| 135 | + // "a'b" -> single token with literal ' |
| 136 | + expect(splitUnix(`"a'b"`)).to.deep.equal([`"a'b"`]); |
| 137 | + }); |
| 138 | + |
| 139 | + test('Adjacent mixed quotes form single token', () => { |
| 140 | + // "foo"'bar' -> foo and bar concatenated |
| 141 | + expect(splitUnix(`"foo"'bar'`)).to.deep.equal([`"foo"'bar'`]); |
| 142 | + }); |
| 143 | + |
| 144 | + test('Complex mixed quoting with spaces', () => { |
| 145 | + // "foo bar"'baz qux' -> single token |
| 146 | + expect(splitUnix(`"foo bar"'baz qux'`)).to.deep.equal([`"foo bar"'baz qux'`]); |
| 147 | + }); |
| 148 | + }); |
| 149 | + |
| 150 | + suite('Cross-platform: Windows path preservation', () => { |
| 151 | + test('Windows paths in Windows mode preserve backslashes', () => { |
| 152 | + expect(splitWin('"C:\\Users\\test\\file.cpp"')).to.deep.equal(['"C:\\Users\\test\\file.cpp"']); |
| 153 | + }); |
| 154 | + |
| 155 | + test('Windows trailing backslash in quoted path', () => { |
| 156 | + // "C:\path\" arg - the backslash before closing quote escapes the quote in Windows mode, |
| 157 | + // so the entire rest of the string becomes one token (this is existing behavior) |
| 158 | + expect(splitWin('"C:\\path\\" arg')).to.deep.equal(['"C:\\path\\" arg']); |
| 159 | + }); |
| 160 | + |
| 161 | + test('UNC paths in Windows mode', () => { |
| 162 | + // In Windows mode, \\ becomes \ (backslash escapes backslash) |
| 163 | + expect(splitWin('"\\\\server\\share\\file.cpp"')).to.deep.equal(['"\\server\\share\\file.cpp"']); |
| 164 | + }); |
| 165 | + }); |
| 166 | +}); |
0 commit comments