2014-08-21 12:19:08 +08:00
|
|
|
# Test expansion of variables
|
|
|
|
|
2017-07-20 13:42:52 +08:00
|
|
|
# We don't use the test utility function of the same name because we want
|
|
|
|
# different behavior. Specifically, that the expansion of any variables or
|
|
|
|
# other strings before we are invoked produce the expected expansion.
|
2017-08-04 12:37:02 +08:00
|
|
|
function expansion --description 'Prints argument count followed by arguments'
|
2014-08-21 12:19:08 +08:00
|
|
|
echo (count $argv) $argv
|
|
|
|
end
|
|
|
|
|
|
|
|
set -l foo
|
2017-08-04 12:37:02 +08:00
|
|
|
expansion "$foo"
|
|
|
|
expansion $foo
|
|
|
|
expansion "prefix$foo"
|
|
|
|
expansion prefix$foo
|
2014-08-21 12:19:08 +08:00
|
|
|
|
2017-08-04 12:37:02 +08:00
|
|
|
expansion "$$foo"
|
|
|
|
expansion $$foo
|
|
|
|
expansion "prefix$$foo"
|
|
|
|
expansion prefix$$foo
|
2014-08-21 12:19:08 +08:00
|
|
|
|
|
|
|
set -l foo ''
|
2017-08-04 12:37:02 +08:00
|
|
|
expansion "$foo"
|
|
|
|
expansion $foo
|
|
|
|
expansion "prefix$foo"
|
|
|
|
expansion prefix$foo
|
2014-08-21 12:19:08 +08:00
|
|
|
|
2017-08-04 12:37:02 +08:00
|
|
|
expansion "$$foo"
|
|
|
|
expansion $$foo
|
|
|
|
expansion "prefix$$foo"
|
|
|
|
expansion prefix$$foo
|
2014-08-21 12:19:08 +08:00
|
|
|
|
|
|
|
set -l foo bar
|
|
|
|
set -l bar
|
2017-08-04 12:37:02 +08:00
|
|
|
expansion "$$foo"
|
|
|
|
expansion $$foo
|
|
|
|
expansion "prefix$$foo"
|
|
|
|
expansion prefix$$foo
|
2014-08-21 12:19:08 +08:00
|
|
|
|
|
|
|
set -l bar baz
|
2017-08-04 12:37:02 +08:00
|
|
|
expansion "$$foo"
|
|
|
|
expansion $$foo
|
|
|
|
expansion "prefix$$foo"
|
|
|
|
expansion prefix$$foo
|
2014-08-21 12:19:08 +08:00
|
|
|
|
|
|
|
set -l bar baz quux
|
2017-08-04 12:37:02 +08:00
|
|
|
expansion "$$foo"
|
|
|
|
expansion $$foo
|
|
|
|
expansion "prefix$$foo"
|
|
|
|
expansion prefix$$foo
|
2014-08-21 12:19:08 +08:00
|
|
|
|
|
|
|
set -l foo bar fooer fooest
|
|
|
|
set -l fooer
|
|
|
|
set -l fooest
|
2017-08-04 12:37:02 +08:00
|
|
|
expansion "$$foo"
|
|
|
|
expansion $$foo
|
|
|
|
expansion "prefix$$foo"
|
|
|
|
expansion prefix$$foo
|
2014-08-21 12:19:08 +08:00
|
|
|
|
|
|
|
set -l fooer ''
|
2017-08-04 12:37:02 +08:00
|
|
|
expansion $$foo
|
|
|
|
expansion prefix$$foo
|
2014-08-21 12:19:08 +08:00
|
|
|
|
|
|
|
set -l foo bar '' fooest
|
2017-08-04 12:37:02 +08:00
|
|
|
expansion "$$foo"
|
|
|
|
expansion $$foo
|
|
|
|
expansion "prefix$$foo"
|
|
|
|
expansion prefix$$foo
|
2018-04-27 04:42:48 +08:00
|
|
|
expansion $foo[-5..2] # No result, because the starting index is invalid and we force-reverse.
|
2017-08-04 12:37:02 +08:00
|
|
|
expansion $foo[-2..-1]
|
|
|
|
expansion $foo[-10..-5]
|
|
|
|
expansion (printf '%s\n' $foo)[-5..2]
|
|
|
|
expansion (printf '%s\n' $foo)[-2..-1]
|
|
|
|
expansion (printf '%s\n' $foo)[-10..-5]
|
2018-09-15 17:03:45 +08:00
|
|
|
expansion (echo one)[2..-1]
|
2014-08-21 13:01:24 +08:00
|
|
|
|
2018-09-15 17:03:45 +08:00
|
|
|
echo "# foo = "
|
2014-08-21 13:01:24 +08:00
|
|
|
set -l foo
|
2017-08-04 12:37:02 +08:00
|
|
|
expansion "$foo[1]"
|
|
|
|
expansion $foo[1]
|
|
|
|
expansion "$foo[-1]"
|
|
|
|
expansion $foo[-1]
|
|
|
|
expansion "$foo[2]"
|
|
|
|
expansion $foo[2]
|
|
|
|
expansion "$foo[1 2]"
|
|
|
|
expansion $foo[1 2]
|
|
|
|
expansion "$foo[2 1]"
|
|
|
|
expansion $foo[2 1]
|
2018-09-15 17:03:45 +08:00
|
|
|
echo "# foo = a b c"
|
2017-06-16 20:17:17 +08:00
|
|
|
set -l foo a b c
|
2017-08-04 12:37:02 +08:00
|
|
|
expansion $foo[17]
|
|
|
|
expansion $foo[-17]
|
|
|
|
expansion $foo[17..18]
|
2018-04-27 04:42:48 +08:00
|
|
|
expansion $foo[4..-2]
|
2018-09-15 17:03:45 +08:00
|
|
|
echo "# foo = a"
|
|
|
|
set -l foo a
|
|
|
|
expansion $foo[2..-1]
|
2014-08-21 15:26:14 +08:00
|
|
|
|
|
|
|
echo "$foo[d]"
|
|
|
|
echo $foo[d]
|
|
|
|
|
|
|
|
echo ()[1]
|
|
|
|
echo ()[d]
|
2014-10-25 13:58:52 +08:00
|
|
|
|
2017-08-04 12:37:02 +08:00
|
|
|
logmsg Catch your breath
|
2015-05-16 08:10:29 +08:00
|
|
|
set paren ')'
|
|
|
|
echo $$paren
|
|
|
|
|
2017-08-04 12:37:02 +08:00
|
|
|
logmsg Test tilde expansion
|
2014-10-28 11:48:39 +08:00
|
|
|
# On OS X, /tmp is symlinked to /private/tmp
|
2014-10-31 12:51:23 +08:00
|
|
|
# $PWD is our best bet for resolving it
|
|
|
|
set -l saved $PWD
|
2014-10-28 11:48:39 +08:00
|
|
|
cd (mktemp -d)
|
2014-10-31 12:51:23 +08:00
|
|
|
set tmpdir $PWD
|
|
|
|
cd $saved
|
2014-10-25 13:58:52 +08:00
|
|
|
mkdir $tmpdir/realhome
|
|
|
|
ln -s $tmpdir/realhome $tmpdir/linkhome
|
2016-02-07 10:08:22 +08:00
|
|
|
set expandedtilde (env HOME=$tmpdir/linkhome ../test/root/bin/fish -c 'echo ~')
|
2014-10-25 13:58:52 +08:00
|
|
|
if test $expandedtilde != $tmpdir/realhome
|
|
|
|
echo '~ expands to' $expandedtilde ' - expected ' $tmpdir/realhome
|
|
|
|
end
|
|
|
|
unlink $tmpdir/linkhome
|
|
|
|
rmdir $tmpdir/realhome
|
|
|
|
rmdir $tmpdir
|