fork/fish-shell
git clone https://git.julia.blue/fork/fish-shell
972528648
master
1# RUN: fish=%fish %fish %s 2# 3# Test function, loops, conditionals and some basic elements 4# 5 6# The test driver always starts each test in its own temporary directory, but to make it easier to 7# run this test directly for whatever reason: 8set -g tmpdir( mktemp -d) 9 10# Comments in odd places don't cause problems 11for iin 1 2 # Comment on same line as command 12# Comment inside loop 13for jin a b 14# Double loop 15echo $i$j 16end ; 17end 18#CHECK: 1a 19#CHECK: 1b 20#CHECK: 2a 21#CHECK: 2b 22 23# Escaped newlines 24echo foo\ bar 25echo foo\ 26bar 27echo "foo\ 28bar" 29echo 'foo\ 30bar' 31#CHECK: foo bar 32#CHECK: foobar 33#CHECK: foobar 34#CHECK: foo\ 35#CHECK: bar 36 37for iin \ 38 a b c 39echo $i 40end 41#CHECK: a 42#CHECK: b 43#CHECK: c 44 45 46# Simple function tests 47 48function foo 49echo > $tmpdir /fish_foo.txt$argv 50end 51 52foo hello 53 54cat $tmpdir /fish_foo.txt| read foo 55 56if test $foo = hello; 57echo Test2 pass 58else 59echo Test2 fail 60end 61#CHECK: Test 2 pass 62 63function foo 64printf 'Test %s' $argv [ 1 ] ;echo ' pass' 65end 66 67foo 3a 68#CHECK: Test 3a pass 69 70for iin Test for continue break and switch builtins problems; 71switch $i 72case Test 73printf "%s " $i 74case "for" 75printf "%s " 3b 76case "c*" 77echo pass 78case break 79continue 80echo fail 81case and 82break 83echo fail 84case "*" 85echo fail 86end 87end 88#CHECK: Test 3b pass 89 90set -l sta 91if eval true 92if eval false 93set sta fail 94else 95set sta pass 96end 97else 98set sta fail 99end 100echo Test4 $sta 101#CHECK: Test 4 pass 102 103# Testing builtin status 104 105function test_builtin_status 106return 1 107end 108test_builtin_status 109if [ $status -eq 1 ] 110set sta pass 111else 112set sta fail 113end 114echo Test5 $sta 115#CHECK: Test 5 pass 116 117 118function test_builtin_status_clamp_to_255 119return 300 120end 121test_builtin_status_clamp_to_255 122echo $status 123#CHECK: 255 124 125$fish -c"exit 300" 126echo $status 127#CHECK: 255 128 129#################### 130# echo tests 131echo 'abc\ndef' 132#CHECK: abc\ndef 133echo -e'abc\ndef' 134#CHECK: abc 135#CHECK: def 136echo -e'abc\zdef' 137#CHECK: abc\zdef 138echo -e'abc\41def' 139echo -e'abc\041def' 140#CHECK: abc!def 141#CHECK: abc!def 142echo -e'abc\121def' 143echo -e'abc\1212def' 144#CHECK: abcQdef 145#CHECK: abcQ2def 146# Test octal overflow: \5555 = 555 octal = 365 decimal, wraps to 109 decimal (155 octal) 147# Followed by literal '5' character (065 octal) 148echo -ne'\5555' | display_bytes 149#CHECK: 0000000 155 065 150#CHECK: 0000002 151echo -e'abc\cdef' # won't output a newline! 152#CHECK: abc 153echo '' 154echo - 155#CHECK: - 156echo -h 157#CHECK: -h 158echo -ne'\376' | display_bytes 159#CHECK: 0000000 376 160#CHECK: 0000001 161echo -e'abc\x21def' 162echo -e'abc\x211def' 163#CHECK: abc!def 164#CHECK: abc!1def 165 166echo \UDE01 167#CHECK: � 168 169# Comments allowed in between lines (#1987) 170echo before comment\ 171# comment 172after comment 173#CHECK: before comment after comment 174 175# Backslashes are part of comments and do not join lines (#1255) 176# This should execute false, not echo it 177echo -n# comment\ 178false 179 180function always_fails 181if true 182return 1 183end 184end 185 186# Verify $argv set correctly in sourced scripts (#139) 187echo 'echo "source argv {$argv}"' | source 188#CHECK: source argv {} 189echo 'echo "source argv {$argv}"' | source - 190#CHECK: source argv {} 191echo 'echo "source argv {$argv}"' | source - abc 192#CHECK: source argv {abc} 193echo 'echo "source argv {$argv}"' | source - abc def 194#CHECK: source argv {abc def} 195 196always_fails 197echo $status 198#CHECK: 1 199 200# Test that subsequent cases do not blow away the status from previous ones 201for valin one two three four 202switch $val 203case one 204/bin/sh -c'exit 1' 205case two 206/bin/sh -c'exit 2' 207case three 208/bin/sh -c'exit 3' 209end 210echo $status 211end 212#CHECK: 1 213#CHECK: 2 214#CHECK: 3 215#CHECK: 0 216 217# Test that the `switch` builtin itself does not blow away status before evaluating a case 218false 219switch one 220case one 221echo $status 222end 223#CHECK: 1 224 225#test contains -i 226contains -i string a b c string d 227#CHECK: 4 228contains -i string a b c d;or echo nothing 229#CHECK: nothing 230contains -i -- string a b c string d 231#CHECK: 4 232contains -i a -q -- a b c 233#CHECK: 3 234contains -i -- -- a b c;or echo nothing 235#CHECK: nothing 236contains -i -- -- a b c -- v 237#CHECK: 4 238contains 239#CHECKERR: contains: Key not specified 240 241# Test if, else, and else if 242if true 243echo alpha1.1 244echo alpha1.2 245else if false 246echo beta1.1 247echo beta1.2 248else if false 249echo gamma1.1 250echo gamma1.2 251else 252echo delta1.1 253echo delta1.2 254end 255#CHECK: alpha1.1 256#CHECK: alpha1.2 257 258if false 259echo alpha2.1 260echo alpha2.2 261else if begin ;true ;end 262echo beta2.1 263echo beta2.2 264else if begin ;echo nope2.1;false ;end 265echo gamma2.1 266echo gamma2.2 267else 268echo delta2.1 269echo delta2.2 270end 271#CHECK: beta2.1 272#CHECK: beta2.2 273 274if false 275echo alpha3.1 276echo alpha3.2 277else if begin ;echo yep3.1;false ;end 278echo beta3.1 279echo beta3.2 280else if begin ;echo yep3.2;true ;end 281echo gamma3.1 282echo gamma3.2 283else 284echo delta3.1 285echo delta3.2 286end 287#CHECK: yep3.1 288#CHECK: yep3.2 289#CHECK: gamma3.1 290#CHECK: gamma3.2 291 292if false 293echo alpha4.1 294echo alpha4.2 295else if begin ;echo yep4.1;false ;end 296echo beta4.1 297echo beta4.2 298else if begin ;echo yep4.2;false ;end 299echo gamma4.1 300echo gamma4.2 301else 302echo delta4.1 303echo delta4.2 304end 305#CHECK: yep4.1 306#CHECK: yep4.2 307#CHECK: delta4.1 308#CHECK: delta4.2 309 310if test !-n "abc" 311else if test -n "def" 312echo "epsilon5.2" 313else if not_a_valid_command but it should be OK because a previous branch was taken 314echo "epsilon 5.3" 315else if test !-n "abc" 316echo "epsilon 5.4" 317end 318#CHECK: epsilon5.2 319 320# Ensure builtins work 321# https://github.com/fish-shell/fish-shell/issues/359 322if not echo skip1> /dev/null 323echo "zeta 6.1" 324else if echo skip2> /dev/null 325echo "zeta 6.2" 326end 327#CHECK: zeta 6.2 328 329echo '###' 330#CHECK: ### 331 332# Ensure 'type' works 333# https://github.com/fish-shell/fish-shell/issues/513 334function fish_test_type_zzz 335true 336end 337# Should succeed 338type -q fish_test_type_zzz ;echo $status 339#CHECK: 0 340# Should fail 341type -q -f fish_test_type_zzz ;echo $status 342#CHECK: 1 343 344# ensure that builtins that produce no output can still truncate files 345# (bug PCA almost reintroduced!) 346echo abc> $tmpdir /file_truncation_test.txt 347cat $tmpdir /file_truncation_test.txt 348echo -n> $tmpdir /file_truncation_test.txt 349cat $tmpdir /file_truncation_test.txt 350#CHECK: abc 351 352# Test events. 353 354 355# This pattern caused a crash; github issue #449 356 357set -g var before 358 359function test1 --on-event test 360set -g var$var :test1 361functions -e test2 362end 363 364function test2 --on-event test 365# this should not run, as test2 gets removed before it has a chance of running 366set -g var$var :test2a 367end 368emit test 369 370echo $var 371#CHECK: before:test1 372 373 374function test3 --on-event test3 375echo received event test3 with args:$argv 376end 377 378emit test3 foo bar 379#CHECK: received event test3 with args: foo bar 380 381# test empty argument 382emit 383#CHECKERR: emit: expected event name 384 385# Test break and continue 386# This should output Ping once 387for iin a b c 388if not contains $i c ;continue ;end 389echo Ping 390end 391#CHECK: Ping 392 393# This should output Pong not at all 394for iin a b c 395if not contains $i c ;break ;end 396echo Pong 397end 398 399# This should output Foop three times, and Boop not at all 400set i a a a 401while contains $i a 402set -e i[ -1 ] 403echo Foop 404continue 405echo Boop 406end 407#CHECK: Foop 408#CHECK: Foop 409#CHECK: Foop 410 411# This should output Doop once 412set i a a a 413while contains $i a 414set -e i[ -1 ] 415echo Doop 416break 417echo Darp 418end 419#CHECK: Doop 420 421# break and continue may be dynamically invoked. 422set dyn_break break 423set dyn_continue continue 424 425while true 426$dyn_break 427echo "I should be unreachable" 428end 429 430for fooin 1 2 3 431$dyn_continue 432echo "I should be unreachable" 433end 434 435# Check that these error correctly. 436$dyn_break 437eval break 438#CHECKERR: break: Not inside of loop 439#CHECKERR: break: Not inside of loop 440$dyn_continue 441eval continue 442#CHECKERR: continue: Not inside of loop 443#CHECKERR: continue: Not inside of loop 444 445break -h 446#CHECKERR: Documentation for break 447break --help 448#CHECKERR: Documentation for break 449$dyn_break -h 450#CHECKERR: Documentation for break 451continue -h 452#CHECKERR: Documentation for continue 453 454for iin abc 455break foo 456end 457#CHECKERR: break: foo: unknown option 458 459# Test implicit cd. This should do nothing. 460./ 461 462# Test special for loop expansion 463# Here we the name of the variable is derived from another variable 464set var1 var2 465for $var1 in1 2 3 466echo -n$var2 467end 468echo 469#CHECK: 123 470 471# Test status -n 472eval 'status -n 473status -n 474status -n' 475#CHECK: 1 476#CHECK: 2 477#CHECK: 3 478 479# Test support for unbalanced blocks 480function try_unbalanced_block 481$fish -c"echo $argv | source " 2>&1 | grep "Missing end" 1>&2 482end 483try_unbalanced_block 'begin' 484#CHECKERR: - (line 1): Missing end to balance this begin 485try_unbalanced_block 'while true' 486#CHECKERR: - (line 1): Missing end to balance this while loop 487try_unbalanced_block 'for x in 1 2 3' 488#CHECKERR: - (line 1): Missing end to balance this for loop 489try_unbalanced_block 'switch abc' 490#CHECKERR: - (line 1): Missing end to balance this switch statement 491try_unbalanced_block 'function anything' 492#CHECKERR: - (line 1): Missing end to balance this function definition 493try_unbalanced_block 'if false' 494#CHECKERR: - (line 1): Missing end to balance this if statement 495 496# Ensure that quoted keywords work 497'while' false;end 498"while" false;end 499"wh"'ile' false;"e" nd 500 501# BOM checking (see #1518). But only in UTF8 locales. 502# (locale guarded because of musl) 503if command -sq locale;and string match -qi'*utf-8*' --( locale ) 504echo \uFEFF"echo bom_test" | source 505else 506echo "echo bom_test" | source 507end 508#CHECK: bom_test 509 510# Comments abutting text (#953) 511echo not#a#comment 512#CHECK: not#a#comment 513echo is# a # comment 514#CHECK: is 515 516# Test that our builtins can all do --query 517command --query cp 518echo $status 519#CHECK: 0 520 521type --query cp 522echo $status 523#CHECK: 0 524 525jobs --query1 526echo $status 527#CHECK: 1 528 529abbr --query thisshouldnotbeanabbreviationohmygoshitssolongwhywouldanyoneeverusethis 530echo $status 531#CHECK: 1 532 533functions --query alias 534echo $status 535#CHECK: 0 536 537set --query status 538echo $status 539#CHECK: 0 540 541builtin --query echo 542echo $status 543#CHECK: 0 544 545# Check that echo doesn't interpret options *and print them* 546# at the start of quoted args: 547echo '-ne \tart' 548# CHECK: -ne \tart 549echo '-n art' 550echo banana 551# CHECK: -n art 552# CHECK: banana 553 554# This used to be a parse error - #7685. 555echo ( echo hello\\ ) 556# CHECK: hello\ 557 558# This used to be a parse error - #7866. 559echo ( echo foo;#) 560) 561# CHECK: foo 562echo ( echo bar#' 563) 564# CHECK: bar 565echo ( #" 566echo baz) 567# CHECK: baz 568 569# Make sure we don't match up brackets within comments (#8022). 570$fish -c'echo f[oo # not valid, no matching ]' 571# CHECKERR: fish: Unexpected end of string, square brackets do not match 572# CHECKERR: echo f[oo # not valid, no matching ] 573# CHECKERR: {{ }}^ 574 575# Should fail because $PWD is read-only. 576for PWDin foo bar 577true 578end 579# CHECKERR: {{.*}}/basic.fish (line {{\d+}}): for: PWD: cannot overwrite read-only variable 580# CHECKERR: for PWD in foo bar 581# CHECKERR: ^~^ 582# XXX FIXME carat should point at PWD 583 584$fish -c'echo \xtest' 585# CHECKERR: fish: Invalid token '\xtest' 586# CHECKERR: echo \xtest 587# CHECKERR: ^~~~~^ 588 589$fish -c'echo \utest' 590# CHECKERR: fish: Invalid token '\utest' 591# CHECKERR: echo \utest 592# CHECKERR: ^~~~~^ 593 594echo $status 595# CHECK: 127 596 597$fish -c'echo \c' 598# CHECKERR: fish: Incomplete escape sequence '\c' 599# CHECKERR: echo \c 600# CHECKERR: ^^ 601 602echo $status 603# CHECK: 127 604 605$fish -c'echo \C' 606# CHECK: C 607echo $status 608# CHECK: 0 609 610$fish -c'echo \U' 611# CHECKERR: fish: Incomplete escape sequence '\U' 612# CHECKERR: echo \U 613# CHECKERR: ^^ 614 615echo $status 616# CHECK: 127 617 618$fish -c'echo \x' 619# CHECKERR: fish: Incomplete escape sequence '\x' 620# CHECKERR: echo \x 621# CHECKERR: ^^ 622 623echo $status 624# CHECK: 127 625 626$fish -c begin 627# CHECKERR: fish: Missing end to balance this begin 628# CHECKERR: begin 629# CHECKERR: ^~~~^ 630 631echo $status 632# CHECK: 127 633 634$fish -c'echo \ufdd2"fart"' 635# CHECKERR: fish: Invalid token '\ufdd2"fart"' 636# CHECKERR: echo \ufdd2"fart" 637# CHECKERR: ^~~~~~~~~~~^ 638 639echo ( printf '\ufdd2foo' ) | string escape 640# CHECK: \Xef\Xb7\X92foo 641 642echo ( printf '\ufdd8foo' ) | string escape 643# CHECK: \Xef\Xb7\X98foo 644 645printf '%s\n' "#!/bin/sh" 'echo $0' > $tmpdir /argv0.sh 646chmod +x$tmpdir /argv0.sh 647cd $tmpdir 648./argv0.sh 649# CHECK: ./argv0.sh