2020-02-08 16:45:22 +00:00
|
|
|
#RUN: %fish -C 'set -g fish %fish' %s
|
2019-01-14 00:57:38 +00:00
|
|
|
|
|
|
|
function never_runs
|
2020-03-09 18:36:12 +00:00
|
|
|
while false
|
|
|
|
end
|
2019-01-14 00:57:38 +00:00
|
|
|
end
|
|
|
|
|
|
|
|
function early_return
|
2020-03-09 18:36:12 +00:00
|
|
|
while true
|
|
|
|
return 2
|
|
|
|
end
|
2019-01-14 00:57:38 +00:00
|
|
|
end
|
|
|
|
|
|
|
|
function runs_once
|
2020-03-09 18:36:12 +00:00
|
|
|
set -l i 1
|
|
|
|
while test $i -ne 0 && set i (math $i - 1)
|
|
|
|
end
|
2019-01-14 00:57:38 +00:00
|
|
|
end
|
|
|
|
|
2020-02-08 14:21:22 +00:00
|
|
|
# this should return 0
|
2020-03-09 18:36:12 +00:00
|
|
|
never_runs
|
|
|
|
echo "Empty Loop in Function: $status"
|
2020-02-08 14:21:22 +00:00
|
|
|
#CHECK: Empty Loop in Function: 0
|
2019-01-14 00:57:38 +00:00
|
|
|
|
|
|
|
# this should return 0
|
2020-03-09 18:36:12 +00:00
|
|
|
runs_once
|
|
|
|
echo "Runs Once: $status"
|
2020-02-08 14:21:22 +00:00
|
|
|
#CHECK: Runs Once: 0
|
2019-01-14 00:57:38 +00:00
|
|
|
|
|
|
|
# this should return 2
|
2020-03-09 18:36:12 +00:00
|
|
|
early_return
|
|
|
|
echo "Early Return: $status"
|
2020-02-08 14:21:22 +00:00
|
|
|
#CHECK: Early Return: 2
|
2019-01-21 00:37:20 +00:00
|
|
|
|
2020-03-09 18:36:12 +00:00
|
|
|
function set_status
|
|
|
|
return $argv[1]
|
|
|
|
end
|
2019-01-21 00:37:20 +00:00
|
|
|
|
|
|
|
# The previous status is visible in the loop condition.
|
|
|
|
# This includes both the incoming status, and the last command in the
|
|
|
|
# loop body.
|
|
|
|
set_status 36
|
|
|
|
while begin
|
2020-03-09 18:36:12 +00:00
|
|
|
set -l saved $status
|
|
|
|
echo "Condition Status: $status"
|
|
|
|
set_status $saved
|
|
|
|
end
|
|
|
|
true
|
2019-01-21 00:37:20 +00:00
|
|
|
end
|
2020-02-08 14:21:22 +00:00
|
|
|
#CHECK: Condition Status: 36
|
2019-01-21 00:37:20 +00:00
|
|
|
|
|
|
|
# The condition status IS visible in the loop body.
|
|
|
|
set_status 55
|
|
|
|
while true
|
2020-03-09 18:36:12 +00:00
|
|
|
echo "Body Status: $status"
|
|
|
|
break
|
2019-01-21 00:37:20 +00:00
|
|
|
end
|
2020-02-08 14:21:22 +00:00
|
|
|
#CHECK: Body Status: 0
|
2019-01-21 00:37:20 +00:00
|
|
|
|
|
|
|
# The status of the last command is visible in the loop condition
|
|
|
|
set_status 13
|
|
|
|
while begin
|
2020-03-09 18:36:12 +00:00
|
|
|
set -l saved $status
|
|
|
|
echo "Condition 2 Status: $saved"
|
|
|
|
test $saved -ne 5
|
|
|
|
end
|
|
|
|
set_status 5
|
2019-01-21 00:37:20 +00:00
|
|
|
end
|
2020-02-08 14:21:22 +00:00
|
|
|
#CHECK: Condition 2 Status: 13
|
|
|
|
#CHECK: Condition 2 Status: 5
|
2019-01-21 00:37:20 +00:00
|
|
|
|
|
|
|
# The status of the last command is visible outside the loop
|
|
|
|
set rem 5 7 11
|
|
|
|
while [ (count $rem) -gt 0 ]
|
2020-03-09 18:36:12 +00:00
|
|
|
set_status $rem[1]
|
|
|
|
set rem $rem[2..-1]
|
2019-01-21 00:37:20 +00:00
|
|
|
end
|
|
|
|
echo "Loop Exit Status: $status"
|
2020-02-08 14:21:22 +00:00
|
|
|
#CHECK: Loop Exit Status: 11
|
2019-01-21 00:37:20 +00:00
|
|
|
|
|
|
|
# Empty loops succeed.
|
|
|
|
false
|
2020-03-09 18:36:12 +00:00
|
|
|
while false
|
|
|
|
end
|
2019-01-21 00:37:20 +00:00
|
|
|
echo "Empty Loop Status: $status"
|
2020-02-08 14:21:22 +00:00
|
|
|
#CHECK: Empty Loop Status: 0
|
2020-02-08 14:37:10 +00:00
|
|
|
|
|
|
|
# Loop control in conditions, should have no output.
|
|
|
|
for i in 1 2 3
|
2020-03-09 18:36:12 +00:00
|
|
|
while break
|
|
|
|
end
|
2020-02-08 16:45:22 +00:00
|
|
|
echo $i
|
2020-02-08 14:37:10 +00:00
|
|
|
end
|
|
|
|
for i in 1 2 3
|
2020-03-09 18:36:12 +00:00
|
|
|
while continue
|
|
|
|
end
|
2020-02-08 16:45:22 +00:00
|
|
|
echo $i
|
2020-02-08 14:37:10 +00:00
|
|
|
end
|
|
|
|
|
2020-03-09 18:36:12 +00:00
|
|
|
if false; or --help
|
|
|
|
end
|
2020-02-08 16:45:22 +00:00
|
|
|
|
|
|
|
# Make sure while loops don't run forever with no-exec (#1543)
|
|
|
|
echo "while true; end" | $fish --no-execute
|
|
|
|
|
|
|
|
# For loops with read-only vars is an error (#4342)
|
|
|
|
for status in a b c
|
|
|
|
echo $status
|
|
|
|
end
|
|
|
|
#CHECKERR: {{.*}}loops.fish (line {{\d+}}): You cannot use read-only variable 'status' in a for loop
|
|
|
|
#CHECKERR: for status in a b c
|
|
|
|
#CHECKERR: ^
|
|
|
|
|
|
|
|
# "That goes for non-electric ones as well (#5548)"
|
|
|
|
for hostname in a b c
|
|
|
|
echo $hostname
|
|
|
|
end
|
|
|
|
#CHECKERR: {{.*}}loops.fish (line {{\d+}}): You cannot use read-only variable 'hostname' in a for loop
|
|
|
|
#CHECKERR: for hostname in a b c
|
|
|
|
#CHECKERR: ^
|
|
|
|
|
|
|
|
# For loop control vars available outside the for block
|
|
|
|
begin
|
|
|
|
set -l loop_var initial-value
|
|
|
|
for loop_var in a b c
|
|
|
|
# do nothing
|
|
|
|
end
|
|
|
|
set --show loop_var
|
|
|
|
end
|
|
|
|
|
|
|
|
set -g loop_var global_val
|
|
|
|
function loop_test
|
|
|
|
for loop_var in a b c
|
|
|
|
if test $loop_var = b
|
|
|
|
break
|
|
|
|
end
|
|
|
|
end
|
|
|
|
set --show loop_var
|
|
|
|
end
|
|
|
|
loop_test
|
|
|
|
set --show loop_var
|
|
|
|
|
|
|
|
begin
|
|
|
|
set -l loop_var
|
|
|
|
for loop_var in aa bb cc
|
|
|
|
end
|
|
|
|
set --show loop_var
|
|
|
|
end
|
|
|
|
set --show loop_var
|
|
|
|
#CHECK: $loop_var: set in local scope, unexported, with 1 elements
|
2020-04-25 16:33:47 +00:00
|
|
|
#CHECK: $loop_var[1]: |c|
|
2020-02-08 16:45:22 +00:00
|
|
|
#CHECK: $loop_var: set in local scope, unexported, with 1 elements
|
2020-04-25 16:33:47 +00:00
|
|
|
#CHECK: $loop_var[1]: |b|
|
2020-02-08 16:45:22 +00:00
|
|
|
#CHECK: $loop_var: set in global scope, unexported, with 1 elements
|
2020-04-25 16:33:47 +00:00
|
|
|
#CHECK: $loop_var[1]: |global_val|
|
2020-02-08 16:45:22 +00:00
|
|
|
#CHECK: $loop_var: set in global scope, unexported, with 1 elements
|
2020-04-25 16:33:47 +00:00
|
|
|
#CHECK: $loop_var[1]: |global_val|
|
2020-02-08 16:45:22 +00:00
|
|
|
#CHECK: $loop_var: set in local scope, unexported, with 1 elements
|
2020-04-25 16:33:47 +00:00
|
|
|
#CHECK: $loop_var[1]: |cc|
|
2020-02-08 16:45:22 +00:00
|
|
|
#CHECK: $loop_var: set in global scope, unexported, with 1 elements
|
2020-04-25 16:33:47 +00:00
|
|
|
#CHECK: $loop_var[1]: |global_val|
|
2020-02-08 16:45:22 +00:00
|
|
|
#CHECK: $loop_var: set in global scope, unexported, with 1 elements
|
2020-04-25 16:33:47 +00:00
|
|
|
#CHECK: $loop_var[1]: |global_val|
|