summaryrefslogtreecommitdiff
path: root/tests/misc/env
blob: ec9f0af1e6ec68f949c4e48a4e41b4da322a7fce (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
#!/bin/sh
# Verify behavior of env.

# Copyright (C) 2009-2012 Free Software Foundation, Inc.

# This program is free software: you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation, either version 3 of the License, or
# (at your option) any later version.

# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
# GNU General Public License for more details.

# You should have received a copy of the GNU General Public License
# along with this program.  If not, see <http://www.gnu.org/licenses/>.


. "${srcdir=.}/init.sh"; path_prepend_ ../src
print_ver_ env


# Verify clearing the environment
a=1
export a
env - > out || fail=1
test -s out && fail=1
env -i > out || fail=1
test -s out && fail=1
env -u a -i -u a -- > out || fail=1
test -s out && fail=1
env -i -- a=b > out || fail=1
echo a=b > exp || framework_failure_
compare exp out || fail=1

# These tests verify exact status of internal failure.
env --- # unknown option
test $? = 125 || fail=1
env -u # missing option argument
test $? = 125 || fail=1
env sh -c 'exit 2' # exit status propagation
test $? = 2 || fail=2
env . # invalid command
test $? = 126 || fail=1
env no_such # no such command
test $? = 127 || fail=1

# POSIX is clear that environ may, but need not be, sorted.
# Environment variable values may contain newlines, which cannot be
# observed by merely inspecting output from env.
# Cygwin requires a minimal environment to launch new processes: execve
# adds missing variables SYSTEMROOT and WINDIR, which show up in a
# subsequent env.  Cygwin also requires /bin to always be part of PATH,
# and attempts to unset or reduce PATH may cause execve to fail.
#
# For these reasons, it is more portable to grep that our desired changes
# took place, rather than comparing output of env over an entire environment.
if env | grep '^ENV_TEST' >/dev/null ; then
  skip_ "environment has potential interference from ENV_TEST*"
fi

ENV_TEST1=a
export ENV_TEST1
: >out || framework_failure_
env ENV_TEST2= > all || fail=1
grep '^ENV_TEST' all | LC_ALL=C sort >> out || framework_failure_
env -u ENV_TEST1 ENV_TEST3=c > all || fail=1
grep '^ENV_TEST' all | LC_ALL=C sort >> out || framework_failure_
env ENV_TEST1=b > all || fail=1
grep '^ENV_TEST' all | LC_ALL=C sort >> out || framework_failure_
env ENV_TEST2= env > all || fail=1
grep '^ENV_TEST' all | LC_ALL=C sort >> out || framework_failure_
env -u ENV_TEST1 ENV_TEST3=c env > all || fail=1
grep '^ENV_TEST' all | LC_ALL=C sort >> out || framework_failure_
env ENV_TEST1=b env > all || fail=1
grep '^ENV_TEST' all | LC_ALL=C sort >> out || framework_failure_
cat <<EOF >exp || framework_failure_
ENV_TEST1=a
ENV_TEST2=
ENV_TEST3=c
ENV_TEST1=b
ENV_TEST1=a
ENV_TEST2=
ENV_TEST3=c
ENV_TEST1=b
EOF
compare exp out || fail=1

# PATH modifications affect exec.
mkdir unlikely_name || framework_failure_
cat <<EOF > unlikely_name/also_unlikely || framework_failure_
#!/bin/sh
echo pass
EOF
chmod +x unlikely_name/also_unlikely || framework_failure_
env also_unlikely && fail=1
test x`PATH=$PATH:unlikely_name env also_unlikely` = xpass || fail=1
test x`env PATH="$PATH":unlikely_name also_unlikely` = xpass || fail=1

# Explicitly put . on the PATH for the rest of this test.
PATH=$PATH:
export PATH

# Use -- to end options (but not variable assignments).
# On some systems, execve("-i") invokes a shebang script ./-i on PATH as
# '/bin/sh -i', rather than '/bin/sh -- -i', which doesn't do what we want.
# Avoid the issue by using an executable rather than a script.
# Test -u, rather than -i, to minimize PATH problems.
ln -s "$abs_top_builddir/src/echo" ./-u || framework_failure_
case `env -u echo echo good` in
  good) ;;
  *) fail=1 ;;
esac
case `env -u echo -- echo good` in
  good) ;;
  *) fail=1 ;;
esac
case `env -- -u pass` in
  pass) ;;
  *) fail=1 ;;
esac

# After options have ended, the first argument not containing = is a program.
env a=b -- true
test $? = 127 || fail=1
ln -s "$abs_top_builddir/src/echo" ./-- || framework_failure_
case `env a=b -- true || echo fail` in
  true) ;;
  *) fail=1 ;;
esac

# No way to directly invoke program name containing =.
cat <<EOF >./c=d || framework_failure_
#!/bin/sh
echo pass
EOF
chmod +x c=d || framework_failure_
test "x`env c=d echo fail`" = xfail || fail=1
test "x`env -- c=d echo fail`" = xfail || fail=1
test "x`env ./c=d echo fail`" = xfail || fail=1
test "x$(env sh -c 'exec "$@"' sh c=d echo fail)" = xpass || fail=1
test "x$(sh -c '\c=d echo fail')" = xpass && #dash 0.5.4 fails so check first
  { test "x$(env sh -c '\c=d echo fail')" = xpass || fail=1; }

# catch unsetenv failure, broken through coreutils 8.0
env -u a=b true && fail=1
test $? = 125 || fail=1
env -u '' true && fail=1
test $? = 125 || fail=1

Exit $fail