Blame tests/misc/printenv.sh

Packit 9e4112
#!/bin/sh
Packit 9e4112
# Verify behavior of printenv.
Packit 9e4112
Packit 9e4112
# Copyright (C) 2009-2018 Free Software Foundation, Inc.
Packit 9e4112
Packit 9e4112
# This program is free software: you can redistribute it and/or modify
Packit 9e4112
# it under the terms of the GNU General Public License as published by
Packit 9e4112
# the Free Software Foundation, either version 3 of the License, or
Packit 9e4112
# (at your option) any later version.
Packit 9e4112
Packit 9e4112
# This program is distributed in the hope that it will be useful,
Packit 9e4112
# but WITHOUT ANY WARRANTY; without even the implied warranty of
Packit 9e4112
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
Packit 9e4112
# GNU General Public License for more details.
Packit 9e4112
Packit 9e4112
# You should have received a copy of the GNU General Public License
Packit 9e4112
# along with this program.  If not, see <https://www.gnu.org/licenses/>.
Packit 9e4112
Packit 9e4112
. "${srcdir=.}/tests/init.sh"; path_prepend_ ./src
Packit 9e4112
print_ver_ printenv
Packit 9e4112
Packit 9e4112
# Without arguments, printenv behaves like env.  Some shells provide
Packit 9e4112
# printenv as a builtin, so we must invoke it via "env".
Packit 9e4112
# But beware of $_, set by many shells to the last command run.
Packit 9e4112
# Also, filter out LD_PRELOAD, which is set when running under valgrind.
Packit 9e4112
# Note the apparently redundant "env  env": this is to ensure to get
Packit 9e4112
# env's output the same way as that of printenv and works around a bug
Packit 9e4112
# on aarch64 at least where libc's execvp reverses the order of the
Packit 9e4112
# output.
Packit 9e4112
env -- env | grep -Ev '^(_|LD_PRELOAD)=' > exp || framework_failure_
Packit 9e4112
env -- printenv | grep -Ev '^(_|LD_PRELOAD)=' > out || fail=1
Packit 9e4112
compare exp out || fail=1
Packit 9e4112
Packit 9e4112
# POSIX is clear that environ may, but need not be, sorted.
Packit 9e4112
# Environment variable values may contain newlines, which cannot be
Packit 9e4112
# observed by merely inspecting output from printenv.
Packit 9e4112
if env -- printenv | grep '^ENV_TEST' >/dev/null ; then
Packit 9e4112
  skip_ "environment has potential interference from ENV_TEST*"
Packit 9e4112
fi
Packit 9e4112
Packit 9e4112
# Printing a single variable's value.
Packit 9e4112
returns_ 1 env -- printenv ENV_TEST > out || fail=1
Packit 9e4112
compare /dev/null out || fail=1
Packit 9e4112
echo a > exp || framework_failure_
Packit 9e4112
ENV_TEST=a env -- printenv ENV_TEST > out || fail=1
Packit 9e4112
compare exp out || fail=1
Packit 9e4112
Packit 9e4112
# Printing multiple variables.  Order follows command line.
Packit 9e4112
ENV_TEST1=a ENV_TEST2=b env -- printenv ENV_TEST2 ENV_TEST1 ENV_TEST2 > out \
Packit 9e4112
  || fail=1
Packit 9e4112
ENV_TEST1=a ENV_TEST2=b env -- printenv ENV_TEST1 ENV_TEST2 >> out || fail=1
Packit 9e4112
cat <<EOF > exp || framework_failure_
Packit 9e4112
b
Packit 9e4112
a
Packit 9e4112
b
Packit 9e4112
a
Packit 9e4112
b
Packit 9e4112
EOF
Packit 9e4112
compare exp out || fail=1
Packit 9e4112
Packit 9e4112
# Exit status reflects missing variable, but remaining arguments processed.
Packit 9e4112
export ENV_TEST1=a
Packit 9e4112
returns_ 1 env -- printenv ENV_TEST2 ENV_TEST1 > out || fail=1
Packit 9e4112
returns_ 1 env -- printenv ENV_TEST1 ENV_TEST2 >> out || fail=1
Packit 9e4112
unset ENV_TEST1
Packit 9e4112
cat <<EOF > exp || framework_failure_
Packit 9e4112
a
Packit 9e4112
a
Packit 9e4112
EOF
Packit 9e4112
compare exp out || fail=1
Packit 9e4112
Packit 9e4112
# Non-standard environment variable name.  Shells won't create it, but
Packit 9e4112
# env can, and printenv must be able to deal with it.
Packit 9e4112
echo b > exp || framework_failure_
Packit 9e4112
env -- -a=b printenv -- -a > out || fail=1
Packit 9e4112
compare exp out || fail=1
Packit 9e4112
Packit 9e4112
# Silently reject invalid env-var names.
Packit 9e4112
# Bug present through coreutils 8.0.
Packit 9e4112
returns_ 1 env a=b=c printenv a=b > out || fail=1
Packit 9e4112
compare /dev/null out || fail=1
Packit 9e4112
Packit 9e4112
Exit $fail