fork/fish-shell

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

NahorAdd tests to exercise all builtin error messages99d63c21f

master
13.4 KiB527 linesraw
1# RUN: fish=%fish %fish %s
2# Read with no vars is not an error
3read
4
5# Read with -a and anything other than exactly on var name is an error
6read -a
7#CHECKERR: read: expected 1 arguments; got 0
8read --array v1 v2
9#CHECKERR: read: expected 1 arguments; got 2
10read --list v1
11
12# Verify correct behavior of subcommands and splitting of input.
13begin
14    count (echo one\ntwo)
15    #CHECK: 2
16    set -l IFS \t
17    count (echo one\ntwo)
18    #CHECK: 2
19    set -l IFS
20    count (echo one\ntwo)
21    #CHECK: 1
22    echo [(echo -n one\ntwo)]
23    #CHECK: [one
24    #CHECK: two]
25    count (echo one\ntwo\n)
26    #CHECK: 1
27    echo [(echo -n one\ntwo\n)]
28    #CHECK: [one
29    #CHECK: two]
30    count (echo one\ntwo\n\n)
31    #CHECK: 1
32    echo [(echo -n one\ntwo\n\n)]
33    #CHECK: [one
34    #CHECK: two
35    #CHECK: ]
36end
37
38function print_vars --no-scope-shadowing
39    set -l space
40    set -l IFS \n # ensure our command substitution works right
41    for var in $argv
42        echo -n $space (count $$var) \'$$var\'
43        set space ''
44    end
45    echo
46end
47
48# Test splitting input
49echo 'hello there' | read -l one two
50print_vars one two
51#CHECK: 1 'hello' 1 'there'
52echo 'hello there' | read -l one
53print_vars one
54#CHECK: 1 'hello there'
55echo '' | read -l one
56print_vars one
57#CHECK: 1 ''
58echo '' | read -l one two
59print_vars one two
60#CHECK: 1 '' 1 ''
61echo test | read -l one two three
62print_vars one two three
63#CHECK: 1 'test' 1 '' 1 ''
64echo 'foo   bar   baz' | read -l one two three
65print_vars one two three
66#CHECK: 1 'foo' 1 'bar' 1 '  baz'
67echo -n a | read -l one
68echo "$status $one"
69#CHECK: 0 a
70
71# Test splitting input with IFS empty
72set -l IFS
73echo hello | read -l one
74print_vars one
75#CHECK: 1 'hello'
76echo hello | read -l one two
77print_vars one two
78#CHECK: 1 'h' 1 'ello'
79echo hello | read -l one two three
80print_vars one two three
81#CHECK: 1 'h' 1 'e' 1 'llo'
82echo '' | read -l one
83print_vars one
84#CHECK: 0
85echo t | read -l one two
86print_vars one two
87#CHECK: 1 't' 0
88echo t | read -l one two three
89print_vars one two three
90#CHECK: 1 't' 0 0
91echo ' t' | read -l one two
92print_vars one two
93#CHECK: 1 ' ' 1 't'
94set -le IFS
95
96echo 'hello there' | read -la ary
97print_vars ary
98#CHECK: 2 'hello' 'there'
99echo hello | read -la ary
100print_vars ary
101#CHECK: 1 'hello'
102echo 'this is a bunch of words' | read -la ary
103print_vars ary
104#CHECK: 6 'this' 'is' 'a' 'bunch' 'of' 'words'
105echo '   one   two     three' | read -la ary
106print_vars ary
107#CHECK: 3 'one' 'two' 'three'
108echo '' | read -la ary
109print_vars ary
110#CHECK: 0
111
112set -l IFS
113echo hello | read -la ary
114print_vars ary
115#CHECK: 5 'h' 'e' 'l' 'l' 'o'
116echo h | read -la ary
117print_vars ary
118#CHECK: 1 'h'
119echo '' | read -la ary
120print_vars ary
121#CHECK: 0
122set -le IFS
123
124# read -n tests
125echo testing | read -n 3 foo
126echo $foo
127#CHECK: tes
128echo test | read -n 10 foo
129echo $foo
130#CHECK: test
131echo test | read -n 0 foo
132echo $foo
133#CHECK: test
134echo testing | begin
135    read -n 3 foo
136    read -n 3 bar
137end
138echo $foo
139#CHECK: tes
140echo $bar
141#CHECK: tin
142echo test | read -n 1 foo
143echo $foo
144#CHECK: t
145echo test | read -n 2147483647 foo
146echo $foo
147#CHECK: test
148echo test | read -n 2147483648 foo
149#CHECKERR: read: Argument '2147483648' is out of range
150#CHECKERR: {{.*}}/checks/read.fish (line {{\d+}}):
151#CHECKERR: echo test | read -n 2147483648 foo
152#CHECKERR: ^
153#CHECKERR: (Type 'help read' for related documentation)
154
155# read -z tests
156echo -n testing | read -lz foo
157echo $foo
158#CHECK: testing
159echo -n 'test ing' | read -lz foo
160echo $foo
161#CHECK: test ing
162echo newline | read -lz foo
163echo $foo
164#CHECK: newline
165#CHECK:
166echo -n 'test ing' | read -lz foo bar
167print_vars foo bar
168#CHECK: 1 'test' 1 'ing'
169echo -ne 'test\0ing' | read -lz foo bar
170print_vars foo bar
171#CHECK: 1 'test' 1 ''
172echo -ne 'foo\nbar' | read -lz foo bar
173print_vars foo bar
174#CHECK: 1 'foo' 1 'bar'
175echo -ne 'foo\nbar\0baz\nquux' | while read -lza foo
176    print_vars foo
177end
178#CHECK: 2 'foo' 'bar'
179#CHECK: 2 'baz' 'quux'
180
181# Chunked read tests
182set -l path /tmp/fish_chunked_read_test.txt
183set -l longstr (seq 1024 | string join ',')
184echo -n $longstr >$path
185read -l longstr2 <$path
186test "$longstr" = "$longstr2"
187and echo "Chunked reads test pass"
188or echo "Chunked reads test failure: long strings don't match!"
189rm $path
190#CHECK: Chunked reads test pass
191
192# ==========
193# The following tests verify that `read` correctly handles the limit on the
194# number of bytes consumed.
195#
196set fish_read_limit 8192
197set line abcdefghijklmnopqrstuvwxyz
198
199# Ensure the `read` command terminates if asked to read too much data. The var
200# should be empty. We throw away any data we read if it exceeds the limit on
201# what we consider reasonable.
202yes $line | head -c (math "1 + $fish_read_limit") | read --null x
203if test $status -ne 122
204    echo reading too much data did not terminate with failure status
205end
206# The read var should be defined but not have any elements when the read
207# aborts due to too much data.
208set -q x
209or echo reading too much data did not define the var
210set -q x[1]
211and echo reading too much data resulted in a var with unexpected data
212
213# Ensure the `read` command terminates if asked to read too much data even if
214# given an explicit limit. The var should be empty. We throw away any data we
215# read if it exceeds the limit on what we consider reasonable.
216yes $line | read --null --nchars=(math "$fish_read_limit + 1") x
217if test $status -ne 122
218    echo reading too much data did not terminate with failure status
219end
220set -q x
221or echo reading too much data with --nchars did not define the var
222set -q x[1]
223and echo reading too much data with --nchars resulted in a var with unexpected data
224
225# Now do the opposite of the previous test and confirm we can read reasonable
226# amounts of data.
227echo $line | read x
228if test $status -ne 0
229    echo the read of a reasonable amount of data failed unexpectedly
230end
231set exp_length (string length $x)
232set act_length (string length $line)
233if test $exp_length -ne $act_length
234    echo reading a reasonable amount of data failed the length test
235    echo expected length $exp_length, actual length $act_length
236end
237
238# Confirm we can read exactly up to the limit.
239yes $line | read --null --nchars $fish_read_limit x
240if test $status -ne 0
241    echo the read of the max amount of data with --nchars failed unexpectedly
242end
243if test (string length "$x") -ne $fish_read_limit
244    echo reading the max amount of data with --nchars failed the length test
245end
246
247# Same as previous test but limit the amount of data fed to `read` rather than
248# using the `--nchars` flag.
249yes $line | head -c $fish_read_limit | read --null x
250if test $status -ne 0
251    echo the read of the max amount of data failed unexpectedly
252end
253if test (string length "$x") -ne $fish_read_limit
254    echo reading with a limited amount of input data failed the length test
255end
256
257# Confirm reading non-interactively works -- #4206 regression
258echo abc\ndef | $fish -i -c 'read a; read b; set --show a; set --show b'
259#CHECK: $a: set in global scope, unexported, with 1 elements
260#CHECK: $a[1]: |abc|
261#CHECK: $b: set in global scope, unexported, with 1 elements
262#CHECK: $b[1]: |def|
263
264# Test --delimiter (and $IFS, for now)
265echo a=b | read -l foo bar
266echo $foo
267echo $bar
268#CHECK: a=b
269
270# Delimiter =
271echo a=b | read -l -d = foo bar
272echo $foo
273#CHECK: a
274echo $bar
275#CHECK: b
276
277# Delimiter empty
278echo a=b | read -l -d '' foo bar baz
279echo $foo
280#CHECK: a
281echo $bar
282#CHECK: =
283echo $baz
284#CHECK: b
285
286# IFS empty string
287set -l IFS ''
288echo a=b | read -l foo bar baz
289echo $foo
290#CHECK: a
291echo $bar
292#CHECK: =
293echo $baz
294#CHECK: b
295
296# IFS unset
297set -e IFS
298echo a=b | read -l foo bar baz
299echo $foo
300#CHECK: a=b
301echo $bar
302#CHECK:
303echo $baz
304#CHECK:
305
306# Delimiter =
307echo a=b | read -l -d = foo bar baz
308echo $foo
309#CHECK: a
310echo $bar
311#CHECK: b
312echo $baz
313#CHECK:
314
315# Multi-char delimiters with -d
316echo a...b...c | read -l -d "..." a b c
317echo $a
318#CHECK: a
319echo $b
320#CHECK: b
321echo $c
322#CHECK: c
323# Multi-char delimiters with IFS
324begin
325    set -l IFS "..."
326    echo a...b...c | read -l a b c
327    echo $a
328    echo $b
329    echo $c
330end
331#CHECK: a
332#CHECK: b
333#CHECK: ..c
334
335# At one point, whatever was read was printed _before_ banana
336echo banana (echo sausage | read)
337echo 'a | b' | read -lt a b c
338#CHECK: banana sausage
339
340echo a $a
341echo b $b
342echo c $c
343# CHECK: a a
344# CHECK: b |
345# CHECK: c b
346
347echo 'a"foo bar"b' | read -lt a b c
348
349echo a \'$a\'
350echo b $b
351echo c $c
352# CHECK: a 'afoo barb'
353# CHECK: b
354# CHECK: c
355
356function function-scoped-read
357    echo foo | read --function skamtebord
358    set -S skamtebord
359    begin
360        echo bar | read skamtebord
361        echo baz | read -f craaab
362    end
363    set -S skamtebord
364    set -S craaab
365end
366
367function-scoped-read
368# CHECK: $skamtebord: set in local scope, unexported, with 1 elements
369# CHECK: $skamtebord[1]: |foo|
370# CHECK: $skamtebord: set in local scope, unexported, with 1 elements
371# CHECK: $skamtebord[1]: |bar|
372# CHECK: $craaab: set in local scope, unexported, with 1 elements
373# CHECK: $craaab[1]: |baz|
374
375echo foo\nbar\nbaz | begin
376    read -l foo
377    read -l bar
378    cat
379    # CHECK: baz
380    echo $bar
381    # CHECK: bar
382end
383
384begin
385    echo 1
386    echo 2
387end | read -l --line foo bar
388echo $foo $bar
389# CHECK: 1 2
390
391echo foo | read status
392# CHECKERR: read: status: cannot overwrite read-only variable
393# CHECKERR: {{.*}}read.fish (line {{\d+}}):
394# CHECKERR: echo foo | read status
395# CHECKERR: ^
396# CHECKERR: (Type 'help read' for related documentation)
397echo read $status
398# CHECK: read 2
399
400echo ' foo' | read -n 1 -la var
401set -S var
402#CHECK: $var: set in local scope, unexported, with 0 elements
403
404echo foo | read -n -1
405# CHECKERR: read: -1: invalid integer
406# CHECKERR: {{.*}}read.fish (line {{\d+}}):
407# CHECKERR: echo foo | read -n -1
408# CHECKERR: ^
409# CHECKERR: (Type 'help read' for related documentation)
410
411echo '1 ( (' | read -lat var
412set -S var
413# CHECK: $var: set in local scope, unexported, with 2 elements
414# CHECK: $var[1]: |1|
415# CHECK: $var[2]: |( (|
416
417echo '1 ) )' | read -lat var
418set -S var
419# CHECK: $var: set in local scope, unexported, with 2 elements
420# CHECK: $var[1]: |1|
421# CHECK: $var[2]: |)|
422
423echo '1 { {' | read -lat var
424set -S var
425# CHECK: $var: set in local scope, unexported, with 2 elements
426# CHECK: $var[1]: |1|
427# CHECK: $var[2]: |{ {|
428
429echo '1 } }' | read -lat var
430set -S var
431# CHECK: $var: set in local scope, unexported, with 2 elements
432# CHECK: $var[1]: |1|
433# CHECK: $var[2]: |}|
434
435# Raw tokens into named variables
436echo 'echo "&" a\ b &
437second line (dropped)' | read -l --tokenize-raw head tail
438set -S head tail
439# CHECK: $head: set in local scope, unexported, with 1 elements
440# CHECK: $head[1]: |echo|
441# CHECK: $tail: set in local scope, unexported, with 1 elements
442# CHECK: $tail[1]: |"&" a\\ b &|
443
444# Raw tokens into list
445echo 'echo "&" & a\ b
446second line (dropped)' | read -l --tokenize-raw -a rawlist
447set -S rawlist
448# CHECK: $rawlist: set in local scope, unexported, with 4 elements
449# CHECK: $rawlist[1]: |echo|
450# CHECK: $rawlist[2]: |"&"|
451# CHECK: $rawlist[3]: |&|
452# CHECK: $rawlist[4]: |a\\ b|
453
454echo 'echo "&" & a\ b
455second line' | read -l --tokenize-raw -a rawlist_null -z
456set -S rawlist_null
457# CHECK: $rawlist_null: set in local scope, unexported, with 8 elements
458# CHECK: $rawlist_null[1]: |echo|
459# CHECK: $rawlist_null[2]: |"&"|
460# CHECK: $rawlist_null[3]: |&|
461# CHECK: $rawlist_null[4]: |a\\ b|
462# CHECK: $rawlist_null[5]: |\n|
463# CHECK: $rawlist_null[6]: |second|
464# CHECK: $rawlist_null[7]: |line|
465# CHECK: $rawlist_null[8]: |\n|
466
467echo 'foo "&" bar' | read -al --tokenize --tokenize tokens
468set -S tokens
469# CHECK: $tokens: set in local scope, unexported, with 3 elements
470# CHECK: $tokens[1]: |foo|
471# CHECK: $tokens[2]: |&|
472# CHECK: $tokens[3]: |bar|
473echo 'foo "&" bar' | read -al --tokenize-raw --tokenize-raw tokens
474set -S tokens
475# CHECK: $tokens: set in local scope, unexported, with 3 elements
476# CHECK: $tokens[1]: |foo|
477# CHECK: $tokens[2]: |"&"|
478# CHECK: $tokens[3]: |bar|
479echo 'foo "&" bar' | read -al --tokenize --tokenize-raw tokens
480# CHECKERR: read: invalid option combination, --tokenize and --tokenize-raw are mutually exclusive
481# CHECKERR: {{.*}}checks/read.fish (line {{\d+}}):
482# CHECKERR: echo 'foo "&" bar' | read -al --tokenize --tokenize-raw tokens
483# CHECKERR: ^
484# CHECKERR: (Type 'help read' for related documentation)
485
486echo '1  {} "{}"' | read -lat var
487echo $var
488# CHECK: 1 {} {}
489
490printf \xf9\x98\xb1\x83\x8b | read -z out_of_range_codepoint
491set -S out_of_range_codepoint
492# CHECK: $out_of_range_codepoint: set in global scope, unexported, with 1 elements
493# CHECK: $out_of_range_codepoint[1]: |\Xf9\X98\Xb1\X83\X8b|
494
495printf \xff | { read invalid_utf8; set -S invalid_utf8 }
496# CHECK: $invalid_utf8: set in global scope, unexported, with 1 elements
497# CHECK: $invalid_utf8[1]: |\Xff|
498
499echo foo | read -l -p "echo little-p" -P big-P var
500# CHECKERR: read: Options -p and -P cannot be used together
501# CHECKERR: {{.*}}checks/read.fish (line {{\d+}}):
502# CHECKERR: echo foo | read -l -p "echo little-p" -P big-P var
503# CHECKERR: ^
504# CHECKERR: (Type 'help read' for related documentation)
505
506echo foo | read -d ";" -L var
507# CHECKERR: read: Options --delimiter and --line cannot be used together
508echo foo | read --null -L var
509# CHECKERR: read: Options -z and --line cannot be used together
510echo foo | read -d "&" --tokenize
511# CHECKERR: read: --delimiter --tokenize: options cannot be used together
512echo foo | read -L --tokenize-raw
513# CHECKERR: read: --line --tokenize-raw: options cannot be used together
514
515echo foo | read -lxu var
516# CHECKERR: read: cannot both export and unexport
517# CHECKERR: {{.*}}checks/read.fish (line {{\d+}}):
518# CHECKERR: echo foo | read -lxu var
519# CHECKERR: ^
520# CHECKERR: (Type 'help read' for related documentation)
521
522echo foo | read -lf var
523# CHECKERR: read: scope can be only one of: universal function global local
524# CHECKERR: {{.*}}checks/read.fish (line {{\d+}}):
525# CHECKERR: echo foo | read -lf var
526# CHECKERR: ^
527# CHECKERR: (Type 'help read' for related documentation)