2014-09-22 02:18:56 +00:00
|
|
|
# vim: set filetype=fish:
|
2014-07-14 05:36:26 +00:00
|
|
|
#
|
2017-02-08 01:21:35 +00:00
|
|
|
# Test read builtin and IFS.
|
2014-07-14 05:36:26 +00:00
|
|
|
#
|
2017-07-20 03:22:34 +00:00
|
|
|
|
2017-10-10 06:34:50 +00:00
|
|
|
logmsg Read with no vars is not an error
|
2017-07-20 03:22:34 +00:00
|
|
|
read
|
2017-08-04 04:25:20 +00:00
|
|
|
|
|
|
|
logmsg Read with -a and anything other than exactly on var name is an error
|
2017-07-20 03:22:34 +00:00
|
|
|
read -a
|
|
|
|
read -a v1 v2
|
|
|
|
read -a v1
|
|
|
|
|
2017-08-09 18:11:58 +00:00
|
|
|
logmsg Verify correct behavior of subcommands and splitting of input.
|
|
|
|
begin
|
|
|
|
count (echo one\ntwo)
|
|
|
|
set -l IFS \t
|
|
|
|
count (echo one\ntwo)
|
|
|
|
set -l IFS
|
|
|
|
count (echo one\ntwo)
|
|
|
|
echo [(echo -n one\ntwo)]
|
|
|
|
count (echo one\ntwo\n)
|
|
|
|
echo [(echo -n one\ntwo\n)]
|
|
|
|
count (echo one\ntwo\n\n)
|
|
|
|
echo [(echo -n one\ntwo\n\n)]
|
|
|
|
end
|
2014-07-14 05:36:26 +00:00
|
|
|
|
|
|
|
function print_vars --no-scope-shadowing
|
|
|
|
set -l space
|
|
|
|
set -l IFS \n # ensure our command substitution works right
|
|
|
|
for var in $argv
|
|
|
|
echo -n $space (count $$var) \'$$var\'
|
|
|
|
set space ''
|
|
|
|
end
|
|
|
|
echo
|
|
|
|
end
|
|
|
|
|
2017-08-04 04:25:20 +00:00
|
|
|
logmsg Test splitting input
|
2014-07-14 05:36:26 +00:00
|
|
|
echo 'hello there' | read -l one two
|
|
|
|
print_vars one two
|
|
|
|
echo 'hello there' | read -l one
|
|
|
|
print_vars one
|
|
|
|
echo '' | read -l one
|
|
|
|
print_vars one
|
|
|
|
echo '' | read -l one two
|
|
|
|
print_vars one two
|
|
|
|
echo 'test' | read -l one two three
|
|
|
|
print_vars one two three
|
2014-09-22 02:18:56 +00:00
|
|
|
echo 'foo bar baz' | read -l one two three
|
|
|
|
print_vars one two three
|
2014-09-22 03:00:26 +00:00
|
|
|
echo -n 'a' | read -l one
|
|
|
|
echo "$status $one"
|
2014-07-14 05:36:26 +00:00
|
|
|
|
2017-08-04 04:25:20 +00:00
|
|
|
logmsg Test splitting input with IFS empty
|
2014-07-14 05:36:26 +00:00
|
|
|
set -l IFS
|
|
|
|
echo 'hello' | read -l one
|
|
|
|
print_vars one
|
|
|
|
echo 'hello' | read -l one two
|
|
|
|
print_vars one two
|
|
|
|
echo 'hello' | read -l one two three
|
|
|
|
print_vars one two three
|
|
|
|
echo '' | read -l one
|
|
|
|
print_vars one
|
|
|
|
echo 't' | read -l one two
|
|
|
|
print_vars one two
|
|
|
|
echo 't' | read -l one two three
|
|
|
|
print_vars one two three
|
|
|
|
echo ' t' | read -l one two
|
|
|
|
print_vars one two
|
|
|
|
set -le IFS
|
|
|
|
|
|
|
|
echo
|
|
|
|
echo 'hello there' | read -la ary
|
|
|
|
print_vars ary
|
|
|
|
echo 'hello' | read -la ary
|
|
|
|
print_vars ary
|
|
|
|
echo 'this is a bunch of words' | read -la ary
|
|
|
|
print_vars ary
|
|
|
|
echo ' one two three' | read -la ary
|
|
|
|
print_vars ary
|
|
|
|
echo '' | read -la ary
|
|
|
|
print_vars ary
|
|
|
|
|
|
|
|
echo
|
|
|
|
set -l IFS
|
|
|
|
echo 'hello' | read -la ary
|
|
|
|
print_vars ary
|
|
|
|
echo 'h' | read -la ary
|
|
|
|
print_vars ary
|
|
|
|
echo '' | read -la ary
|
|
|
|
print_vars ary
|
|
|
|
set -le IFS
|
2014-08-19 12:28:08 +00:00
|
|
|
|
2017-08-04 19:04:32 +00:00
|
|
|
logmsg read -n tests
|
2014-08-19 12:28:08 +00:00
|
|
|
echo 'testing' | read -n 3 foo
|
|
|
|
echo $foo
|
|
|
|
echo 'test' | read -n 10 foo
|
|
|
|
echo $foo
|
|
|
|
echo 'test' | read -n 0 foo
|
|
|
|
echo $foo
|
|
|
|
echo 'testing' | begin; read -n 3 foo; read -n 3 bar; end
|
|
|
|
echo $foo
|
|
|
|
echo $bar
|
|
|
|
echo 'test' | read -n 1 foo
|
|
|
|
echo $foo
|
2014-09-22 02:18:56 +00:00
|
|
|
|
2017-08-04 19:04:32 +00:00
|
|
|
logmsg read -z tests
|
2014-09-22 02:18:56 +00:00
|
|
|
echo -n 'testing' | read -lz foo
|
|
|
|
echo $foo
|
|
|
|
echo -n 'test ing' | read -lz foo
|
|
|
|
echo $foo
|
|
|
|
echo 'newline' | read -lz foo
|
|
|
|
echo $foo
|
|
|
|
echo -n 'test ing' | read -lz foo bar
|
|
|
|
print_vars foo bar
|
|
|
|
echo -ne 'test\0ing' | read -lz foo bar
|
|
|
|
print_vars foo bar
|
|
|
|
echo -ne 'foo\nbar' | read -lz foo bar
|
|
|
|
print_vars foo bar
|
|
|
|
echo -ne 'foo\nbar\0baz\nquux' | while read -lza foo
|
|
|
|
print_vars foo
|
|
|
|
end
|
|
|
|
|
2017-08-04 04:25:20 +00:00
|
|
|
logmsg Chunked read tests
|
2017-01-21 20:43:20 +00:00
|
|
|
set -l path /tmp/fish_chunked_read_test.txt
|
|
|
|
set -l longstr (seq 1024 | string join ',')
|
|
|
|
echo -n $longstr > $path
|
|
|
|
read -l longstr2 < $path
|
|
|
|
test "$longstr" = "$longstr2"
|
|
|
|
and echo "Chunked reads test pass"
|
|
|
|
or echo "Chunked reads test failure: long strings don't match!"
|
2017-01-21 20:47:05 +00:00
|
|
|
rm $path
|
2017-01-21 20:43:20 +00:00
|
|
|
|
2017-02-08 01:21:35 +00:00
|
|
|
# ==========
|
|
|
|
# The following tests verify that `read` correctly handles the limit on the
|
|
|
|
# number of bytes consumed.
|
|
|
|
#
|
2017-10-14 15:22:57 +00:00
|
|
|
set fish_read_limit 8192
|
2017-02-08 01:21:35 +00:00
|
|
|
set line abcdefghijklmnopqrstuvwxyz
|
|
|
|
|
|
|
|
# Ensure the `read` command terminates if asked to read too much data. The var
|
|
|
|
# should be empty. We throw away any data we read if it exceeds the limit on
|
|
|
|
# what we consider reasonable.
|
2018-04-01 20:43:05 +00:00
|
|
|
yes $line | dd bs=1024 count=(math "1 + $fish_read_limit / 1024") 2>/dev/null | read --null x
|
2017-02-08 01:21:35 +00:00
|
|
|
if test $status -ne 122
|
|
|
|
echo reading too much data did not terminate with failure status
|
|
|
|
end
|
2017-07-03 23:14:28 +00:00
|
|
|
# The read var should be defined but not have any elements when the read
|
|
|
|
# aborts due to too much data.
|
|
|
|
set -q x
|
|
|
|
or echo reading too much data did not define the var
|
|
|
|
set -q x[1]
|
|
|
|
and echo reading too much data resulted in a var with unexpected data
|
2017-02-08 01:21:35 +00:00
|
|
|
|
|
|
|
# Ensure the `read` command terminates if asked to read too much data even if
|
|
|
|
# given an explicit limit. The var should be empty. We throw away any data we
|
|
|
|
# read if it exceeds the limit on what we consider reasonable.
|
2017-10-14 15:22:57 +00:00
|
|
|
yes $line | read --null --nchars=(math "$fish_read_limit + 1") x
|
2017-02-08 01:21:35 +00:00
|
|
|
if test $status -ne 122
|
|
|
|
echo reading too much data did not terminate with failure status
|
|
|
|
end
|
2017-07-03 23:14:28 +00:00
|
|
|
set -q x
|
|
|
|
or echo reading too much data with --nchars did not define the var
|
|
|
|
set -q x[1]
|
|
|
|
and echo reading too much data with --nchars resulted in a var with unexpected data
|
2017-02-08 01:21:35 +00:00
|
|
|
|
|
|
|
# Now do the opposite of the previous test and confirm we can read reasonable
|
|
|
|
# amounts of data.
|
|
|
|
echo $line | read x
|
|
|
|
if test $status -ne 0
|
|
|
|
echo the read of a reasonable amount of data failed unexpectedly
|
|
|
|
end
|
|
|
|
set exp_length (string length $x)
|
|
|
|
set act_length (string length $line)
|
|
|
|
if test $exp_length -ne $act_length
|
|
|
|
echo reading a reasonable amount of data failed the length test
|
|
|
|
echo expected length $exp_length, actual length $act_length
|
|
|
|
end
|
|
|
|
|
|
|
|
# Confirm we can read exactly up to the limit.
|
2017-10-14 15:22:57 +00:00
|
|
|
yes $line | read --null --nchars $fish_read_limit x
|
2017-02-08 01:21:35 +00:00
|
|
|
if test $status -ne 0
|
|
|
|
echo the read of the max amount of data with --nchars failed unexpectedly
|
|
|
|
end
|
2017-10-14 15:22:57 +00:00
|
|
|
if test (string length "$x") -ne $fish_read_limit
|
2017-02-08 01:21:35 +00:00
|
|
|
echo reading the max amount of data with --nchars failed the length test
|
|
|
|
end
|
|
|
|
|
|
|
|
# Same as previous test but limit the amount of data fed to `read` rather than
|
|
|
|
# using the `--nchars` flag.
|
2018-04-01 20:43:05 +00:00
|
|
|
yes $line | dd bs=1024 count=(math "$fish_read_limit / 1024") 2>/dev/null | read --null x
|
2017-02-08 01:21:35 +00:00
|
|
|
if test $status -ne 0
|
|
|
|
echo the read of the max amount of data failed unexpectedly
|
|
|
|
end
|
2017-10-14 15:22:57 +00:00
|
|
|
if test (string length "$x") -ne $fish_read_limit
|
2017-02-08 01:21:35 +00:00
|
|
|
echo reading the max amount of data with --nchars failed the length test
|
|
|
|
end
|
2017-07-16 04:21:24 +00:00
|
|
|
|
2017-08-04 04:25:20 +00:00
|
|
|
logmsg Confirm reading non-interactively works -- \#4206 regression
|
|
|
|
echo abc\ndef | ../test/root/bin/fish -i -c 'read a; read b; set --show a; set --show b'
|
|
|
|
|
|
|
|
logmsg Test --delimiter '(and $IFS, for now)'
|
2017-07-27 13:06:01 +00:00
|
|
|
echo a=b | read -l foo bar
|
|
|
|
echo $foo
|
|
|
|
echo $bar
|
2017-08-04 04:25:20 +00:00
|
|
|
|
|
|
|
logmsg Delimiter =
|
2017-07-27 13:06:01 +00:00
|
|
|
echo a=b | read -l -d = foo bar
|
|
|
|
echo $foo
|
|
|
|
echo $bar
|
2017-08-04 04:25:20 +00:00
|
|
|
|
|
|
|
logmsg Delimiter empty
|
2017-07-27 13:06:01 +00:00
|
|
|
echo a=b | read -l -d '' foo bar baz
|
|
|
|
echo $foo
|
|
|
|
echo $bar
|
|
|
|
echo $baz
|
2017-08-04 04:25:20 +00:00
|
|
|
|
|
|
|
logmsg IFS empty string
|
2017-07-27 13:06:01 +00:00
|
|
|
set -l IFS ''
|
|
|
|
echo a=b | read -l foo bar baz
|
|
|
|
echo $foo
|
|
|
|
echo $bar
|
|
|
|
echo $baz
|
2017-08-04 04:25:20 +00:00
|
|
|
|
|
|
|
logmsg IFS unset
|
2017-07-27 13:06:01 +00:00
|
|
|
set -e IFS
|
|
|
|
echo a=b | read -l foo bar baz
|
|
|
|
echo $foo
|
|
|
|
echo $bar
|
|
|
|
echo $baz
|
2017-08-04 04:25:20 +00:00
|
|
|
|
|
|
|
logmsg Delimiter =
|
2017-07-27 13:06:01 +00:00
|
|
|
echo a=b | read -l -d = foo bar baz
|
|
|
|
echo $foo
|
|
|
|
echo $bar
|
|
|
|
echo $baz
|
|
|
|
echo
|
|
|
|
|
|
|
|
echo 'Multi-char delimiters with -d'
|
|
|
|
echo a...b...c | read -l -d "..." a b c
|
|
|
|
echo $a
|
|
|
|
echo $b
|
|
|
|
echo $c
|
|
|
|
echo 'Multi-char delimiters with IFS'
|
|
|
|
begin
|
|
|
|
set IFS "..."
|
|
|
|
echo a...b...c | read -l a b c
|
|
|
|
echo $a; echo $b; echo $c
|
|
|
|
end
|
2018-04-10 19:45:28 +00:00
|
|
|
echo
|
|
|
|
|
|
|
|
# At one point, whatever was read was printed _before_ banana
|
|
|
|
echo banana (echo sausage | read)
|