fork/fish-shell

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

NahorAdd tests to exercise all builtin error messages99d63c21f

master
9.0 KiB265 linesraw
1#RUN: %fish %s
2function t --argument-names a b c
3    echo t
4end
5
6set -g foo 'global foo'
7set -l foo 'local foo'
8set bar one 'two    2' \t '' 3
9set baz
10function frob -V foo -V bar -V baz
11    set --show foo bar baz
12end
13
14frob
15#CHECK: $foo: set in local scope, unexported, with 1 elements
16#CHECK: $foo[1]: |local foo|
17#CHECK: $foo: set in global scope, unexported, with 1 elements
18#CHECK: $foo[1]: |global foo|
19#CHECK: $bar: set in local scope, unexported, with 5 elements
20#CHECK: $bar[1]: |one|
21#CHECK: $bar[2]: |two    2|
22#CHECK: $bar[3]: |\t|
23#CHECK: $bar[4]: ||
24#CHECK: $bar[5]: |3|
25#CHECK: $bar: set in global scope, unexported, with 5 elements
26#CHECK: $bar[1]: |one|
27#CHECK: $bar[2]: |two    2|
28#CHECK: $bar[3]: |\t|
29#CHECK: $bar[4]: ||
30#CHECK: $bar[5]: |3|
31#CHECK: $baz: set in local scope, unexported, with 0 elements
32#CHECK: $baz: set in global scope, unexported, with 0 elements
33
34set foo 'bad foo'
35set bar 'bad bar'
36set baz 'bad baz'
37frob
38#CHECK: $foo: set in local scope, unexported, with 1 elements
39#CHECK: $foo[1]: |local foo|
40#CHECK: $foo: set in global scope, unexported, with 1 elements
41#CHECK: $foo[1]: |global foo|
42#CHECK: $bar: set in local scope, unexported, with 5 elements
43#CHECK: $bar[1]: |one|
44#CHECK: $bar[2]: |two    2|
45#CHECK: $bar[3]: |\t|
46#CHECK: $bar[4]: ||
47#CHECK: $bar[5]: |3|
48#CHECK: $bar: set in global scope, unexported, with 1 elements
49#CHECK: $bar[1]: |bad bar|
50#CHECK: $baz: set in local scope, unexported, with 0 elements
51#CHECK: $baz: set in global scope, unexported, with 1 elements
52#CHECK: $baz[1]: |bad baz|
53
54# This sequence of tests originally verified that functions `name2` and
55# `name4` were created. See issue #2068. That behavior is not what we want.
56# The function name must always be the first argument of the `function`
57# command. See issue #2827.
58function name1 -a arg1 arg2
59    echo hello
60end
61function -a arg1 arg2 name2
62end
63#CHECKERR: {{.*}}checks/function.fish (line {{\d+}}): function: -a: invalid function name
64#CHECKERR: function -a arg1 arg2 name2
65#CHECKERR: ^~~~~~~~~~~~~~~~~~~~~~~~~~^
66function name3 --argument-names arg1 arg2
67    echo hello
68    echo goodbye
69end
70function --argument-names arg1 arg2 name4
71end
72#CHECKERR: {{.*}}checks/function.fish (line {{\d+}}): function: --argument-names: invalid function name
73#CHECKERR: function --argument-names arg1 arg2 name4
74#CHECKERR: ^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~^
75function name5 abc --argument-names def
76end
77#CHECKERR: {{.*}}checks/function.fish (line {{\d+}}): function: abc: unexpected positional argument
78#CHECKERR: function name5 abc --argument-names def
79#CHECKERR: ^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~^
80functions -q name1; and echo "Function name1 found"
81functions -q name2; or echo "Function name2 not found as expected"
82functions -q name3; and echo "Function name3 found"
83functions -q name4; or echo "Function name4 not found as expected"
84#CHECK: Function name1 found
85#CHECK: Function name2 not found as expected
86#CHECK: Function name3 found
87#CHECK: Function name4 not found as expected
88
89functions -c name1 name1a
90functions --copy name3 name3a
91functions -q name1a
92or echo "Function name1a not found as expected"
93functions -q name3a
94or echo "Function name3a not found as expected"
95
96# Poor man's diff because on some systems diff defaults to unified output, but that prints filenames.
97#
98set -l name1 (functions name1)
99set -l name1a (functions name1a)
100set -l name3 (functions name3)
101set -l name3a (functions name3a)
102# First two lines for the copied and non-copied functions are different. Skip it for now.
103test "$name1[3..-1]" = "$name1a[3..-1]"; and echo "1 = 1a"
104#CHECK: 1 = 1a
105test "$name3[3..-1]" = "$name3a[3..-1]"; and echo "3 = 3a"
106#CHECK: 3 = 3a
107
108# Test the first two lines.
109string join \n -- $name1[1..2]
110#CHECK: # Defined in {{(?:(?!, copied).)*}}
111#CHECK: function name1 --argument-names arg1 arg2
112string join \n -- $name1a[1..2]
113#CHECK: # Defined in {{.*}}, copied in {{.*}}
114#CHECK: function name1a --argument-names arg1 arg2
115string join \n -- $name3[1..2]
116#CHECK: # Defined in {{(?:(?!, copied).)*}}
117#CHECK: function name3 --argument-names arg1 arg2
118string join \n -- $name3a[1..2]
119#CHECK: # Defined in {{.*}}, copied in {{.*}}
120#CHECK: function name3a --argument-names arg1 arg2
121
122function test
123    echo banana
124end
125#CHECKERR: {{.*}}checks/function.fish (line {{\d+}}): function: test: cannot use reserved keyword as function name
126#CHECKERR: function test
127#CHECKERR: ^~~~~~~~~~~~^
128
129functions -q; or echo False
130#CHECK: False
131
132# See that we don't count a file with an empty function name,
133# or directories
134set -l tmpdir (mktemp -d)
135touch $tmpdir/.fish
136mkdir $tmpdir/directory.fish
137touch $tmpdir/actual_function.fish
138
139begin
140    set -l fish_function_path $tmpdir
141    functions | grep '^$\|directory\|^actual_function$'
142end
143# CHECK: actual_function
144
145rm -r $tmpdir
146
147functions -e foo
148
149function foo -p bar; end
150# CHECKERR: {{.*}}function.fish (line {{\d+}}): function: 'bar' is not a valid process ID
151# CHECKERR: function foo -p bar; end
152# CHECKERR: ^~~~~~~~~~~~~~~~~~~^
153
154function foo --argument-names "banana pajama"; end
155# CHECKERR: {{.*}}function.fish (line {{\d+}}): function: banana pajama: invalid variable name. See `help language#shell-variable-and-function-names`
156# CHECKERR: function foo --argument-names "banana pajama"; end
157# CHECKERR: ^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~^
158
159
160function foo --argument-names status; end
161# CHECKERR: {{.*}}function.fish (line {{\d+}}): function: variable 'status' is read-only
162# CHECKERR: function foo --argument-names status; end
163# CHECKERR: ^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~^
164
165function foo --inherit-variable status; end
166# CHECKERR: {{.*}}function.fish (line {{\d+}}): function: variable 'status' is read-only
167# CHECKERR: function foo --inherit-variable status; end
168# CHECKERR: ^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~^
169
170echo status $status
171# CHECK: status 2
172
173function foo --argument-names foo status; end
174# CHECKERR: {{.*}}function.fish (line {{\d+}}): function: variable 'status' is read-only
175# CHECKERR: function foo --argument-names foo status; end
176# CHECKERR: ^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~^
177
178functions -q foo
179echo exists $status
180# CHECK: exists 1
181
182# If a function is changed as part of its own arguments, then we see the change.
183# This codifies historic behavior.
184function foo; echo before; end
185foo (function foo; echo after; end)
186# CHECK: after
187
188# If a function is deleted as part of its own arguments, then we see the change.
189# This codifies historic behavior.
190function foo; echo before; end
191foo (functions --erase foo)
192# CHECKERR: error: Unknown function 'foo'
193
194function ()
195end
196# CHECKERR: {{.*}}/tests/checks/function.fish (line {{\d+}}): function: function name required
197# CHECKERR: function ()
198# CHECKERR: ^~~~~~~~~~^
199
200# Tests the --argument-names and --inherit-variable can overwrite argv
201function t --argument-names a argv c
202    echo $argv
203end
204t 1 2 3
205#CHECK: 2
206
207function t -a argv
208    echo $argv
209end
210t 1 2 3
211#CHECK: 1
212
213function outer
214    function inner -v argv -V argv
215        echo $argv
216    end
217    set -gx argv 4 5 6
218end
219outer 1 2 3
220#CHECK: 1 2 3
221
222for flag in --on-process-exit --on-job-exit
223    for invalid_pid in (math 2 ^ 31) -1 -(math 2 ^ 31)
224        function invalid $flag=$invalid_pid
225        end
226    end
227    # CHECKERR: {{.*}}/function.fish (line {{\d+}}): function: '2147483648' is not a valid process ID
228    # CHECKERR:     function invalid $flag=$invalid_pid
229    # CHECKERR:     ^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~^
230    # CHECKERR: {{.*}}/function.fish (line {{\d+}}): function: '-1' is not a valid process ID
231    # CHECKERR:     function invalid $flag=$invalid_pid
232    # CHECKERR:     ^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~^
233    # CHECKERR: {{.*}}/function.fish (line {{\d+}}): function: '-2147483648' is not a valid process ID
234    # CHECKERR:     function invalid $flag=$invalid_pid
235    # CHECKERR:     ^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~^
236    # CHECKERR: {{.*}}/function.fish (line {{\d+}}): function: '2147483648' is not a valid process ID
237    # CHECKERR:     function invalid $flag=$invalid_pid
238    # CHECKERR:     ^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~^
239    # CHECKERR: {{.*}}/function.fish (line {{\d+}}): function: '-1' is not a valid process ID
240    # CHECKERR:     function invalid $flag=$invalid_pid
241    # CHECKERR:     ^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~^
242    # CHECKERR: {{.*}}/function.fish (line {{\d+}}): function: '-2147483648' is not a valid process ID
243    # CHECKERR:     function invalid $flag=$invalid_pid
244    # CHECKERR:     ^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~^
245end
246
247function handle_some_sig --on-signal SOME_SIG
248end
249#CHECKERR: {{.*}}/function.fish (line {{\d+}}): function: Unknown signal 'SOME_SIG'
250#CHECKERR: function handle_some_sig --on-signal SOME_SIG
251#CHECKERR: ^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~^
252
253function handle_exit_invalid_pid -j caller
254end
255#CHECKERR: {{.*}}/function.fish (line {{\d+}}): function: calling job for event handler not found
256#CHECKERR: function handle_exit_invalid_pid -j caller
257#CHECKERR: ^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~^
258
259function fn_with_args -- arg
260end
261#CHECKERR: {{.*}}/function.fish (line {{\d+}}): function: arg: unexpected positional argument
262#CHECKERR: function fn_with_args -- arg
263#CHECKERR: ^~~~~~~~~~~~~~~~~~~~~~~~~~~^
264
265exit 0