Source-Changes-HG archive

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

[src/trunk]: src Add tests for pattern matching (filename expansion (glob), c...



details:   https://anonhg.NetBSD.org/src/rev/fa845a8b471c
branches:  trunk
changeset: 363072:fa845a8b471c
user:      kre <kre%NetBSD.org@localhost>
date:      Tue Jul 10 06:49:29 2018 +0000

description:
Add tests for pattern matching (filename expansion (glob), case statement
patterns, and variable expansion substring matching)

Currently (2018-07-10) all 3 sub-tests fail (sh bugs...)
Expect to see 14 (of 261) case matching sub-tests fail, 11 (of 167) filename
expansion (glob) sub-tests fail, and 6 (of 87) var substring sub-tests fail.

Also expect those numbers to reduce as sh bugs are fixed.

diffstat:

 distrib/sets/lists/tests/mi |    3 +-
 tests/bin/sh/Makefile       |    3 +-
 tests/bin/sh/t_patterns.sh  |  784 ++++++++++++++++++++++++++++++++++++++++++++
 3 files changed, 788 insertions(+), 2 deletions(-)

diffs (truncated from 822 to 300 lines):

diff -r 0b0a26d7ce20 -r fa845a8b471c distrib/sets/lists/tests/mi
--- a/distrib/sets/lists/tests/mi       Tue Jul 10 06:44:49 2018 +0000
+++ b/distrib/sets/lists/tests/mi       Tue Jul 10 06:49:29 2018 +0000
@@ -1,4 +1,4 @@
-# $NetBSD: mi,v 1.788 2018/06/20 03:51:27 maya Exp $
+# $NetBSD: mi,v 1.789 2018/07/10 06:49:29 kre Exp $
 #
 # Note: don't delete entries from here - mark them as "obsolete" instead.
 #
@@ -1277,6 +1277,7 @@
 ./usr/tests/bin/sh/t_fsplit                    tests-bin-tests         compattestfile,atf
 ./usr/tests/bin/sh/t_here                      tests-bin-tests         compattestfile,atf
 ./usr/tests/bin/sh/t_option                    tests-bin-tests         compattestfile,atf
+./usr/tests/bin/sh/t_patterns                  tests-bin-tests         compattestfile,atf
 ./usr/tests/bin/sh/t_redir                     tests-bin-tests         compattestfile,atf
 ./usr/tests/bin/sh/t_redircloexec              tests-bin-tests         compattestfile,atf
 ./usr/tests/bin/sh/t_set_e                     tests-bin-tests         compattestfile,atf
diff -r 0b0a26d7ce20 -r fa845a8b471c tests/bin/sh/Makefile
--- a/tests/bin/sh/Makefile     Tue Jul 10 06:44:49 2018 +0000
+++ b/tests/bin/sh/Makefile     Tue Jul 10 06:49:29 2018 +0000
@@ -1,4 +1,4 @@
-# $NetBSD: Makefile,v 1.12 2017/05/20 16:35:55 kre Exp $
+# $NetBSD: Makefile,v 1.13 2018/07/10 06:49:29 kre Exp $
 #
 
 .include <bsd.own.mk>
@@ -15,6 +15,7 @@
 TESTS_SH+=     t_fsplit
 TESTS_SH+=     t_here
 TESTS_SH+=     t_option
+TESTS_SH+=     t_patterns
 TESTS_SH+=     t_redir
 TESTS_SH+=     t_redircloexec
 TESTS_SH+=     t_set_e
diff -r 0b0a26d7ce20 -r fa845a8b471c tests/bin/sh/t_patterns.sh
--- /dev/null   Thu Jan 01 00:00:00 1970 +0000
+++ b/tests/bin/sh/t_patterns.sh        Tue Jul 10 06:49:29 2018 +0000
@@ -0,0 +1,784 @@
+# $NetBSD: t_patterns.sh,v 1.1 2018/07/10 06:49:29 kre Exp $
+#
+# Copyright (c) 2018 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}
+
+#
+# This file tests pattern matching (glob)
+#
+# Three forms:
+#      standard filename expansion (echo *.c)
+#      case statements (case word in (*.c) ...;;)
+#      var expansions with substring matching ${var%*.c}
+#
+# Note: the emphasis here is on testing the various possible patterns,
+# not that case statements, or var expansions (etc) work in general.
+
+### Helper functions
+
+nl='
+'
+reset()
+{
+       TEST_NUM=0
+       TEST_FAILURES=''
+       TEST_FAIL_COUNT=0
+       TEST_ID="$1"
+}
+
+# Test run & validate.
+#
+#      $1 is the command to run (via sh -c)
+#      $2 is the expected output (with any \n's in output replaced by spaces)
+#      $3 is the expected exit status from sh
+#
+# Stderr is exxpected to be empty, unless the expected exit code ($3) is != 0
+# in which case some message there is expected (and nothing is a failure).
+# When non-zero exit is expected, we note a different (non-zero) value
+# observed, but do not fail the test because of that.
+
+check()
+{
+       fail=false
+       # Note TEMP_FILE must not be in the current directory (or nearby).
+       TEMP_FILE=$( mktemp /tmp/OUT.XXXXXX )
+       TEST_NUM=$(( $TEST_NUM + 1 ))
+       MSG=
+
+       # our local shell (ATF_SHELL) better do quoting correctly...
+       # some of the tests expect us to expand $nl internally...
+       CMD="$1"
+
+       result="$( ${TEST_SH} -c "${CMD}" 2>"${TEMP_FILE}" )"
+       STATUS=$?
+
+       if [ "${STATUS}" -ne "$3" ]; then
+               MSG="${MSG}${MSG:+${nl}}[$TEST_NUM]"
+               MSG="${MSG} expected exit code $3, got ${STATUS}"
+
+               # don't actually fail just because of wrong exit code
+               # unless we either expected, or received "good"
+               # or something else is detected as incorrect as well.
+               case "$3/${STATUS}" in
+               (*/0|0/*) fail=true;;
+               esac
+       fi
+
+       if [ "$3" -eq 0 ]; then
+               if [ -s "${TEMP_FILE}" ]; then
+                       MSG="${MSG}${MSG:+${nl}}[$TEST_NUM]"
+                       MSG="${MSG} Messages produced on stderr unexpected..."
+                       MSG="${MSG}${nl}$( cat "${TEMP_FILE}" )"
+                       fail=true
+               fi
+       else
+               if ! [ -s "${TEMP_FILE}" ]; then
+                       MSG="${MSG}${MSG:+${nl}}[$TEST_NUM]"
+                       MSG="${MSG} Expected messages on stderr,"
+                       MSG="${MSG} nothing produced"
+                       fail=true
+               fi
+       fi
+       rm -f "${TEMP_FILE}"
+
+       case "${result}" in
+       (*[!0-9"        $nl"]*)
+               # A word of some kind: at least 1 char that is not digit or wsp
+               # Remove newlines (use local shell for this)
+               result="$(
+                       set -f
+                       IFS="$nl"
+                       set -- $result
+                       IFS=' '
+                       printf '%s' "$*"
+               )"
+               ;;
+       (*[0-9]*)
+               # a numeric result, return just the number, trim whitespace
+               result=$(( ${result} ))
+               ;;
+       (*)
+               # whitespace only, or empty string: just leave it as is
+               ;;
+       esac
+
+       if [ "$2" != "${result}" ]
+       then
+               MSG="${MSG}${MSG:+${nl}}[$TEST_NUM]"
+               MSG="${MSG} Expected output '$2', received '$result'"
+               fail=true
+       fi
+
+       if $fail
+       then
+               MSG="${MSG}${MSG:+${nl}}[$TEST_NUM]"
+               MSG="${MSG} Full command: <<${CMD}>>"
+       fi
+
+       $fail && test -n "$TEST_ID" && {
+               TEST_FAILURES="${TEST_FAILURES}${TEST_FAILURES:+${nl}}"
+               TEST_FAILURES="${TEST_FAILURES}${TEST_ID}[$TEST_NUM]:"
+               TEST_FAILURES="${TEST_FAILURES} Test of '$1' failed.";
+               TEST_FAILURES="${TEST_FAILURES}${nl}${MSG}"
+               TEST_FAIL_COUNT=$(( $TEST_FAIL_COUNT + 1 ))
+               return 0
+       }
+       $fail && atf_fail "Test[$TEST_NUM] failed: $(
+           # ATF does not like newlines in messages, so change them...
+                   printf '%s' "${MSG}" | tr '\n' ';'
+           )"
+       return 0
+}
+
+results()
+{
+       test -n "$1" && atf_expect_fail "$1"
+
+       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 (of $TEST_NUM) subtests failed - see stderr"
+}
+
+####### End helpers
+
+atf_test_case filename_expansion
+filename_expansion() {
+       atf_set descr "Test correct operation of filename expansion"
+}
+filename_expansion_body() {
+       atf_require_prog mktemp
+       atf_require_prog wc
+       atf_require_prog mv
+       atf_require_prog rm
+       atf_require_prog mkdir
+
+       reset filename_expansion
+
+       # First create a known set of filenames to match against
+
+       # Note: This creates almost 17000 files/directories, so
+       # needs at least that many free inodes (only space consumed
+       # is for the directory contents, with a 1K frag size, it
+       # should be about 1.2MiB).  Switching to making links would
+       # save inodes, but would require running "ln" many times, so
+       # would be a lot slower.
+
+       # This should work on a case insensitive, but preserving,
+       # filesystem - but case sensitive filesystems are preferred.
+
+       D=$(mktemp -d "DIR.$$.XXXXXX") || atf_fail "mktemp -d failed"
+       cd "${D}" || atf_fail "cd to temp dir '$D' failed"
+
+       # we need another level of directory, so we know what
+       # files to expect in ".." (ie: in $D) - only ".D".
+       mkdir .D && cd .D || atf_fail "failed to make or enter .D in $D"
+
+       > Xx || atf_fail "Unable to make files in temporary directory"
+       case "$( printf '%s\n' *)" in
+               (Xx) rm Xx || atf_fail "Unable to delete file";;
+               (\*) atf_fail "Created file vanished";;
+               (xx|XX|xX) atf_skip "Case preserving filesystem required";;
+               (*) atf_fail "Unexpected file expansion for '*'";;
+       esac
+
+       # from here on we make files/directories that we will be
+       # using in the tests.
+
+       # CAUTION: Change *any* of this and the expected results from the
+       # tests will all need verifying and updating as well.
+
+       mkdir D || atf_fail "mkdir D failed"
+
+       for F in a b c e V W X Y 1 2 3 4 5 \\ \] \[ \* \? \- \! \^ \| \# \' \"
+       do
+               > "${F}"
+               > ".${F}"
+               > "${F}.${F}"
+               > "${F}-${F}"
+               > "${F}${F}${F}${F}"
+               > "x${F}z"
+               > ab"${F}"yz
+               > "m${F}n${F}p${F}q"
+
+               > "D/${F}"
+               > "D/.${F}"
+               > "D/${F}${F}${F}${F}"
+
+               mkdir "D${F}" || atf_fail "mkdir 'D${F}' failed"
+               mkdir ".D${F}" || atf_fail "mkdir '.D${F}' failed"
+
+               for G in a b c e W X Y 0 2 4 6 \\ \] \[ \* \? \- \! \^ \| \#
+               do
+                       > "${F}${G}"
+                       > "${F}${G}${G}"
+                       > "${F}${G}${F}"
+                       > "${G}${F}${G}"
+                       > "${F}${G}${G}${F}"
+                       > "${G}${F}${F}${G}"
+                       > "${F}.${G}"
+                       > "${F}${G}.${G}${G}"
+                       > "${F}${G}${F}${G}.${G}"
+                       > "x${F}${G}y"
+                       > "${F}z${G}"
+                       > "${G}zz${F}"
+                       > "${G}+${G}"
+
+                       > "D${F}/${G}"
+                       > "D${F}/.${G}"
+                       > "D${F}/${G}${F}${G}"
+                       > "D${F}/.${G}${F}${G}"
+
+                       > ".D${F}/${G}"



Home | Main Index | Thread Index | Old Index