tests: avoid false failure in tail inotify test
[coreutils.git] / tests / misc / env.sh
blob666d7771721b00ebcbfbed5692f7f9efe030883b
1 #!/bin/sh
2 # Verify behavior of env.
4 # Copyright (C) 2009-2016 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=.}/tests/init.sh"; path_prepend_ ./src
21 print_ver_ env
23 # A simple shebang program to call "echo" from symlinks like "./-u" or "./--".
24 echo "#!$abs_top_builddir/src/echo simple_echo" > simple_echo \
25 || framework_failure_
26 chmod a+x simple_echo || framework_failure_
28 # Verify we can run the shebang which is not the case if
29 # there are spaces in $abs_top_builddir.
30 ./simple_echo || skip_ "Error running simple_echo script"
32 # Verify clearing the environment
33 a=1
34 export a
35 env - > out || fail=1
36 compare /dev/null out || fail=1
37 env -i > out || fail=1
38 compare /dev/null out || fail=1
39 env -u a -i -u a -- > out || fail=1
40 compare /dev/null out || fail=1
41 env -i -- a=b > out || fail=1
42 echo a=b > exp || framework_failure_
43 compare exp out || fail=1
45 # These tests verify exact status of internal failure.
46 env --- # unknown option
47 test $? = 125 || fail=1
48 env -u # missing option argument
49 test $? = 125 || fail=1
50 env sh -c 'exit 2' # exit status propagation
51 test $? = 2 || fail=2
52 env . # invalid command
53 test $? = 126 || fail=1
54 env no_such # no such command
55 test $? = 127 || fail=1
57 # POSIX is clear that environ may, but need not be, sorted.
58 # Environment variable values may contain newlines, which cannot be
59 # observed by merely inspecting output from env.
60 # Cygwin requires a minimal environment to launch new processes: execve
61 # adds missing variables SYSTEMROOT and WINDIR, which show up in a
62 # subsequent env. Cygwin also requires /bin to always be part of PATH,
63 # and attempts to unset or reduce PATH may cause execve to fail.
65 # For these reasons, it is more portable to grep that our desired changes
66 # took place, rather than comparing output of env over an entire environment.
67 if env | grep '^ENV_TEST' >/dev/null ; then
68 skip_ "environment has potential interference from ENV_TEST*"
71 ENV_TEST1=a
72 export ENV_TEST1
73 >out || framework_failure_
74 env ENV_TEST2= > all || fail=1
75 grep '^ENV_TEST' all | LC_ALL=C sort >> out || framework_failure_
76 env -u ENV_TEST1 ENV_TEST3=c > all || fail=1
77 grep '^ENV_TEST' all | LC_ALL=C sort >> out || framework_failure_
78 env ENV_TEST1=b > all || fail=1
79 grep '^ENV_TEST' all | LC_ALL=C sort >> out || framework_failure_
80 env ENV_TEST2= env > all || fail=1
81 grep '^ENV_TEST' all | LC_ALL=C sort >> out || framework_failure_
82 env -u ENV_TEST1 ENV_TEST3=c env > all || fail=1
83 grep '^ENV_TEST' all | LC_ALL=C sort >> out || framework_failure_
84 env ENV_TEST1=b env > all || fail=1
85 grep '^ENV_TEST' all | LC_ALL=C sort >> out || framework_failure_
86 cat <<EOF >exp || framework_failure_
87 ENV_TEST1=a
88 ENV_TEST2=
89 ENV_TEST3=c
90 ENV_TEST1=b
91 ENV_TEST1=a
92 ENV_TEST2=
93 ENV_TEST3=c
94 ENV_TEST1=b
95 EOF
96 compare exp out || fail=1
98 # PATH modifications affect exec.
99 mkdir unlikely_name || framework_failure_
100 cat <<EOF > unlikely_name/also_unlikely || framework_failure_
101 #!/bin/sh
102 echo pass
104 chmod +x unlikely_name/also_unlikely || framework_failure_
105 returns_ 127 env also_unlikely || fail=1
106 test x$(PATH=$PATH:unlikely_name env also_unlikely) = xpass || fail=1
107 test x$(env PATH="$PATH":unlikely_name also_unlikely) = xpass || fail=1
109 # Explicitly put . on the PATH for the rest of this test.
110 PATH=$PATH:
111 export PATH
113 # Use -- to end options (but not variable assignments).
114 # On some systems, execve("-i") invokes a shebang script ./-i on PATH as
115 # '/bin/sh -i', rather than '/bin/sh -- -i', which doesn't do what we want.
116 # Avoid the issue by using a shebang to 'echo' passing a second parameter
117 # before the '-i'. See the definition of simple_echo before.
118 # Test -u, rather than -i, to minimize PATH problems.
119 ln -s "simple_echo" ./-u || framework_failure_
120 case $(env -u echo echo good) in
121 good) ;;
122 *) fail=1 ;;
123 esac
124 case $(env -u echo -- echo good) in
125 good) ;;
126 *) fail=1 ;;
127 esac
128 case $(env -- -u pass) in
129 *pass) ;;
130 *) fail=1 ;;
131 esac
133 # After options have ended, the first argument not containing = is a program.
134 env a=b -- true
135 test $? = 127 || fail=1
136 ln -s "simple_echo" ./-- || framework_failure_
137 case $(env a=b -- true || echo fail) in
138 *true) ;;
139 *) fail=1 ;;
140 esac
142 # No way to directly invoke program name containing =.
143 cat <<EOF >./c=d || framework_failure_
144 #!/bin/sh
145 echo pass
147 chmod +x c=d || framework_failure_
148 test "x$(env c=d echo fail)" = xfail || fail=1
149 test "x$(env -- c=d echo fail)" = xfail || fail=1
150 test "x$(env ./c=d echo fail)" = xfail || fail=1
151 test "x$(env sh -c 'exec "$@"' sh c=d echo fail)" = xpass || fail=1
152 test "x$(sh -c '\c=d echo fail')" = xpass && #dash 0.5.4 fails so check first
153 { test "x$(env sh -c '\c=d echo fail')" = xpass || fail=1; }
155 # catch unsetenv failure, broken through coreutils 8.0
156 returns_ 125 env -u a=b true || fail=1
157 returns_ 125 env -u '' true || fail=1
159 Exit $fail