fork/fish-shell
git clone https://git.julia.blue/fork/fish-shell
99d63c21f
master
1# RUN: fish_indent=%fish_indent %fish %s 2# Test file for fish_indent 3# Note that littlecheck ignores leading whitespace, so we have to use {{ }} to explicitly match it. 4 5fish_indent --no-such-option 6#CHECKERR: fish_indent: --no-such-option: unknown option 7 8fish_indent --check=foo 9#CHECKERR: fish_indent: --check=foo: option does not take an argument 10 11fish_indent -w 12#CHECKERR: Expected file path to read/write for -w: 13#CHECKERR: 14#CHECKERR: {{ }}$ fish -w foo.fish 15 16fish_indent -w nonexistent 17#CHECKERR: Opening "nonexistent" failed: No such file or directory (os error {{\d+}}) 18 19echo 'echo foo \\ 20| cat' | $fish_indent 21#CHECK: echo foo \ 22#CHECK: | cat 23 24echo 'echo foo | \\ 25cat' | $fish_indent 26#CHECK: echo foo | cat 27 28echo 'echo foo | 29cat' | $fish_indent 30#CHECK: echo foo | 31#CHECK: cat 32 33echo 'if true; \\ 34or false 35echo something 36end' | $fish_indent 37#CHECK: if true; or false 38#CHECK: echo something 39#CHECK: end 40 41echo '\\ 42echo wurst' | $fish_indent 43#CHECK: echo wurst 44 45echo 'echo foo \\ 46brot' | $fish_indent 47#CHECK: echo foo \ 48#CHECK: brot 49 50echo 'echo rabarber \\ 51banana' | $fish_indent 52#CHECK: echo rabarber \ 53#CHECK: banana 54 55echo 'for x in a \\ 56b \\ 57c 58echo thing 59end' | $fish_indent 60#CHECK: for x in a \ 61#CHECK: b \ 62#CHECK: c 63#CHECK: echo thing 64#CHECK: end 65 66echo 'echo foo | 67echo banana' | $fish_indent 68#CHECK: echo foo | 69#CHECK: echo banana 70 71echo 'echo foo \\ 72;' | $fish_indent 73#CHECK: echo foo 74 75echo 'echo foo \\ 76' | $fish_indent 77#CHECK: echo foo 78 79echo -n' 80begin 81echo hi 82 83 84end | cat | cat | begin ; echo hi ; end | begin ; begin ; echo hi ; end ; end arg 85' | $fish_indent 86 87#CHECK: begin 88#CHECK: {{ }}echo hi 89#CHECK: 90#CHECK: end | cat | cat | begin 91#CHECK: {{ }}echo hi 92#CHECK: end | begin 93#CHECK: {{ }}begin 94#CHECK: {{ }}{{ }}echo hi 95#CHECK: {{ }}end 96#CHECK: end arg 97 98echo -n' 99switch aloha 100 101case alpha 102echo sup 103 104case beta gamma 105echo hi 106 107end 108' | $fish_indent 109 110#CHECK: switch aloha 111#CHECK: {{ }}case alpha 112#CHECK: {{ }}{{ }}echo sup 113#CHECK: {{ }}case beta gamma 114#CHECK: {{ }}{{ }}echo hi 115#CHECK: 116#CHECK: end 117 118echo -n' 119function hello_world 120 121\'begin\' 122echo hi 123end | cat 124 125echo sup; echo sup 126echo hello; 127 128echo hello 129end 130' | $fish_indent 131 132#CHECK: function hello_world 133#CHECK: 134#CHECK: {{ }}begin 135#CHECK: {{ }}{{ }}echo hi 136#CHECK: {{ }}end | cat 137#CHECK: 138#CHECK: {{ }}echo sup 139#CHECK: {{ }}echo sup 140#CHECK: {{ }}echo hello 141#CHECK: 142#CHECK: {{ }}echo hello 143#CHECK: end 144 145echo -n' 146echo alpha #comment1 147#comment2 148 149#comment3 150for i in abc #comment1 151#comment2 152echo hi 153end 154 155switch foo #abc 156# bar 157case bar 158echo baz\ 159qqq 160case "*" 161echo sup 162end' | $fish_indent 163#CHECK: echo alpha #comment1 164#CHECK: #comment2 165#CHECK: 166#CHECK: #comment3 167#CHECK: for i in abc #comment1 168#CHECK: {{ }}#comment2 169#CHECK: {{ }}echo hi 170#CHECK: end 171#CHECK: 172#CHECK: switch foo #abc 173#CHECK: {{ }}# bar 174#CHECK: {{ }}case bar 175#CHECK: {{ }}{{ }}echo baz\ 176#CHECK: qqq 177#CHECK: {{ }}case "*" 178#CHECK: {{ }}{{ }}echo sup 179#CHECK: end 180 181# No indent 182echo -n' 183if true 184else if false 185echo alpha 186switch beta 187case gamma 188echo delta 189end 190end 191' | $fish_indent -i 192#CHECK: if true 193#CHECK: else if false 194#CHECK: {{^}}echo alpha 195#CHECK: {{^}}switch beta 196#CHECK: {{^}}case gamma 197#CHECK: {{^}}echo delta 198#CHECK: {{^}}end 199#CHECK: end 200 201# Test errors 202echo -n' 203begin 204echo hi 205else 206echo bye 207end; echo alpha " 208' | $fish_indent 209#CHECK: begin 210#CHECK: {{ }}echo hi 211#CHECK: else 212#CHECK: 213#CHECK: {{^}}echo bye 214#CHECK: end; echo alpha " 215 216# issue 1665 217echo -n' 218if begin ; false; end; echo hi ; end 219while begin ; false; end; echo hi ; end 220' | $fish_indent 221#CHECK: if begin 222#CHECK: {{ }}{{ }}false 223#CHECK: {{ }}end 224#CHECK: {{ }}echo hi 225#CHECK: end 226#CHECK: while begin 227#CHECK: {{ }}{{ }}false 228#CHECK: {{ }}end 229#CHECK: {{ }}echo hi 230#CHECK: end 231 232# issue 2899 233echo -n' 234echo < stdin >>appended yes 2>&1 no > stdout maybe 2>& 4 | cat 2>| cat 235' | $fish_indent 236#CHECK: echo <stdin >>appended yes 2>&1 no >stdout maybe 2>&4 | cat 2>| cat 237 238# issue 7252 239echo -n' 240begin 241# comment 242end 243' | $fish_indent 244#CHECK: {{^}}begin 245#CHECK: {{^ }}# comment 246#CHECK: {{^}}end 247 248echo -n' 249begin 250cmd 251# comment 252end 253' | $fish_indent 254#CHECK: {{^}}begin 255#CHECK: {{^ }}cmd 256#CHECK: {{^ }}# comment 257#CHECK: {{^}}end 258 259echo -n' 260cmd \\ 261continuation 262' | $fish_indent 263#CHECK: {{^}}cmd \ 264#CHECK: {{^ }}continuation 265 266echo -n' 267begin 268cmd \ 269continuation 270end 271' | $fish_indent 272#CHECK: {{^}}begin 273#CHECK: {{^ }}cmd \ 274#CHECK: {{^ }}{{ }}continuation 275#CHECK: {{^}}end 276 277echo -n' 278i\ 279f true 280echo yes 281en\ 282d 283 284"whil\ 285e" true 286"builtin" yes 287en"d" 288 289alpha | \ 290beta 291 292gamma | \ 293# comment3 294delta 295 296if true 297echo abc 298end 299 300if false # comment4 301and true && false 302echo abc;end 303 304echo hi | 305 306echo bye 307' | $fish_indent 308#CHECK: i\ 309#CHECK: f true 310#CHECK: {{ }}echo yes 311#CHECK: en\ 312#CHECK: d 313#CHECK: 314#CHECK: while true 315#CHECK: {{ }}builtin yes 316#CHECK: end 317#CHECK: 318#CHECK: alpha | beta 319#CHECK: 320#CHECK: gamma | \ 321#CHECK: # comment3 322#CHECK: delta 323#CHECK: 324#CHECK: if true 325#CHECK: {{ }}echo abc 326#CHECK: end 327#CHECK: 328#CHECK: if false # comment4 329#CHECK: {{ }}and true && false 330#CHECK: {{ }}echo abc 331#CHECK: end 332#CHECK: 333#CHECK: echo hi | 334#CHECK: {{ }}echo bye 335 336echo 'a;;;;;;' | $fish_indent 337#CHECK: a 338echo 'echo; echo' | $fish_indent 339#CHECK: echo 340#CHECK: echo 341 342# Check that we keep semicolons for and and or, but only on the same line. 343printf '%s\n' "a; and b" | $fish_indent 344#CHECK: a; and b 345printf '%s\n' "a;" "and b" | $fish_indent 346#CHECK: a 347#CHECK: and b 348printf '%s\n' a"; and b" | $fish_indent 349#CHECK: a 350#CHECK: and b 351printf '%s\n' "a; b" | $fish_indent 352#CHECK: a 353#CHECK: b 354 355echo 'foo && 356# 357bar' | $fish_indent 358#CHECK: {{^}}foo && 359#CHECK: {{^ }}# 360#CHECK: {{ }}bar 361 362echo 'command 1 | 363command 1 cont || 364command 2' | $fish_indent 365#CHECK: {{^}}command 1 | 366#CHECK: {{^ }}command 1 cont || 367#CHECK: {{^ }}command 2 368 369echo " foo" | $fish_indent --check 370echo $status 371#CHECK: 1 372echo foo| $fish_indent --check 373echo $status 374#CHECK: 0 375 376echo 'thing | # comment 377thing' | $fish_indent --check 378echo $status 379#CHECK: 0 380 381echo 'echo \ 382# first indented comment 383# second indented comment 384indented argument 385echo' | $fish_indent --check 386echo $status 387#CHECK: 0 388 389echo 'if true; 390end' | $fish_indent 391#CHECK: if true{{$}} 392#CHECK: end 393 394echo 'if true; and false; or true 395end' | $fish_indent --check 396echo $status 397#CHECK: 0 398 399echo -n' 400function hello_continuations 401echo cmd \ 402echo --opt1 \ 403echo --opt2 \ 404echo --opt3 405end 406' | $fish_indent 407 408#CHECK: function hello_continuations 409#CHECK: {{^}} echo cmd \ 410#CHECK: {{^}} echo --opt1 \ 411#CHECK: {{^}} echo --opt2 \ 412#CHECK: {{^}} echo --opt3 413#CHECK: end 414 415echo "\ 416a=1 \\ 417a=2 \\ 418echo" | $fish_indent --check 419echo $status #CHECK: 0 420 421echo "\ 422a=1 \\ 423a=2 echo" | $fish_indent --check 424echo $status #CHECK: 0 425 426echo 'first-word\\ 427second-word' | $fish_indent 428# CHECK: first-word \ 429# CHECK: {{^}} second-word 430 431echo 'begin 432first-indented-word\\ 433second-indented-word' | $fish_indent 434# CHECK: begin 435# CHECK: {{^}} first-indented-word \ 436# CHECK: {{^}} second-indented-word 437 438{ 439 echo'{ no semi }' 440# CHECK: { no semi } 441echo '{ semi; }' 442# CHECK: { semi; } 443 444echo '{ multi; no semi }' 445# CHECK: { multi; no semi } 446echo '{ multi; semi; }' 447# CHECK: { multi; semi; } 448 449echo '{ conj && no semi }' 450# CHECK: { conj && no semi } 451echo '{ conj && semi; }' 452# CHECK: { conj && semi; } 453 454echo '{ }' 455# CHECK: { } 456echo '{ ; }' 457# CHECK: { } 458 459echo '#foo 460{ }' | $fish_indent 461# CHECK: #foo 462# CHECK: { } 463 464echo ' 465{ 466echo \\ 467# continuation comment 468}' 469# CHECK: { 470# CHECK: {{^ }}echo \ 471# CHECK: {{^ }}# continuation comment 472# TODO: This is currently broken; so this the begin/end equivalent. 473# CHECK: {{^ [}]}} 474 475echo '{ { } }' 476# CHECK: { { } } 477 478echo '{{}}' 479# CHECK: { { } } 480 481echo ' 482{ 483 484{ 485} 486 487} 488' 489# CHECK: {{^\{$}} 490# CHECK: {{^ \{$}} 491# CHECK: {{^ \}$}} 492# CHECK: {{^\}$}} 493 494echo ' 495{ level 1; { 496level 2 } } 497' 498# TODO Should add a line break here. 499# CHECK: {{^{ level 1$}} 500# CHECK: {{^ \{$}} 501# CHECK: {{^ level 2$}} 502# CHECK: {{^ \}$}} 503# CHECK: {{^\}$}} 504} | $fish_indent 505 506echo 'test 1 -eq 1; or { 507echo a 508echo b 509}' | $fish_indent 510# CHECK: test 1 -eq 1; or { 511# CHECK: {{^ }}echo a 512# CHECK: {{^ }}echo b 513# CHECK: {{^}}{{[}]}} 514 515echo 'not { 516echo hi 517}' | $fish_indent 518# CHECK: not { 519# CHECK: {{^ }}echo hi 520# CHECK: {{^}}{{[}]}} 521 522echo 'time { 523echo hi 524}' | $fish_indent 525# CHECK: time { 526# CHECK: {{^ }}echo hi 527# CHECK: {{^}}{{[}]}} 528 529echo 'if { 530true 531} 532echo ok 533end' | $fish_indent 534# CHECK: if { 535# CHECK: {{^ }}true 536# CHECK: {{^ }}{{[}]}} 537# CHECK: {{^ }}echo ok 538# CHECK: {{^}}end 539 540echo 'while { 541true 542} 543echo ok 544end' | $fish_indent 545# CHECK: while { 546# CHECK: {{^ }}true 547# CHECK: {{^ }}{{[}]}} 548# CHECK: {{^ }}echo ok 549# CHECK: {{^}}end 550 551echo 'echo x{a, 552b}y' | $fish_indent 553# CHECK: echo x{a, 554# CHECK: {{^ }}b}y 555 556echo 'multiline-\\ 557-word' | $fish_indent --check 558echo $status #CHECK: 0 559 560echo 'PATH={$PATH[echo " "' | $fish_indent --ansi 561# CHECK: PATH={$PATH[echo " " 562 563fish_config theme choose "ayu Dark" 564echo -n 'echo hello' | builtin fish_indent --ansi 565echo end 566# CHECK: {{\x1b\[38;2;57;186;230mecho\x1b\[38;2;179;177;173m hello\x1b\[38;2;242;150;104m\x1b\[39m}} 567# CHECK: end 568 569echo a \> | $fish_indent 570# CHECK: a > 571 572echo a \<\) | $fish_indent 573# CHECK: a < ) 574echo b \|\{ | $fish_indent 575# CHECK: b | { 576 577echo "\'\\\\\x00\'" | string unescape | $fish_indent | string escape 578# CHECK: \'\\\x00\' 579 580echo '\"\"\|\x00' | string unescape | $fish_indent | string unescape 581# CHECK: | 582 583echo 'a 584 585 586; 587 588 589b 590' | $fish_indent 591#CHECK: a 592#CHECK: 593#CHECK: b 594 595echo " 596 597 598 599echo this file starts late 600" | $fish_indent 601#CHECK: echo this file starts late 602 603echo 'foo|bar; begin 604echo' | $fish_indent --only-indent 605# CHECK: foo|bar; begin 606# CHECK: {{^}} echo 607 608echo 'begin 609echo 610end' | $fish_indent --only-unindent 611# CHECK: {{^}}begin 612# CHECK: {{^}}echo 613# CHECK: {{^}}end 614 615echo 'if true 616begin 617echo 618end 619end' | $fish_indent --only-unindent 620# CHECK: {{^}}if true 621# CHECK: {{^}}begin 622# CHECK: {{^}}echo 623# CHECK: {{^}}end 624# CHECK: {{^}}end 625 626echo 'begin 627echo 628not indented properly 629end' | $fish_indent --only-unindent 630# CHECK: {{^}}begin 631# CHECK: {{^}} echo 632# CHECK: {{^}} not indented properly 633# CHECK: {{^}}end 634 635echo 'echo ( 636if true 637echo 638end 639)' | $fish_indent --only-indent 640# CHECK: {{^}}echo ( 641# CHECK: {{^}} if true 642# CHECK: {{^}} echo 643# CHECK: {{^}} end 644# CHECK: {{^}}) 645 646echo 'echo ( 647if true 648echo " 649multi 650line 651" 652end 653)' | $fish_indent --only-indent 654# CHECK: {{^}}echo ( 655# CHECK: {{^}} if true 656# CHECK: {{^}} echo " 657# CHECK: {{^}}multi 658# CHECK: {{^}}line 659# CHECK: {{^}}" 660# CHECK: {{^}} end 661# CHECK: {{^}}) 662 663echo 'echo ( 664if true 665echo " 666multi 667line 668" 669end 670)' | builtin fish_indent --only-indent 671# CHECK: {{^}}echo ( 672# CHECK: {{^}} if true 673# CHECK: {{^}} echo " 674# CHECK: {{^}}multi 675# CHECK: {{^}}line 676# CHECK: {{^}}" 677# CHECK: {{^}} end 678# CHECK: {{^}}) 679 680set -l tmpdir ( mktemp -d) 681echo 'echo "foo" "bar"' > $tmpdir /indent_test.fish 682$fish_indent --write $tmpdir /indent_test.fish 683cat $tmpdir /indent_test.fish 684# CHECK: echo foo bar 685 686echo 'echo "foo" "bar"' > $tmpdir /indent_test.fish 687chmod 400 $tmpdir /indent_test.fish 688$fish_indent --write $tmpdir /indent_test.fish 689# CHECKERR: Opening "{{.*}}/indent_test.fish" failed: {{.*}}) 690 691# See that the builtin can be redirected 692printf %s \n a b c | builtin fish_indent | grep b 693# CHECK: b 694 695# Regression test that fish_indent doesn't panic with closed stdin. 696fish_indent <&- 697# CHECKERR: fish_indent: stdin is closed 698 699function __fish_print_help 700echo Help using PATH[ 1 ]= $PATH [ 1 ] 701end 702PATH=hello fish_indent --help 703# CHECK: Help using PATH[1]=hello