tests: factor 350 fail=0 initializations into test-lib.sh
[platform/upstream/coreutils.git] / tests / misc / env
1 #!/bin/sh
2 # Verify behavior of env.
3
4 # Copyright (C) 2009 Free Software Foundation, Inc.
5
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.
10
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.
15
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/>.
18
19
20 if test "$VERBOSE" = yes; then
21   set -x
22   env --version
23 fi
24
25 . $srcdir/test-lib.sh
26
27
28 # Verify clearing the environment
29 a=1
30 export a
31 env - > out || fail=1
32 test -s out && fail=1
33 env -i > out || fail=1
34 test -s out && fail=1
35 env -u a -i -u a -- > out || fail=1
36 test -s out && fail=1
37 env -i -- a=b > out || fail=1
38 echo a=b > exp || framework_failure
39 compare exp out || fail=1
40
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
47 test $? = 2 || fail=2
48 env . # invalid command
49 test $? = 126 || fail=1
50 env no_such # no such command
51 test $? = 127 || fail=1
52
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.
60 #
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*"
65 fi
66
67 ENV_TEST1=a
68 export ENV_TEST1
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
83 ENV_TEST1=a
84 ENV_TEST2=
85 ENV_TEST3=c
86 ENV_TEST1=b
87 ENV_TEST1=a
88 ENV_TEST2=
89 ENV_TEST3=c
90 ENV_TEST1=b
91 EOF
92 compare exp out || fail=1
93
94 # PATH modifications affect exec.
95 mkdir unlikely_name || framework_failure
96 cat <<EOF > unlikely_name/also_unlikely || framework_failure
97 #!/bin/sh
98 echo pass
99 EOF
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
104
105 # Explicitly put . on the PATH for the rest of this test.
106 PATH=$PATH:
107 export PATH
108
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
116   good) ;;
117   *) fail=1 ;;
118 esac
119 case `env -u echo -- echo good` in
120   good) ;;
121   *) fail=1 ;;
122 esac
123 case `env -- -u pass` in
124   pass) ;;
125   *) fail=1 ;;
126 esac
127
128 # After options have ended, the first argument not containing = is a program.
129 env a=b -- true
130 test $? = 127 || fail=1
131 ln -s "$abs_top_builddir/src/echo" ./-- || framework_failure
132 case `env a=b -- true || echo fail` in
133   true) ;;
134   *) fail=1 ;;
135 esac
136
137 # No way to directly invoke program name containing =.
138 cat <<EOF >./c=d || framework_failure
139 #!/bin/sh
140 echo pass
141 EOF
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
148
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
154
155 Exit $fail