Source-Changes-HG archive

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index][Old Index]

[src/trunk]: src/tests/bin/sh Add t_option, from kre (with minor edits from me)



details:   https://anonhg.NetBSD.org/src/rev/b24e4f953813
branches:  trunk
changeset: 343726:b24e4f953813
user:      christos <christos%NetBSD.org@localhost>
date:      Tue Feb 23 16:20:42 2016 +0000

description:
Add t_option, from kre (with minor edits from me)

diffstat:

 tests/bin/sh/Makefile    |    3 +-
 tests/bin/sh/t_option.sh |  602 +++++++++++++++++++++++++++++++++++++++++++++++
 2 files changed, 604 insertions(+), 1 deletions(-)

diffs (truncated from 623 to 300 lines):

diff -r c6e6564e78dd -r b24e4f953813 tests/bin/sh/Makefile
--- a/tests/bin/sh/Makefile     Tue Feb 23 15:22:14 2016 +0000
+++ b/tests/bin/sh/Makefile     Tue Feb 23 16:20:42 2016 +0000
@@ -1,4 +1,4 @@
-# $NetBSD: Makefile,v 1.5 2016/02/22 19:52:03 christos Exp $
+# $NetBSD: Makefile,v 1.6 2016/02/23 16:20:42 christos Exp $
 #
 
 .include <bsd.own.mk>
@@ -13,6 +13,7 @@
 TESTS_SH+=     t_evaltested
 TESTS_SH+=     t_fsplit
 TESTS_SH+=     t_here
+TESTS_SH+=     t_option
 TESTS_SH+=     t_redir
 TESTS_SH+=     t_set_e
 TESTS_SH+=     t_ulimit
diff -r c6e6564e78dd -r b24e4f953813 tests/bin/sh/t_option.sh
--- /dev/null   Thu Jan 01 00:00:00 1970 +0000
+++ b/tests/bin/sh/t_option.sh  Tue Feb 23 16:20:42 2016 +0000
@@ -0,0 +1,602 @@
+# $NetBSD: t_option.sh,v 1.1 2016/02/23 16:20:42 christos Exp $
+#
+# Copyright (c) 2016 The NetBSD Foundation, Inc.
+# All rights reserved.
+#
+# Redistribution and use in source and binary forms, with or without
+# modification, are permitted provided that the following conditions
+# are met:
+# 1. Redistributions of source code must retain the above copyright
+#    notice, this list of conditions and the following disclaimer.
+# 2. Redistributions in binary form must reproduce the above copyright
+#    notice, this list of conditions and the following disclaimer in the
+#    documentation and/or other materials provided with the distribution.
+#
+# THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
+# ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
+# TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
+# PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
+# BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
+# CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
+# SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
+# INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
+# CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
+# ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
+# POSSIBILITY OF SUCH DAMAGE.
+#
+# the implementation of "sh" to test
+: ${TEST_SH:="/bin/sh"}
+
+# The standard
+# http://pubs.opengroup.org/onlinepubs/9699919799/utilities/V3_chap02.html
+# says:
+#      ...[lots]
+
+test_option_on_off()
+{
+       atf_require_prog tr
+
+       for opt
+       do
+                               # t is needed, as inside $()` $- appears to lose
+                               # the 'e' option if it happened to already be
+                               # set.  Must check if that is what should
+                               # happen, but that is a different issue.
+
+               test -z "${opt}" && continue
+
+               # if we are playing with more that one option at a
+               # time, the code below requires that we start with no
+               # options set, or it will mis-diagnose the situation
+               CLEAR=''
+               test "${#opt}" -gt 1 &&
+  CLEAR='xx="$-" && xx=$(echo "$xx" | tr -d cs) && test -n "$xx" && set +"$xx";'
+
+               atf_check -s exit:0 -o empty -e empty "${TEST_SH}" -c \
+                       "opt=${opt}"'
+                       x() {
+                               echo "ERROR: Unable to $1 option $2" >&2
+                               exit 1
+                       }
+                       s() {
+                               set -"$1"
+                               t="$-"
+                               x=$(echo "$t" | tr -d "$1")
+                               test "$t" = "$x" && x set "$1"
+                               return 0
+                       }
+                       c() {
+                               set +"$1"
+                               t="$-"
+                               x=$(echo "$t" | tr -d "$1")
+                               test "$t" != "$x" && x clear "$1"
+                               return 0
+                       }
+                       '"${CLEAR}"'
+
+                       # if we do not do this, -x tracing splatters stderr
+                       # for some shells, -v does as well (is that correct?)
+                       case "${opt}" in
+                       (*[xv]*)        exec 2>/dev/null;;
+                       esac
+
+                       o="$-"
+                       x=$(echo "$o" | tr -d "$opt")
+
+                       if [ "$o" = "$x" ]; then        # option was off
+                               s "${opt}"
+                               c "${opt}"
+                       else
+                               c "${opt}"
+                               s "${opt}"
+                       fi
+               '
+       done
+}
+
+test_optional_on_off()
+{
+       RET=0
+       OPTS=
+       for opt
+       do
+               test "${opt}" = n && continue
+               "${TEST_SH}" -c "set -${opt}" 2>/dev/null  &&
+                       OPTS="${OPTS} ${opt}" || RET=1
+       done
+
+       test -n "${OPTS}" && test_option_on_off ${OPTS}
+
+       return "${RET}"
+}
+
+atf_test_case set_a
+set_a_head() {
+       atf_set "descr" "Tests that 'set -a' turns on all var export " \
+                       "and that it behaves as defined by the standard"
+}
+set_a_body() {
+       atf_require_prog env
+       atf_require_prog grep
+
+       test_option_on_off a
+
+       # without -a, new variables should not be exported (so grep "fails")
+       atf_check -s exit:1 -o empty -e empty "${TEST_SH}" -ce \
+               'unset VAR; set +a; VAR=value; env | grep "^VAR="'
+
+       # with -a, they should be
+       atf_check -s exit:0 -o match:VAR=value -e empty "${TEST_SH}" -ce \
+               'unset VAR; set -a; VAR=value; env | grep "^VAR="'
+}
+
+atf_test_case set_C
+set_C_head() {
+       atf_set "descr" "Tests that 'set -C' turns on no clobber mode " \
+                       "and that it behaves as defined by the standard"
+}
+set_C_body() {
+       atf_require_prog ls
+
+       test_option_on_off C
+
+       # Check that the environment to use for the tests is sane ...
+       # we assume current dir is a new tempory directory & is empty
+
+       test -z "$(ls)" || atf_skip "Test execution directory not clean"
+       test -c "/dev/null" || atf_skip "Problem with /dev/null"
+
+       echo Dummy_Content > Junk_File
+       echo Precious_Content > Important_File
+
+       # Check that we can redirect onto file when -C is not set
+       atf_check -s exit:0 -o empty -e empty "${TEST_SH}" -c \
+               '
+               D=$(ls -l Junk_File) || exit 1
+               set +C
+               echo "Overwrite it now" > Junk_File
+               A=$(ls -l Junk_File) || exit 1
+               test "${A}" != "${D}"
+               '
+
+       # Check that we cannot redirect onto file when -C is set
+       atf_check -s exit:0 -o empty -e not-empty "${TEST_SH}" -c \
+               '
+               D=$(ls -l Important_File) || exit 1
+               set -C
+               echo "Fail to Overwrite it now" > Important_File
+               A=$(ls -l Important_File) || exit 1
+               test "${A}" = "${D}"
+               '
+
+       # Check that we can append to file, even when -C is set
+       atf_check -s exit:0 -o empty -e empty "${TEST_SH}" -c \
+               '
+               D=$(ls -l Junk_File) || exit 1
+               set -C
+               echo "Append to it now" >> Junk_File
+               A=$(ls -l Junk_File) || exit 1
+               test "${A}" != "${D}"
+               '
+
+       # Check that we abort on attempt to redirect onto file when -Ce is set
+       atf_check -s not-exit:0 -o empty -e not-empty "${TEST_SH}" -c \
+               '
+               set -Ce
+               echo "Fail to Overwrite it now" > Important_File
+               echo "Should not reach this point"
+               '
+
+       # Last check that we can override -C for when we really need to
+       atf_check -s exit:0 -o empty -e empty "${TEST_SH}" -c \
+               '
+               D=$(ls -l Junk_File) || exit 1
+               set -C
+               echo "Change the poor bugger again" >| Junk_File
+               A=$(ls -l Junk_File) || exit 1
+               test "${A}" != "${D}"
+               '
+}
+
+atf_test_case set_e
+set_e_head() {
+       atf_set "descr" "Tests that 'set -e' turns on error detection " \
+               "and that a simple case behaves as defined by the standard"
+}
+set_e_body() {
+       test_option_on_off e
+
+       # Check that -e does nothing if no commands fail
+       atf_check -s exit:0 -o match:I_am_OK -e empty \
+           "${TEST_SH}" -c \
+               'false; printf "%s" I_am; set -e; true; printf "%s\n" _OK'
+
+       # and that it (silently, but with exit status) aborts if cmd fails
+       atf_check -s not-exit:0 -o match:I_am -o not-match:Broken -e empty \
+           "${TEST_SH}" -c \
+               'false; printf "%s" I_am; set -e; false; printf "%s\n" _Broken'
+
+       # same, except -e this time is on from the beginning
+       atf_check -s not-exit:0 -o match:I_am -o not-match:Broken -e empty \
+           "${TEST_SH}" -ec 'printf "%s" I_am; false; printf "%s\n" _Broken'
+
+       # More checking of -e in other places, there is lots to deal with.
+}
+
+atf_test_case set_f
+set_f_head() {
+       atf_set "descr" "Tests that 'set -f' turns off pathname expansion " \
+                       "and that it behaves as defined by the standard"
+}
+set_f_body() {
+       atf_require_prog ls
+
+       test_option_on_off f
+
+       # Check that the environment to use for the tests is sane ...
+       # we assume current dir is a new tempory directory & is empty
+
+       test -z "$(ls)" || atf_skip "Test execution directory not clean"
+
+       # we will assume that atf will clean up this junk directory
+       # when we are done.   But for testing pathname expansion
+       # we need files
+
+       for f in a b c d e f aa ab ac ad ae aaa aab aac aad aba abc bbb ccc
+       do
+               echo "$f" > "$f"
+       done
+
+       atf_check -s exit:0 -o empty -e empty "${TEST_SH}" -ec \
+           'X=$(echo b*); Y=$(echo b*); test "${X}" != "a*";
+               test "${X}" = "${Y}"'
+
+       # now test expansion is different when -f is set
+       atf_check -s exit:0 -o empty -e empty "${TEST_SH}" -ec \
+          'X=$(echo b*); Y=$(set -f; echo b*); test "${X}" != "${Y}"'
+}
+
+atf_test_case set_n
+set_n_head() {
+       atf_set "descr" "Tests that 'set -n' supresses command execution " \
+                       "and that it behaves as defined by the standard"
+}
+set_n_body() {
+       # pointless to test this, if it turns on, it stays on...
+       # test_option_on_off n
+       # so just allow the tests below to verify it can be turned on
+
+       # nothing should be executed, hence no output...
+       atf_check -s exit:0 -o empty -e empty \
+               "${TEST_SH}" -enc 'echo ABANDON HOPE; echo ALL YE; echo ...'
+
+       # this is true even when the "commands" do not exist
+       atf_check -s exit:0 -o empty -e empty \
+               "${TEST_SH}" -enc 'ERR; FAIL; ABANDON HOPE'
+
+       # but if there is a syntax error, it should be detected
+       atf_check -s not-exit:0 -o empty -e not-empty \
+               "${TEST_SH}" -enc 'echo JUMP; for frogs swim; echo in puddles'



Home | Main Index | Thread Index | Old Index