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
if test (../fish -c "echo $t8") = foo
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
2010-09-18 10:18:26 +08:00
if test (../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
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-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
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-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
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-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
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-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
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
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
../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 09:05:28 +08:00
# test SHLVL
# use a subshell to ensure a clean slate
env SHLVL= ../fish -c 'echo SHLVL: $SHLVL; ../fish -c \'echo SHLVL: $SHLVL\''
2012-12-30 02:25:00 +08:00
true