tests: factor 350 fail=0 initializations into test-lib.sh
[platform/upstream/coreutils.git] / tests / misc / printenv
1 #!/bin/sh
2 # Verify behavior of printenv.
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 if test "$VERBOSE" = yes; then
20   set -x
21   env -- printenv --version
22 fi
23
24 . $srcdir/test-lib.sh
25
26
27 # Without arguments, printenv behaves like env.  Some shells provide
28 # printenv as a builtin, so we must test via absolute name.
29 # But beware of $_, set by many shells to the last command run.
30 env | grep -v '^_' > exp || framework_failure
31 env -- printenv | grep -v '^_' > out || fail=1
32 compare exp out || fail=1
33
34 # POSIX is clear that environ may, but need not be, sorted.
35 # Environment variable values may contain newlines, which cannot be
36 # observed by merely inspecting output from printenv.
37 if env -- printenv | grep '^ENV_TEST' >/dev/null ; then
38   skip_test_ "environment has potential interference from ENV_TEST*"
39 fi
40
41 # Printing a single variable's value.
42 env -- printenv ENV_TEST > out
43 test $? = 1 || fail=1
44 test -s out && fail=1
45 echo a > exp || framework_failure
46 ENV_TEST=a env -- printenv ENV_TEST > out || fail=1
47 compare exp out || fail=1
48
49 # Printing multiple variables.  Order follows command line.
50 ENV_TEST1=a ENV_TEST2=b env -- printenv ENV_TEST2 ENV_TEST1 ENV_TEST2 > out \
51   || fail=1
52 ENV_TEST1=a ENV_TEST2=b env -- printenv ENV_TEST1 ENV_TEST2 >> out || fail=1
53 cat <<EOF > exp || framework_failure
54 b
55 a
56 b
57 a
58 b
59 EOF
60 compare exp out || fail=1
61
62 # Exit status reflects missing variable, but remaining arguments processed.
63 ENV_TEST1=a env -- printenv ENV_TEST2 ENV_TEST1 > out
64 test $? = 1 || fail=1
65 ENV_TEST1=a env -- printenv ENV_TEST1 ENV_TEST2 >> out
66 test $? = 1 || fail=1
67 cat <<EOF > exp || framework_failure
68 a
69 a
70 EOF
71 compare exp out || fail=1
72
73 # Non-standard environment variable name.  Shells won't create it, but
74 # env can, and printenv must be able to deal with it.
75 echo b > exp || framework_failure
76 env -- -a=b printenv -- -a > out || fail=1
77 compare exp out || fail=1
78
79 # Silently reject invalid env-var names.
80 # Bug present through coreutils 8.0.
81 env a=b=c printenv a=b > out
82 test $? = 1 || fail=1
83 test -s out && fail=1
84
85 Exit $fail