2 # Verify behavior of env.
4 # Copyright (C) 2009-2012 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 .
"${srcdir=.}/init.sh"; path_prepend_ ..
/src
24 # Verify clearing the environment
29 env
-i > out || fail
=1
31 env
-u a
-i -u a
-- > out || fail
=1
33 env
-i -- a
=b
> out || fail
=1
34 echo a
=b
> exp || framework_failure_
35 compare exp out || fail
=1
37 # These tests verify exact status of internal failure.
38 env
--- # unknown option
39 test $?
= 125 || fail
=1
40 env
-u # missing option argument
41 test $?
= 125 || fail
=1
42 env sh
-c 'exit 2' # exit status propagation
44 env .
# invalid command
45 test $?
= 126 || fail
=1
46 env no_such
# no such command
47 test $?
= 127 || fail
=1
49 # POSIX is clear that environ may, but need not be, sorted.
50 # Environment variable values may contain newlines, which cannot be
51 # observed by merely inspecting output from env.
52 # Cygwin requires a minimal environment to launch new processes: execve
53 # adds missing variables SYSTEMROOT and WINDIR, which show up in a
54 # subsequent env. Cygwin also requires /bin to always be part of PATH,
55 # and attempts to unset or reduce PATH may cause execve to fail.
57 # For these reasons, it is more portable to grep that our desired changes
58 # took place, rather than comparing output of env over an entire environment.
59 if env |
grep '^ENV_TEST' >/dev
/null
; then
60 skip_
"environment has potential interference from ENV_TEST*"
65 : >out || framework_failure_
66 env ENV_TEST2
= > all || fail
=1
67 grep '^ENV_TEST' all | LC_ALL
=C
sort >> out || framework_failure_
68 env
-u ENV_TEST1 ENV_TEST3
=c
> all || fail
=1
69 grep '^ENV_TEST' all | LC_ALL
=C
sort >> out || framework_failure_
70 env ENV_TEST1
=b
> all || fail
=1
71 grep '^ENV_TEST' all | LC_ALL
=C
sort >> out || framework_failure_
72 env ENV_TEST2
= env
> all || fail
=1
73 grep '^ENV_TEST' all | LC_ALL
=C
sort >> out || framework_failure_
74 env
-u ENV_TEST1 ENV_TEST3
=c env
> all || fail
=1
75 grep '^ENV_TEST' all | LC_ALL
=C
sort >> out || framework_failure_
76 env ENV_TEST1
=b env
> all || fail
=1
77 grep '^ENV_TEST' all | LC_ALL
=C
sort >> out || framework_failure_
78 cat <<EOF >exp || framework_failure_
88 compare exp out || fail
=1
90 # PATH modifications affect exec.
91 mkdir unlikely_name || framework_failure_
92 cat <<EOF > unlikely_name/also_unlikely || framework_failure_
96 chmod +x unlikely_name
/also_unlikely || framework_failure_
97 env also_unlikely
&& fail
=1
98 test x
`PATH=$PATH:unlikely_name env also_unlikely` = xpass || fail
=1
99 test x
`env PATH="$PATH":unlikely_name also_unlikely` = xpass || fail
=1
101 # Explicitly put . on the PATH for the rest of this test.
105 # Use -- to end options (but not variable assignments).
106 # On some systems, execve("-i") invokes a shebang script ./-i on PATH as
107 # '/bin/sh -i', rather than '/bin/sh -- -i', which doesn't do what we want.
108 # Avoid the issue by using an executable rather than a script.
109 # Test -u, rather than -i, to minimize PATH problems.
110 ln -s "$abs_top_builddir/src/echo" .
/-u || framework_failure_
111 case `env -u echo echo good` in
115 case `env -u echo -- echo good` in
119 case `env -- -u pass` in
124 # After options have ended, the first argument not containing = is a program.
126 test $?
= 127 || fail
=1
127 ln -s "$abs_top_builddir/src/echo" .
/-- || framework_failure_
128 case `env a=b -- true || echo fail` in
133 # No way to directly invoke program name containing =.
134 cat <<EOF >./c=d || framework_failure_
138 chmod +x c
=d || framework_failure_
139 test "x`env c=d echo fail`" = xfail || fail
=1
140 test "x`env -- c=d echo fail`" = xfail || fail
=1
141 test "x`env ./c=d echo fail`" = xfail || fail
=1
142 test "x$(env sh -c 'exec "$@
"' sh c=d echo fail)" = xpass || fail
=1
143 test "x$(sh -c '\c=d echo fail')" = xpass
&& #dash 0.5.4 fails so check first
144 { test "x$(env sh -c '\c=d echo fail')" = xpass || fail
=1; }
146 # catch unsetenv failure, broken through coreutils 8.0
147 env
-u a
=b true
&& fail
=1
148 test $?
= 125 || fail
=1
149 env
-u '' true
&& fail
=1
150 test $?
= 125 || fail
=1