Source-Changes-HG archive
[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index][Old Index]
[src/trunk]: src/tests/bin/sh more tests from kre
details: https://anonhg.NetBSD.org/src/rev/e4ed783bd7af
branches: trunk
changeset: 343893:e4ed783bd7af
user: christos <christos%NetBSD.org@localhost>
date: Mon Feb 29 23:52:53 2016 +0000
description:
more tests from kre
diffstat:
tests/bin/sh/t_here.sh | 230 ++++++++++++++++++++++++++++++++++++++++++++----
1 files changed, 209 insertions(+), 21 deletions(-)
diffs (268 lines):
diff -r ee9cd4780c1b -r e4ed783bd7af tests/bin/sh/t_here.sh
--- a/tests/bin/sh/t_here.sh Mon Feb 29 23:52:04 2016 +0000
+++ b/tests/bin/sh/t_here.sh Mon Feb 29 23:52:53 2016 +0000
@@ -1,4 +1,4 @@
-# $NetBSD: t_here.sh,v 1.1 2012/03/17 16:33:11 jruoho Exp $
+# $NetBSD: t_here.sh,v 1.2 2016/02/29 23:52:53 christos Exp $
#
# Copyright (c) 2007 The NetBSD Foundation, Inc.
# All rights reserved.
@@ -24,50 +24,238 @@
# 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"}
nl='
'
check()
{
- SVIFS="$IFS"
- result="$(eval $1)"
- # Remove newlines
+ fail=false
+ TEMP_FILE=$( mktemp OUT.XXXXXX )
+
+ # our local shell (ATF_SHELL) better do quoting correctly...
+ # some of the tests expect us to expand $nl internally...
+ CMD="nl='${nl}'; $1"
+
+echo "${CMD}" >/tmp/CMD
+ rm -f trace.*
+ result="$( ${TEST_SH} -c "${CMD}" 2>"${TEMP_FILE}" )"
+ STATUS=$?
+
+ if [ -s "${O_FILE}" ]; then
+ echo >&2 "unexpected shell output noise on stdout"
+ cat "${O_FILE}" >&2
+ fail=true
+ fi
+
+ if [ "${STATUS}" -ne "$3" ]; then
+ echo >&2 "expected exit code $3, got ${STATUS}"
+
+ # don't actually fail just because of wrong exit code
+ # unless we either expected, or received "good"
+ case "$3/${STATUS}" in
+ (*/0|0/*) fail=true;;
+ esac
+ fi
+
+ if [ "$3" -eq 0 ]; then
+ if [ -s "${TEMP_FILE}" ]; then
+ echo >&2 "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"
+ fail=true
+ fi
+ fi
+ rm -f "${TEMP_FILE}"
+
+ # Remove newlines (use local shell for this)
oifs="$IFS"
IFS="$nl"
result="$(echo $result)"
IFS="$oifs"
if [ "$2" != "$result" ]
then
- atf_fail "expected [$2], found [$result]"
+ echo >&2 "Expected output '$2', received '$result'"
+ fail=true
fi
- IFS="$SVIFS"
+
+ $fail && atf_fail "test of '$1' failed"
+ return 0
}
-atf_test_case all
-all_head() {
+atf_test_case do_simple
+do_simple_head() {
atf_set "descr" "Basic tests for here documents"
}
-all_body() {
+do_simple_body() {
y=x
IFS=
- check 'x=`cat <<EOF'$nl'text'${nl}EOF$nl'`; echo $x' 'text'
- check 'x=`cat <<\EOF'$nl'text'${nl}EOF$nl'`; echo $x' 'text'
+ 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
+
+ check "y=${y};"'x=`cat <<EOF'$nl'te${y}t'${nl}EOF$nl'`; echo $x' \
+ 'text' 0
+ check "y=${y};"'x=`cat <<\EOF'$nl'te${y}t'${nl}EOF$nl'`; echo $x' \
+ 'te${y}t' 0
+ check "y=${y};"'x=`cat <<"EOF"'$nl'te${y}t'${nl}EOF$nl'`; echo $x' \
+ 'te${y}t' 0
+ check "y=${y};"'x=`cat <<'"'EOF'"$nl'te${y}t'${nl}EOF$nl'`; echo $x' \
+ 'te${y}t' 0
+
+ # check that quotes in the here doc survive and cause no problems
+ check "cat <<EOF${nl}te'xt${nl}EOF$nl" "te'xt" 0
+ check "cat <<\EOF${nl}te'xt${nl}EOF$nl" "te'xt" 0
+ check "cat <<'EOF'${nl}te'xt${nl}EOF$nl" "te'xt" 0
+ check "cat <<EOF${nl}te\"xt${nl}EOF$nl" 'te"xt' 0
+ check "cat <<\EOF${nl}te\"xt${nl}EOF$nl" 'te"xt' 0
+ check "cat <<'EOF'${nl}te\"xt${nl}EOF$nl" 'te"xt' 0
+ check "cat <<'EO'F${nl}te\"xt${nl}EOF$nl" 'te"xt' 0
+
+ check "y=${y};"'x=`cat <<EOF'$nl'te'"'"'${y}t'${nl}EOF$nl'`; echo $x' \
+ 'te'"'"'xt' 0
+ check "y=${y};"'x=`cat <<EOF'$nl'te'"''"'${y}t'${nl}EOF$nl'`; echo $x' \
+ 'te'"''"'xt' 0
+
+ # note that the blocks of empty space in the following must
+ # be entirely tab characters, no spaces.
+
+ 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
+ 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
+ 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
+}
+
+atf_test_case incomplete
+incomplete_head() {
+ atf_set "descr" "Basic tests for incomplete here documents"
+}
+incomplete_body() {
+ check 'cat <<EOF' '' 2
+ check 'cat <<- EOF' '' 2
+ check 'cat <<\EOF' '' 2
+ check 'cat <<- \EOF' '' 2
+
+ check 'cat <<EOF'"${nl}" '' 2
+ check 'cat <<- EOF'"${nl}" '' 2
+ check 'cat <<'"'EOF'${nl}" '' 2
+ check 'cat <<- "EOF"'"${nl}" '' 2
+
+ check 'cat << EOF'"${nl}${nl}" '' 2
+ check 'cat <<-EOF'"${nl}${nl}" '' 2
+ check 'cat << '"'EOF'${nl}${nl}" '' 2
+ check 'cat <<-"EOF"'"${nl}${nl}" '' 2
+
+ check 'cat << EOF'"${nl}"'line 1'"${nl}" '' 2
+ check 'cat <<-EOF'"${nl}"' line 1'"${nl}" '' 2
+ check 'cat << EOF'"${nl}"'line 1'"${nl}"' line 2'"${nl}" '' 2
+ 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
+}
- check 'x=`cat <<EOF'$nl'te${y}t'${nl}EOF$nl'`; echo $x' 'text'
- check 'x=`cat <<\EOF'$nl'te${y}t'${nl}EOF$nl'`; echo $x' 'te${y}t'
- check 'x=`cat <<"EOF"'$nl'te${y}t'${nl}EOF$nl'`; echo $x' 'te${y}t'
- check 'x=`cat <<'"'EOF'"$nl'te${y}t'${nl}EOF$nl'`; echo $x' 'te${y}t'
+atf_test_case multiple
+multiple_head() {
+ atf_set "descr" "Tests for multiple here documents for one cmd"
+}
+multiple_body() {
+ check \
+ "(cat ; cat <&3) <<EOF0 3<<EOF3${nl}STDIN${nl}EOF0${nl}-3-${nl}EOF3${nl}" \
+ 'STDIN -3-' 0
+
+ check "(read line; echo \"\$line\"; cat <<EOF1; echo \"\$line\") <<EOF2
+The File
+EOF1
+The Line
+EOF2
+" 'The Line The File The Line' 0
+
+ check "(read line; echo \"\$line\"; cat <<EOF; echo \"\$line\") <<EOF
+The File
+EOF
+The Line
+EOF
+" 'The Line The File The Line' 0
+
+}
+
+atf_test_case viscious
+viscious_head() {
+ atf_set "descr" "Tests for obscure and obnoxious uses of here docs"
+}
+viscious_body() {
+
+ cat <<- \END_SCRIPT > script
+ cat <<ONE && cat \
+ <<TWO
+ a
+ ONE
+ b
+ TWO
+ END_SCRIPT
+
+ atf_check -s exit:0 -o inline:'a\nb\n' -e empty ${TEST_SH} script
- check 'x=`cat <<EOF'$nl'te'"'"'xt'${nl}EOF$nl'`; echo $x' 'te'"'"'xt'
- check 'x=`cat <<\EOF'$nl'te'"'"'xt'${nl}EOF$nl'`; echo $x' 'te'"'"'xt'
- check 'x=`cat <<"EOF"'$nl'te'"'"'xt'${nl}EOF$nl'`; echo $x' 'te'"'"'xt'
+ # This next one is causing discussion currently (late Feb 2016)
+ # amongst stds writers & implementors. Consequently we
+ # will not check what it produces. The eventual result
+ # seems unlikely to be what we currently output, which
+ # is:
+ # :echo line 1
+ # B:echo line 2)" && prefix DASH_CODE <<DASH_CODE
+ # B:echo line 3
+ # line 4
+ # line 5
+ #
+ # The likely intended output is ...
+ #
+ # A:echo line 3
+ # B:echo line 1
+ # line 2
+ # DASH_CODE:echo line 4)"
+ # DASH_CODE:echo line 5
+ #
+ # The difference is explained by differeng opinions on just
+ # when processing of a here doc should start
- check 'x=`cat <<EOF'$nl'te'"'"'${y}t'${nl}EOF$nl'`; echo $x' 'te'"'"'xt'
- check 'x=`cat <<EOF'$nl'te'"''"'${y}t'${nl}EOF$nl'`; echo $x' 'te'"''"'xt'
+ cat <<- \END_SCRIPT > script
+ prefix() { sed -e "s/^/$1:/"; }
+ DASH_CODE() { :; }
+
+ prefix A <<XXX && echo "$(prefix B <<XXX
+ echo line 1
+ XXX
+ echo line 2)" && prefix DASH_CODE <<DASH_CODE
+ echo line 3
+ XXX
+ echo line 4)"
+ echo line 5
+ DASH_CODE
+ END_SCRIPT
+
+ # we will just verify that the shell can parse the
+ # script somehow, and doesn't fall over completely...
+
+ atf_check -s exit:0 -o ignore -e empty ${TEST+SH} script
}
atf_init_test_cases() {
- atf_add_test_case all
+ atf_add_test_case do_simple
+ atf_add_test_case incomplete
+ atf_add_test_case multiple # multiple << operators on one cmd
+ atf_add_test_case viscious # evil test from the austin-l list...
}
Home |
Main Index |
Thread Index |
Old Index