2 # Verify behavior of env.
4 # Copyright (C) 2009 Free Software Foundation, Inc.
6 # This program is free software: you can redistribute it and/or modify
7 # it under the terms of the GNU General Public License as published by
8 # the Free Software Foundation, either version 3 of the License, or
9 # (at your option) any later version.
11 # This program is distributed in the hope that it will be useful,
12 # but WITHOUT ANY WARRANTY; without even the implied warranty of
13 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 # GNU General Public License for more details.
16 # You should have received a copy of the GNU General Public License
17 # along with this program. If not, see <http://www.gnu.org/licenses/>.
20 if test "$VERBOSE" = yes; then
28 # Verify clearing the environment
33 env -i > out || fail=1
35 env -u a -i -u a -- > out || fail=1
37 env -i -- a=b > out || fail=1
38 echo a=b > exp || framework_failure
39 compare exp out || fail=1
41 # These tests verify exact status of internal failure.
42 env --- # unknown option
43 test $? = 125 || fail=1
44 env -u # missing option argument
45 test $? = 125 || fail=1
46 env sh -c 'exit 2' # exit status propagation
48 env . # invalid command
49 test $? = 126 || fail=1
50 env no_such # no such command
51 test $? = 127 || fail=1
53 # POSIX is clear that environ may, but need not be, sorted.
54 # Environment variable values may contain newlines, which cannot be
55 # observed by merely inspecting output from env.
56 # Cygwin requires a minimal environment to launch new processes: execve
57 # adds missing variables SYSTEMROOT and WINDIR, which show up in a
58 # subsequent env. Cygwin also requires /bin to always be part of PATH,
59 # and attempts to unset or reduce PATH may cause execve to fail.
61 # For these reasons, it is more portable to grep that our desired changes
62 # took place, rather than comparing output of env over an entire environment.
63 if env | grep '^ENV_TEST' >/dev/null ; then
64 skip_test_ "environment has potential interference from ENV_TEST*"
69 : >out || framework_failure
70 env ENV_TEST2= > all || fail=1
71 grep '^ENV_TEST' all | LC_ALL=C sort >> out || framework_failure
72 env -u ENV_TEST1 ENV_TEST3=c > all || fail=1
73 grep '^ENV_TEST' all | LC_ALL=C sort >> out || framework_failure
74 env ENV_TEST1=b > all || fail=1
75 grep '^ENV_TEST' all | LC_ALL=C sort >> out || framework_failure
76 env ENV_TEST2= env > all || fail=1
77 grep '^ENV_TEST' all | LC_ALL=C sort >> out || framework_failure
78 env -u ENV_TEST1 ENV_TEST3=c env > all || fail=1
79 grep '^ENV_TEST' all | LC_ALL=C sort >> out || framework_failure
80 env ENV_TEST1=b env > all || fail=1
81 grep '^ENV_TEST' all | LC_ALL=C sort >> out || framework_failure
82 cat <<EOF >exp || framework_failure
92 compare exp out || fail=1
94 # PATH modifications affect exec.
95 mkdir unlikely_name || framework_failure
96 cat <<EOF > unlikely_name/also_unlikely || framework_failure
100 chmod +x unlikely_name/also_unlikely || framework_failure
101 env also_unlikely && fail=1
102 test x`PATH=$PATH:unlikely_name env also_unlikely` = xpass || fail=1
103 test x`env PATH="$PATH":unlikely_name also_unlikely` = xpass || fail=1
105 # Explicitly put . on the PATH for the rest of this test.
109 # Use -- to end options (but not variable assignments).
110 # On some systems, execve("-i") invokes a shebang script ./-i on PATH as
111 # '/bin/sh -i', rather than '/bin/sh -- -i', which doesn't do what we want.
112 # Avoid the issue by using an executable rather than a script.
113 # Test -u, rather than -i, to minimize PATH problems.
114 ln -s "$abs_top_builddir/src/echo" ./-u || framework_failure
115 case `env -u echo echo good` in
119 case `env -u echo -- echo good` in
123 case `env -- -u pass` in
128 # After options have ended, the first argument not containing = is a program.
130 test $? = 127 || fail=1
131 ln -s "$abs_top_builddir/src/echo" ./-- || framework_failure
132 case `env a=b -- true || echo fail` in
137 # No way to directly invoke program name containing =.
138 cat <<EOF >./c=d || framework_failure
142 chmod +x c=d || framework_failure
143 test "x`env c=d echo fail`" = xfail || fail=1
144 test "x`env -- c=d echo fail`" = xfail || fail=1
145 test "x`env ./c=d echo fail`" = xfail || fail=1
146 test "x$(env sh -c '\c=d echo fail')" = xpass || fail=1
147 test "x$(env sh -c 'exec "$@"' sh c=d echo fail)" = xpass || fail=1
149 # catch unsetenv failure, broken through coreutils 8.0
150 env -u a=b true && fail=1
151 test $? = 125 || fail=1
152 env -u '' true && fail=1
153 test $? = 125 || fail=1