2005-09-20 13:31:55 +00: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 08:35:54 +00:00
set -e t3
2005-09-20 13:31:55 +00:00
if true
2005-09-27 17:40:25 +00:00
set -l t3 bar
2005-09-20 13:31:55 +00:00
end
if test $t3
2005-09-27 17:40:25 +00:00
echo Test 3 fail
2005-09-20 13:31:55 +00:00
else
2005-09-27 17:40:25 +00:00
echo Test 3 pass
2005-09-20 13:31:55 +00:00
end
# Test if globals can be set in block scope
if true
2005-09-27 17:40:25 +00:00
set -g baz qux
2005-09-20 13:31:55 +00:00
end
if test $baz = qux
2005-09-27 17:40:25 +00:00
echo Test 4 pass
2005-09-20 13:31:55 +00:00
else
2005-09-27 17:40:25 +00:00
echo Test 4 fail
2005-09-20 13:31:55 +00:00
end
#Test that scope is preserved when setting a new value
set t5 a
if true
2005-09-27 17:40:25 +00:00
set t5 b
2005-09-20 13:31:55 +00:00
end
if test $t5 = b
2005-09-27 17:40:25 +00:00
echo Test 5 pass
2005-09-20 13:31:55 +00:00
else
2005-09-27 17:40:25 +00:00
echo Test 5 fail
2005-09-20 13:31:55 +00:00
end
# Test that scope is preserved in double blocks
2010-09-18 02:18:26 +00:00
for i in 1
2005-09-20 13:31:55 +00:00
set t6 $i
2010-09-18 02:18:26 +00:00
for j in a
2005-09-27 17:40:25 +00:00
if test $t6$j = 1a
2005-09-20 13:31:55 +00: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 13:44:06 +00:00
set -e t7
2005-09-20 13:31:55 +00:00
for i in 1 2
2005-09-27 17:40:25 +00:00
if test $i = 1
2005-10-02 13:44:06 +00:00
set t7 lala
2005-09-27 17:40:25 +00:00
else
2005-10-02 13:44:06 +00:00
if test $t7
2005-09-27 17:40:25 +00:00
set res pass
end
end
2005-09-20 13:31:55 +00:00
end
echo Test 7 $res
# Test if variables are properly exported
2005-09-27 08:35:54 +00:00
set -e t8
2005-09-20 13:31:55 +00:00
if true
2005-09-27 17:40:25 +00:00
set -lx t8 foo
if test (../fish -c "echo $t8") = foo
echo Test 8 pass
2005-09-20 13:31:55 +00:00
else
2005-09-27 17:40:25 +00:00
echo Test 8 fail
2005-09-20 13:31:55 +00:00
end
end
# Test if exported variables go out of scope
2010-09-18 02:18:26 +00:00
if test (../fish -c "echo $t8")
2005-09-27 17:40:25 +00:00
echo Test 9 fail
2005-09-20 13:31:55 +00:00
else
2005-09-27 17:40:25 +00:00
echo Test 9 pass
2005-09-20 13:31:55 +00:00
end
2006-06-05 13:31:33 +00:00
# Test erasing variables in specific scope
2005-09-20 13:31:55 +00:00
2014-07-07 03:41:21 +00:00
set -eU __fish_test_universal_variables_variable_foo
set -g __fish_test_universal_variables_variable_foo bar
2006-06-05 13:31:33 +00:00
begin
2014-07-07 03:41:21 +00:00
set -l __fish_test_universal_variables_variable_foo baz
set -eg __fish_test_universal_variables_variable_foo
2006-06-05 13:31:33 +00:00
end
2014-07-07 03:41:21 +00:00
if set -q __fish_test_universal_variables_variable_foo
2006-06-05 13:31:33 +00:00
echo Test 10 fail
else
echo Test 10 pass
end
2014-07-07 03:41:21 +00: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 08:04:18 +00:00
echo Test 11 pass
else
echo Test 11 fail
end
2012-12-29 18:25:00 +00:00
# Test combinations of export and scope
2014-07-07 03:41:21 +00:00
set -ge __fish_test_universal_variables_variable_foo
2012-12-29 18:25:00 +00:00
2014-07-07 03:41:21 +00: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
if test (/bin/sh -c 'echo $__fish_test_universal_variables_variable_foo') = baz -a (../fish -c 'echo $__fish_test_universal_variables_variable_foo') = baz
2012-12-29 18:25:00 +00:00
echo Test 12 pass
else
echo Test 12 fail
end
2014-07-07 03:41:21 +00:00
set -Ue __fish_test_universal_variables_variable_foo
set -Ux __fish_test_universal_variables_variable_foo bar
set -U __fish_test_universal_variables_variable_foo baz
if test (/bin/sh -c 'echo $__fish_test_universal_variables_variable_foo') = baz -a (../fish -c 'echo $__fish_test_universal_variables_variable_foo') = baz
2012-12-29 18:25:00 +00:00
echo Test 13 pass
else
echo Test 13 fail
end
2014-07-07 03:41:21 +00:00
set -Ux __fish_test_universal_variables_variable_foo bar
set -u __fish_test_universal_variables_variable_foo bar
if test (/bin/sh -c 'echo $__fish_test_universal_variables_variable_foo') = '' -a (../fish -c 'echo $__fish_test_universal_variables_variable_foo') = bar
2012-12-29 18:25:00 +00:00
echo Test 14 pass
else
echo Test 14 fail
end
2014-07-07 03:41:21 +00:00
set -Ux __fish_test_universal_variables_variable_foo bar
set -Uu __fish_test_universal_variables_variable_foo baz
if test (/bin/sh -c 'echo $__fish_test_universal_variables_variable_foo') = '' -a (../fish -c 'echo $__fish_test_universal_variables_variable_foo') = baz
2012-12-29 18:25:00 +00:00
echo Test 15 pass
else
echo Test 15 fail
end
2014-07-07 03:41:21 +00:00
set -eU __fish_test_universal_variables_variable_foo
2012-12-29 18:25:00 +00:00
# test erasing variables without a specified scope
set -g test16res
2014-07-07 03:41:21 +00:00
set -U __fish_test_universal_variables_variable_foo universal
set -g __fish_test_universal_variables_variable_foo global
2012-12-29 18:25:00 +00:00
begin
2014-07-07 03:41:21 +00:00
set -l __fish_test_universal_variables_variable_foo blocklocal
2012-12-29 18:25:00 +00:00
function test16
2014-07-07 03:41:21 +00:00
set -l __fish_test_universal_variables_variable_foo function
2012-12-29 18:25:00 +00:00
begin
2014-07-07 03:41:21 +00: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-29 18:25:00 +00:00
end
2014-07-07 03:41:21 +00:00
set test16res $test16res (echo $__fish_test_universal_variables_variable_foo)
set -e __fish_test_universal_variables_variable_foo
2012-12-29 18:25:00 +00:00
end
test16
2014-07-07 03:41:21 +00:00
set test16res $test16res (echo $__fish_test_universal_variables_variable_foo)
2012-12-29 18:25:00 +00:00
end
2014-07-07 03:41:21 +00:00
set test16res $test16res (echo $__fish_test_universal_variables_variable_food)
2012-12-29 18:25:00 +00: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
2014-07-07 03:41:21 +00:00
# clear for other shells
set -eU __fish_test_universal_variables_variable_foo
2014-07-07 00:57:23 +00:00
# Test behavior of universals on startup (#1526)
echo Testing Universal Startup
set -U testu 0
../fish -c 'set -U testu 1'
echo $testu
../fish -c 'echo $testu'
../fish -c 'set -U testu 2'
echo $testu
../fish -c 'echo $testu'
../fish -c 'set -e testu';
echo Missing: $testu
../fish -c 'echo Missing: $testu'
2014-08-23 01:05:28 +00:00
# test SHLVL
# use a subshell to ensure a clean slate
env SHLVL= ../fish -c 'echo SHLVL: $SHLVL; ../fish -c \'echo SHLVL: $SHLVL\''
2014-09-20 00:31:34 +00:00
# exec should decrement SHLVL
env SHLVL= ../fish -c 'echo SHLVL: $SHLVL; exec ../fish -c \'echo SHLVL: $SHLVL\''
# garbage SHLVLs should be treated as garbage
env SHLVL=3foo ../fish -c 'echo SHLVL: $SHLVL'
# whitespace is allowed though (for bash compatibility)
env SHLVL="3 " ../fish -c 'echo SHLVL: $SHLVL'
env SHLVL=" 3" ../fish -c 'echo SHLVL: $SHLVL'
2014-08-23 01:05:28 +00:00
2014-10-08 01:51:49 +00:00
# Test transformation of inherited variables
env DISPLAY="localhost:0.0" ../fish -c 'echo Elements in DISPLAY: (count $DISPLAY)'
env PATH="/bin:/sbin:/usr/bin:/usr/sbin" ../fish -c 'echo Elements in PATH: (count $PATH)'
2012-12-29 18:25:00 +00:00
true