2005-09-20 21:31:55 +08:00
# Environment variable tests
#Test if variables can be properly set
set smurf blue
if test $smurf = blue
echo Test 1 pass
else
echo Test 1 fail
end
# Test if variables can be erased
set -e smurf
if test $smurf
echo Test 2 fail
else
echo Test 2 pass
end
# Test if local variables go out of scope
2005-09-27 16:35:54 +08:00
set -e t3
2005-09-20 21:31:55 +08:00
if true
2005-09-28 01:40:25 +08:00
set -l t3 bar
2005-09-20 21:31:55 +08:00
end
if test $t3
2005-09-28 01:40:25 +08:00
echo Test 3 fail
2005-09-20 21:31:55 +08:00
else
2005-09-28 01:40:25 +08:00
echo Test 3 pass
2005-09-20 21:31:55 +08:00
end
# Test if globals can be set in block scope
if true
2005-09-28 01:40:25 +08:00
set -g baz qux
2005-09-20 21:31:55 +08:00
end
if test $baz = qux
2005-09-28 01:40:25 +08:00
echo Test 4 pass
2005-09-20 21:31:55 +08:00
else
2005-09-28 01:40:25 +08:00
echo Test 4 fail
2005-09-20 21:31:55 +08:00
end
#Test that scope is preserved when setting a new value
set t5 a
if true
2005-09-28 01:40:25 +08:00
set t5 b
2005-09-20 21:31:55 +08:00
end
if test $t5 = b
2005-09-28 01:40:25 +08:00
echo Test 5 pass
2005-09-20 21:31:55 +08:00
else
2005-09-28 01:40:25 +08:00
echo Test 5 fail
2005-09-20 21:31:55 +08:00
end
# Test that scope is preserved in double blocks
2010-09-18 10:18:26 +08:00
for i in 1
2005-09-20 21:31:55 +08:00
set t6 $i
2010-09-18 10:18:26 +08:00
for j in a
2005-09-28 01:40:25 +08:00
if test $t6 $j = 1a
2005-09-20 21:31:55 +08:00
echo Test 6 pass
else
echo Test 6 fail
end
end
end
# Test if variables in for loop blocks do not go out of scope on new laps
set res fail
2005-10-02 21:44:06 +08:00
set -e t7
2005-09-20 21:31:55 +08:00
for i in 1 2
2005-09-28 01:40:25 +08:00
if test $i = 1
2005-10-02 21:44:06 +08:00
set t7 lala
2005-09-28 01:40:25 +08:00
else
2005-10-02 21:44:06 +08:00
if test $t7
2005-09-28 01:40:25 +08:00
set res pass
end
end
2005-09-20 21:31:55 +08:00
end
echo Test 7 $res
# Test if variables are properly exported
2005-09-27 16:35:54 +08:00
set -e t8
2005-09-20 21:31:55 +08:00
if true
2005-09-28 01:40:25 +08:00
set -lx t8 foo
2016-02-07 10:08:22 +08:00
if test ( ../test/root/bin/fish -c " echo $t8 " ) = foo
2005-09-28 01:40:25 +08:00
echo Test 8 pass
2005-09-20 21:31:55 +08:00
else
2005-09-28 01:40:25 +08:00
echo Test 8 fail
2005-09-20 21:31:55 +08:00
end
end
# Test if exported variables go out of scope
2016-02-07 10:08:22 +08:00
if test ( ../test/root/bin/fish -c " echo $t8 " )
2005-09-28 01:40:25 +08:00
echo Test 9 fail
2005-09-20 21:31:55 +08:00
else
2005-09-28 01:40:25 +08:00
echo Test 9 pass
2005-09-20 21:31:55 +08:00
end
2006-06-05 21:31:33 +08:00
# Test erasing variables in specific scope
2005-09-20 21:31:55 +08:00
2014-07-07 11:41:21 +08:00
set -eU __fish_test_universal_variables_variable_foo
set -g __fish_test_universal_variables_variable_foo bar
2006-06-05 21:31:33 +08:00
begin
2014-07-07 11:41:21 +08:00
set -l __fish_test_universal_variables_variable_foo baz
set -eg __fish_test_universal_variables_variable_foo
2006-06-05 21:31:33 +08:00
end
2014-07-07 11:41:21 +08:00
if set -q __fish_test_universal_variables_variable_foo
2006-06-05 21:31:33 +08:00
echo Test 10 fail
else
echo Test 10 pass
end
2014-07-07 11:41:21 +08:00
set __fish_test_universal_variables_variable_foo abc def
set -e __fish_test_universal_variables_variable_foo [ 1 ]
if test $__fish_test_universal_variables_variable_foo '=' def
2012-05-10 16:04:18 +08:00
echo Test 11 pass
else
echo Test 11 fail
end
2012-12-30 02:25:00 +08:00
# Test combinations of export and scope
2014-07-07 11:41:21 +08:00
set -ge __fish_test_universal_variables_variable_foo
2012-12-30 02:25:00 +08:00
2014-07-07 11:41:21 +08:00
set -Ue __fish_test_universal_variables_variable_foo
set -Ux __fish_test_universal_variables_variable_foo bar
set __fish_test_universal_variables_variable_foo baz
2016-02-07 10:08:22 +08:00
if test ( /bin/sh -c 'echo $__fish_test_universal_variables_variable_foo' ) = baz -a ( ../test/root/bin/fish -c 'echo $__fish_test_universal_variables_variable_foo' ) = baz
2012-12-30 02:25:00 +08:00
echo Test 12 pass
else
echo Test 12 fail
end
2014-07-07 11:41:21 +08:00
set -Ue __fish_test_universal_variables_variable_foo
2015-04-30 08:28:49 +08:00
# Should no longer be in environment (#2046)
2015-09-10 02:55:04 +08:00
env | __fish_sgrep __fish_test_universal_variables_variable_foo
2015-04-30 08:28:49 +08:00
2014-07-07 11:41:21 +08:00
set -Ux __fish_test_universal_variables_variable_foo bar
set -U __fish_test_universal_variables_variable_foo baz
2016-02-07 10:08:22 +08:00
if test ( /bin/sh -c 'echo $__fish_test_universal_variables_variable_foo' ) = baz -a ( ../test/root/bin/fish -c 'echo $__fish_test_universal_variables_variable_foo' ) = baz
2012-12-30 02:25:00 +08:00
echo Test 13 pass
else
echo Test 13 fail
end
2014-07-07 11:41:21 +08:00
set -Ux __fish_test_universal_variables_variable_foo bar
set -u __fish_test_universal_variables_variable_foo bar
2016-02-07 10:08:22 +08:00
if test ( /bin/sh -c 'echo $__fish_test_universal_variables_variable_foo' ) = '' -a ( ../test/root/bin/fish -c 'echo $__fish_test_universal_variables_variable_foo' ) = bar
2012-12-30 02:25:00 +08:00
echo Test 14 pass
else
echo Test 14 fail
end
2014-07-07 11:41:21 +08:00
set -Ux __fish_test_universal_variables_variable_foo bar
set -Uu __fish_test_universal_variables_variable_foo baz
2016-02-07 10:08:22 +08:00
if test ( /bin/sh -c 'echo $__fish_test_universal_variables_variable_foo' ) = '' -a ( ../test/root/bin/fish -c 'echo $__fish_test_universal_variables_variable_foo' ) = baz
2012-12-30 02:25:00 +08:00
echo Test 15 pass
else
echo Test 15 fail
end
2014-07-07 11:41:21 +08:00
set -eU __fish_test_universal_variables_variable_foo
2015-02-04 08:13:02 +08:00
function watch_foo --on-variable __fish_test_universal_variables_variable_foo
echo Foo change detected
end
set -U __fish_test_universal_variables_variable_foo 1234
set -eU __fish_test_universal_variables_variable_foo
functions -e watch_foo
2012-12-30 02:25:00 +08:00
# test erasing variables without a specified scope
set -g test16res
2014-07-07 11:41:21 +08:00
set -U __fish_test_universal_variables_variable_foo universal
set -g __fish_test_universal_variables_variable_foo global
2012-12-30 02:25:00 +08:00
begin
2014-07-07 11:41:21 +08:00
set -l __fish_test_universal_variables_variable_foo blocklocal
2012-12-30 02:25:00 +08:00
function test16
2014-07-07 11:41:21 +08:00
set -l __fish_test_universal_variables_variable_foo function
2012-12-30 02:25:00 +08:00
begin
2014-07-07 11:41:21 +08:00
set -l __fish_test_universal_variables_variable_foo functionblock
set test16res $test16res ( echo $__fish_test_universal_variables_variable_foo )
set -e __fish_test_universal_variables_variable_foo
set test16res $test16res ( echo $__fish_test_universal_variables_variable_foo )
set -e __fish_test_universal_variables_variable_foo
set test16res $test16res ( echo $__fish_test_universal_variables_variable_foo )
set -e __fish_test_universal_variables_variable_foo
set test16res $test16res ( echo $__fish_test_universal_variables_variable_foo )
set -e __fish_test_universal_variables_variable_foo
set test16res $test16res ( echo $__fish_test_universal_variables_variable_foo )
2012-12-30 02:25:00 +08:00
end
2014-07-07 11:41:21 +08:00
set test16res $test16res ( echo $__fish_test_universal_variables_variable_foo )
set -e __fish_test_universal_variables_variable_foo
2012-12-30 02:25:00 +08:00
end
test16
2014-07-07 11:41:21 +08:00
set test16res $test16res ( echo $__fish_test_universal_variables_variable_foo )
2012-12-30 02:25:00 +08:00
end
2014-07-07 11:41:21 +08:00
set test16res $test16res ( echo $__fish_test_universal_variables_variable_food )
2012-12-30 02:25:00 +08:00
#echo count: (count $test16res) "content:[$test16res]"
if test ( count $test16res ) = 8 -a " $test16res " = "functionblock function global universal blocklocal "
echo Test 16 pass
else
echo Test 16 fail
end
2015-06-13 07:05:59 +08:00
# Test that shadowing with a non-exported variable works
set -gx __fish_test_env17 UNSHADOWED
2015-09-10 02:55:04 +08:00
env | __fish_sgrep __fish_test_env17
2015-06-13 07:05:59 +08:00
function __fish_test_shadow
set -l __fish_test_env17
2015-09-10 02:55:04 +08:00
env | __fish_sgrep __fish_test_env17 ; or echo SHADOWED
2015-06-13 07:05:59 +08:00
end
__fish_test_shadow
2012-12-30 02:25:00 +08:00
2014-07-07 11:41:21 +08:00
# clear for other shells
set -eU __fish_test_universal_variables_variable_foo
2014-07-07 08:57:23 +08:00
# Test behavior of universals on startup (#1526)
echo Testing Universal Startup
set -U testu 0
2016-02-07 10:08:22 +08:00
../test/root/bin/fish -c 'set -U testu 1'
2014-07-07 08:57:23 +08:00
echo $testu
2016-02-07 10:08:22 +08:00
../test/root/bin/fish -c 'echo $testu'
2014-07-07 08:57:23 +08:00
2016-02-07 10:08:22 +08:00
../test/root/bin/fish -c 'set -U testu 2'
2014-07-07 08:57:23 +08:00
echo $testu
2016-02-07 10:08:22 +08:00
../test/root/bin/fish -c 'echo $testu'
2014-07-07 08:57:23 +08:00
2016-02-07 10:08:22 +08:00
../test/root/bin/fish -c 'set -e testu' ;
2014-07-07 08:57:23 +08:00
echo Missing: $testu
2016-02-07 10:08:22 +08:00
../test/root/bin/fish -c 'echo Missing: $testu'
2014-07-07 08:57:23 +08:00
2014-08-23 09:05:28 +08:00
# test SHLVL
# use a subshell to ensure a clean slate
2016-02-07 10:08:22 +08:00
env SHLVL = ../test/root/bin/fish -c 'echo SHLVL: $SHLVL; ../test/root/bin/fish -c \' echo SHLVL: $SHLVL \' '
2014-09-20 08:31:34 +08:00
# exec should decrement SHLVL
2016-02-07 10:08:22 +08:00
env SHLVL = ../test/root/bin/fish -c 'echo SHLVL: $SHLVL; exec ../test/root/bin/fish -c \' echo SHLVL: $SHLVL \' '
2014-09-20 08:31:34 +08:00
# garbage SHLVLs should be treated as garbage
2016-02-07 10:08:22 +08:00
env SHLVL = 3foo ../test/root/bin/fish -c 'echo SHLVL: $SHLVL'
2014-09-20 08:31:34 +08:00
# whitespace is allowed though (for bash compatibility)
2016-02-07 10:08:22 +08:00
env SHLVL = "3 " ../test/root/bin/fish -c 'echo SHLVL: $SHLVL'
env SHLVL = " 3" ../test/root/bin/fish -c 'echo SHLVL: $SHLVL'
2014-08-23 09:05:28 +08:00
2014-10-08 09:51:49 +08:00
# Test transformation of inherited variables
2016-02-07 10:08:22 +08:00
env DISPLAY = "localhost:0.0" ../test/root/bin/fish -c 'echo Elements in DISPLAY: (count $DISPLAY)'
2014-10-08 10:37:13 +08:00
# We can't use PATH for this because the global configuration will modify PATH
# based on /etc/paths and /etc/paths.d.
2014-10-13 06:01:35 +08:00
# Exported arrays should use record separator, with a few exceptions. So we can use an arbitrary variable for this.
2016-02-07 10:08:22 +08:00
env FOO = one\x 1etwo\x 1ethree\x 1efour ../test/root/bin/fish -c 'echo Elements in FOO: (count $FOO)'
2014-10-13 06:01:35 +08:00
# some must use colon separators!
set -lx MANPATH man1 man2 man3 ; env | grep MANPATH
2014-10-08 09:51:49 +08:00
2012-12-30 02:25:00 +08:00
true