Source-Changes-HG archive

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

[src/trunk]: src Add a test of sh syntax & parsing (first attempt anyway.)



details:   https://anonhg.NetBSD.org/src/rev/71edd0535c07
branches:  trunk
changeset: 353751:71edd0535c07
user:      kre <kre%NetBSD.org@localhost>
date:      Sat May 20 16:35:55 2017 +0000

description:
Add a test of sh syntax & parsing (first attempt anyway.)

diffstat:

 distrib/sets/lists/tests/mi |    3 +-
 tests/bin/sh/Makefile       |    3 +-
 tests/bin/sh/t_syntax.sh    |  888 ++++++++++++++++++++++++++++++++++++++++++++
 3 files changed, 892 insertions(+), 2 deletions(-)

diffs (truncated from 926 to 300 lines):

diff -r 245a5cc8655c -r 71edd0535c07 distrib/sets/lists/tests/mi
--- a/distrib/sets/lists/tests/mi       Sat May 20 10:12:29 2017 +0000
+++ b/distrib/sets/lists/tests/mi       Sat May 20 16:35:55 2017 +0000
@@ -1,4 +1,4 @@
-# $NetBSD: mi,v 1.743 2017/05/15 09:58:22 ozaki-r Exp $
+# $NetBSD: mi,v 1.744 2017/05/20 16:35:55 kre Exp $
 #
 # Note: don't delete entries from here - mark them as "obsolete" instead.
 #
@@ -1278,6 +1278,7 @@
 ./usr/tests/bin/sh/t_redircloexec              tests-bin-tests         compattestfile,atf
 ./usr/tests/bin/sh/t_set_e                     tests-bin-tests         compattestfile,atf
 ./usr/tests/bin/sh/t_shift                     tests-bin-tests         compattestfile,atf
+./usr/tests/bin/sh/t_syntax                    tests-bin-tests         compattestfile,atf
 ./usr/tests/bin/sh/t_ulimit                    tests-bin-tests         compattestfile,atf
 ./usr/tests/bin/sh/t_varquote                  tests-bin-tests         compattestfile,atf
 ./usr/tests/bin/sh/t_varval                    tests-bin-tests         compattestfile,atf
diff -r 245a5cc8655c -r 71edd0535c07 tests/bin/sh/Makefile
--- a/tests/bin/sh/Makefile     Sat May 20 10:12:29 2017 +0000
+++ b/tests/bin/sh/Makefile     Sat May 20 16:35:55 2017 +0000
@@ -1,4 +1,4 @@
-# $NetBSD: Makefile,v 1.11 2016/03/20 22:57:04 christos Exp $
+# $NetBSD: Makefile,v 1.12 2017/05/20 16:35:55 kre Exp $
 #
 
 .include <bsd.own.mk>
@@ -19,6 +19,7 @@
 TESTS_SH+=     t_redircloexec
 TESTS_SH+=     t_set_e
 TESTS_SH+=     t_shift
+TESTS_SH+=     t_syntax
 TESTS_SH+=     t_ulimit
 TESTS_SH+=     t_varquote
 TESTS_SH+=     t_varval
diff -r 245a5cc8655c -r 71edd0535c07 tests/bin/sh/t_syntax.sh
--- /dev/null   Thu Jan 01 00:00:00 1970 +0000
+++ b/tests/bin/sh/t_syntax.sh  Sat May 20 16:35:55 2017 +0000
@@ -0,0 +1,888 @@
+# $NetBSD: t_syntax.sh,v 1.1 2017/05/20 16:35:55 kre Exp $
+#
+# Copyright (c) 2017 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.
+#
+: ${TEST_SH:=/bin/sh}
+
+# This set of tests verifies various requirementgs relating to correct
+# (and incorrect) syntax of shell input
+#
+# There is no intent in these tests to verify correct operation
+# (though that sometimes cannot be separated from correct parsing.)
+# That is (or should be) verified elsewhere.
+#
+# Also, some very basic syntax is tested in almost every test
+# (they cannot work without basic parsing of elementary commands)
+# so that is also not explicitly tested here.
+#
+# Similarly word expansion, field splitting, redirection, all have
+# tests of their own (though we do test parsing of redirect ops).
+#
+# Note that in order to test the basic facilities, other shell operations
+# are used - a test failure here does not necessarily mean that the
+# operation intended to be tested is faulty, just that something is.
+
+atf_test_case a_basic_tokenisation
+a_basic_tokenisation_head() {
+       atf_set "descr" "Test the shell correctly finds various tokens"
+}
+a_basic_tokenisation_body() {
+       atf_check -s exit:0 -o 'inline:3\n' -e empty ${TEST_SH} -c \
+               'set -- a b c; echo $#'
+       atf_check -s exit:0 -o 'inline:2\n' -e empty ${TEST_SH} -c \
+               'set -- a""b c; echo $#'
+       atf_check -s exit:0 -o 'inline:3\n' -e empty ${TEST_SH} -c \
+               'set -- a"" b c; echo $#'
+       atf_check -s exit:0 -o 'inline:3\n' -e empty ${TEST_SH} -c \
+               'set -- ""a b c\;; echo $#'
+
+       atf_check -s exit:0 -o 'inline:3\n' -e empty ${TEST_SH} -c \
+               'set -- set -- c; echo $#'
+       atf_check -s exit:0 -o 'inline:1\n' -e empty ${TEST_SH} -c \
+               'set --;set -- c; echo $#'
+       atf_check -s exit:0 -o 'inline:1\n' -e empty ${TEST_SH} -c \
+               'set --&set -- c; echo $#'
+       atf_check -s exit:0 -o 'inline:1\n' -e empty ${TEST_SH} -c \
+               'set -- a b&&set -- c; echo $#'
+       atf_check -s exit:0 -o 'inline:2\n' -e empty ${TEST_SH} -c \
+               'set -- a b||set -- c; echo $#'
+}
+
+atf_test_case b_comments
+b_comments_head() {
+       atf_set "descr" "Test the shell correctly handles comments"
+}
+b_comments_body() {
+
+       atf_check -s exit:0 -o empty -e empty ${TEST_SH} -c '#'
+       atf_check -s exit:0 -o empty -e empty ${TEST_SH} -c '# exit 1'
+       atf_check -s exit:0 -o empty -e empty ${TEST_SH} -c 'true # exit 1'
+       atf_check -s exit:1 -o empty -e empty ${TEST_SH} -c 'false # exit 0'
+
+       atf_check -s exit:0 -o 'inline:foo\n' -e empty ${TEST_SH} -c \
+               'echo foo # bar'
+       atf_check -s exit:0 -o 'inline:foo # bar\n' -e empty ${TEST_SH} -c \
+               'echo foo \# bar'
+       atf_check -s exit:0 -o 'inline:foo\n' -e empty ${TEST_SH} -c \
+               'echo foo; # echo bar'
+       atf_check -s exit:0 -o 'inline:foo#bar\n' -e empty ${TEST_SH} -c \
+               'echo foo#bar'
+       atf_check -s exit:0 -o 'inline:foo# bar\n' -e empty ${TEST_SH} -c \
+               'echo foo# bar'
+       atf_check -s exit:0 -o 'inline:foo\n' -e empty ${TEST_SH} -c \
+               'x=foo; echo ${x#bar}'
+
+       atf_check -s exit:0 -o 'inline:#\n' -e empty ${TEST_SH} -c \
+               'echo "#"'
+       atf_check -s exit:0 -o 'inline:#\n' -e empty ${TEST_SH} -c \
+               "echo '#'"
+       atf_check -s exit:0 -o 'inline:#\n' -e empty ${TEST_SH} -c \
+               'echo \#'
+       atf_check -s exit:0 -o 'inline:##\n' -e empty ${TEST_SH} -c \
+               'echo "#"#'
+       atf_check -s exit:0 -o 'inline:##\n' -e empty ${TEST_SH} -c \
+               "echo '#'#"
+       atf_check -s exit:0 -o 'inline:##\n' -e empty ${TEST_SH} -c \
+               'echo \##'
+       atf_check -s exit:0 -o 'inline:##\n' -e empty ${TEST_SH} -c \
+               'echo "#"# #"#"'
+       atf_check -s exit:0 -o 'inline:##\n' -e empty ${TEST_SH} -c \
+               "echo '#'# #'#'"
+       atf_check -s exit:0 -o 'inline:##\n' -e empty ${TEST_SH} -c \
+               'echo \## #\#'
+
+       cat <<-'DONE'|atf_check -s exit:0 -o inline:'foo\n' -e empty ${TEST_SH}
+               # test comments do not provoke synax errors !\
+               echo foo # ( { " hello
+               while : # that's forever
+               do      # the following command list
+                       # starting with nothing ${unset?error}
+                       break   # done loop terminate $( echo bar; exit 1 )
+               done #####################################################
+               # "hello
+               exit 0
+       DONE
+}
+
+atf_test_case c_line_wrapping
+c_line_wrapping_head() {
+       atf_set "descr" "check aspects of command line wrapping"
+}
+c_line_wrapping_body() {
+       atf_require_prog ls
+       atf_require_prog printf
+
+       cat <<- 'DONE' | atf_check -s exit:0 -o ignore -e empty ${TEST_SH} -e
+               l\
+               s
+       DONE
+
+       cat <<- 'DONE' | atf_check -s exit:7 -o empty -e empty ${TEST_SH}
+               e\
+               x\
+               it \
+               7
+       DONE
+
+       # Have to do this twice as cannot say "any exit code but 0 or 7" ...
+       cat <<- 'DONE' | atf_check -s not-exit:0 -o empty -e not-empty \
+           ${TEST_SH}
+               e\
+               x\
+               it\
+               7
+       DONE
+       cat <<- 'DONE' | atf_check -s not-exit:7 -o empty -e not-empty \
+           ${TEST_SH}
+               e\
+               x\
+               it\
+               7
+       DONE
+
+       cat <<- 'DONE' | atf_check -s exit:0 -o empty -e empty  ${TEST_SH}
+               wh\
+               il\
+               e \
+               f\a\
+               \l\s\e
+               do
+               :\
+               ;
+               done
+       DONE
+
+       cat <<- 'DONE' | atf_check -s exit:0 -o inline:'hellohellohellohello' \
+           -e empty ${TEST_SH}
+               V\
+               AR=hel\
+               lo
+               unset U V1
+               pri\
+               ntf '%s' ${\
+               VAR\
+               }
+               p\
+               r\
+               i\
+               n\
+               t\
+               f\
+                \
+               '%s' \
+               $\
+               {\
+               V\
+               A\
+               R}
+               printf '%s' ${U\
+               -\
+               "$\
+               {V\
+               1:\
+               =$\
+               {V\
+               AR+\
+               ${V\
+               AR}\
+               }\
+               }}
+               printf '%s' ${V\
+               1?V1\
+                \
+               FAIL}
+       DONE
+       cat <<- 'DONE' | atf_check -s exit:0 -o inline:'2\n' ${TEST_SH}
+               l\
+               s=7 bi\
+               n\
+               =\
+               3
+               echo $(\
+               ( ls /bin )\
+               )
+       DONE
+}
+
+atf_test_case d_redirects
+d_redirects_head() {
+       atf_set "descr" "Check parsing of redirect operators"
+}
+d_redirects_body() {
+
+       atf_check -s exit:0 -o empty -e empty ${TEST_SH} -c \
+               '>/dev/null'
+       atf_check -s exit:0 -o empty -e empty ${TEST_SH} -c \
+               '</dev/null'
+       atf_check -s exit:0 -o empty -e empty ${TEST_SH} -c \
+               '>>/dev/null'
+       atf_check -s exit:0 -o empty -e empty ${TEST_SH} -c \
+               '<>/dev/null'
+       atf_check -s exit:0 -o empty -e empty ${TEST_SH} -c \
+               '</dev/null>/dev/null'
+       atf_check -s exit:0 -o empty -e empty ${TEST_SH} -c \
+               '>|/dev/null'
+       atf_check -s exit:0 -o empty -e empty ${TEST_SH} -c \
+               '>/dev/null>/dev/null>/dev/null'
+
+       atf-check -s exit:0 -o empty -e empty ${TEST_SH} -c \
+               'echo hello >/dev/null'
+       atf-check -s exit:0 -o empty -e empty ${TEST_SH} -c \
+               'echo >/dev/null hello'
+       atf-check -s exit:0 -o empty -e empty ${TEST_SH} -c \
+               '>/dev/null echo hello'
+       atf-check -s exit:0 -o empty -e empty ${TEST_SH} -c \
+               'echo hello >/dev/null world'
+       atf-check -s exit:0 -o 'inline:hello world\n' -e empty ${TEST_SH} -c \
+               'echo hello </dev/null world'
+
+       atf-check -s exit:0 -o 'inline:hello\n' -e empty ${TEST_SH} -c \
+               'echo hello </dev/null'



Home | Main Index | Thread Index | Old Index