Source-Changes-HG archive

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

[src/trunk]: src Add ATF tests for printf(1)



details:   https://anonhg.NetBSD.org/src/rev/9d0f03ed0b29
branches:  trunk
changeset: 433285:9d0f03ed0b29
user:      kre <kre%NetBSD.org@localhost>
date:      Wed Sep 05 21:05:40 2018 +0000

description:
Add ATF tests for printf(1)

Two new test programs, one for the version of printf in /bin/sh
and one for the command /usr/bin/printf (t_builtin and t_command)

Each test program has 28 test cases (the same in each) of which
currently 27 pass, and 1 is skipped.

See the test scripts themselves for more information.

diffstat:

 distrib/sets/lists/tests/mi       |     7 +-
 etc/mtree/NetBSD.dist.tests       |     3 +-
 tests/usr.bin/Makefile            |     4 +-
 tests/usr.bin/printf/Makefile     |    13 +
 tests/usr.bin/printf/printf.sh    |  1781 +++++++++++++++++++++++++++++++++++++
 tests/usr.bin/printf/t_builtin.sh |   123 ++
 tests/usr.bin/printf/t_command.sh |   144 ++
 7 files changed, 2071 insertions(+), 4 deletions(-)

diffs (truncated from 2138 to 300 lines):

diff -r fc3d60a4ed8c -r 9d0f03ed0b29 distrib/sets/lists/tests/mi
--- a/distrib/sets/lists/tests/mi       Wed Sep 05 17:10:27 2018 +0000
+++ b/distrib/sets/lists/tests/mi       Wed Sep 05 21:05:40 2018 +0000
@@ -1,4 +1,4 @@
-# $NetBSD: mi,v 1.793 2018/08/21 11:07:40 christos Exp $
+# $NetBSD: mi,v 1.794 2018/09/05 21:05:40 kre Exp $
 #
 # Note: don't delete entries from here - mark them as "obsolete" instead.
 #
@@ -3945,6 +3945,11 @@
 ./usr/tests/usr.bin/pr/d_basic.in              tests-usr.bin-tests     compattestfile,atf
 ./usr/tests/usr.bin/pr/d_basic.out             tests-usr.bin-tests     compattestfile,atf
 ./usr/tests/usr.bin/pr/t_basic                 tests-usr.bin-tests     compattestfile,atf
+./usr/tests/usr.bin/printf                     tests-usr.bin-tests     compattestfile,atf
+./usr/tests/usr.bin/printf/Atffile             tests-usr.bin-tests     compattestfile,atf
+./usr/tests/usr.bin/printf/Kyuafile            tests-usr.bin-tests     compattestfile,atf,kyua
+./usr/tests/usr.bin/printf/t_builtin           tests-usr.bin-tests     compattestfile,atf
+./usr/tests/usr.bin/printf/t_command           tests-usr.bin-tests     compattestfile,atf
 ./usr/tests/usr.bin/rump_server                        tests-usr.bin-tests     compattestfile,atf
 ./usr/tests/usr.bin/rump_server/Atffile                tests-usr.bin-tests     compattestfile,atf
 ./usr/tests/usr.bin/rump_server/Kyuafile       tests-usr.bin-tests     compattestfile,atf,kyua
diff -r fc3d60a4ed8c -r 9d0f03ed0b29 etc/mtree/NetBSD.dist.tests
--- a/etc/mtree/NetBSD.dist.tests       Wed Sep 05 17:10:27 2018 +0000
+++ b/etc/mtree/NetBSD.dist.tests       Wed Sep 05 21:05:40 2018 +0000
@@ -1,4 +1,4 @@
-#      $NetBSD: NetBSD.dist.tests,v 1.152 2018/08/03 04:24:41 kamil Exp $
+#      $NetBSD: NetBSD.dist.tests,v 1.153 2018/09/05 21:05:40 kre Exp $
 
 ./usr/libdata/debug/usr/tests
 ./usr/libdata/debug/usr/tests/atf
@@ -410,6 +410,7 @@
 ./usr/tests/usr.bin/nbperf
 ./usr/tests/usr.bin/pkill
 ./usr/tests/usr.bin/pr
+./usr/tests/usr.bin/printf
 ./usr/tests/usr.bin/rump_server
 ./usr/tests/usr.bin/sdiff
 ./usr/tests/usr.bin/sed
diff -r fc3d60a4ed8c -r 9d0f03ed0b29 tests/usr.bin/Makefile
--- a/tests/usr.bin/Makefile    Wed Sep 05 17:10:27 2018 +0000
+++ b/tests/usr.bin/Makefile    Wed Sep 05 21:05:40 2018 +0000
@@ -1,4 +1,4 @@
-#      $NetBSD: Makefile,v 1.25 2017/05/14 00:07:07 kamil Exp $
+#      $NetBSD: Makefile,v 1.26 2018/09/05 21:05:40 kre Exp $
 #
 
 .include <bsd.own.mk>
@@ -8,7 +8,7 @@
 TESTS_SUBDIRS= awk basename bzip2 cc cmp config cut \
                diff dirname find gdb grep gzip id \
                infocmp jot ld m4 make mixerctl mkdep nbperf netpgpverify \
-               pkill pr rump_server shmif_dumpbus sdiff \
+               pkill pr printf rump_server shmif_dumpbus sdiff \
                sed sort tmux tr unifdef uniq vmstat xlint
 
 .if ${MKCXX} != "no"
diff -r fc3d60a4ed8c -r 9d0f03ed0b29 tests/usr.bin/printf/Makefile
--- /dev/null   Thu Jan 01 00:00:00 1970 +0000
+++ b/tests/usr.bin/printf/Makefile     Wed Sep 05 21:05:40 2018 +0000
@@ -0,0 +1,13 @@
+# $NetBSD: Makefile,v 1.1 2018/09/05 21:05:40 kre Exp $
+
+.include <bsd.own.mk>
+
+TESTSDIR=      ${TESTSBASE}/usr.bin/printf
+
+TESTS_SH=      t_builtin t_command
+
+t_builtin: t_builtin.sh printf.sh
+
+t_command: t_command.sh printf.sh
+
+.include <bsd.test.mk>
diff -r fc3d60a4ed8c -r 9d0f03ed0b29 tests/usr.bin/printf/printf.sh
--- /dev/null   Thu Jan 01 00:00:00 1970 +0000
+++ b/tests/usr.bin/printf/printf.sh    Wed Sep 05 21:05:40 2018 +0000
@@ -0,0 +1,1781 @@
+# $NetBSD: printf.sh,v 1.1 2018/09/05 21:05:40 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.
+#
+
+Running_under_ATF=false
+test -n "${Atf_Shell}" && test -n "${Atf_Check}" && Running_under_ATF=true
+
+Tests=
+
+# create a test case:
+#      "$1" is basic test name, "$2" is description
+define()
+{
+       NAME=$1; shift
+
+       if $Running_under_ATF
+       then
+           eval "${NAME}_head() { set descr 'Tests printf: $*'; }"
+           eval "${NAME}_body() { ${NAME} ; }"
+       else
+           eval "TEST_${NAME}_MSG="'"$*"'
+       fi
+
+       Tests="${Tests} ${NAME}"
+}
+
+
+# 1st arg is printf format conversion specifier
+# other args (if any) are args to that format
+# returns success if that conversion specifier is supported, false otherwise
+supported()
+{
+       FMT="$1"; shift
+
+       case "$#" in
+       0)      set -- 123;;    # provide an arg for format to use
+       esac
+
+       (do_printf >/dev/null 2>&1 "%${FMT}" "$@")
+}
+
+LastErrorTest=
+
+$Running_under_ATF || {
+
+       # Provide functions to emulate (roughly) what ATF gives us
+       # (that we actually use)
+
+       atf_skip() {
+               echo >&2 "${CurrentTest} skipped: ${MSG} $*"
+       }
+       atf_fail() {
+               if [ "${CurrentTest}" != "${LastErrorTest}" ]
+               then
+                       echo >&2 "========   In Test ${CurrentTest}:"
+                       LastErrorTest="${CurrentTest}"
+               fi
+               echo >&2 "${CurrentTest} FAIL: ${MSG} $*"
+               RVAL=1
+       }
+       atf_require_prog() {
+               # Just allow progs we want to run to be, or not be, found
+               return 0
+       }
+}
+
+# 1st arg is the result expected, remaining args are handed to do_printf
+# to execute, fail if result does not match the expected result (treated
+# as a sh pattern), or if do_printf fails
+expect()
+{
+       WANT="$1";  shift
+       negated=false
+
+       case "${WANT}" in
+       ('!')   WANT="$1"; negated=true; shift;;
+       esac
+
+       RES="$( do_printf "$@" 2>&3 && echo X )" || atf_fail "$*  ... Exit $?"
+
+       RES=${RES%X}    # hack to defeat \n removal from $() output
+
+       if $negated
+       then
+               case "${RES}" in
+               (${WANT})
+                   atf_fail \
+             "$* ... Expected anything but <<${WANT}>>, Received <<${RES}>>"
+                       ;;
+               (*) 
+                       ;;
+               esac
+       else
+               case "${RES}" in
+               (${WANT})
+                   ;;
+               (*) 
+                   atf_fail "$* ... Expected <<${WANT}>> Received <<${RES}>"
+                   ;;
+               esac
+       fi
+       return 0
+}
+
+# a variant which allows for two possible results
+# It would be nice to have just one function, and allow the pattern
+# to contain alternatives ... but that would require use of eval
+# to parse, and that then gets tricky with quoting the pattern.
+# and we only ever need two (so far anyway), so this is easier...
+expect2()
+{
+       WANT1="$1";  shift
+       WANT2="$1";  shift
+
+       RES="$( do_printf "$@" 2>&3 && echo X )" || atf_fail "$* ... Exit $?"
+
+       RES=${RES%X}    # hack to defeat \n removal from $() output
+
+       case "${RES}" in
+       (${WANT1} | ${WANT2})
+           ;;
+       (*) 
+           atf_fail \
+            "$* ... Expected <<${WANT1}|${WANT2}>> Received <<${RES}>>"
+           ;;
+       esac
+       return 0
+}
+
+expect_fail()
+{
+       WANT="$1";      shift   # we do not really expect this, but ...
+
+       RES=$( do_printf "$@" 2>/dev/null && echo X ) && {
+               RES=${RES%X}
+               case "${RES}" in
+               (${WANT})
+                   atf_fail \
+                       "$*  ... success${WANT:+ with expected <<${WANT}>>}"
+                   ;;
+               ('')
+                   atf_fail "$*  ... success (without output)"
+                   ;;
+               (*) 
+                   atf_fail "$*  ... success with <<${RES}>> (not <<${WANT}>>)"
+                   ;;
+               esac
+
+               RVAL=1
+               return 0
+       }
+
+       RES=$( do_printf "$@" 2>&1 >/dev/null )
+       STAT=$?
+       test -z "${RES}" &&
+               atf_fail "$*  ... failed (${STAT}) without error message"
+
+       RES="$( do_printf "$@" 2>/dev/null ; echo X )"
+       RES=${RES%X}    # hack to defeat \n removal from $() output
+
+       case "${RES}" in
+       (${WANT})
+           # All is good, printf failed, sent a message to stderr
+           # and printed what it should to stdout
+           ;;
+       (*) 
+           atf_fail \
+     "$*  ... should fail with <<${WANT}>> did exit(${STAT}) with <<${RES}>>"
+           ;;
+       esac
+       return 0
+}
+
+##########################################################################
+##########################################################################
+#
+#              Actual tests follow
+#
+##########################################################################
+##########################################################################
+
+basic()
+{
+       setmsg basic
+
+       for A in '' -- -@       # hope that '@' is not an option to printf...
+       do
+               if (do_printf $A >/dev/null 2>&1)
+               then
+                       atf_fail "${A:-with no args} successful"
+               fi
+               if test -n "$( do_printf 2>/dev/null )"
+               then
+                       atf_fail "${A:-with no args} produces text on stdout"
+               fi
+               if test -z "$( do_printf 2>&1 )"
+               then
+                       atf_fail "${A:-with no args} no err/usage message"
+               fi
+



Home | Main Index | Thread Index | Old Index