@@ -14,28 +14,30 @@ import {
1414 containsDangerousSubstitution ,
1515 protectNewlinesInQuotes ,
1616 NEWLINE_PLACEHOLDER ,
17+ CARRIAGE_RETURN_PLACEHOLDER ,
1718} from "../command-validation"
1819
1920describe ( "protectNewlinesInQuotes" , ( ) => {
20- const placeholder = NEWLINE_PLACEHOLDER
21+ const newlinePlaceholder = NEWLINE_PLACEHOLDER
22+ const crPlaceholder = CARRIAGE_RETURN_PLACEHOLDER
2123
2224 describe ( "basic quote handling" , ( ) => {
2325 it ( "protects newlines in double quotes" , ( ) => {
2426 const input = 'echo "hello\nworld"'
25- const expected = `echo "hello${ placeholder } world"`
26- expect ( protectNewlinesInQuotes ( input , placeholder ) ) . toBe ( expected )
27+ const expected = `echo "hello${ newlinePlaceholder } world"`
28+ expect ( protectNewlinesInQuotes ( input , newlinePlaceholder , crPlaceholder ) ) . toBe ( expected )
2729 } )
2830
2931 it ( "protects newlines in single quotes" , ( ) => {
3032 const input = "echo 'hello\nworld'"
31- const expected = `echo 'hello${ placeholder } world'`
32- expect ( protectNewlinesInQuotes ( input , placeholder ) ) . toBe ( expected )
33+ const expected = `echo 'hello${ newlinePlaceholder } world'`
34+ expect ( protectNewlinesInQuotes ( input , newlinePlaceholder , crPlaceholder ) ) . toBe ( expected )
3335 } )
3436
3537 it ( "does not protect newlines outside quotes" , ( ) => {
3638 const input = "echo hello\necho world"
3739 const expected = "echo hello\necho world"
38- expect ( protectNewlinesInQuotes ( input , placeholder ) ) . toBe ( expected )
40+ expect ( protectNewlinesInQuotes ( input , newlinePlaceholder , crPlaceholder ) ) . toBe ( expected )
3941 } )
4042 } )
4143
@@ -45,91 +47,91 @@ describe("protectNewlinesInQuotes", () => {
4547 const input = `echo '"'A\n'"'`
4648 // The newline after A is NOT inside quotes, so it should NOT be protected
4749 const expected = `echo '"'A\n'"'`
48- expect ( protectNewlinesInQuotes ( input , placeholder ) ) . toBe ( expected )
50+ expect ( protectNewlinesInQuotes ( input , newlinePlaceholder , crPlaceholder ) ) . toBe ( expected )
4951 } )
5052
5153 it ( "handles alternating quotes correctly" , ( ) => {
5254 // echo "hello"world"test" -> hello is quoted, world is not, test is quoted
5355 const input = `echo "hello\n"world\n"test\n"`
54- const expected = `echo "hello${ placeholder } "world\n"test${ placeholder } "`
55- expect ( protectNewlinesInQuotes ( input , placeholder ) ) . toBe ( expected )
56+ const expected = `echo "hello${ newlinePlaceholder } "world\n"test${ newlinePlaceholder } "`
57+ expect ( protectNewlinesInQuotes ( input , newlinePlaceholder , crPlaceholder ) ) . toBe ( expected )
5658 } )
5759
5860 it ( "handles single quote after double quote" , ( ) => {
5961 const input = `echo "hello"'world\n'`
60- const expected = `echo "hello"'world${ placeholder } '`
61- expect ( protectNewlinesInQuotes ( input , placeholder ) ) . toBe ( expected )
62+ const expected = `echo "hello"'world${ newlinePlaceholder } '`
63+ expect ( protectNewlinesInQuotes ( input , newlinePlaceholder , crPlaceholder ) ) . toBe ( expected )
6264 } )
6365
6466 it ( "handles double quote after single quote" , ( ) => {
6567 const input = `echo 'hello'"world\n"`
66- const expected = `echo 'hello'"world${ placeholder } "`
67- expect ( protectNewlinesInQuotes ( input , placeholder ) ) . toBe ( expected )
68+ const expected = `echo 'hello'"world${ newlinePlaceholder } "`
69+ expect ( protectNewlinesInQuotes ( input , newlinePlaceholder , crPlaceholder ) ) . toBe ( expected )
6870 } )
6971 } )
7072
7173 describe ( "escaped quotes" , ( ) => {
7274 it ( "handles escaped double quotes in double-quoted strings" , ( ) => {
7375 const input = 'echo "hello\\"world\n"'
74- const expected = `echo "hello\\"world${ placeholder } "`
75- expect ( protectNewlinesInQuotes ( input , placeholder ) ) . toBe ( expected )
76+ const expected = `echo "hello\\"world${ newlinePlaceholder } "`
77+ expect ( protectNewlinesInQuotes ( input , newlinePlaceholder , crPlaceholder ) ) . toBe ( expected )
7678 } )
7779
7880 it ( "does not treat backslash as escape in single quotes" , ( ) => {
7981 // In single quotes, backslash is literal (except for \' in some shells)
8082 const input = "echo 'hello\\'world\n'"
8183 // The \\ is literal, the ' ends the quote, so world\n is outside quotes
8284 const expected = "echo 'hello\\'world\n'"
83- expect ( protectNewlinesInQuotes ( input , placeholder ) ) . toBe ( expected )
85+ expect ( protectNewlinesInQuotes ( input , newlinePlaceholder , crPlaceholder ) ) . toBe ( expected )
8486 } )
8587 } )
8688
8789 describe ( "edge cases" , ( ) => {
8890 it ( "handles unclosed quotes" , ( ) => {
8991 const input = 'echo "unclosed\n'
90- const expected = `echo "unclosed${ placeholder } `
91- expect ( protectNewlinesInQuotes ( input , placeholder ) ) . toBe ( expected )
92+ const expected = `echo "unclosed${ newlinePlaceholder } `
93+ expect ( protectNewlinesInQuotes ( input , newlinePlaceholder , crPlaceholder ) ) . toBe ( expected )
9294 } )
9395
9496 it ( "handles empty string" , ( ) => {
95- expect ( protectNewlinesInQuotes ( "" , placeholder ) ) . toBe ( "" )
97+ expect ( protectNewlinesInQuotes ( "" , newlinePlaceholder , crPlaceholder ) ) . toBe ( "" )
9698 } )
9799
98100 it ( "handles string with no quotes" , ( ) => {
99101 const input = "echo hello\nworld"
100- expect ( protectNewlinesInQuotes ( input , placeholder ) ) . toBe ( input )
102+ expect ( protectNewlinesInQuotes ( input , newlinePlaceholder , crPlaceholder ) ) . toBe ( input )
101103 } )
102104
103105 it ( "handles multiple newlines in quotes" , ( ) => {
104106 const input = 'echo "line1\nline2\nline3"'
105- const expected = `echo "line1${ placeholder } line2${ placeholder } line3"`
106- expect ( protectNewlinesInQuotes ( input , placeholder ) ) . toBe ( expected )
107+ const expected = `echo "line1${ newlinePlaceholder } line2${ newlinePlaceholder } line3"`
108+ expect ( protectNewlinesInQuotes ( input , newlinePlaceholder , crPlaceholder ) ) . toBe ( expected )
107109 } )
108110
109111 it ( "handles carriage returns" , ( ) => {
110112 const input = 'echo "hello\rworld"'
111- const expected = `echo "hello${ placeholder } world"`
112- expect ( protectNewlinesInQuotes ( input , placeholder ) ) . toBe ( expected )
113+ const expected = `echo "hello${ crPlaceholder } world"`
114+ expect ( protectNewlinesInQuotes ( input , newlinePlaceholder , crPlaceholder ) ) . toBe ( expected )
113115 } )
114116
115117 it ( "handles CRLF" , ( ) => {
116118 const input = 'echo "hello\r\nworld"'
117- const expected = `echo "hello${ placeholder } ${ placeholder } world"`
118- expect ( protectNewlinesInQuotes ( input , placeholder ) ) . toBe ( expected )
119+ const expected = `echo "hello${ crPlaceholder } ${ newlinePlaceholder } world"`
120+ expect ( protectNewlinesInQuotes ( input , newlinePlaceholder , crPlaceholder ) ) . toBe ( expected )
119121 } )
120122 } )
121123
122124 describe ( "real-world git commit examples" , ( ) => {
123125 it ( "protects newlines in git commit message" , ( ) => {
124126 const input = `git commit -m "feat: title\n\n- point a\n- point b"`
125- const expected = `git commit -m "feat: title${ placeholder } ${ placeholder } - point a${ placeholder } - point b"`
126- expect ( protectNewlinesInQuotes ( input , placeholder ) ) . toBe ( expected )
127+ const expected = `git commit -m "feat: title${ newlinePlaceholder } ${ newlinePlaceholder } - point a${ newlinePlaceholder } - point b"`
128+ expect ( protectNewlinesInQuotes ( input , newlinePlaceholder , crPlaceholder ) ) . toBe ( expected )
127129 } )
128130
129131 it ( "handles complex git command with multiple quoted sections" , ( ) => {
130132 const input = `git add . && git commit -m "feat: title\n\n- point a" && echo "done\n"`
131- const expected = `git add . && git commit -m "feat: title${ placeholder } ${ placeholder } - point a" && echo "done${ placeholder } "`
132- expect ( protectNewlinesInQuotes ( input , placeholder ) ) . toBe ( expected )
133+ const expected = `git add . && git commit -m "feat: title${ newlinePlaceholder } ${ newlinePlaceholder } - point a" && echo "done${ newlinePlaceholder } "`
134+ expect ( protectNewlinesInQuotes ( input , newlinePlaceholder , crPlaceholder ) ) . toBe ( expected )
133135 } )
134136 } )
135137} )
0 commit comments