@@ -13,134 +13,6 @@ import {
1313 createCommandValidator ,
1414 containsDangerousSubstitution ,
1515} from "../command-validation"
16- // kilocode_change start
17- import {
18- protectNewlinesInQuotes ,
19- NEWLINE_PLACEHOLDER ,
20- CARRIAGE_RETURN_PLACEHOLDER ,
21- } from "../command-validation-quote-protection"
22- // kilocode_change end
23-
24- // kilocode_change start
25- describe ( "protectNewlinesInQuotes" , ( ) => {
26- const newlinePlaceholder = NEWLINE_PLACEHOLDER
27- const crPlaceholder = CARRIAGE_RETURN_PLACEHOLDER
28-
29- describe ( "basic quote handling" , ( ) => {
30- it ( "protects newlines in double quotes" , ( ) => {
31- const input = 'echo "hello\nworld"'
32- const expected = `echo "hello${ newlinePlaceholder } world"`
33- expect ( protectNewlinesInQuotes ( input , newlinePlaceholder , crPlaceholder ) ) . toBe ( expected )
34- } )
35-
36- it ( "protects newlines in single quotes" , ( ) => {
37- const input = "echo 'hello\nworld'"
38- const expected = `echo 'hello${ newlinePlaceholder } world'`
39- expect ( protectNewlinesInQuotes ( input , newlinePlaceholder , crPlaceholder ) ) . toBe ( expected )
40- } )
41-
42- it ( "does not protect newlines outside quotes" , ( ) => {
43- const input = "echo hello\necho world"
44- const expected = "echo hello\necho world"
45- expect ( protectNewlinesInQuotes ( input , newlinePlaceholder , crPlaceholder ) ) . toBe ( expected )
46- } )
47- } )
48-
49- describe ( "quote concatenation" , ( ) => {
50- it ( "handles quote concatenation where content between quotes is NOT quoted" , ( ) => {
51- // In bash: echo '"'A'"' prints "A" (A is not quoted)
52- const input = `echo '"'A\n'"'`
53- // The newline after A is NOT inside quotes, so it should NOT be protected
54- const expected = `echo '"'A\n'"'`
55- expect ( protectNewlinesInQuotes ( input , newlinePlaceholder , crPlaceholder ) ) . toBe ( expected )
56- } )
57-
58- it ( "handles alternating quotes correctly" , ( ) => {
59- // echo "hello"world"test" -> hello is quoted, world is not, test is quoted
60- const input = `echo "hello\n"world\n"test\n"`
61- const expected = `echo "hello${ newlinePlaceholder } "world\n"test${ newlinePlaceholder } "`
62- expect ( protectNewlinesInQuotes ( input , newlinePlaceholder , crPlaceholder ) ) . toBe ( expected )
63- } )
64-
65- it ( "handles single quote after double quote" , ( ) => {
66- const input = `echo "hello"'world\n'`
67- const expected = `echo "hello"'world${ newlinePlaceholder } '`
68- expect ( protectNewlinesInQuotes ( input , newlinePlaceholder , crPlaceholder ) ) . toBe ( expected )
69- } )
70-
71- it ( "handles double quote after single quote" , ( ) => {
72- const input = `echo 'hello'"world\n"`
73- const expected = `echo 'hello'"world${ newlinePlaceholder } "`
74- expect ( protectNewlinesInQuotes ( input , newlinePlaceholder , crPlaceholder ) ) . toBe ( expected )
75- } )
76- } )
77-
78- describe ( "escaped quotes" , ( ) => {
79- it ( "handles escaped double quotes in double-quoted strings" , ( ) => {
80- const input = 'echo "hello\\"world\n"'
81- const expected = `echo "hello\\"world${ newlinePlaceholder } "`
82- expect ( protectNewlinesInQuotes ( input , newlinePlaceholder , crPlaceholder ) ) . toBe ( expected )
83- } )
84-
85- it ( "does not treat backslash as escape in single quotes" , ( ) => {
86- // In single quotes, backslash is literal (except for \' in some shells)
87- const input = "echo 'hello\\'world\n'"
88- // The \\ is literal, the ' ends the quote, so world\n is outside quotes
89- const expected = "echo 'hello\\'world\n'"
90- expect ( protectNewlinesInQuotes ( input , newlinePlaceholder , crPlaceholder ) ) . toBe ( expected )
91- } )
92- } )
93-
94- describe ( "edge cases" , ( ) => {
95- it ( "handles unclosed quotes" , ( ) => {
96- const input = 'echo "unclosed\n'
97- const expected = `echo "unclosed${ newlinePlaceholder } `
98- expect ( protectNewlinesInQuotes ( input , newlinePlaceholder , crPlaceholder ) ) . toBe ( expected )
99- } )
100-
101- it ( "handles empty string" , ( ) => {
102- expect ( protectNewlinesInQuotes ( "" , newlinePlaceholder , crPlaceholder ) ) . toBe ( "" )
103- } )
104-
105- it ( "handles string with no quotes" , ( ) => {
106- const input = "echo hello\nworld"
107- expect ( protectNewlinesInQuotes ( input , newlinePlaceholder , crPlaceholder ) ) . toBe ( input )
108- } )
109-
110- it ( "handles multiple newlines in quotes" , ( ) => {
111- const input = 'echo "line1\nline2\nline3"'
112- const expected = `echo "line1${ newlinePlaceholder } line2${ newlinePlaceholder } line3"`
113- expect ( protectNewlinesInQuotes ( input , newlinePlaceholder , crPlaceholder ) ) . toBe ( expected )
114- } )
115-
116- it ( "handles carriage returns" , ( ) => {
117- const input = 'echo "hello\rworld"'
118- const expected = `echo "hello${ crPlaceholder } world"`
119- expect ( protectNewlinesInQuotes ( input , newlinePlaceholder , crPlaceholder ) ) . toBe ( expected )
120- } )
121-
122- it ( "handles CRLF" , ( ) => {
123- const input = 'echo "hello\r\nworld"'
124- const expected = `echo "hello${ crPlaceholder } ${ newlinePlaceholder } world"`
125- expect ( protectNewlinesInQuotes ( input , newlinePlaceholder , crPlaceholder ) ) . toBe ( expected )
126- } )
127- } )
128-
129- describe ( "real-world git commit examples" , ( ) => {
130- it ( "protects newlines in git commit message" , ( ) => {
131- const input = `git commit -m "feat: title\n\n- point a\n- point b"`
132- const expected = `git commit -m "feat: title${ newlinePlaceholder } ${ newlinePlaceholder } - point a${ newlinePlaceholder } - point b"`
133- expect ( protectNewlinesInQuotes ( input , newlinePlaceholder , crPlaceholder ) ) . toBe ( expected )
134- } )
135-
136- it ( "handles complex git command with multiple quoted sections" , ( ) => {
137- const input = `git add . && git commit -m "feat: title\n\n- point a" && echo "done\n"`
138- const expected = `git add . && git commit -m "feat: title${ newlinePlaceholder } ${ newlinePlaceholder } - point a" && echo "done${ newlinePlaceholder } "`
139- expect ( protectNewlinesInQuotes ( input , newlinePlaceholder , crPlaceholder ) ) . toBe ( expected )
140- } )
141- } )
142- } )
143- // kilocode_change end
14416
14517describe ( "Command Validation" , ( ) => {
14618 describe ( "parseCommand" , ( ) => {
0 commit comments