fork/fish-shell

git clone https://git.julia.blue/fork/fish-shell

Fabian BoehmMake tests usable with path with spacese66f6878b

master
3.5 KiB163 linesraw
1#RUN: fish=%fish %fish %s
2
3function never_runs
4    while false
5    end
6end
7
8function early_return
9    while true
10        return 2
11    end
12end
13
14function runs_once
15    set -l i 1
16    while test $i -ne 0 && set i (math $i - 1)
17    end
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
36    return $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
44        set -l saved $status
45        echo "Condition Status: $status"
46        set_status $saved
47    end
48    true
49end
50#CHECK: Condition Status: 36
51
52# The condition status IS visible in the loop body.
53set_status 55
54while true
55    echo "Body Status: $status"
56    break
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
63        set -l saved $status
64        echo "Condition 2 Status: $saved"
65        test $saved -ne 5
66    end
67    set_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 rem 5 7 11
74while [ (count $rem) -gt 0 ]
75    set_status $rem[1]
76    set 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 i in 1 2 3
90    while break
91    end
92    echo $i
93end
94for i in 1 2 3
95    while continue
96    end
97    echo $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 status in a b c
108    echo $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 hostname in a b c
116    echo $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
124    set -l loop_var initial-value
125    for loop_var in a b c
126        # do nothing
127    end
128    set --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
135    for loop_var in a b c
136        if test $loop_var = b
137            break
138        end
139    end
140    set --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
148    set -l loop_var
149    for loop_var in aa bb cc
150    end
151    set --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|