Source-Changes-HG archive

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

[src/trunk]: src/tests/bin/sh PR bin/50993 - lots of new here document tests ...



details:   https://anonhg.NetBSD.org/src/rev/ffb14b5f7e83
branches:  trunk
changeset: 344418:ffb14b5f7e83
user:      christos <christos%NetBSD.org@localhost>
date:      Sun Mar 27 14:52:40 2016 +0000

description:
PR bin/50993 - lots of new here document tests to validate all of
the changes made to fix that PR.   LOTS more tests...  A few general
improvements to the way the tests work and results are reported
as well. (from kre@)

diffstat:

 tests/bin/sh/t_here.sh |  331 +++++++++++++++++++++++++++++++++++++++++++++++-
 1 files changed, 318 insertions(+), 13 deletions(-)

diffs (truncated from 448 to 300 lines):

diff -r 941738e66927 -r ffb14b5f7e83 tests/bin/sh/t_here.sh
--- a/tests/bin/sh/t_here.sh    Sun Mar 27 14:50:40 2016 +0000
+++ b/tests/bin/sh/t_here.sh    Sun Mar 27 14:52:40 2016 +0000
@@ -1,4 +1,4 @@
-# $NetBSD: t_here.sh,v 1.4 2016/03/08 14:21:02 christos Exp $
+# $NetBSD: t_here.sh,v 1.5 2016/03/27 14:52:40 christos Exp $
 #
 # Copyright (c) 2007 The NetBSD Foundation, Inc.
 # All rights reserved.
@@ -30,21 +30,29 @@
 nl='
 '
 
+reset()
+{
+       TEST_NUM=0
+       TEST_FAILURES=''
+       TEST_FAIL_COUNT=0
+       TEST_ID="$1"
+}
+
 check()
 {
        fail=false
        TEMP_FILE=$( mktemp OUT.XXXXXX )
+       TEST_NUM=$(( $TEST_NUM + 1 ))
 
        # our local shell (ATF_SHELL) better do quoting correctly...
        # some of the tests expect us to expand $nl internally...
        CMD="nl='${nl}'; $1"
 
-       rm -f trace.*
        result="$( ${TEST_SH} -c "${CMD}" 2>"${TEMP_FILE}" )"
        STATUS=$?
 
        if [ "${STATUS}" -ne "$3" ]; then
-               echo >&2 "expected exit code $3, got ${STATUS}"
+               echo >&2 "[$TEST_NUM] expected exit code $3, got ${STATUS}"
 
                # don't actually fail just because of wrong exit code
                # unless we either expected, or received "good"
@@ -55,13 +63,15 @@
 
        if [ "$3" -eq 0 ]; then
                if [ -s "${TEMP_FILE}" ]; then
-                       echo >&2 "Messages produced on stderr unexpected..."
+                       echo >&2 \
+                        "[$TEST_NUM] Messages produced on stderr unexpected..."
                        cat "${TEMP_FILE}" >&2
                        fail=true
                fi
        else
                if ! [ -s "${TEMP_FILE}" ]; then
-                       echo >&2 "Expected messages on stderr, nothing produced"
+                       echo >&2 \
+                   "[$TEST_NUM] Expected messages on stderr, nothing produced"
                        fail=true
                fi
        fi
@@ -74,14 +84,33 @@
        IFS="$oifs"
        if [ "$2" != "$result" ]
        then
-               echo >&2 "Expected output '$2', received '$result'"
+               echo >&2 "[$TEST_NUM] Expected output '$2', received '$result'"
                fail=true
        fi
 
-       $fail && atf_fail "test of '$1' failed"
+       $fail && test -n "$TEST_ID" && {
+               TEST_FAILURES="${TEST_FAILURES}${TEST_FAILURES:+
+}${TEST_ID}[$TEST_NUM]: test of '$1' failed";
+               TEST_FAIL_COUNT=$(( $TEST_FAIL_COUNT + 1 ))
+               return 0
+       }
+       $fail && atf_fail "Test[$TEST_NUM] of '$1' failed"
        return 0
 }
 
+results()
+{
+       test -z "${TEST_ID}" && return 0
+       test -z "${TEST_FAILURES}" && return 0
+
+       echo >&2 "=========================================="
+       echo >&2 "While testing '${TEST_ID}'"
+       echo >&2 " - - - - - - - - - - - - - - - - -"
+       echo >&2 "${TEST_FAILURES}"
+       atf_fail \
+ "Test ${TEST_ID}: $TEST_FAIL_COUNT subtests (of $TEST_NUM) failed - see stderr"
+}
+
 atf_test_case do_simple
 do_simple_head() {
        atf_set "descr" "Basic tests for here documents"
@@ -89,7 +118,8 @@
 do_simple_body() {
        y=x
 
-       IFS=
+       reset 'simple'
+       IFS='   '
        check 'x=`cat <<EOF'$nl'text'${nl}EOF$nl'`; echo $x' 'text' 0
        check 'x=`cat <<\EOF'$nl'text'${nl}EOF$nl'`; echo $x' 'text' 0
 
@@ -131,6 +161,100 @@
                        'text' 0
        check 'x=`cat <<- '"'EOF'${nl}text${nl} EOF$nl"'`; echo $x' \
                        'text' 0
+       results
+}
+
+atf_test_case end_markers
+end_markers_head() {
+       atf_set "descr" "Tests for various end markers of here documents"
+}
+end_markers_body() {
+
+       reset 'end_markers'
+       for end in EOF 1 \! '$$$' "string " a\\\  '&' '' ' ' '  ' --STRING-- . '~~~' \
+VERYVERYVERYVERYLONGLONGLONGin_fact_absurdly_LONG_LONG_HERE_DOCUMENT_TERMINATING_MARKER_THAT_goes_On_forever_and_ever_and_ever...
+       do
+               # check unquoted end markers
+               case "${end}" in
+               ('' | *[' $&#*~']* ) ;; # skip unquoted endmark test for these
+               (*)     check \
+       'x=$(cat << '"${end}${nl}text${nl}${end}${nl}"'); echo "$x"' 'text' 0
+                       ;;
+               esac
+
+               # and quoted end markers
+               check \
+       'x=$(cat <<'"'${end}'${nl}text${nl}${end}${nl}"'); echo "$x"' 'text' 0
+
+               # and see what happens if we encounter "almost" an end marker
+               case "${#end}" in
+               (0|1)   ;;              # too short to try truncation tests
+               (*)     check \
+   'x=$(cat <<'"'${end}'${nl}text${nl}${end%?}${nl}${end}${nl}"'); echo "$x"' \
+                               "text ${end%?}" 0
+                       check \
+   'x=$(cat <<'"'${end}'${nl}text${nl}${end#?}${nl}${end}${nl}"'); echo "$x"' \
+                               "text ${end#?}" 0
+                       check \
+   'x=$(cat <<'"'${end}'${nl}text${nl}${end%?}+${nl}${end}${nl}"');echo "$x"' \
+                               "text ${end%?}+" 0
+                       ;;
+               esac
+
+               # or something that is a little longer
+               check \
+   'x=$(cat <<'"'${end}'${nl}text${nl}${end}x${nl}${end}${nl}"'); echo "$x"' \
+                               "text ${end}x" 0
+               check \
+    'x=$(cat <<'"'${end}'${nl}text${nl}!${end}${nl}${end}${nl}"'); echo "$x"' \
+                               "text !${end}" 0
+
+               # or which does not begin at start of line
+               check \
+    'x=$(cat <<'"'${end}'${nl}text${nl} ${end}${nl}${end}${nl}"'); echo "$x"' \
+                               "text  ${end}" 0
+               check \
+    'x=$(cat <<'"'${end}'${nl}text${nl}        ${end}${nl}${end}${nl}"'); echo "$x"' \
+                               "text   ${end}" 0
+
+               # or end at end of line
+               check \
+    'x=$(cat <<'"'${end}'${nl}text${nl}${end} ${nl}${end}${nl}"'); echo "$x"' \
+                               "text ${end} " 0
+
+               # or something that is correct much of the way, but then...
+
+               case "${#end}" in
+               (0)     ;;              # cannot test this one
+               (1)     check \
+    'x=$(cat <<'"'${end}'${nl}text${nl}${end}${end}${nl}${end}${nl}"'); echo "$x"' \
+                               "text ${end}${end}" 0
+                       ;;
+               (2-7)   pfx="${end%?}"
+                       check \
+    'x=$(cat <<'"'${end}'${nl}text${nl}${end}${pfx}${nl}${end}${nl}"'); echo "$x"' \
+                               "text ${end}${pfx}" 0
+                       check \
+    'x=$(cat <<'"'${end}'${nl}text${nl}${pfx}${end}${nl}${end}${nl}"'); echo "$x"' \
+                               "text ${pfx}${end}" 0
+                       ;;
+               (*)     pfx=${end%??????}; sfx=${end#??????}
+                       check \
+    'x=$(cat <<'"'${end}'${nl}text${nl}${end}${sfx}${nl}${end}${nl}"'); echo "$x"' \
+                               "text ${end}${sfx}" 0
+                       check \
+    'x=$(cat <<'"'${end}'${nl}text${nl}${pfx}${end}${nl}${end}${nl}"'); echo "$x"' \
+                               "text ${pfx}${end}" 0
+                       check \
+    'x=$(cat <<'"'${end}'${nl}text${nl}${pfx}${sfx}${nl}${end}${nl}"'); echo "$x"' \
+                               "text ${pfx}${sfx}" 0
+                       ;;
+               esac
+       done
+
+       # Add striptabs tests (in similar way) here one day...
+
+       results
 }
 
 atf_test_case incomplete
@@ -138,6 +262,8 @@
        atf_set "descr" "Basic tests for incomplete here documents"
 }
 incomplete_body() {
+       reset incomplete
+
        check 'cat <<EOF' '' 2
        check 'cat <<- EOF' '' 2
        check 'cat <<\EOF' '' 2
@@ -159,13 +285,54 @@
        check 'cat <<-EOF'"${nl}"'      line 1'"${nl}"'line 2'"${nl}" '' 2
 
        check 'cat << EOF'"${nl}line 1${nl}${nl}line3${nl}${nl}5!${nl}" '' 2
+
+       results
+}
+
+atf_test_case lineends
+lineends_head() {
+       atf_set "descr" "Tests for line endings in here documents"
+}
+lineends_body() {
+       reset lineends
+
+       # note that "check" removes newlines from stdout before comparing.
+       # (they become blanks, provided there is something before & after)
+
+       check 'cat << \echo'"${nl}"'\'"${nl}echo${nl}echo${nl}" '\' 0
+       check 'cat <<  echo'"${nl}"'\'"${nl}echo${nl}echo${nl}" 'echo' 0
+       check 'cat << echo'"${nl}"'\\'"${nl}echo${nl}echo${nl}" '\' 0
+
+       check 'X=3; cat << ec\ho'"${nl}"'$X\'"${nl}echo${nl}echo${nl}" \
+               '$X\'  0
+       check 'X=3; cat <<  echo'"${nl}"'$X'"${nl}echo${nl}echo${nl}" \
+               '3'  0
+       check 'X=3; cat <<  echo'"${nl}"'$X\'"${nl}echo${nl}echo${nl}" \
+               ''  0
+       check 'X=3; cat <<  echo'"${nl}"'${X}\'"${nl}echo${nl}echo${nl}" \
+               '3echo'  0
+       check 'X=3; cat <<  echo'"${nl}"'\$X\'"${nl}echo${nl}echo${nl}" \
+               '$Xecho'  0
+       check 'X=3; cat <<  echo'"${nl}"'\\$X \'"${nl}echo${nl}echo${nl}" \
+               '\3 echo'  0
+
+       check \
+  'cat << "echo"'"${nl}"'line1\'"${nl}"'line2\'"${nl}echo${nl}echo${nl}" \
+                'line1\ line2\'  0
+       check \
+         'cat << echo'"${nl}"'line1\'"${nl}"'line2\'"${nl}echo${nl}echo${nl}" \
+         'line1line2echo'  0
+
+       results
 }
 
 atf_test_case multiple
 multiple_head() {
-       atf_set "descr" "Tests for multiple here documents for one cmd"
+       atf_set "descr" "Tests for multiple here documents on one cmd line"
 }
 multiple_body() {
+       reset multiple
+
        check \
     "(cat ; cat <&3) <<EOF0 3<<EOF3${nl}STDIN${nl}EOF0${nl}-3-${nl}EOF3${nl}" \
                'STDIN -3-' 0
@@ -184,6 +351,138 @@
 EOF
 "                      'The Line The File The Line' 0
 
+       check "V=1; W=2; cat <<-1; cat <<2; cat <<- 3; cat <<'4';"' cat <<\5
+               $V
+               $W
+               3
+       4
+       5
+                       1
+2
+       5
+                                       4*$W+\$V
+       3
+$W
+1
+2
+3
+4
+7+$V
+$W+6
+5
+'                      '1 2 3 4 5 5 4*2+$V $W 1 2 3 7+$V $W+6' 0
+
+       results
+}
+
+atf_test_case nested



Home | Main Index | Thread Index | Old Index