tests: add test for locale decimal processing
[coreutils.git] / tests / misc / env.sh
blob5ae77ce92f3cf1bdbc9489d1ab1d264d9315ba4f
1 #!/bin/sh
2 # Verify behavior of env.
4 # Copyright (C) 2009-2019 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 <https://www.gnu.org/licenses/>.
20 . "${srcdir=.}/tests/init.sh"; path_prepend_ ./src
21 print_ver_ env pwd
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 returns_ 125 env --- || fail=1 # unknown option
47 returns_ 125 env -u || fail=1 # missing option argument
48 returns_ 2 env sh -c 'exit 2' || fail=1 # exit status propagation
49 returns_ 126 env . || fail=1 # invalid command
50 returns_ 127 env no_such || fail=1 # no such command
52 # POSIX is clear that environ may, but need not be, sorted.
53 # Environment variable values may contain newlines, which cannot be
54 # observed by merely inspecting output from env.
55 # Cygwin requires a minimal environment to launch new processes: execve
56 # adds missing variables SYSTEMROOT and WINDIR, which show up in a
57 # subsequent env. Cygwin also requires /bin to always be part of PATH,
58 # and attempts to unset or reduce PATH may cause execve to fail.
60 # For these reasons, it is more portable to grep that our desired changes
61 # took place, rather than comparing output of env over an entire environment.
62 if env | grep '^ENV_TEST' >/dev/null ; then
63 skip_ "environment has potential interference from ENV_TEST*"
66 ENV_TEST1=a
67 export ENV_TEST1
68 >out || framework_failure_
69 env ENV_TEST2= > all || fail=1
70 grep '^ENV_TEST' all | LC_ALL=C sort >> out || framework_failure_
71 env -u ENV_TEST1 ENV_TEST3=c > all || fail=1
72 grep '^ENV_TEST' all | LC_ALL=C sort >> out || framework_failure_
73 env ENV_TEST1=b > all || fail=1
74 grep '^ENV_TEST' all | LC_ALL=C sort >> out || framework_failure_
75 env ENV_TEST2= env > all || fail=1
76 grep '^ENV_TEST' all | LC_ALL=C sort >> out || framework_failure_
77 env -u ENV_TEST1 ENV_TEST3=c env > all || fail=1
78 grep '^ENV_TEST' all | LC_ALL=C sort >> out || framework_failure_
79 env ENV_TEST1=b env > all || fail=1
80 grep '^ENV_TEST' all | LC_ALL=C sort >> out || framework_failure_
81 cat <<EOF >exp || framework_failure_
82 ENV_TEST1=a
83 ENV_TEST2=
84 ENV_TEST3=c
85 ENV_TEST1=b
86 ENV_TEST1=a
87 ENV_TEST2=
88 ENV_TEST3=c
89 ENV_TEST1=b
90 EOF
91 compare exp out || fail=1
93 # PATH modifications affect exec.
94 mkdir unlikely_name || framework_failure_
95 cat <<EOF > unlikely_name/also_unlikely || framework_failure_
96 #!/bin/sh
97 echo pass
98 EOF
99 chmod +x unlikely_name/also_unlikely || framework_failure_
100 returns_ 127 env also_unlikely || fail=1
101 test x$(PATH=$PATH:unlikely_name env also_unlikely) = xpass || fail=1
102 test x$(env PATH="$PATH":unlikely_name also_unlikely) = xpass || fail=1
104 # Explicitly put . on the PATH for the rest of this test.
105 PATH=$PATH:
106 export PATH
108 # Use -- to end options (but not variable assignments).
109 # On some systems, execve("-i") invokes a shebang script ./-i on PATH as
110 # '/bin/sh -i', rather than '/bin/sh -- -i', which doesn't do what we want.
111 # Avoid the issue by using a shebang to 'echo' passing a second parameter
112 # before the '-i'. See the definition of simple_echo before.
113 # Test -u, rather than -i, to minimize PATH problems.
114 ln -s "simple_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
128 # After options have ended, the first argument not containing = is a program.
129 returns_ 127 env a=b -- true || fail=1
130 ln -s "simple_echo" ./-- || framework_failure_
131 case $(env a=b -- true || echo fail) in
132 *true) ;;
133 *) fail=1 ;;
134 esac
136 # No way to directly invoke program name containing =.
137 cat <<EOF >./c=d || framework_failure_
138 #!/bin/sh
139 echo pass
141 chmod +x c=d || framework_failure_
142 test "x$(env c=d echo fail)" = xfail || fail=1
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 sh -c 'exec "$@"' sh c=d echo fail)" = xpass || fail=1
146 test "x$(sh -c '\c=d echo fail')" = xpass && #dash 0.5.4 fails so check first
147 { test "x$(env sh -c '\c=d echo fail')" = xpass || fail=1; }
149 # catch unsetenv failure, broken through coreutils 8.0
150 returns_ 125 env -u a=b true || fail=1
151 returns_ 125 env -u '' true || fail=1
153 # Verify changing directory.
154 mkdir empty || framework_failure_
155 returns_ 125 env --chdir=empty/nonexistent true || fail=1
156 returns_ 125 env -C empty 2>out || fail=1
157 printf '%s\n' \
158 'env: must specify command with --chdir (-C)' \
159 "Try 'env --help' for more information." > exp ||
160 framework_failure_
161 compare exp out || fail=1
162 exp=$(cd empty && env pwd) || framework_failure_
163 got=$(env --chdir=empty pwd) || fail=1
164 test "$exp" = "$got" || fail=1
166 Exit $fail