2009-10-23 11:01:25 -06:00
|
|
|
#!/bin/sh
|
|
|
|
|
# Verify behavior of env.
|
|
|
|
|
|
2015-01-01 04:49:02 +00:00
|
|
|
# Copyright (C) 2009-2015 Free Software Foundation, Inc.
|
2009-10-23 11:01:25 -06:00
|
|
|
|
|
|
|
|
# This program is free software: you can redistribute it and/or modify
|
|
|
|
|
# it under the terms of the GNU General Public License as published by
|
|
|
|
|
# the Free Software Foundation, either version 3 of the License, or
|
|
|
|
|
# (at your option) any later version.
|
|
|
|
|
|
|
|
|
|
# This program is distributed in the hope that it will be useful,
|
|
|
|
|
# but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
|
|
|
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
|
|
|
|
# GNU General Public License for more details.
|
|
|
|
|
|
|
|
|
|
# You should have received a copy of the GNU General Public License
|
|
|
|
|
# along with this program. If not, see <http://www.gnu.org/licenses/>.
|
|
|
|
|
|
|
|
|
|
|
2012-09-02 21:55:12 +02:00
|
|
|
. "${srcdir=.}/tests/init.sh"; path_prepend_ ./src
|
2010-11-17 21:35:31 +01:00
|
|
|
print_ver_ env
|
2009-10-23 11:01:25 -06:00
|
|
|
|
2014-06-05 19:50:32 -07:00
|
|
|
# A simple shebang program to call "echo" from symlinks like "./-u" or "./--".
|
|
|
|
|
echo "#!$abs_top_builddir/src/echo simple_echo" > simple_echo \
|
|
|
|
|
|| framework_failure_
|
|
|
|
|
chmod a+x simple_echo || framework_failure_
|
2009-10-23 11:01:25 -06:00
|
|
|
|
2014-07-18 18:59:29 +01:00
|
|
|
# Verify we can run the shebang which is not the case if
|
|
|
|
|
# there are spaces in $abs_top_builddir.
|
|
|
|
|
./simple_echo || skip_ "Error running simple_echo script"
|
|
|
|
|
|
2009-10-23 11:01:25 -06:00
|
|
|
# Verify clearing the environment
|
|
|
|
|
a=1
|
|
|
|
|
export a
|
|
|
|
|
env - > out || fail=1
|
2014-05-06 02:37:43 +01:00
|
|
|
compare /dev/null out || fail=1
|
2009-10-23 11:01:25 -06:00
|
|
|
env -i > out || fail=1
|
2014-05-06 02:37:43 +01:00
|
|
|
compare /dev/null out || fail=1
|
2009-10-23 11:01:25 -06:00
|
|
|
env -u a -i -u a -- > out || fail=1
|
2014-05-06 02:37:43 +01:00
|
|
|
compare /dev/null out || fail=1
|
2009-10-23 11:01:25 -06:00
|
|
|
env -i -- a=b > out || fail=1
|
2011-08-04 20:52:31 +02:00
|
|
|
echo a=b > exp || framework_failure_
|
2009-10-23 11:01:25 -06:00
|
|
|
compare exp out || fail=1
|
|
|
|
|
|
|
|
|
|
# These tests verify exact status of internal failure.
|
|
|
|
|
env --- # unknown option
|
|
|
|
|
test $? = 125 || fail=1
|
|
|
|
|
env -u # missing option argument
|
|
|
|
|
test $? = 125 || fail=1
|
|
|
|
|
env sh -c 'exit 2' # exit status propagation
|
|
|
|
|
test $? = 2 || fail=2
|
|
|
|
|
env . # invalid command
|
|
|
|
|
test $? = 126 || fail=1
|
2009-10-26 07:10:51 -06:00
|
|
|
env no_such # no such command
|
2009-10-23 11:01:25 -06:00
|
|
|
test $? = 127 || fail=1
|
|
|
|
|
|
2009-10-26 09:26:00 -06:00
|
|
|
# POSIX is clear that environ may, but need not be, sorted.
|
|
|
|
|
# Environment variable values may contain newlines, which cannot be
|
|
|
|
|
# observed by merely inspecting output from env.
|
|
|
|
|
# Cygwin requires a minimal environment to launch new processes: execve
|
|
|
|
|
# adds missing variables SYSTEMROOT and WINDIR, which show up in a
|
|
|
|
|
# subsequent env. Cygwin also requires /bin to always be part of PATH,
|
|
|
|
|
# and attempts to unset or reduce PATH may cause execve to fail.
|
|
|
|
|
#
|
|
|
|
|
# For these reasons, it is more portable to grep that our desired changes
|
|
|
|
|
# took place, rather than comparing output of env over an entire environment.
|
2009-10-23 11:01:25 -06:00
|
|
|
if env | grep '^ENV_TEST' >/dev/null ; then
|
2011-06-14 16:22:41 +02:00
|
|
|
skip_ "environment has potential interference from ENV_TEST*"
|
2009-10-23 11:01:25 -06:00
|
|
|
fi
|
|
|
|
|
|
|
|
|
|
ENV_TEST1=a
|
|
|
|
|
export ENV_TEST1
|
2014-03-05 18:41:16 +00:00
|
|
|
>out || framework_failure_
|
2009-10-23 11:01:25 -06:00
|
|
|
env ENV_TEST2= > all || fail=1
|
2011-08-04 20:52:31 +02:00
|
|
|
grep '^ENV_TEST' all | LC_ALL=C sort >> out || framework_failure_
|
2009-10-23 11:01:25 -06:00
|
|
|
env -u ENV_TEST1 ENV_TEST3=c > all || fail=1
|
2011-08-04 20:52:31 +02:00
|
|
|
grep '^ENV_TEST' all | LC_ALL=C sort >> out || framework_failure_
|
2009-10-23 11:01:25 -06:00
|
|
|
env ENV_TEST1=b > all || fail=1
|
2011-08-04 20:52:31 +02:00
|
|
|
grep '^ENV_TEST' all | LC_ALL=C sort >> out || framework_failure_
|
2009-10-23 11:01:25 -06:00
|
|
|
env ENV_TEST2= env > all || fail=1
|
2011-08-04 20:52:31 +02:00
|
|
|
grep '^ENV_TEST' all | LC_ALL=C sort >> out || framework_failure_
|
2009-10-23 11:01:25 -06:00
|
|
|
env -u ENV_TEST1 ENV_TEST3=c env > all || fail=1
|
2011-08-04 20:52:31 +02:00
|
|
|
grep '^ENV_TEST' all | LC_ALL=C sort >> out || framework_failure_
|
2009-10-23 11:01:25 -06:00
|
|
|
env ENV_TEST1=b env > all || fail=1
|
2011-08-04 20:52:31 +02:00
|
|
|
grep '^ENV_TEST' all | LC_ALL=C sort >> out || framework_failure_
|
|
|
|
|
cat <<EOF >exp || framework_failure_
|
2009-10-23 11:01:25 -06:00
|
|
|
ENV_TEST1=a
|
|
|
|
|
ENV_TEST2=
|
|
|
|
|
ENV_TEST3=c
|
|
|
|
|
ENV_TEST1=b
|
|
|
|
|
ENV_TEST1=a
|
|
|
|
|
ENV_TEST2=
|
|
|
|
|
ENV_TEST3=c
|
|
|
|
|
ENV_TEST1=b
|
|
|
|
|
EOF
|
|
|
|
|
compare exp out || fail=1
|
|
|
|
|
|
2009-10-26 09:26:00 -06:00
|
|
|
# PATH modifications affect exec.
|
2011-08-04 20:52:31 +02:00
|
|
|
mkdir unlikely_name || framework_failure_
|
|
|
|
|
cat <<EOF > unlikely_name/also_unlikely || framework_failure_
|
2009-10-26 09:26:00 -06:00
|
|
|
#!/bin/sh
|
|
|
|
|
echo pass
|
|
|
|
|
EOF
|
2011-08-04 20:52:31 +02:00
|
|
|
chmod +x unlikely_name/also_unlikely || framework_failure_
|
2015-01-13 03:30:33 +00:00
|
|
|
returns_ 127 env also_unlikely || fail=1
|
2012-04-03 20:32:44 +02:00
|
|
|
test x$(PATH=$PATH:unlikely_name env also_unlikely) = xpass || fail=1
|
|
|
|
|
test x$(env PATH="$PATH":unlikely_name also_unlikely) = xpass || fail=1
|
2009-10-26 09:26:00 -06:00
|
|
|
|
2009-10-26 13:32:49 -06:00
|
|
|
# Explicitly put . on the PATH for the rest of this test.
|
|
|
|
|
PATH=$PATH:
|
|
|
|
|
export PATH
|
|
|
|
|
|
|
|
|
|
# Use -- to end options (but not variable assignments).
|
|
|
|
|
# On some systems, execve("-i") invokes a shebang script ./-i on PATH as
|
|
|
|
|
# '/bin/sh -i', rather than '/bin/sh -- -i', which doesn't do what we want.
|
2014-06-05 19:50:32 -07:00
|
|
|
# Avoid the issue by using a shebang to 'echo' passing a second parameter
|
|
|
|
|
# before the '-i'. See the definition of simple_echo before.
|
2009-10-26 13:32:49 -06:00
|
|
|
# Test -u, rather than -i, to minimize PATH problems.
|
2014-06-05 19:50:32 -07:00
|
|
|
ln -s "simple_echo" ./-u || framework_failure_
|
2012-04-03 20:32:44 +02:00
|
|
|
case $(env -u echo echo good) in
|
2009-10-23 11:01:25 -06:00
|
|
|
good) ;;
|
|
|
|
|
*) fail=1 ;;
|
|
|
|
|
esac
|
2012-04-03 20:32:44 +02:00
|
|
|
case $(env -u echo -- echo good) in
|
2009-10-26 13:32:49 -06:00
|
|
|
good) ;;
|
|
|
|
|
*) fail=1 ;;
|
|
|
|
|
esac
|
2012-04-03 20:32:44 +02:00
|
|
|
case $(env -- -u pass) in
|
2014-06-05 19:50:32 -07:00
|
|
|
*pass) ;;
|
2009-10-26 13:32:49 -06:00
|
|
|
*) fail=1 ;;
|
|
|
|
|
esac
|
|
|
|
|
|
|
|
|
|
# After options have ended, the first argument not containing = is a program.
|
|
|
|
|
env a=b -- true
|
|
|
|
|
test $? = 127 || fail=1
|
2014-06-05 19:50:32 -07:00
|
|
|
ln -s "simple_echo" ./-- || framework_failure_
|
2012-04-03 20:32:44 +02:00
|
|
|
case $(env a=b -- true || echo fail) in
|
2014-06-05 19:50:32 -07:00
|
|
|
*true) ;;
|
2009-10-23 11:01:25 -06:00
|
|
|
*) fail=1 ;;
|
|
|
|
|
esac
|
|
|
|
|
|
2009-10-26 13:32:49 -06:00
|
|
|
# No way to directly invoke program name containing =.
|
2011-08-04 20:52:31 +02:00
|
|
|
cat <<EOF >./c=d || framework_failure_
|
2009-10-26 13:32:49 -06:00
|
|
|
#!/bin/sh
|
|
|
|
|
echo pass
|
|
|
|
|
EOF
|
2011-08-04 20:52:31 +02:00
|
|
|
chmod +x c=d || framework_failure_
|
2012-04-03 20:32:44 +02:00
|
|
|
test "x$(env c=d echo fail)" = xfail || fail=1
|
|
|
|
|
test "x$(env -- c=d echo fail)" = xfail || fail=1
|
|
|
|
|
test "x$(env ./c=d echo fail)" = xfail || fail=1
|
2009-10-26 13:32:49 -06:00
|
|
|
test "x$(env sh -c 'exec "$@"' sh c=d echo fail)" = xpass || fail=1
|
2010-09-07 15:53:14 +01:00
|
|
|
test "x$(sh -c '\c=d echo fail')" = xpass && #dash 0.5.4 fails so check first
|
|
|
|
|
{ test "x$(env sh -c '\c=d echo fail')" = xpass || fail=1; }
|
2009-10-23 11:01:25 -06:00
|
|
|
|
2009-10-26 07:10:51 -06:00
|
|
|
# catch unsetenv failure, broken through coreutils 8.0
|
|
|
|
|
env -u a=b true && fail=1
|
|
|
|
|
test $? = 125 || fail=1
|
|
|
|
|
env -u '' true && fail=1
|
|
|
|
|
test $? = 125 || fail=1
|
2009-10-23 11:01:25 -06:00
|
|
|
|
|
|
|
|
Exit $fail
|