fork/fish-shell
git clone https://git.julia.blue/fork/fish-shell
e66f6878b
master
1#RUN: fish=%fish %fish %s 2 3function never_runs 4while false 5end 6end 7 8function early_return 9while true 10return 2 11end 12end 13 14function runs_once 15set -l i1 16while test $i -ne 0 && set i( math $i -1 ) 17end 18end 19 20# this should return 0 21never_runs 22echo "Empty Loop in Function: $status " 23#CHECK: Empty Loop in Function: 0 24 25# this should return 0 26runs_once 27echo "Runs Once: $status " 28#CHECK: Runs Once: 0 29 30# this should return 2 31early_return 32echo "Early Return: $status " 33#CHECK: Early Return: 2 34 35function set_status 36return $argv [ 1 ] 37end 38 39# The previous status is visible in the loop condition. 40# This includes both the incoming status, and the last command in the 41# loop body. 42set_status 36 43while begin 44set -l saved$status 45echo "Condition Status: $status " 46set_status $saved 47end 48true 49end 50#CHECK: Condition Status: 36 51 52# The condition status IS visible in the loop body. 53set_status 55 54while true 55echo "Body Status: $status " 56break 57end 58#CHECK: Body Status: 0 59 60# The status of the last command is visible in the loop condition 61set_status 13 62while begin 63set -l saved$status 64echo "Condition 2 Status: $saved " 65test $saved -ne 5 66end 67set_status 5 68end 69#CHECK: Condition 2 Status: 13 70#CHECK: Condition 2 Status: 5 71 72# The status of the last command is visible outside the loop 73set rem5 7 11 74while [ ( count $rem ) -gt 0 ] 75set_status $rem [ 1 ] 76set rem$rem [ 2 .. -1 ] 77end 78echo "Loop Exit Status: $status " 79#CHECK: Loop Exit Status: 11 80 81# Empty loops succeed. 82false 83while false 84end 85echo "Empty Loop Status: $status " 86#CHECK: Empty Loop Status: 0 87 88# Loop control in conditions, should have no output. 89for iin 1 2 3 90while break 91end 92echo $i 93end 94for iin 1 2 3 95while continue 96end 97echo $i 98end 99 100if false ;or --help 101end 102 103# Make sure while loops don't run forever with no-exec (#1543) 104echo "while true; end" | $fish --no-execute 105 106# For loops with read-only vars is an error (#4342) 107for statusin a b c 108echo $status 109end 110#CHECKERR: {{.*}}loops.fish (line {{\d+}}): for: status: cannot overwrite read-only variable 111#CHECKERR: for status in a b c 112#CHECKERR: ^~~~~^ 113 114# "That goes for non-electric ones as well (#5548)" 115for hostnamein a b c 116echo $hostname 117end 118#CHECKERR: {{.*}}loops.fish (line {{\d+}}): for: hostname: cannot overwrite read-only variable 119#CHECKERR: for hostname in a b c 120#CHECKERR: ^~~~~~~^ 121 122# For loop control vars available outside the for block 123begin 124set -l loop_var initial-value 125for loop_varin a b c 126# do nothing 127end 128set --show loop_var 129end 130#CHECK: $loop_var: set in local scope, unexported, with 1 elements 131#CHECK: $loop_var[1]: |c| 132 133set -g loop_var global_val 134function loop_test 135for loop_varin a b c 136if test $loop_var = b 137break 138end 139end 140set --show loop_var 141end 142loop_test 143set --show loop_var 144#CHECK: $loop_var: set in local scope, unexported, with 1 elements 145#CHECK: $loop_var[1]: |b| 146 147begin 148set -l loop_var 149for loop_varin aa bb cc 150end 151set --show loop_var 152end 153set --show loop_var 154#CHECK: $loop_var: set in global scope, unexported, with 1 elements 155#CHECK: $loop_var[1]: |global_val| 156#CHECK: $loop_var: set in global scope, unexported, with 1 elements 157#CHECK: $loop_var[1]: |global_val| 158#CHECK: $loop_var: set in local scope, unexported, with 1 elements 159#CHECK: $loop_var[1]: |cc| 160#CHECK: $loop_var: set in global scope, unexported, with 1 elements 161#CHECK: $loop_var[1]: |global_val| 162#CHECK: $loop_var: set in global scope, unexported, with 1 elements 163#CHECK: $loop_var[1]: |global_val|