Source-Changes-HG archive

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

[src/trunk]: src/tests/bin/sh Added more test cases, more exhaustive testing....



details:   https://anonhg.NetBSD.org/src/rev/03d273a895bd
branches:  trunk
changeset: 343996:03d273a895bd
user:      christos <christos%NetBSD.org@localhost>
date:      Tue Mar 08 14:26:54 2016 +0000

description:
Added more test cases, more exhaustive testing. (from kre)

diffstat:

 tests/bin/sh/t_expand.sh |  157 ++++++++++++++---
 tests/bin/sh/t_fsplit.sh |  238 ++++++++++++++++++++++---
 tests/bin/sh/t_redir.sh  |  426 ++++++++++++++++++++++++++++++++++++++++++++++-
 3 files changed, 747 insertions(+), 74 deletions(-)

diffs (truncated from 1011 to 300 lines):

diff -r f2ca8886be23 -r 03d273a895bd tests/bin/sh/t_expand.sh
--- a/tests/bin/sh/t_expand.sh  Tue Mar 08 14:26:34 2016 +0000
+++ b/tests/bin/sh/t_expand.sh  Tue Mar 08 14:26:54 2016 +0000
@@ -1,4 +1,4 @@
-# $NetBSD: t_expand.sh,v 1.5 2016/02/22 20:02:29 christos Exp $
+# $NetBSD: t_expand.sh,v 1.6 2016/03/08 14:26:54 christos Exp $
 #
 # Copyright (c) 2007, 2009 The NetBSD Foundation, Inc.
 # All rights reserved.
@@ -24,6 +24,8 @@
 # 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"}
 
 #
 # This file tests the functions in expand.c.
@@ -50,19 +52,15 @@
 }
 dollar_at_body() {
        # This one should work everywhere.
-       got=`echo "" "" | sed 's,$,EOL,'`
-       atf_check_equal ' EOL' '$got'
+       atf_check -s exit:0 -o inline:' EOL\n' -e empty \
+               ${TEST_SH} -c 'echo "" "" | '" sed 's,\$,EOL,'"
 
        # This code triggered the bug.
-       set -- "" ""
-       got=`echo "$@" | sed 's,$,EOL,'`
-       atf_check_equal ' EOL' '$got'
+       atf_check -s exit:0 -o inline:' EOL\n' -e empty \
+               ${TEST_SH} -c 'set -- "" ""; echo "$@" | '" sed 's,\$,EOL,'"
 
-       set -- -
-       shift
-       n_arg() { echo $#; }
-       n_args=`n_arg "$@"`
-       atf_check_equal '0' '$n_args'
+       atf_check -s exit:0 -o inline:'0\n' -e empty ${TEST_SH} -c \
+               'set -- -; shift; n_arg() { echo $#; }; n_arg "$@"'
 }
 
 atf_test_case dollar_at_with_text
@@ -71,27 +69,91 @@
                        "within the quotes.  PR bin/33956."
 }
 dollar_at_with_text_body() {
-       set --
-       atf_check_equal '' "$(delim_argv "$@")"
-       atf_check_equal '>foobar<' "$(delim_argv "foo$@bar")"
-       atf_check_equal '>foo  bar<' "$(delim_argv "foo $@ bar")"
+
+       cat <<'EOF' > h-f1
+
+delim_argv() {
+       str=
+       while [ $# -gt 0 ]; do
+               if [ -z "${str}" ]; then
+                       str=">$1<"
+               else
+                       str="${str} >$1<"
+               fi
+               shift
+       done
+       echo "${str}"
+}
+
+EOF
+       cat <<'EOF' > h-f2
+
+delim_argv() {
+       str=
+       while [ $# -gt 0 ]; do
+
+               str="${str}${str:+ }>$1<"
+               shift
 
-       set -- a b c
-       atf_check_equal '>a< >b< >c<' "$(delim_argv "$@")"
-       atf_check_equal '>fooa< >b< >cbar<' "$(delim_argv "foo$@bar")"
-       atf_check_equal '>foo a< >b< >c bar<' "$(delim_argv "foo $@ bar")"
+       done
+       echo "${str}"
+}
+
+EOF
+
+       chmod +x h-f1 h-f2
+
+       for f in 1 2
+       do
+               atf_check -s exit:0 -o inline:'\n' -e empty ${TEST_SH} -c \
+                       ". ./h-f${f}; "'set -- ; delim_argv "$@"'
+               atf_check -s exit:0 -o inline:'>foobar<\n' -e empty \
+                       ${TEST_SH} -c \
+                       ". ./h-f${f}; "'set -- ; delim_argv "foo$@bar"'
+               atf_check -s exit:0 -o inline:'>foo  bar<\n' -e empty \
+                       ${TEST_SH} -c \
+                       ". ./h-f${f}; "'set -- ; delim_argv "foo $@ bar"'
+
+               atf_check -s exit:0 -o inline:'>a< >b< >c<\n' -e empty \
+                       ${TEST_SH} -c \
+                       ". ./h-f${f}; "'set -- a b c; delim_argv "$@"'
+               atf_check -s exit:0 -o inline:'>fooa< >b< >cbar<\n' -e empty \
+                       ${TEST_SH} -c \
+                       ". ./h-f${f}; "'set -- a b c; delim_argv "foo$@bar"'
+               atf_check -s exit:0 -o inline:'>foo a< >b< >c bar<\n' -e empty \
+                       ${TEST_SH} -c \
+                       ". ./h-f${f}; "'set -- a b c; delim_argv "foo $@ bar"'
+       done
 }
 
 atf_test_case strip
 strip_head() {
        atf_set "descr" "Checks that the %% operator works and strips" \
                        "the contents of a variable from the given point" \
-                       "to the end (PR bin/43469)"
+                       "to the end"
 }
 strip_body() {
        line='#define bindir "/usr/bin" /* comment */'
        stripped='#define bindir "/usr/bin" '
-       atf_check_equal '$stripped' '${line%%/\**}'
+
+       # atf_expect_fail "PR bin/43469" -- now fixed
+       for exp in                              \
+               '${line%%/\**}'                 \
+               '${line%%"/*"*}'                \
+               '${line%%'"'"'/*'"'"'*}'        \
+               '"${line%%/\**}"'               \
+               '"${line%%"/*"*}"'              \
+               '"${line%%'"'"'/*'"'"'*}"'      \
+               '${line%/\**}'                  \
+               '${line%"/*"*}'                 \
+               '${line%'"'"'/*'"'"'*}'         \
+               '"${line%/\**}"'                \
+               '"${line%"/*"*}"'               \
+               '"${line%'"'"'/*'"'"'*}"'
+       do
+               atf_check -o inline:":$stripped:\n" -e empty ${TEST_SH} -c \
+                       "line='${line}'; echo :${exp}:"
+       done
 }
 
 atf_test_case varpattern_backslashes
@@ -102,7 +164,8 @@
 varpattern_backslashes_body() {
        line='/foo/bar/*/baz'
        stripped='/foo/bar/'
-       atf_check_equal $stripped ${line%%\**}
+       atf_check -o inline:'/foo/bar/\n' -e empty ${TEST_SH} -c \
+               'line="/foo/bar/*/baz"; echo ${line%%\**}'
 }
 
 atf_test_case arithmetic
@@ -113,9 +176,13 @@
                        "this is true."
 }
 arithmetic_body() {
-       atf_check_equal '3' '$((1 + 2))'
-       atf_check_equal '2147483647' '$((0x7fffffff))'
-       atf_check_equal '9223372036854775807' '$(((1 << 63) - 1))'
+
+       atf_check -o inline:'3' -e empty ${TEST_SH} -c \
+               'printf %s $((1 + 2))'
+       atf_check -o inline:'2147483647' -e empty ${TEST_SH} -c \
+               'printf %s $((0x7fffffff))'
+       atf_check -o inline:'9223372036854775807' -e empty ${TEST_SH} -c \
+               'printf %s $(((1 << 63) - 1))'
 }
 
 atf_test_case iteration_on_null_parameter
@@ -125,10 +192,39 @@
                        "PR bin/48202."
 }
 iteration_on_null_parameter_body() {
-       s1=`/bin/sh -uc 'N=; set -- ${N};   for X; do echo "[$X]"; done' 2>&1`
-       s2=`/bin/sh -uc 'N=; set -- ${N:-}; for X; do echo "[$X]"; done' 2>&1`
-       atf_check_equal ''   '$s1'
-       atf_check_equal '[]' '$s2'
+       atf_check -o empty -e empty ${TEST_SH} -c \
+               'N=; set -- ${N};   for X; do echo "[$X]"; done'
+}
+
+atf_test_case iteration_on_quoted_null_parameter
+iteration_on_quoted_null_parameter_head() {
+       atf_set "descr" \
+               'Check iteration of "$@" in for loop when set to null;'
+}
+iteration_on_quoted_null_parameter_body() {
+       atf_check -o inline:'[]\n' -e empty ${TEST_SH} -c \
+               'N=; set -- "${N}"; for X; do echo "[$X]"; done'
+}
+
+atf_test_case iteration_on_null_or_null_parameter
+iteration_on_null_or_null_parameter_head() {
+       atf_set "descr" \
+               'Check expansion of null parameter as default for another null'
+}
+iteration_on_null_or_null_parameter_body() {
+       atf_check -o empty -e empty ${TEST_SH} -c \
+               'N=; E=; set -- ${N:-${E}}; for X; do echo "[$X]"; done'
+}
+
+atf_test_case iteration_on_null_or_missing_parameter
+iteration_on_null_or_missing_parameter_head() {
+       atf_set "descr" \
+           'Check expansion of missing parameter as default for another null'
+}
+iteration_on_null_or_missing_parameter_body() {
+       # atf_expect_fail 'PR bin/50834'
+       atf_check -o empty -e empty ${TEST_SH} -c \
+               'N=; set -- ${N:-}; for X; do echo "[$X]"; done'
 }
 
 atf_init_test_cases() {
@@ -138,4 +234,7 @@
        atf_add_test_case varpattern_backslashes
        atf_add_test_case arithmetic
        atf_add_test_case iteration_on_null_parameter
+       atf_add_test_case iteration_on_quoted_null_parameter
+       atf_add_test_case iteration_on_null_or_null_parameter
+       atf_add_test_case iteration_on_null_or_missing_parameter
 }
diff -r f2ca8886be23 -r 03d273a895bd tests/bin/sh/t_fsplit.sh
--- a/tests/bin/sh/t_fsplit.sh  Tue Mar 08 14:26:34 2016 +0000
+++ b/tests/bin/sh/t_fsplit.sh  Tue Mar 08 14:26:54 2016 +0000
@@ -1,6 +1,6 @@
-# $NetBSD: t_fsplit.sh,v 1.1 2012/03/17 16:33:11 jruoho Exp $
+# $NetBSD: t_fsplit.sh,v 1.2 2016/03/08 14:26:54 christos Exp $
 #
-# Copyright (c) 2007 The NetBSD Foundation, Inc.
+# Copyright (c) 2007-2016 The NetBSD Foundation, Inc.
 # All rights reserved.
 #
 # Redistribution and use in source and binary forms, with or without
@@ -33,6 +33,7 @@
 # the "${x-" and "}" were absent from the input line.
 #
 # So: sh -c 'set ${x-a b c}; echo $#' should give 3.
+# and: sh -c 'set -- ${x-}' echo $#' shold give 0
 #
 
 nl='
@@ -40,15 +41,40 @@
 
 check()
 {
-       result="$(eval $1)"
+       TEST=$((${TEST} + 1))
+
+       case "$#" in
+       (2)     ;;
+       (*)     atf_fail "Internal test error, $# args to check test ${TEST}";;
+       esac
+
+       result=$( ${TEST_SH} -c "unset x; $1" )
+       STATUS="$?"
+
        # Remove newlines
        oifs="$IFS"
        IFS="$nl"
        result="$(echo $result)"
        IFS="$oifs"
+
+       # trim the test text in case we use it in a message below
+       case "$1" in
+       ????????????????*)
+               set -- "$(expr "$1" : '\(............\).*')..." "$2" ;;
+       esac
+
        if [ "$2" != "$result" ]
        then
-               atf_fail "expected [$2], found [$result]"
+               if [ "${STATUS}" = "0" ]
+               then
+                 atf_fail "Test ${TEST} '$1': expected [$2], found [$result]"
+               else
+                 atf_fail \
+         "TEST ${TEST} '$1' failed ($STATUS): expected [$2], found [$result]"
+               fi
+       elif [ "${STATUS}" != 0 ]
+       then
+                 atf_fail "TEST ${TEST} '$1' failed ($STATUS)"
        fi
 }
 
@@ -59,6 +85,7 @@
 for_body() {
        unset x
 
+       TEST=0
        # Since I managed to break this, leave the test in
        check 'for f in $x; do echo x${f}y; done' ''
 }
@@ -68,17 +95,121 @@
        atf_set "descr" "Checks field splitting in variable default values"
 }
 default_val_body() {



Home | Main Index | Thread Index | Old Index