| 1 |
// Copyright (c) 2016, Daniel Martí <[email protected]> |
| 2 |
// See LICENSE for licensing information |
| 3 |
|
| 4 |
package syntax |
| 5 |
|
| 6 |
import ( |
| 7 |
"bytes" |
| 8 |
"cmp" |
| 9 |
"fmt" |
| 10 |
"os" |
| 11 |
"regexp" |
| 12 |
"strings" |
| 13 |
"testing" |
| 14 |
) |
| 15 |
|
| 16 |
func TestPrintFiles(t *testing.T) { |
| 17 |
t.Parallel() |
| 18 |
for lang := range langResolvedVariants.bits() { |
| 19 |
t.Run(lang.String(), func(t *testing.T) { |
| 20 |
parser := NewParser(Variant(lang), KeepComments(true)) |
| 21 |
printer := NewPrinter() |
| 22 |
for _, c := range append(fileTests, fileTestsKeepComments...) { |
| 23 |
want, _ := c.byLangIndex[lang.index()].(*File) |
| 24 |
if want == nil { |
| 25 |
continue |
| 26 |
} |
| 27 |
if wantBash, _ := c.byLangIndex[LangBash.index()].(*File); wantBash != nil && |
| 28 |
lang != LangBash && want != wantBash { |
| 29 |
// Skip cases where a non-Bash language parses differently than Bash. |
| 30 |
// For example, `((foo))` prints as `( (foo))` only when in POSIX. |
| 31 |
continue |
| 32 |
} |
| 33 |
t.Run("", func(t *testing.T) { |
| 34 |
in := c.inputs[0] |
| 35 |
printTest(t, parser, printer, in, cmp.Or(c.printedAs, in)) |
| 36 |
}) |
| 37 |
} |
| 38 |
}) |
| 39 |
} |
| 40 |
} |
| 41 |
|
| 42 |
func strPrint(p *Printer, node Node) (string, error) { |
| 43 |
var buf bytes.Buffer |
| 44 |
err := p.Print(&buf, node) |
| 45 |
return buf.String(), err |
| 46 |
} |
| 47 |
|
| 48 |
type printCase struct { |
| 49 |
in, want string |
| 50 |
} |
| 51 |
|
| 52 |
func samePrint(s string) printCase { return printCase{in: s, want: s} } |
| 53 |
|
| 54 |
var printTests = []printCase{ |
| 55 |
samePrint(`fo○ b\år`), |
| 56 |
samePrint(`"fo○ b\år"`), |
| 57 |
samePrint(`'fo○ b\år'`), |
| 58 |
samePrint(`${a#fo○ b\år}`), |
| 59 |
samePrint(`#fo○ b\år`), |
| 60 |
samePrint("<<EOF\nfo○ b\\år\nEOF"), |
| 61 |
samePrint(`$'○ b\år'`), |
| 62 |
samePrint("${a/b//○}"), |
| 63 |
{strings.Repeat(" ", bufSize-3) + "○", "○"}, // at the end of a chunk |
| 64 |
{strings.Repeat(" ", bufSize-0) + "○", "○"}, // at the start of a chunk |
| 65 |
{strings.Repeat(" ", bufSize-2) + "○", "○"}, // split after 1st byte |
| 66 |
{strings.Repeat(" ", bufSize-1) + "○", "○"}, // split after 2nd byte |
| 67 |
// peekByte that would (but cannot) go to the next chunk |
| 68 |
{strings.Repeat(" ", bufSize-2) + ">(a)", ">(a)"}, |
| 69 |
// escaped newline at end of chunk |
| 70 |
{"a" + strings.Repeat(" ", bufSize-2) + "\\\nb", "a \\\n\tb"}, |
| 71 |
// panics if padding is only 4 (utf8.UTFMax) |
| 72 |
{strings.Repeat(" ", bufSize-10) + "${a/b//○}", "${a/b//○}"}, |
| 73 |
// multiple p.fill calls |
| 74 |
{"a" + strings.Repeat(" ", bufSize*4) + "b", "a b"}, |
| 75 |
// newline at the beginning of second chunk |
| 76 |
{"a" + strings.Repeat(" ", bufSize-2) + "\nb", "a\nb"}, |
| 77 |
{"foo; bar", "foo\nbar"}, |
| 78 |
{"foo\n\n\nbar", "foo\n\nbar"}, |
| 79 |
{"foo\n\n", "foo"}, |
| 80 |
{"\n\nfoo", "foo"}, |
| 81 |
{"# foo \n # bar\t", "# foo\n# bar"}, |
| 82 |
samePrint("#"), |
| 83 |
samePrint("#c1\\\n#c2"), |
| 84 |
samePrint("#\\\n#"), |
| 85 |
{"#\\\r\n#", "#\\\n#"}, |
| 86 |
samePrint("{\n\tfoo\n\t# bar \\\n}"), |
| 87 |
samePrint("foo\\\\\nbar"), |
| 88 |
samePrint("a=b # inline\nbar"), |
| 89 |
samePrint("a=$(b) # inline"), |
| 90 |
samePrint("foo # inline\n# after"), |
| 91 |
samePrint("$(a) $(b)"), |
| 92 |
{"if a\nthen\n\tb\nfi", "if a; then\n\tb\nfi"}, |
| 93 |
samePrint("if a; then\n\tb\nelse\n\tc\nfi"), |
| 94 |
{"if a; then b\nelse c\nfi", "if a; then\n\tb\nelse\n\tc\nfi"}, |
| 95 |
samePrint("foo >&2 <f bar"), |
| 96 |
samePrint("foo >&2 bar <f"), |
| 97 |
samePrint(">&2 foo bar <f"), |
| 98 |
samePrint(">&2 foo"), |
| 99 |
samePrint(">&2 foo 2>&1 bar <f"), |
| 100 |
{"foo >&2>/dev/null", "foo >&2 >/dev/null"}, |
| 101 |
samePrint("exec {foo[1]}>&-"), |
| 102 |
samePrint("exec {foo}>&-"), |
| 103 |
{"foo <<EOF bar\nl1\nEOF", "foo bar <<EOF\nl1\nEOF"}, |
| 104 |
samePrint("foo <<\\\\\\\\EOF\nbar\n\\\\EOF"), |
| 105 |
samePrint("foo <<\"\\EOF\"\nbar\n\\EOF"), |
| 106 |
samePrint("foo <<\"EOF\"\nbar\nEOF\nbar"), |
| 107 |
samePrint("foo <<EOF && bar\nl1\nEOF"), |
| 108 |
samePrint("foo <<EOF &&\nl1\nEOF\n\tbar"), |
| 109 |
samePrint("foo <<EOF\nl1\nEOF\n\nfoo2"), |
| 110 |
samePrint("<<EOF\nfoo\\\nbar\nEOF"), |
| 111 |
samePrint("<<'EOF'\nfoo\\\nbar\nEOF"), |
| 112 |
samePrint("<<EOF\n\\\n$foo\nEOF"), |
| 113 |
samePrint("<<'EOF'\n\\\nEOF"), |
| 114 |
samePrint("{\n\t<<EOF\nfoo\\\nbar\nEOF\n}"), |
| 115 |
samePrint("{\n\t<<'EOF'\nfoo\\\nbar\nEOF\n}"), |
| 116 |
samePrint("<<EOF\nEOF"), |
| 117 |
samePrint("foo <<EOF\nEOF\n\nbar"), |
| 118 |
samePrint("foo <<'EOF'\nEOF\n\nbar"), |
| 119 |
samePrint("if cmd <<EOF\nbody\nEOF\nthen\n\tfoo\nfi"), |
| 120 |
samePrint("while cmd <<EOF\nbody\nEOF\ndo\n\tfoo\ndone"), |
| 121 |
samePrint("if true; then\n\tcat <<-EOF # comment\n\t\tcontent\n\tEOF\nfi"), |
| 122 |
{ |
| 123 |
"{ foo; bar; }", |
| 124 |
"{\n\tfoo\n\tbar\n}", |
| 125 |
}, |
| 126 |
{ |
| 127 |
"{ foo; bar; }\n#etc", |
| 128 |
"{\n\tfoo\n\tbar\n}\n#etc", |
| 129 |
}, |
| 130 |
{ |
| 131 |
"{\n\tfoo; }", |
| 132 |
"{\n\tfoo\n}", |
| 133 |
}, |
| 134 |
{ |
| 135 |
"{ foo\n}", |
| 136 |
"{\n\tfoo\n}", |
| 137 |
}, |
| 138 |
{ |
| 139 |
"(foo\n)", |
| 140 |
"(\n\tfoo\n)", |
| 141 |
}, |
| 142 |
{ |
| 143 |
"$(foo\n)", |
| 144 |
"$(\n\tfoo\n)", |
| 145 |
}, |
| 146 |
{ |
| 147 |
"a\n\n\n# etc\nb", |
| 148 |
"a\n\n# etc\nb", |
| 149 |
}, |
| 150 |
{ |
| 151 |
"a b\\\nc d", |
| 152 |
"a bc d", |
| 153 |
}, |
| 154 |
{ |
| 155 |
"a bb\\\ncc d", |
| 156 |
"a bbcc d", |
| 157 |
}, |
| 158 |
samePrint("a \\\n\tb \\\n\t\"c\" \\\n\t;"), |
| 159 |
samePrint("a=1 \\\n\tb=2 \\\n\tc=\"3\" \\\n\t;"), |
| 160 |
{ |
| 161 |
"a=\\\nfoo\nb=\\\n\"bar\"\nc=\\\n'baz'", |
| 162 |
"a=foo\nb=\"bar\"\nc='baz'", |
| 163 |
}, |
| 164 |
|
| 165 |
samePrint("if a \\\n\t; then b; fi"), |
| 166 |
samePrint("a > \\\n\tfoo"), |
| 167 |
samePrint("a <<< \\\n\t\"foo\""), |
| 168 |
samePrint("a 'b\nb' c"), |
| 169 |
samePrint("a $'b\nb' c"), |
| 170 |
{ |
| 171 |
"(foo; bar)", |
| 172 |
"(\n\tfoo\n\tbar\n)", |
| 173 |
}, |
| 174 |
{ |
| 175 |
"{\nfoo\nbar; }", |
| 176 |
"{\n\tfoo\n\tbar\n}", |
| 177 |
}, |
| 178 |
samePrint("\"$foo\"\n{\n\tbar\n}"), |
| 179 |
{ |
| 180 |
"{\nbar\n# extra\n}", |
| 181 |
"{\n\tbar\n\t# extra\n}", |
| 182 |
}, |
| 183 |
{ |
| 184 |
"foo\nbar # extra", |
| 185 |
"foo\nbar # extra", |
| 186 |
}, |
| 187 |
{ |
| 188 |
"foo # 1\nfooo # 2\nfo # 3", |
| 189 |
"foo # 1\nfooo # 2\nfo # 3", |
| 190 |
}, |
| 191 |
{ |
| 192 |
" foo # 1\n fooo # 2\n fo # 3", |
| 193 |
"foo # 1\nfooo # 2\nfo # 3", |
| 194 |
}, |
| 195 |
{ |
| 196 |
"foo # 1\nfooo # 2\nfo # 3", |
| 197 |
"foo # 1\nfooo # 2\nfo # 3", |
| 198 |
}, |
| 199 |
{ |
| 200 |
"foooooa\nfoo # 1\nfooo # 2\nfo # 3\nfooooo", |
| 201 |
"foooooa\nfoo # 1\nfooo # 2\nfo # 3\nfooooo", |
| 202 |
}, |
| 203 |
{ |
| 204 |
"foo\nbar\nfoo # 1\nfooo # 2", |
| 205 |
"foo\nbar\nfoo # 1\nfooo # 2", |
| 206 |
}, |
| 207 |
samePrint("foobar # 1\nfoo\nfoo # 2"), |
| 208 |
samePrint("foobar # 1\n#foo\nfoo # 2"), |
| 209 |
samePrint("foobar # 1\n\nfoo # 2"), |
| 210 |
{ |
| 211 |
"foo # 2\nfoo2 bar # 1", |
| 212 |
"foo # 2\nfoo2 bar # 1", |
| 213 |
}, |
| 214 |
{ |
| 215 |
"foo bar # 1\n! foo # 2", |
| 216 |
"foo bar # 1\n! foo # 2", |
| 217 |
}, |
| 218 |
{ |
| 219 |
"aa #b\nc #d\ne\nf #g", |
| 220 |
"aa #b\nc #d\ne\nf #g", |
| 221 |
}, |
| 222 |
{ |
| 223 |
"{ a; } #x\nbbb #y\n{ #z\nc\n}", |
| 224 |
"{ a; } #x\nbbb #y\n{ #z\n\tc\n}", |
| 225 |
}, |
| 226 |
{ |
| 227 |
"foo; foooo # 1", |
| 228 |
"foo\nfoooo # 1", |
| 229 |
}, |
| 230 |
{ |
| 231 |
"aaa; b #1\nc #2", |
| 232 |
"aaa\nb #1\nc #2", |
| 233 |
}, |
| 234 |
{ |
| 235 |
"a #1\nbbb; c #2\nd #3", |
| 236 |
"a #1\nbbb\nc #2\nd #3", |
| 237 |
}, |
| 238 |
samePrint("aa #c1\n{ #c2\n\tb\n}"), |
| 239 |
{ |
| 240 |
"aa #c1\n{ b; c; } #c2", |
| 241 |
"aa #c1\n{\n\tb\n\tc\n} #c2", |
| 242 |
}, |
| 243 |
samePrint("a #c1\n'b\ncc' #c2"), |
| 244 |
{ |
| 245 |
"(\nbar\n# extra\n)", |
| 246 |
"(\n\tbar\n\t# extra\n)", |
| 247 |
}, |
| 248 |
{ |
| 249 |
"for a in 1 2\ndo\n\t# bar\n\tx\ndone", |
| 250 |
"for a in 1 2; do\n\t# bar\n\tx\ndone", |
| 251 |
}, |
| 252 |
samePrint("#before\nfoo | bar"), |
| 253 |
samePrint("#before\nfoo && bar"), |
| 254 |
samePrint("foo | bar # inline"), |
| 255 |
samePrint("foo && bar # inline"), |
| 256 |
samePrint("foo `# inline` \\\n\tbar"), |
| 257 |
samePrint("for a in 1 2; do\n\n\tbar\ndone"), |
| 258 |
{ |
| 259 |
"a \\\n\t&& b", |
| 260 |
"a &&\n\tb", |
| 261 |
}, |
| 262 |
{ |
| 263 |
"a \\\n\t&& b\nc", |
| 264 |
"a &&\n\tb\nc", |
| 265 |
}, |
| 266 |
{ |
| 267 |
"{\n(a \\\n&& b)\nc\n}", |
| 268 |
"{\n\t(a &&\n\t\tb)\n\tc\n}", |
| 269 |
}, |
| 270 |
{ |
| 271 |
"a && b \\\n&& c", |
| 272 |
"a && b &&\n\tc", |
| 273 |
}, |
| 274 |
{ |
| 275 |
"a \\\n&& $(b) && c \\\n&& d", |
| 276 |
"a &&\n\t$(b) && c &&\n\td", |
| 277 |
}, |
| 278 |
{ |
| 279 |
"a \\\n&& b\nc \\\n&& d", |
| 280 |
"a &&\n\tb\nc &&\n\td", |
| 281 |
}, |
| 282 |
{ |
| 283 |
"a \\\n&&\n#c\nb", |
| 284 |
"a &&\n\t#c\n\tb", |
| 285 |
}, |
| 286 |
{ |
| 287 |
"a | {\nb \\\n| c\n}", |
| 288 |
"a | {\n\tb |\n\t\tc\n}", |
| 289 |
}, |
| 290 |
{ |
| 291 |
"a \\\n\t&& if foo; then\nbar\nfi", |
| 292 |
"a &&\n\tif foo; then\n\t\tbar\n\tfi", |
| 293 |
}, |
| 294 |
{ |
| 295 |
"if\nfoo\nthen\nbar\nfi", |
| 296 |
"if\n\tfoo\nthen\n\tbar\nfi", |
| 297 |
}, |
| 298 |
{ |
| 299 |
"if foo \\\nbar\nthen\nbar\nfi", |
| 300 |
"if foo \\\n\tbar; then\n\tbar\nfi", |
| 301 |
}, |
| 302 |
{ |
| 303 |
"if foo \\\n&& bar\nthen\nbar\nfi", |
| 304 |
"if foo &&\n\tbar; then\n\tbar\nfi", |
| 305 |
}, |
| 306 |
{ |
| 307 |
"a |\nb |\nc", |
| 308 |
"a |\n\tb |\n\tc", |
| 309 |
}, |
| 310 |
samePrint("a |\n\tb | c |\n\td"), |
| 311 |
samePrint("a | b |\n\tc |\n\td"), |
| 312 |
{ |
| 313 |
"foo |\n# misplaced\nbar", |
| 314 |
"foo |\n\t# misplaced\n\tbar", |
| 315 |
}, |
| 316 |
samePrint("{\n\tfoo\n\t#a\n\tbar\n} | etc"), |
| 317 |
{ |
| 318 |
"foo &&\n#a1\n#a2\n$(bar)", |
| 319 |
"foo &&\n\t#a1\n\t#a2\n\t$(bar)", |
| 320 |
}, |
| 321 |
{ |
| 322 |
"{\n\tfoo\n\t#a\n} |\n# misplaced\nbar", |
| 323 |
"{\n\tfoo\n\t#a\n} |\n\t# misplaced\n\tbar", |
| 324 |
}, |
| 325 |
samePrint("foo | bar\n#after"), |
| 326 |
{ |
| 327 |
"a |\nb | #c2\nc", |
| 328 |
"a |\n\tb | #c2\n\tc", |
| 329 |
}, |
| 330 |
{ |
| 331 |
"{\nfoo &&\n#a1\n#a2\n$(bar)\n}", |
| 332 |
"{\n\tfoo &&\n\t\t#a1\n\t\t#a2\n\t\t$(bar)\n}", |
| 333 |
}, |
| 334 |
{ |
| 335 |
"foo | while read l; do\nbar\ndone", |
| 336 |
"foo | while read l; do\n\tbar\ndone", |
| 337 |
}, |
| 338 |
samePrint("while x; do\n\t#comment\n\ty\ndone"), |
| 339 |
{ |
| 340 |
"while x\ndo\n\ty\ndone", |
| 341 |
"while x; do\n\ty\ndone", |
| 342 |
}, |
| 343 |
samePrint("\"\\\nfoo\""), |
| 344 |
samePrint("'\\\nfoo'"), |
| 345 |
samePrint("\"foo\\\n bar\""), |
| 346 |
samePrint("'foo\\\n bar'"), |
| 347 |
samePrint("v=\"\\\nfoo\""), |
| 348 |
{ |
| 349 |
"v=foo\\\nbar", |
| 350 |
"v=foobar", |
| 351 |
}, |
| 352 |
{ |
| 353 |
"v='foo'\\\n'bar'", |
| 354 |
"v='foo''bar'", |
| 355 |
}, |
| 356 |
{ |
| 357 |
"v=\\\n\"foo\"", |
| 358 |
"v=\"foo\"", |
| 359 |
}, |
| 360 |
{ |
| 361 |
"v=\\\nfoo\\\n$bar", |
| 362 |
"v=foo$bar", |
| 363 |
}, |
| 364 |
samePrint("\"\\\n\\\nfoo\\\n\\\n\""), |
| 365 |
samePrint("'\\\n\\\nfoo\\\n\\\n'"), |
| 366 |
{ |
| 367 |
"foo \\\n>bar\netc", |
| 368 |
"foo \\\n\t>bar\netc", |
| 369 |
}, |
| 370 |
{ |
| 371 |
"foo \\\nfoo2 \\\n>bar", |
| 372 |
"foo \\\n\tfoo2 \\\n\t>bar", |
| 373 |
}, |
| 374 |
samePrint("> >(foo)"), |
| 375 |
samePrint("x > >(foo) y"), |
| 376 |
samePrint("a | (x) |\n\tb"), |
| 377 |
samePrint("a | (\n\tx\n\ty\n) |\n\tb"), |
| 378 |
samePrint("a |\n\tif foo; then\n\t\tbar\n\tfi |\n\tb"), |
| 379 |
samePrint("a | if foo; then\n\tbar\nfi"), |
| 380 |
samePrint("a | b | if foo; then\n\tbar\nfi"), |
| 381 |
{ |
| 382 |
"case $i in\n1)\nfoo\n;;\nesac", |
| 383 |
"case $i in\n1)\n\tfoo\n\t;;\nesac", |
| 384 |
}, |
| 385 |
{ |
| 386 |
"case $i in\n1)\nfoo\nesac", |
| 387 |
"case $i in\n1)\n\tfoo\n\t;;\nesac", |
| 388 |
}, |
| 389 |
{ |
| 390 |
"case $i in\n1) foo\nesac", |
| 391 |
"case $i in\n1) foo ;;\nesac", |
| 392 |
}, |
| 393 |
{ |
| 394 |
"case $i in\n1) foo; bar\nesac", |
| 395 |
"case $i in\n1)\n\tfoo\n\tbar\n\t;;\nesac", |
| 396 |
}, |
| 397 |
{ |
| 398 |
"case $i in\n1) foo; bar;;\nesac", |
| 399 |
"case $i in\n1)\n\tfoo\n\tbar\n\t;;\nesac", |
| 400 |
}, |
| 401 |
{ |
| 402 |
"case $i in\n1)\n#foo \t\n;;\nesac", |
| 403 |
"case $i in\n1)\n\t#foo\n\t;;\nesac", |
| 404 |
}, |
| 405 |
samePrint("case $i in\n1)\n\t;;\n\n2)\n\t;;\nesac"), |
| 406 |
{ |
| 407 |
"case $i\nin\n1)\n\t;;\nesac", |
| 408 |
"case $i in\n1)\n\t;;\nesac", |
| 409 |
}, |
| 410 |
samePrint("case $i in\n1)\n\ta\n\t#b\n\t;;\nesac"), |
| 411 |
samePrint("case $i in\n1) foo() { bar; } ;;\nesac"), |
| 412 |
samePrint("case $i in\n1) ;; #foo\nesac"), |
| 413 |
samePrint("case $i in\n#foo\nesac"), |
| 414 |
samePrint("case $i in\n#before\n1) ;;\nesac"), |
| 415 |
samePrint("case $i in\n#bef\n1) ;; #inl\nesac"), |
| 416 |
samePrint("case $i in\n#before1\n'1') ;;\n#before2\n'2') ;;\nesac"), |
| 417 |
samePrint("case $i in\n1) ;; #inl1\n2) ;; #inl2\nesac"), |
| 418 |
samePrint("case $i in\n#bef\n1) #inl\n\tfoo\n\t;;\nesac"), |
| 419 |
samePrint("case $i in\n1) #inl\n\t;;\nesac"), |
| 420 |
samePrint("case $i in\n1) a \\\n\tb ;;\nesac"), |
| 421 |
samePrint("case $i in\n1 | 2 | \\\n\t3 | 4) a b ;;\nesac"), |
| 422 |
samePrint("case $i in\n1 | 2 | \\\n\t3 | 4)\n\ta b\n\t;;\nesac"), |
| 423 |
samePrint("case $i in\nx) ;;\ny) for n in 1; do echo $n; done ;;\nesac"), |
| 424 |
samePrint("case a in b) [[ x =~ y ]] ;; esac"), |
| 425 |
samePrint("case a in b) [[ a =~ b$ || c =~ d$ ]] ;; esac"), |
| 426 |
samePrint("case a in b) [[ a =~ (b) ]] ;; esac"), |
| 427 |
samePrint("[[ (a =~ b$) ]]"), |
| 428 |
samePrint("[[ a && ((b || c) && d) ]]"), |
| 429 |
samePrint("[[ a &&\n\tb ]]"), |
| 430 |
samePrint("[[ a ||\n\tb ]]"), |
| 431 |
{ |
| 432 |
"[[ -f \\\n\tfoo ]]", |
| 433 |
"[[ -f foo ]]", |
| 434 |
}, |
| 435 |
{ |
| 436 |
"[[ foo \\\n\t-ef \\\n\tbar ]]", |
| 437 |
"[[ foo -ef bar ]]", |
| 438 |
}, |
| 439 |
{ |
| 440 |
"[[ a && \\\nb \\\n && c ]]", |
| 441 |
"[[ a &&\n\tb &&\n\tc ]]", |
| 442 |
}, |
| 443 |
samePrint("{\n\t[[ a || b ]]\n}"), |
| 444 |
{ |
| 445 |
"a=(\nb\nc\n) b=c", |
| 446 |
"a=(\n\tb\n\tc\n) b=c", |
| 447 |
}, |
| 448 |
samePrint("a=(\n\t#before\n\tb #inline\n)"), |
| 449 |
samePrint("a=(\n\tb #foo\n\tc #bar\n)"), |
| 450 |
samePrint("a=(\n\tb\n\n\t#foo\n\t#bar\n\tc\n)"), |
| 451 |
samePrint("a=(\n\t#foo\n\t#bar\n\tc\n)"), |
| 452 |
samePrint("a=(\n\t#lone\n)"), |
| 453 |
samePrint("a=(\n\n)"), |
| 454 |
samePrint("a=(\n\tx\n\n\ty\n)"), |
| 455 |
samePrint("foo <<EOF | $(bar)\n3\nEOF"), |
| 456 |
{ |
| 457 |
"a <<EOF\n$(\n\tb\n\tc)\nEOF", |
| 458 |
"a <<EOF\n$(\n\tb\n\tc\n)\nEOF", |
| 459 |
}, |
| 460 |
samePrint("<<EOF1\n$(\n\t<<EOF2\ninner\nEOF2\n)\nEOF1"), |
| 461 |
{ |
| 462 |
"<(<<EOF\nbody\nEOF\n)", |
| 463 |
"<(\n\t<<EOF\nbody\nEOF\n)", |
| 464 |
}, |
| 465 |
{ |
| 466 |
"( (foo) )\n$( (foo) )\n<( (foo) )", |
| 467 |
"( (foo) )\n$( (foo) )\n<((foo))", |
| 468 |
}, |
| 469 |
{ |
| 470 |
"if ( ((foo)) || bar ); then baz; fi", |
| 471 |
"if ( ((foo)) || bar); then baz; fi", |
| 472 |
}, |
| 473 |
samePrint("if x; then (\n\ty\n) & fi"), |
| 474 |
samePrint("\"foo\n$(bar)\""), |
| 475 |
samePrint("\"foo\\\n$(bar)\""), |
| 476 |
samePrint("\"foo\\\nbar\""), |
| 477 |
samePrint("((foo++)) || bar"), |
| 478 |
{ |
| 479 |
"(( ! 0 )) && echo true", |
| 480 |
"((! 0)) && echo true", |
| 481 |
}, |
| 482 |
{ |
| 483 |
"a=b \\\nc=d \\\nfoo", |
| 484 |
"a=b \\\n\tc=d \\\n\tfoo", |
| 485 |
}, |
| 486 |
{ |
| 487 |
"a=b \\\nc=d \\\nfoo \\\nbar", |
| 488 |
"a=b \\\n\tc=d \\\n\tfoo \\\n\tbar", |
| 489 |
}, |
| 490 |
samePrint("a $(x) \\\n\tb"), |
| 491 |
samePrint("\"foo\nbar\"\netc"), |
| 492 |
samePrint("\"foo\nbar\nbar2\"\netc"), |
| 493 |
samePrint("a=\"$b\n\"\nd=e"), |
| 494 |
samePrint("\"\n\"\n\nfoo"), |
| 495 |
samePrint("$\"\n\"\n\nfoo"), |
| 496 |
samePrint("'\n'\n\nfoo"), |
| 497 |
samePrint("$'\n'\n\nfoo"), |
| 498 |
samePrint("foo <<EOF\na\nb\nc\nd\nEOF\n{\n\tbar\n}"), |
| 499 |
samePrint("foo bar # one\nif a; then\n\tb\nfi # two"), |
| 500 |
{ |
| 501 |
"# foo\n\n\nbar", |
| 502 |
"# foo\n\nbar", |
| 503 |
}, |
| 504 |
{ |
| 505 |
"# foo\n\n\nbar\nbaz", |
| 506 |
"# foo\n\nbar\nbaz", |
| 507 |
}, |
| 508 |
samePrint("#foo\n#\n#bar"), |
| 509 |
{ |
| 510 |
"(0 #\n0)#\n0", |
| 511 |
"(\n\t0 #\n\t0\n) #\n0", |
| 512 |
}, |
| 513 |
samePrint("a | #c1\n\t(\n\t\tb\n\t)"), |
| 514 |
samePrint("a | #c1\n\t{\n\t\tb\n\t}"), |
| 515 |
samePrint("a | #c1\n\tif b; then\n\t\tc\n\tfi"), |
| 516 |
samePrint("a | #c1\n\t#c2\n\t#c3\n\tb"), |
| 517 |
samePrint("a && #c1\n\t(\n\t\tb\n\t)"), |
| 518 |
samePrint("f() body # comment"), |
| 519 |
samePrint("f <<EOF\nbody\nEOF"), |
| 520 |
samePrint("f <<EOF\nEOF"), |
| 521 |
samePrint("f <<-EOF\n\tbody\nEOF"), |
| 522 |
{ |
| 523 |
"f <<-EOF\nbody\nEOF", |
| 524 |
"f <<-EOF\n\tbody\nEOF", |
| 525 |
}, |
| 526 |
samePrint("f <<-EOF\nEOF"), |
| 527 |
samePrint("f <<-EOF\n\n\nEOF"), |
| 528 |
samePrint("f <<-EOF\n\n\tindented\n\nEOF"), |
| 529 |
samePrint("{\n\tf <<EOF\nEOF\n}"), |
| 530 |
samePrint("{\n\tf <<-EOF\n\t\tbody\n\tEOF\n}"), |
| 531 |
samePrint("{\n\tf <<-EOF\n\t\tbody\n\tEOF\n\tf2\n}"), |
| 532 |
samePrint("f <<-EOF\n\t{\n\t\tnicely indented\n\t}\nEOF"), |
| 533 |
samePrint("f <<-EOF\n\t{\n\t\tnicely indented\n\t}\nEOF"), |
| 534 |
{ |
| 535 |
"f <<-EOF\n\t{\nbadly indented\n\t}\nEOF", |
| 536 |
"f <<-EOF\n\t{\n\tbadly indented\n\t}\nEOF", |
| 537 |
}, |
| 538 |
{ |
| 539 |
"f <<-EOF\n\t\t{\n\t\t\ttoo indented\n\t\t}\nEOF", |
| 540 |
"f <<-EOF\n\t{\n\t\ttoo indented\n\t}\nEOF", |
| 541 |
}, |
| 542 |
{ |
| 543 |
"f <<-EOF\n{\n\ttoo little indented\n}\nEOF", |
| 544 |
"f <<-EOF\n\t{\n\t\ttoo little indented\n\t}\nEOF", |
| 545 |
}, |
| 546 |
samePrint("<<-EOF\n\t$foo\nEOF\n\n{\n\tbar\n}"), |
| 547 |
samePrint("f <<EOF\nEOF\n# comment"), |
| 548 |
samePrint("f <<EOF\nEOF\n# comment\nbar"), |
| 549 |
samePrint("f <<EOF # inline\n$(\n\t# inside\n)\nEOF\n# outside\nbar"), |
| 550 |
samePrint("while foo; do\n\tbar\ndone <<-EOF # inline\n\tbaz\nEOF"), |
| 551 |
samePrint("{\n\tcat <<EOF\nEOF\n\t# comment\n}"), |
| 552 |
{ |
| 553 |
"if foo # inline\nthen\n\tbar\nfi", |
| 554 |
"if foo; then # inline\n\tbar\nfi", |
| 555 |
}, |
| 556 |
samePrint("for i; do echo $i; done"), |
| 557 |
samePrint("for i in; do echo $i; done"), |
| 558 |
{ |
| 559 |
"for foo in a b # inline\ndo\n\tbar\ndone", |
| 560 |
"for foo in a b; do # inline\n\tbar\ndone", |
| 561 |
}, |
| 562 |
{ |
| 563 |
"if x # inline\nthen bar; fi", |
| 564 |
"if x; then # inline\n\tbar\nfi", |
| 565 |
}, |
| 566 |
{ |
| 567 |
"for i in a b # inline\ndo bar; done", |
| 568 |
"for i in a b; do # inline\n\tbar\ndone", |
| 569 |
}, |
| 570 |
{ |
| 571 |
"for i #a\n\tin 1; do #b\nx\ndone", |
| 572 |
"for i in \\\n\t1; do #a\n\t#b\n\tx\ndone", |
| 573 |
}, |
| 574 |
{ |
| 575 |
"foo() # inline\n{\n\tbar\n}", |
| 576 |
"foo() { # inline\n\tbar\n}", |
| 577 |
}, |
| 578 |
{ |
| 579 |
"foo() #before\n(\n\tbar #inline\n)", |
| 580 |
"foo() ( #before\n\tbar #inline\n)", |
| 581 |
}, |
| 582 |
{ |
| 583 |
"foo() (#before\n\tbar #inline\n)", |
| 584 |
"foo() ( #before\n\tbar #inline\n)", |
| 585 |
}, |
| 586 |
{ |
| 587 |
"foo()\n#before-1\n(#before-2\n\tbar #inline\n)", |
| 588 |
"foo() ( #before-1\n\t#before-2\n\tbar #inline\n)", |
| 589 |
}, |
| 590 |
{ |
| 591 |
"(#before\n\tbar #inline\n)", |
| 592 |
"( #before\n\tbar #inline\n)", |
| 593 |
}, |
| 594 |
{ |
| 595 |
"(\n#before\n\tbar #inline\n)", |
| 596 |
"(\n\t#before\n\tbar #inline\n)", |
| 597 |
}, |
| 598 |
{ |
| 599 |
"foo=$(#before\n\tbar #inline\n)", |
| 600 |
"foo=$( #before\n\tbar #inline\n)", |
| 601 |
}, |
| 602 |
{ |
| 603 |
"foo=`#before\nbar`", |
| 604 |
"foo=$( #before\n\tbar\n)", |
| 605 |
}, |
| 606 |
samePrint("if foo; then\n\tbar\n\t# comment\nfi"), |
| 607 |
samePrint("if foo; then\n\tbar\n# else commented out\nfi"), |
| 608 |
samePrint("if foo; then\n\tx\nelse\n\tbar\n\t# comment\nfi"), |
| 609 |
samePrint("if foo; then\n\tx\n#comment\nelse\n\ty\nfi"), |
| 610 |
samePrint("if foo; then\n\tx\n\t#comment\nelse\n\ty\nfi"), |
| 611 |
{ |
| 612 |
"if foo; then\n\tx\n#a\n\t#b\n\t#c\nelse\n\ty\nfi", |
| 613 |
"if foo; then\n\tx\n\t#a\n\t#b\n\t#c\nelse\n\ty\nfi", |
| 614 |
}, |
| 615 |
samePrint("if foo; then\n\tx\nelse #comment\n\ty\nfi"), |
| 616 |
samePrint("if foo; then\n\tx\n#comment\nelif bar; then\n\ty\nfi"), |
| 617 |
samePrint("if foo; then\n\tx\n\t#comment\nelif bar; then\n\ty\nfi"), |
| 618 |
samePrint("case i in\nx)\n\ta\n\t;;\n#comment\ny) ;;\nesac"), |
| 619 |
samePrint("case i in\nx)\n\ta\n\t;;\n\t#comment\ny) ;;\nesac"), |
| 620 |
{ |
| 621 |
"case i in\nx)\n\ta\n\t;;\n\t#a\n#b\n\t#c\ny) ;;\nesac", |
| 622 |
"case i in\nx)\n\ta\n\t;;\n\t#a\n\t#b\n\t#c\ny) ;;\nesac", |
| 623 |
}, |
| 624 |
samePrint("'foo\tbar'\n'foooo\tbar'"), |
| 625 |
samePrint("\"foo\tbar\"\n\"foooo\tbar\""), |
| 626 |
samePrint("foo\\\tbar\nfoooo\\\tbar"), |
| 627 |
samePrint("#foo\tbar\n#foooo\tbar"), |
| 628 |
{ |
| 629 |
"array=('one'\n\t\t# 'two'\n\t\t'three')", |
| 630 |
"array=('one'\n\t# 'two'\n\t'three')", |
| 631 |
}, |
| 632 |
samePrint("#comment\n>redir"), |
| 633 |
{ |
| 634 |
">redir \\\n\tfoo", |
| 635 |
">redir foo", |
| 636 |
}, |
| 637 |
samePrint("$(declare)"), |
| 638 |
{ |
| 639 |
"`declare`", |
| 640 |
"$(declare)", |
| 641 |
}, |
| 642 |
{ |
| 643 |
"(\n(foo >redir))", |
| 644 |
"(\n\t(foo >redir)\n)", |
| 645 |
}, |
| 646 |
samePrint("( (foo) )"), |
| 647 |
samePrint("$( (foo) )"), |
| 648 |
samePrint("$( ((foo++)) )"), |
| 649 |
{ |
| 650 |
"$( (foo); bar )", |
| 651 |
"$(\n\t(foo)\n\tbar\n)", |
| 652 |
}, |
| 653 |
{ |
| 654 |
"( (foo); bar )", |
| 655 |
"(\n\t(foo)\n\tbar\n)", |
| 656 |
}, |
| 657 |
samePrint("( ((foo++)) )"), |
| 658 |
{ |
| 659 |
"( ((foo++)); bar )", |
| 660 |
"(\n\t((foo++))\n\tbar\n)", |
| 661 |
}, |
| 662 |
samePrint("(foo && (bar) )"), |
| 663 |
samePrint("( (foo) >redir)"), |
| 664 |
samePrint("(\n\t((foo++))\n)"), |
| 665 |
samePrint("(foo && bar)"), |
| 666 |
samePrint(`$foo#bar ${foo}#bar 'foo'#bar "foo"#bar`), |
| 667 |
samePrint("case $i in\n1)\n\ta\n\t;;\n(esac)\n\tb\n\t;;\nesac"), |
| 668 |
samePrint("case $i in\n1)\n\ta\n\t;;\n(esac | 2)\n\tb\n\t;;\nesac"), |
| 669 |
samePrint("case $i in\n1)\n\ta\n\t;;\n2 | esac)\n\tb\n\t;;\nesac"), |
| 670 |
// TODO: support cases with command substitutions as well |
| 671 |
// { |
| 672 |
// "`foo`#bar", |
| 673 |
// "$(foo)#bar", |
| 674 |
// }, |
| 675 |
// samePrint(`$("foo"#bar)#bar`), |
| 676 |
samePrint("${array[dash - string]}"), |
| 677 |
samePrint("array[dash - string]=123"), |
| 678 |
samePrint("${arr[0,1]}"), |
| 679 |
samePrint("arr[0,1]=x"), |
| 680 |
} |
| 681 |
|
| 682 |
func TestPrintTable(t *testing.T) { |
| 683 |
t.Parallel() |
| 684 |
parser := NewParser(KeepComments(true)) |
| 685 |
printer := NewPrinter() |
| 686 |
for i, tc := range printTests { |
| 687 |
t.Run(fmt.Sprintf("#%03d", i), func(t *testing.T) { |
| 688 |
printTest(t, parser, printer, tc.in, tc.want) |
| 689 |
}) |
| 690 |
t.Run(fmt.Sprintf("#%03d-nl", i), func(t *testing.T) { |
| 691 |
printTest(t, parser, printer, "\n"+tc.in+"\n", tc.want) |
| 692 |
}) |
| 693 |
t.Run(fmt.Sprintf("#%03d-redo", i), func(t *testing.T) { |
| 694 |
printTest(t, parser, printer, tc.want, tc.want) |
| 695 |
}) |
| 696 |
} |
| 697 |
} |
| 698 |
|
| 699 |
func parsePath(tb testing.TB, path string) *File { |
| 700 |
f, err := os.Open(path) |
| 701 |
if err != nil { |
| 702 |
tb.Fatal(err) |
| 703 |
} |
| 704 |
defer f.Close() |
| 705 |
prog, err := NewParser(KeepComments(true)).Parse(f, "") |
| 706 |
if err != nil { |
| 707 |
tb.Fatal(err) |
| 708 |
} |
| 709 |
return prog |
| 710 |
} |
| 711 |
|
| 712 |
const canonicalPath = "canonical.sh" |
| 713 |
|
| 714 |
func TestPrintMultiline(t *testing.T) { |
| 715 |
t.Parallel() |
| 716 |
prog := parsePath(t, canonicalPath) |
| 717 |
got, err := strPrint(NewPrinter(), prog) |
| 718 |
if err != nil { |
| 719 |
t.Fatal(err) |
| 720 |
} |
| 721 |
|
| 722 |
wantBs, err := os.ReadFile(canonicalPath) |
| 723 |
if err != nil { |
| 724 |
t.Fatal(err) |
| 725 |
} |
| 726 |
|
| 727 |
// If we're on Windows and it was set up to automatically replace LF |
| 728 |
// with CRLF, that might make this test fail. Just ignore \r characters. |
| 729 |
want := strings.ReplaceAll(string(wantBs), "\r", "") |
| 730 |
got = strings.ReplaceAll(got, "\r", "") |
| 731 |
if got != want { |
| 732 |
t.Fatalf("Print mismatch in canonical.sh") |
| 733 |
} |
| 734 |
} |
| 735 |
|
| 736 |
func TestPrintSpaces(t *testing.T) { |
| 737 |
t.Parallel() |
| 738 |
spaceFormats := [...]struct { |
| 739 |
spaces uint |
| 740 |
in, want string |
| 741 |
}{ |
| 742 |
{ |
| 743 |
0, |
| 744 |
"{\nfoo \\\nbar\n}", |
| 745 |
"{\n\tfoo \\\n\t\tbar\n}", |
| 746 |
}, |
| 747 |
{ |
| 748 |
2, |
| 749 |
"{\nfoo \\\nbar\n}", |
| 750 |
"{\n foo \\\n bar\n}", |
| 751 |
}, |
| 752 |
{ |
| 753 |
4, |
| 754 |
"{\nfoo \\\nbar\n}", |
| 755 |
"{\n foo \\\n bar\n}", |
| 756 |
}, |
| 757 |
{ |
| 758 |
2, |
| 759 |
"if foo; then # inline1\nbar # inline2\n# withfi\nfi", |
| 760 |
"if foo; then # inline1\n bar # inline2\n# withfi\nfi", |
| 761 |
}, |
| 762 |
{ |
| 763 |
2, |
| 764 |
"array=('one'\n # 'two'\n 'three')", |
| 765 |
"array=('one'\n # 'two'\n 'three')", |
| 766 |
}, |
| 767 |
} |
| 768 |
|
| 769 |
parser := NewParser(KeepComments(true)) |
| 770 |
for _, tc := range spaceFormats { |
| 771 |
t.Run("", func(t *testing.T) { |
| 772 |
printer := NewPrinter(Indent(tc.spaces)) |
| 773 |
printTest(t, parser, printer, tc.in, tc.want) |
| 774 |
}) |
| 775 |
} |
| 776 |
} |
| 777 |
|
| 778 |
var errBadWriter = fmt.Errorf("write: expected error") |
| 779 |
|
| 780 |
type badWriter struct{} |
| 781 |
|
| 782 |
func (b badWriter) Write(p []byte) (int, error) { return 0, errBadWriter } |
| 783 |
|
| 784 |
func TestWriteErr(t *testing.T) { |
| 785 |
t.Parallel() |
| 786 |
f := &File{Stmts: []*Stmt{ |
| 787 |
{ |
| 788 |
Redirs: []*Redirect{{ |
| 789 |
Op: RdrOut, |
| 790 |
Word: litWord("foo"), |
| 791 |
}}, |
| 792 |
Cmd: &Subshell{}, |
| 793 |
}, |
| 794 |
}} |
| 795 |
err := NewPrinter().Print(badWriter{}, f) |
| 796 |
if err == nil { |
| 797 |
t.Fatalf("Expected error with bad writer") |
| 798 |
} |
| 799 |
if err != errBadWriter { |
| 800 |
t.Fatalf("Error mismatch with bad writer:\nwant: %v\ngot: %v", |
| 801 |
errBadWriter, err) |
| 802 |
} |
| 803 |
} |
| 804 |
|
| 805 |
func TestPrintBinaryNextLine(t *testing.T) { |
| 806 |
t.Parallel() |
| 807 |
tests := [...]printCase{ |
| 808 |
{ |
| 809 |
"foo <<EOF &&\nl1\nEOF\nbar", |
| 810 |
"foo <<EOF && bar\nl1\nEOF", |
| 811 |
}, |
| 812 |
samePrint("a \\\n\t&& b"), |
| 813 |
samePrint("a \\\n\t&& b\nc"), |
| 814 |
{ |
| 815 |
"{\n(a \\\n&& b)\nc\n}", |
| 816 |
"{\n\t(a \\\n\t\t&& b)\n\tc\n}", |
| 817 |
}, |
| 818 |
{ |
| 819 |
"a && b \\\n&& c", |
| 820 |
"a && b \\\n\t&& c", |
| 821 |
}, |
| 822 |
{ |
| 823 |
"a \\\n&& $(b) && c \\\n&& d", |
| 824 |
"a \\\n\t&& $(b) && c \\\n\t&& d", |
| 825 |
}, |
| 826 |
{ |
| 827 |
"a \\\n&& b\nc \\\n&& d", |
| 828 |
"a \\\n\t&& b\nc \\\n\t&& d", |
| 829 |
}, |
| 830 |
{ |
| 831 |
"a | {\nb \\\n| c\n}", |
| 832 |
"a | {\n\tb \\\n\t\t| c\n}", |
| 833 |
}, |
| 834 |
{ |
| 835 |
"a \\\n\t&& if foo; then\nbar\nfi", |
| 836 |
"a \\\n\t&& if foo; then\n\t\tbar\n\tfi", |
| 837 |
}, |
| 838 |
{ |
| 839 |
"if foo \\\n&& bar\nthen\nbar\nfi", |
| 840 |
"if foo \\\n\t&& bar; then\n\tbar\nfi", |
| 841 |
}, |
| 842 |
{ |
| 843 |
"a |\nb |\nc", |
| 844 |
"a \\\n\t| b \\\n\t| c", |
| 845 |
}, |
| 846 |
{ |
| 847 |
"foo |\n# misplaced\nbar", |
| 848 |
"foo \\\n\t|\n\t# misplaced\n\tbar", |
| 849 |
}, |
| 850 |
samePrint("{\n\tfoo\n\t#a\n\tbar\n} | etc"), |
| 851 |
{ |
| 852 |
"foo &&\n#a1\n#a2\n$(bar)", |
| 853 |
"foo \\\n\t&&\n\t#a1\n\t#a2\n\t$(bar)", |
| 854 |
}, |
| 855 |
{ |
| 856 |
"{\n\tfoo\n\t#a\n} |\n# misplaced\nbar", |
| 857 |
"{\n\tfoo\n\t#a\n} \\\n\t|\n\t# misplaced\n\tbar", |
| 858 |
}, |
| 859 |
samePrint("foo | bar\n#after"), |
| 860 |
{ |
| 861 |
"a |\nb | #c2\nc", |
| 862 |
"a \\\n\t| b \\\n\t|\n\t#c2\n\tc", |
| 863 |
}, |
| 864 |
samePrint("a \\\n\t&"), |
| 865 |
{ |
| 866 |
"a \\\n\tb \\\n\tc \\\n\t# EOC", |
| 867 |
"a \\\n\tb \\\n\tc\n# EOC", |
| 868 |
}, |
| 869 |
{ |
| 870 |
"a \\\n\t| b \\\n\t| c \\\n\t# EOC", |
| 871 |
"a \\\n\t| b \\\n\t| c\n# EOC", |
| 872 |
}, |
| 873 |
} |
| 874 |
parser := NewParser(KeepComments(true)) |
| 875 |
printer := NewPrinter(BinaryNextLine(true)) |
| 876 |
for _, tc := range tests { |
| 877 |
t.Run("", func(t *testing.T) { |
| 878 |
printTest(t, parser, printer, tc.in, tc.want) |
| 879 |
}) |
| 880 |
} |
| 881 |
} |
| 882 |
|
| 883 |
func TestPrintSwitchCaseIndent(t *testing.T) { |
| 884 |
t.Parallel() |
| 885 |
tests := [...]printCase{ |
| 886 |
{ |
| 887 |
"case $i in\n1)\nfoo\n;;\nesac", |
| 888 |
"case $i in\n\t1)\n\t\tfoo\n\t\t;;\nesac", |
| 889 |
}, |
| 890 |
{ |
| 891 |
"case $i in\n1)\na\n;;\n2)\nb\n;;\nesac", |
| 892 |
"case $i in\n\t1)\n\t\ta\n\t\t;;\n\t2)\n\t\tb\n\t\t;;\nesac", |
| 893 |
}, |
| 894 |
samePrint("case $i in\n\t#foo\nesac"), |
| 895 |
} |
| 896 |
parser := NewParser(KeepComments(true)) |
| 897 |
printer := NewPrinter(SwitchCaseIndent(true)) |
| 898 |
for _, tc := range tests { |
| 899 |
t.Run("", func(t *testing.T) { |
| 900 |
printTest(t, parser, printer, tc.in, tc.want) |
| 901 |
}) |
| 902 |
} |
| 903 |
} |
| 904 |
|
| 905 |
func TestPrintFunctionNextLine(t *testing.T) { |
| 906 |
t.Parallel() |
| 907 |
tests := [...]printCase{ |
| 908 |
{ |
| 909 |
"foo() { bar; }", |
| 910 |
"foo()\n{\n\tbar\n}", |
| 911 |
}, |
| 912 |
{ |
| 913 |
"foo()\n{ bar; }", |
| 914 |
"foo()\n{\n\tbar\n}", |
| 915 |
}, |
| 916 |
{ |
| 917 |
"foo()\n\n{\n\n\tbar\n}", |
| 918 |
"foo()\n{\n\n\tbar\n}", |
| 919 |
}, |
| 920 |
{ |
| 921 |
"function foo {\n\tbar\n}", |
| 922 |
"function foo\n{\n\tbar\n}", |
| 923 |
}, |
| 924 |
{ |
| 925 |
"function foo() {\n\tbar\n}", |
| 926 |
"function foo()\n{\n\tbar\n}", |
| 927 |
}, |
| 928 |
{ |
| 929 |
"{ foo() { bar; }; }", |
| 930 |
"{\n\tfoo()\n\t{\n\t\tbar\n\t}\n}", |
| 931 |
}, |
| 932 |
} |
| 933 |
parser := NewParser(KeepComments(true)) |
| 934 |
printer := NewPrinter(FunctionNextLine(true)) |
| 935 |
for _, tc := range tests { |
| 936 |
t.Run("", func(t *testing.T) { |
| 937 |
printTest(t, parser, printer, tc.in, tc.want) |
| 938 |
}) |
| 939 |
} |
| 940 |
} |
| 941 |
|
| 942 |
func TestPrintSpaceRedirects(t *testing.T) { |
| 943 |
t.Parallel() |
| 944 |
tests := [...]printCase{ |
| 945 |
samePrint("echo foo bar > f"), |
| 946 |
samePrint("echo > f foo bar"), |
| 947 |
samePrint("echo >(cmd)"), |
| 948 |
samePrint("echo > >(cmd)"), |
| 949 |
samePrint("<< EOF\nfoo\nEOF"), |
| 950 |
samePrint("<<- EOF\n\t$(< foo)\nEOF"), |
| 951 |
samePrint("echo 2> f"), |
| 952 |
samePrint("echo foo bar >&1"), |
| 953 |
samePrint("echo 2<&1 foo bar"), |
| 954 |
} |
| 955 |
parser := NewParser(KeepComments(true)) |
| 956 |
printer := NewPrinter(SpaceRedirects(true)) |
| 957 |
for _, tc := range tests { |
| 958 |
t.Run("", func(t *testing.T) { |
| 959 |
printTest(t, parser, printer, tc.in, tc.want) |
| 960 |
}) |
| 961 |
} |
| 962 |
} |
| 963 |
|
| 964 |
func TestPrintKeepPadding(t *testing.T) { |
| 965 |
t.Parallel() |
| 966 |
tests := [...]printCase{ |
| 967 |
samePrint("echo foo bar"), |
| 968 |
samePrint("echo foo bar"), |
| 969 |
samePrint("a=b c=d bar"), |
| 970 |
samePrint("echo foo >bar"), |
| 971 |
samePrint("echo foo 2>bar"), |
| 972 |
samePrint("{ foo; }"), |
| 973 |
samePrint("a() { foo; }"), |
| 974 |
samePrint("a && b"), |
| 975 |
samePrint("a | b"), |
| 976 |
samePrint("a | b"), |
| 977 |
samePrint("{ a b c; }"), |
| 978 |
samePrint("foo # x\nbaaar # y"), |
| 979 |
samePrint("{ { a; }; }"), |
| 980 |
samePrint("{ a; }"), |
| 981 |
samePrint("( a )"), |
| 982 |
samePrint("'foo\nbar' # x"), |
| 983 |
{"\tfoo", "foo"}, |
| 984 |
{" if foo; then bar; fi", "if foo; then bar; fi"}, |
| 985 |
samePrint("echo '★' || true"), |
| 986 |
{ |
| 987 |
"1234 || { x; y; }", |
| 988 |
"1234 || {\n\tx\n\ty\n}", |
| 989 |
}, |
| 990 |
{ |
| 991 |
"array=('one'\n\t\t# 'two'\n\t\t'three')", |
| 992 |
"array=('one'\n\t# 'two'\n\t'three')", |
| 993 |
}, |
| 994 |
} |
| 995 |
parser := NewParser(KeepComments(true)) |
| 996 |
printer := NewPrinter(KeepPadding(true)) |
| 997 |
for _, tc := range tests { |
| 998 |
t.Run("", func(t *testing.T) { |
| 999 |
// ensure that Reset does properly reset colCounter |
| 1000 |
printer.w.WriteByte('x') |
| 1001 |
printer.w.Reset(nil) |
| 1002 |
printTest(t, parser, printer, tc.in, tc.want) |
| 1003 |
}) |
| 1004 |
} |
| 1005 |
} |
| 1006 |
|
| 1007 |
func TestPrintKeepPaddingSpaces(t *testing.T) { |
| 1008 |
t.Parallel() |
| 1009 |
tests := [...]printCase{ |
| 1010 |
samePrint("array=('one'\n # 'two'\n 'three')"), |
| 1011 |
samePrint(" abc=123"), |
| 1012 |
samePrint("foo \\\n bar \\\n baz"), |
| 1013 |
samePrint("{\n foo\n bar\n}"), |
| 1014 |
samePrint("# foo\n # bar"), |
| 1015 |
} |
| 1016 |
parser := NewParser(KeepComments(true)) |
| 1017 |
printer := NewPrinter(KeepPadding(true), Indent(2)) |
| 1018 |
for _, tc := range tests { |
| 1019 |
t.Run("", func(t *testing.T) { |
| 1020 |
printTest(t, parser, printer, tc.in, tc.want) |
| 1021 |
}) |
| 1022 |
} |
| 1023 |
} |
| 1024 |
|
| 1025 |
func TestPrintMinify(t *testing.T) { |
| 1026 |
t.Parallel() |
| 1027 |
tests := [...]printCase{ |
| 1028 |
samePrint("echo foo bar $a $(b)"), |
| 1029 |
{ |
| 1030 |
"#comment", |
| 1031 |
"", |
| 1032 |
}, |
| 1033 |
{ |
| 1034 |
"foo #comment", |
| 1035 |
"foo", |
| 1036 |
}, |
| 1037 |
{ |
| 1038 |
"foo\n\nbar", |
| 1039 |
"foo\nbar", |
| 1040 |
}, |
| 1041 |
{ |
| 1042 |
"foo &", |
| 1043 |
"foo&", |
| 1044 |
}, |
| 1045 |
samePrint("foo >bar 2>baz <etc"), |
| 1046 |
{ |
| 1047 |
"{\n\tfoo\n}", |
| 1048 |
"{\nfoo\n}", |
| 1049 |
}, |
| 1050 |
{ |
| 1051 |
"(\n\ta\n)\n(\n\tb\n\tc\n)", |
| 1052 |
"(a)\n(b\nc)", |
| 1053 |
}, |
| 1054 |
{ |
| 1055 |
"$(\n\ta\n)\n$(\n\tb\n\tc\n)", |
| 1056 |
"$(a)\n$(b\nc)", |
| 1057 |
}, |
| 1058 |
{ |
| 1059 |
"f() { x; }", |
| 1060 |
"f(){ x;}", |
| 1061 |
}, |
| 1062 |
{ |
| 1063 |
"((1 + 2))", |
| 1064 |
"((1+2))", |
| 1065 |
}, |
| 1066 |
// TODO: see https://github.com/mvdan/sh/issues/1169 |
| 1067 |
{ |
| 1068 |
"((\nfoo\n))", |
| 1069 |
"((\\\nfoo))", |
| 1070 |
}, |
| 1071 |
{ |
| 1072 |
"((\na > 0\n&&\na < 10\n))", |
| 1073 |
"((\\\na>0&& \\\na<10))", |
| 1074 |
}, |
| 1075 |
{ |
| 1076 |
"((a > 0 &&\na < 10))", |
| 1077 |
"((a>0&& \\\na<10))", |
| 1078 |
}, |
| 1079 |
{ |
| 1080 |
"echo $a ${b} ${c}-d ${e}f ${g}_h", |
| 1081 |
"echo $a $b $c-d ${e}f ${g}_h", |
| 1082 |
}, |
| 1083 |
{ |
| 1084 |
"echo ${0} ${3} ${10} ${22}", |
| 1085 |
"echo $0 $3 ${10} ${22}", |
| 1086 |
}, |
| 1087 |
{ |
| 1088 |
"case $a in\nx) c ;;\ny | z)\n\td\n\t;;\nesac", |
| 1089 |
"case $a in\nx)c;;\ny|z)d\nesac", |
| 1090 |
}, |
| 1091 |
{ |
| 1092 |
"a && b | c", |
| 1093 |
"a&&b|c", |
| 1094 |
}, |
| 1095 |
{ |
| 1096 |
"a &&\n\tb |\n\tc", |
| 1097 |
"a&&b|c", |
| 1098 |
}, |
| 1099 |
{ |
| 1100 |
"${0/${a}\\\n}", |
| 1101 |
"${0/$a/}", |
| 1102 |
}, |
| 1103 |
{ |
| 1104 |
"#!/bin/sh\necho 1\n#!/bin/sh\necho 2", |
| 1105 |
"#!/bin/sh\necho 1\necho 2", |
| 1106 |
}, |
| 1107 |
samePrint("foo >bar 2>baz <etc"), |
| 1108 |
samePrint("<<-EOF\n$(a|b)\nEOF"), |
| 1109 |
{ |
| 1110 |
"a=$(\n\tcat <<'EOF'\n hello\nEOF\n)", |
| 1111 |
"a=$(cat <<'EOF'\n hello\nEOF\n)", |
| 1112 |
}, |
| 1113 |
{ |
| 1114 |
"(\n\tcat <<EOF\n hello\nEOF\n)", |
| 1115 |
"(cat <<EOF\n hello\nEOF\n)", |
| 1116 |
}, |
| 1117 |
samePrint("diff -y <(cat <<EOF\n1\n2\n3\nEOF\n) <(cat <<EOF\n1\n4\n3\nEOF\n)"), |
| 1118 |
} |
| 1119 |
parser := NewParser(KeepComments(true)) |
| 1120 |
printer := NewPrinter(Minify(true)) |
| 1121 |
for _, tc := range tests { |
| 1122 |
t.Run("", func(t *testing.T) { |
| 1123 |
printTest(t, parser, printer, tc.in, tc.want) |
| 1124 |
}) |
| 1125 |
} |
| 1126 |
} |
| 1127 |
|
| 1128 |
func TestPrintSingleLine(t *testing.T) { |
| 1129 |
t.Parallel() |
| 1130 |
tests := [...]printCase{ |
| 1131 |
samePrint("echo foo bar $a $(b)"), |
| 1132 |
samePrint("foo #comment"), |
| 1133 |
{ |
| 1134 |
"foo\n\nbar", |
| 1135 |
"foo; bar", |
| 1136 |
}, |
| 1137 |
samePrint("foo &"), |
| 1138 |
samePrint("foo >bar 2>baz <etc"), |
| 1139 |
{ |
| 1140 |
"{\n\tfoo\n}", |
| 1141 |
"{ foo; }", |
| 1142 |
}, |
| 1143 |
{ |
| 1144 |
"(\n\ta\n)\n(\n\tb\n\tc\n)", |
| 1145 |
"(a); (b; c)", |
| 1146 |
}, |
| 1147 |
{ |
| 1148 |
"$(\n\ta\n)\n$(\n\tb\n\tc\n)", |
| 1149 |
"$(a); $(b; c)", |
| 1150 |
}, |
| 1151 |
samePrint("f() { x; }"), |
| 1152 |
samePrint("((1 + 2))"), |
| 1153 |
samePrint("echo $a ${b} ${c}-d ${e}f ${g}_h"), |
| 1154 |
samePrint("echo ${0} ${3} ${10} ${22}"), |
| 1155 |
{ |
| 1156 |
"case $a in\nx)c;;\ny|z)d\nesac", |
| 1157 |
"case $a in x) c ;; y | z) d ;; esac", |
| 1158 |
}, |
| 1159 |
samePrint("a && b | c"), |
| 1160 |
{ |
| 1161 |
"a &&\n\tb |\n\tc", |
| 1162 |
"a && b | c", |
| 1163 |
}, |
| 1164 |
{ |
| 1165 |
"if\nfoo\nthen\nbar\nfi", |
| 1166 |
"if foo; then bar; fi", |
| 1167 |
}, |
| 1168 |
{ |
| 1169 |
"a \\\n >b", |
| 1170 |
"a >b", |
| 1171 |
}, |
| 1172 |
samePrint("foo >bar 2>baz <etc"), |
| 1173 |
samePrint("<<-EOF\n\t$(a | b)\nEOF"), |
| 1174 |
} |
| 1175 |
parser := NewParser(KeepComments(true)) |
| 1176 |
printer := NewPrinter(SingleLine(true)) |
| 1177 |
for _, tc := range tests { |
| 1178 |
t.Run("", func(t *testing.T) { |
| 1179 |
printTest(t, parser, printer, tc.in, tc.want) |
| 1180 |
}) |
| 1181 |
} |
| 1182 |
} |
| 1183 |
|
| 1184 |
func TestPrintOptionsNotBroken(t *testing.T) { |
| 1185 |
t.Parallel() |
| 1186 |
singleLineException := regexp.MustCompile(`#|<<|'|"`) |
| 1187 |
checkSingleLine := func(t *testing.T, got string) { |
| 1188 |
if singleLineException.MatchString(got) { |
| 1189 |
return |
| 1190 |
} |
| 1191 |
got = strings.TrimSuffix(got, "\n") // trailing newline is expected |
| 1192 |
if strings.Contains(got, "\n") { |
| 1193 |
t.Fatalf("unexpected newline with SingleLine: %q", got) |
| 1194 |
} |
| 1195 |
} |
| 1196 |
|
| 1197 |
for _, opts := range []struct { |
| 1198 |
name string |
| 1199 |
list []PrinterOption |
| 1200 |
}{ |
| 1201 |
{"Minify", []PrinterOption{Minify(true)}}, |
| 1202 |
{"SingleLine", []PrinterOption{SingleLine(true)}}, |
| 1203 |
} { |
| 1204 |
printer := NewPrinter(opts.list...) |
| 1205 |
for lang := range langResolvedVariants.bits() { |
| 1206 |
parser := NewParser(Variant(lang), KeepComments(true)) |
| 1207 |
for _, tc := range append(fileTests, fileTestsNoPrint...) { |
| 1208 |
want, _ := tc.byLangIndex[lang.index()].(*File) |
| 1209 |
if want == nil { |
| 1210 |
continue |
| 1211 |
} |
| 1212 |
t.Run(fmt.Sprintf("File%s/%s", opts.name, lang), func(t *testing.T) { |
| 1213 |
in := tc.inputs[0] |
| 1214 |
t.Logf("input: %s", in) |
| 1215 |
prog, err := parser.Parse(strings.NewReader(in), "") |
| 1216 |
if err != nil { |
| 1217 |
t.Skipf("skipping due to error; TestParsePass should catch this") |
| 1218 |
} |
| 1219 |
got, err := strPrint(printer, prog) |
| 1220 |
if err != nil { |
| 1221 |
t.Fatal(err) |
| 1222 |
} |
| 1223 |
if opts.name == "SingleLine" { |
| 1224 |
checkSingleLine(t, got) |
| 1225 |
} |
| 1226 |
_, err = parser.Parse(strings.NewReader(got), "") |
| 1227 |
if err != nil { |
| 1228 |
t.Fatalf("program was broken: %v\noriginal:\n%s\nfinal:\n%s", err, in, got) |
| 1229 |
} |
| 1230 |
}) |
| 1231 |
} |
| 1232 |
} |
| 1233 |
parser := NewParser(KeepComments(true)) |
| 1234 |
for _, tc := range printTests { |
| 1235 |
t.Run(fmt.Sprintf("Print%s", opts.name), func(t *testing.T) { |
| 1236 |
prog, err := parser.Parse(strings.NewReader(tc.in), "") |
| 1237 |
if err != nil { |
| 1238 |
t.Skipf("skipping due to error; TestPrintTable should catch this") |
| 1239 |
} |
| 1240 |
got, err := strPrint(printer, prog) |
| 1241 |
if err != nil { |
| 1242 |
t.Fatal(err) |
| 1243 |
} |
| 1244 |
if opts.name == "SingleLine" { |
| 1245 |
checkSingleLine(t, got) |
| 1246 |
} |
| 1247 |
_, err = parser.Parse(strings.NewReader(got), "") |
| 1248 |
if err != nil { |
| 1249 |
t.Fatalf("program was broken: %v\noriginal:\n%s\nfinal:\n%s", err, tc.in, got) |
| 1250 |
} |
| 1251 |
}) |
| 1252 |
} |
| 1253 |
} |
| 1254 |
} |
| 1255 |
|
| 1256 |
func printTest(t *testing.T, parser *Parser, printer *Printer, in, want string) { |
| 1257 |
t.Helper() |
| 1258 |
t.Logf("input: %s", in) |
| 1259 |
prog, err := parser.Parse(strings.NewReader(in), "") |
| 1260 |
if err != nil { |
| 1261 |
t.Fatalf("parsing got an error: %s:\n%s", err, in) |
| 1262 |
} |
| 1263 |
want += "\n" |
| 1264 |
got, err := strPrint(printer, prog) |
| 1265 |
if err != nil { |
| 1266 |
t.Fatal(err) |
| 1267 |
} |
| 1268 |
if got != want { |
| 1269 |
t.Fatalf("Print mismatch:\nwant:\n%q\ngot:\n%q", want, got) |
| 1270 |
} |
| 1271 |
|
| 1272 |
// With the "want" output string, including the added trailing newline, |
| 1273 |
// make sure that it's idempotent when formatted again. |
| 1274 |
progAgain, err := parser.Parse(strings.NewReader(want), "") |
| 1275 |
if err != nil { |
| 1276 |
t.Fatalf("Result is not valid shell:\n%s", want) |
| 1277 |
} |
| 1278 |
gotAgain, err := strPrint(printer, progAgain) |
| 1279 |
if err != nil { |
| 1280 |
t.Fatal(err) |
| 1281 |
} |
| 1282 |
if gotAgain != want { |
| 1283 |
t.Fatalf("Re-print mismatch:\nwant:\n%q\ngot:\n%q", want, gotAgain) |
| 1284 |
} |
| 1285 |
} |
| 1286 |
|
| 1287 |
func TestPrintNodeTypes(t *testing.T) { |
| 1288 |
t.Parallel() |
| 1289 |
|
| 1290 |
multiline, err := NewParser().Parse(strings.NewReader(` |
| 1291 |
echo foo |
| 1292 |
`), "") |
| 1293 |
if err != nil { |
| 1294 |
t.Fatal(err) |
| 1295 |
} |
| 1296 |
|
| 1297 |
tests := [...]struct { |
| 1298 |
in Node |
| 1299 |
want string |
| 1300 |
wantErr bool |
| 1301 |
}{ |
| 1302 |
{ |
| 1303 |
in: &File{Stmts: litStmts("foo")}, |
| 1304 |
want: "foo\n", |
| 1305 |
}, |
| 1306 |
{ |
| 1307 |
in: &File{Stmts: litStmts("foo", "bar")}, |
| 1308 |
want: "foo\nbar\n", |
| 1309 |
}, |
| 1310 |
{ |
| 1311 |
in: litStmt("foo", "bar"), |
| 1312 |
want: "foo bar", |
| 1313 |
}, |
| 1314 |
{ |
| 1315 |
in: litCall("foo", "bar"), |
| 1316 |
want: "foo bar", |
| 1317 |
}, |
| 1318 |
{ |
| 1319 |
in: litWord("foo"), |
| 1320 |
want: "foo", |
| 1321 |
}, |
| 1322 |
{ |
| 1323 |
in: lit("foo"), |
| 1324 |
want: "foo", |
| 1325 |
}, |
| 1326 |
{ |
| 1327 |
in: sglQuoted("foo"), |
| 1328 |
want: "'foo'", |
| 1329 |
}, |
| 1330 |
{ |
| 1331 |
in: &Comment{}, |
| 1332 |
wantErr: true, |
| 1333 |
}, |
| 1334 |
{ |
| 1335 |
in: multiline.Stmts[0], |
| 1336 |
want: "echo foo", |
| 1337 |
}, |
| 1338 |
{ |
| 1339 |
in: multiline.Stmts[0].Cmd, |
| 1340 |
want: "echo foo", |
| 1341 |
}, |
| 1342 |
{ |
| 1343 |
in: multiline.Stmts[0].Cmd.(*CallExpr).Args[0], |
| 1344 |
want: "echo", |
| 1345 |
}, |
| 1346 |
{ |
| 1347 |
in: multiline.Stmts[0].Cmd.(*CallExpr).Args[0].Parts[0], |
| 1348 |
want: "echo", |
| 1349 |
}, |
| 1350 |
} |
| 1351 |
printer := NewPrinter() |
| 1352 |
for _, tc := range tests { |
| 1353 |
t.Run("", func(t *testing.T) { |
| 1354 |
got, err := strPrint(printer, tc.in) |
| 1355 |
if err == nil && tc.wantErr { |
| 1356 |
t.Fatalf("wanted an error but found none") |
| 1357 |
} else if err != nil && !tc.wantErr { |
| 1358 |
t.Fatalf("didn't want an error but got %v", err) |
| 1359 |
} |
| 1360 |
if got != tc.want { |
| 1361 |
t.Fatalf("Print mismatch:\nwant:\n%s\ngot:\n%s", |
| 1362 |
tc.want, got) |
| 1363 |
} |
| 1364 |
}) |
| 1365 |
} |
| 1366 |
} |
| 1367 |
|
| 1368 |
func TestPrintManyStmts(t *testing.T) { |
| 1369 |
t.Parallel() |
| 1370 |
tests := [...]struct { |
| 1371 |
in, want string |
| 1372 |
}{ |
| 1373 |
{"foo; bar", "foo\nbar\n"}, |
| 1374 |
{"foo\nbar", "foo\nbar\n"}, |
| 1375 |
{"\n\nfoo\nbar\n\n", "foo\nbar\n"}, |
| 1376 |
{"foo\nbar <<EOF\nbody\nEOF\n", "foo\nbar <<EOF\nbody\nEOF\n"}, |
| 1377 |
{"foo\nbar # inline", "foo\nbar # inline\n"}, |
| 1378 |
{"# comment before\nfoo bar", "# comment before\nfoo bar\n"}, |
| 1379 |
} |
| 1380 |
parser := NewParser(KeepComments(true)) |
| 1381 |
printer := NewPrinter() |
| 1382 |
for _, tc := range tests { |
| 1383 |
t.Run("", func(t *testing.T) { |
| 1384 |
f, err := parser.Parse(strings.NewReader(tc.in), "") |
| 1385 |
if err != nil { |
| 1386 |
t.Fatal(err) |
| 1387 |
} |
| 1388 |
var buf bytes.Buffer |
| 1389 |
for _, stmt := range f.Stmts { |
| 1390 |
printer.Print(&buf, stmt) |
| 1391 |
buf.WriteByte('\n') |
| 1392 |
} |
| 1393 |
got := buf.String() |
| 1394 |
if got != tc.want { |
| 1395 |
t.Fatalf("Print mismatch:\nwant:\n%s\ngot:\n%s", |
| 1396 |
tc.want, got) |
| 1397 |
} |
| 1398 |
}) |
| 1399 |
} |
| 1400 |
} |
| 1401 |
|
| 1402 |
func TestKeepPaddingRepeated(t *testing.T) { |
| 1403 |
t.Parallel() |
| 1404 |
parser := NewParser() |
| 1405 |
printer := NewPrinter() |
| 1406 |
|
| 1407 |
// Enable the KeepPadding option twice. This used to crash, since the |
| 1408 |
// option made an invalid type assertion the second time. |
| 1409 |
KeepPadding(true)(printer) |
| 1410 |
KeepPadding(true)(printer) |
| 1411 |
|
| 1412 |
// Ensure the option is enabled. |
| 1413 |
printTest(t, parser, printer, "foo bar", "foo bar") |
| 1414 |
|
| 1415 |
// Disable the option, and ensure it's disabled. |
| 1416 |
KeepPadding(false)(printer) |
| 1417 |
printTest(t, parser, printer, "foo bar", "foo bar") |
| 1418 |
} |