Home | History | Annotate | Line # | Download | only in sh
t_here.sh revision 1.5
      1  1.5  christos # $NetBSD: t_here.sh,v 1.5 2016/03/27 14:52:40 christos Exp $
      2  1.1    jruoho #
      3  1.1    jruoho # Copyright (c) 2007 The NetBSD Foundation, Inc.
      4  1.1    jruoho # All rights reserved.
      5  1.1    jruoho #
      6  1.1    jruoho # Redistribution and use in source and binary forms, with or without
      7  1.1    jruoho # modification, are permitted provided that the following conditions
      8  1.1    jruoho # are met:
      9  1.1    jruoho # 1. Redistributions of source code must retain the above copyright
     10  1.1    jruoho #    notice, this list of conditions and the following disclaimer.
     11  1.1    jruoho # 2. Redistributions in binary form must reproduce the above copyright
     12  1.1    jruoho #    notice, this list of conditions and the following disclaimer in the
     13  1.1    jruoho #    documentation and/or other materials provided with the distribution.
     14  1.1    jruoho #
     15  1.1    jruoho # THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
     16  1.1    jruoho # ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
     17  1.1    jruoho # TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
     18  1.1    jruoho # PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
     19  1.1    jruoho # BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
     20  1.1    jruoho # CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
     21  1.1    jruoho # SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
     22  1.1    jruoho # INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
     23  1.1    jruoho # CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
     24  1.1    jruoho # ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
     25  1.1    jruoho # POSSIBILITY OF SUCH DAMAGE.
     26  1.1    jruoho #
     27  1.2  christos # the implementation of "sh" to test
     28  1.2  christos : ${TEST_SH:="/bin/sh"}
     29  1.1    jruoho 
     30  1.1    jruoho nl='
     31  1.1    jruoho '
     32  1.1    jruoho 
     33  1.5  christos reset()
     34  1.5  christos {
     35  1.5  christos 	TEST_NUM=0
     36  1.5  christos 	TEST_FAILURES=''
     37  1.5  christos 	TEST_FAIL_COUNT=0
     38  1.5  christos 	TEST_ID="$1"
     39  1.5  christos }
     40  1.5  christos 
     41  1.1    jruoho check()
     42  1.1    jruoho {
     43  1.2  christos 	fail=false
     44  1.2  christos 	TEMP_FILE=$( mktemp OUT.XXXXXX )
     45  1.5  christos 	TEST_NUM=$(( $TEST_NUM + 1 ))
     46  1.2  christos 
     47  1.2  christos 	# our local shell (ATF_SHELL) better do quoting correctly...
     48  1.2  christos 	# some of the tests expect us to expand $nl internally...
     49  1.2  christos 	CMD="nl='${nl}'; $1"
     50  1.2  christos 
     51  1.2  christos 	result="$( ${TEST_SH} -c "${CMD}" 2>"${TEMP_FILE}" )"
     52  1.2  christos 	STATUS=$?
     53  1.2  christos 
     54  1.2  christos 	if [ "${STATUS}" -ne "$3" ]; then
     55  1.5  christos 		echo >&2 "[$TEST_NUM] expected exit code $3, got ${STATUS}"
     56  1.2  christos 
     57  1.2  christos 		# don't actually fail just because of wrong exit code
     58  1.2  christos 		# unless we either expected, or received "good"
     59  1.2  christos 		case "$3/${STATUS}" in
     60  1.2  christos 		(*/0|0/*) fail=true;;
     61  1.2  christos 		esac
     62  1.2  christos 	fi
     63  1.2  christos 
     64  1.2  christos 	if [ "$3" -eq 0 ]; then
     65  1.2  christos 		if [ -s "${TEMP_FILE}" ]; then
     66  1.5  christos 			echo >&2 \
     67  1.5  christos 			 "[$TEST_NUM] Messages produced on stderr unexpected..."
     68  1.2  christos 			cat "${TEMP_FILE}" >&2
     69  1.2  christos 			fail=true
     70  1.2  christos 		fi
     71  1.2  christos 	else
     72  1.2  christos 		if ! [ -s "${TEMP_FILE}" ]; then
     73  1.5  christos 			echo >&2 \
     74  1.5  christos 		    "[$TEST_NUM] Expected messages on stderr, nothing produced"
     75  1.2  christos 			fail=true
     76  1.2  christos 		fi
     77  1.2  christos 	fi
     78  1.2  christos 	rm -f "${TEMP_FILE}"
     79  1.2  christos 
     80  1.2  christos 	# Remove newlines (use local shell for this)
     81  1.1    jruoho 	oifs="$IFS"
     82  1.1    jruoho 	IFS="$nl"
     83  1.1    jruoho 	result="$(echo $result)"
     84  1.1    jruoho 	IFS="$oifs"
     85  1.1    jruoho 	if [ "$2" != "$result" ]
     86  1.1    jruoho 	then
     87  1.5  christos 		echo >&2 "[$TEST_NUM] Expected output '$2', received '$result'"
     88  1.2  christos 		fail=true
     89  1.1    jruoho 	fi
     90  1.2  christos 
     91  1.5  christos 	$fail && test -n "$TEST_ID" && {
     92  1.5  christos 		TEST_FAILURES="${TEST_FAILURES}${TEST_FAILURES:+
     93  1.5  christos }${TEST_ID}[$TEST_NUM]: test of '$1' failed";
     94  1.5  christos 		TEST_FAIL_COUNT=$(( $TEST_FAIL_COUNT + 1 ))
     95  1.5  christos 		return 0
     96  1.5  christos 	}
     97  1.5  christos 	$fail && atf_fail "Test[$TEST_NUM] of '$1' failed"
     98  1.2  christos 	return 0
     99  1.1    jruoho }
    100  1.1    jruoho 
    101  1.5  christos results()
    102  1.5  christos {
    103  1.5  christos 	test -z "${TEST_ID}" && return 0
    104  1.5  christos 	test -z "${TEST_FAILURES}" && return 0
    105  1.5  christos 
    106  1.5  christos 	echo >&2 "=========================================="
    107  1.5  christos 	echo >&2 "While testing '${TEST_ID}'"
    108  1.5  christos 	echo >&2 " - - - - - - - - - - - - - - - - -"
    109  1.5  christos 	echo >&2 "${TEST_FAILURES}"
    110  1.5  christos 	atf_fail \
    111  1.5  christos  "Test ${TEST_ID}: $TEST_FAIL_COUNT subtests (of $TEST_NUM) failed - see stderr"
    112  1.5  christos }
    113  1.5  christos 
    114  1.2  christos atf_test_case do_simple
    115  1.2  christos do_simple_head() {
    116  1.1    jruoho 	atf_set "descr" "Basic tests for here documents"
    117  1.1    jruoho }
    118  1.2  christos do_simple_body() {
    119  1.1    jruoho 	y=x
    120  1.1    jruoho 
    121  1.5  christos 	reset 'simple'
    122  1.5  christos 	IFS=' 	'
    123  1.2  christos 	check 'x=`cat <<EOF'$nl'text'${nl}EOF$nl'`; echo $x' 'text' 0
    124  1.2  christos 	check 'x=`cat <<\EOF'$nl'text'${nl}EOF$nl'`; echo $x' 'text' 0
    125  1.2  christos 
    126  1.2  christos 	check "y=${y};"'x=`cat <<EOF'$nl'te${y}t'${nl}EOF$nl'`; echo $x' \
    127  1.2  christos 			'text' 0
    128  1.2  christos 	check "y=${y};"'x=`cat <<\EOF'$nl'te${y}t'${nl}EOF$nl'`; echo $x'  \
    129  1.2  christos 			'te${y}t' 0
    130  1.2  christos 	check "y=${y};"'x=`cat <<"EOF"'$nl'te${y}t'${nl}EOF$nl'`; echo $x'  \
    131  1.2  christos 			'te${y}t' 0
    132  1.2  christos 	check "y=${y};"'x=`cat <<'"'EOF'"$nl'te${y}t'${nl}EOF$nl'`; echo $x'  \
    133  1.2  christos 			'te${y}t' 0
    134  1.2  christos 
    135  1.2  christos 	# check that quotes in the here doc survive and cause no problems
    136  1.2  christos 	check "cat <<EOF${nl}te'xt${nl}EOF$nl" "te'xt" 0
    137  1.2  christos 	check "cat <<\EOF${nl}te'xt${nl}EOF$nl" "te'xt" 0
    138  1.2  christos 	check "cat <<'EOF'${nl}te'xt${nl}EOF$nl" "te'xt" 0
    139  1.2  christos 	check "cat <<EOF${nl}te\"xt${nl}EOF$nl" 'te"xt' 0
    140  1.2  christos 	check "cat <<\EOF${nl}te\"xt${nl}EOF$nl" 'te"xt' 0
    141  1.2  christos 	check "cat <<'EOF'${nl}te\"xt${nl}EOF$nl" 'te"xt' 0
    142  1.2  christos 	check "cat <<'EO'F${nl}te\"xt${nl}EOF$nl" 'te"xt' 0
    143  1.2  christos 
    144  1.2  christos 	check "y=${y};"'x=`cat <<EOF'$nl'te'"'"'${y}t'${nl}EOF$nl'`; echo $x' \
    145  1.2  christos 			'te'"'"'xt' 0
    146  1.2  christos 	check "y=${y};"'x=`cat <<EOF'$nl'te'"''"'${y}t'${nl}EOF$nl'`; echo $x' \
    147  1.2  christos 			'te'"''"'xt' 0
    148  1.2  christos 
    149  1.2  christos 	# note that the blocks of empty space in the following must
    150  1.2  christos 	# be entirely tab characters, no spaces.
    151  1.2  christos 
    152  1.2  christos 	check 'x=`cat <<EOF'"$nl	text${nl}EOF$nl"'`; echo "$x"' \
    153  1.2  christos 			'	text' 0
    154  1.2  christos 	check 'x=`cat <<-EOF'"$nl	text${nl}EOF$nl"'`; echo $x' \
    155  1.2  christos 			'text' 0
    156  1.2  christos 	check 'x=`cat <<-EOF'"${nl}text${nl}	EOF$nl"'`; echo $x' \
    157  1.2  christos 			'text' 0
    158  1.2  christos 	check 'x=`cat <<-\EOF'"$nl	text${nl}	EOF$nl"'`; echo $x' \
    159  1.2  christos 			'text' 0
    160  1.2  christos 	check 'x=`cat <<- "EOF"'"$nl	text${nl}EOF$nl"'`; echo $x' \
    161  1.2  christos 			'text' 0
    162  1.2  christos 	check 'x=`cat <<- '"'EOF'${nl}text${nl}	EOF$nl"'`; echo $x' \
    163  1.2  christos 			'text' 0
    164  1.5  christos 	results
    165  1.5  christos }
    166  1.5  christos 
    167  1.5  christos atf_test_case end_markers
    168  1.5  christos end_markers_head() {
    169  1.5  christos 	atf_set "descr" "Tests for various end markers of here documents"
    170  1.5  christos }
    171  1.5  christos end_markers_body() {
    172  1.5  christos 
    173  1.5  christos 	reset 'end_markers'
    174  1.5  christos 	for end in EOF 1 \! '$$$' "string " a\\\  '&' '' ' ' '  ' --STRING-- . '~~~' \
    175  1.5  christos VERYVERYVERYVERYLONGLONGLONGin_fact_absurdly_LONG_LONG_HERE_DOCUMENT_TERMINATING_MARKER_THAT_goes_On_forever_and_ever_and_ever...
    176  1.5  christos 	do
    177  1.5  christos 		# check unquoted end markers
    178  1.5  christos 		case "${end}" in
    179  1.5  christos 		('' | *[' $&#*~']* ) ;;	# skip unquoted endmark test for these
    180  1.5  christos 		(*)	check \
    181  1.5  christos 	'x=$(cat << '"${end}${nl}text${nl}${end}${nl}"'); echo "$x"' 'text' 0
    182  1.5  christos 			;;
    183  1.5  christos 		esac
    184  1.5  christos 
    185  1.5  christos 		# and quoted end markers
    186  1.5  christos 		check \
    187  1.5  christos 	'x=$(cat <<'"'${end}'${nl}text${nl}${end}${nl}"'); echo "$x"' 'text' 0
    188  1.5  christos 
    189  1.5  christos 		# and see what happens if we encounter "almost" an end marker
    190  1.5  christos 		case "${#end}" in
    191  1.5  christos 		(0|1)	;;		# too short to try truncation tests
    192  1.5  christos 		(*)	check \
    193  1.5  christos    'x=$(cat <<'"'${end}'${nl}text${nl}${end%?}${nl}${end}${nl}"'); echo "$x"' \
    194  1.5  christos 				"text ${end%?}" 0
    195  1.5  christos 			check \
    196  1.5  christos    'x=$(cat <<'"'${end}'${nl}text${nl}${end#?}${nl}${end}${nl}"'); echo "$x"' \
    197  1.5  christos 				"text ${end#?}" 0
    198  1.5  christos 			check \
    199  1.5  christos    'x=$(cat <<'"'${end}'${nl}text${nl}${end%?}+${nl}${end}${nl}"');echo "$x"' \
    200  1.5  christos 				"text ${end%?}+" 0
    201  1.5  christos 			;;
    202  1.5  christos 		esac
    203  1.5  christos 
    204  1.5  christos 		# or something that is a little longer
    205  1.5  christos 		check \
    206  1.5  christos    'x=$(cat <<'"'${end}'${nl}text${nl}${end}x${nl}${end}${nl}"'); echo "$x"' \
    207  1.5  christos 				"text ${end}x" 0
    208  1.5  christos 		check \
    209  1.5  christos     'x=$(cat <<'"'${end}'${nl}text${nl}!${end}${nl}${end}${nl}"'); echo "$x"' \
    210  1.5  christos 				"text !${end}" 0
    211  1.5  christos 
    212  1.5  christos 		# or which does not begin at start of line
    213  1.5  christos 		check \
    214  1.5  christos     'x=$(cat <<'"'${end}'${nl}text${nl} ${end}${nl}${end}${nl}"'); echo "$x"' \
    215  1.5  christos 				"text  ${end}" 0
    216  1.5  christos 		check \
    217  1.5  christos     'x=$(cat <<'"'${end}'${nl}text${nl}	${end}${nl}${end}${nl}"'); echo "$x"' \
    218  1.5  christos 				"text 	${end}" 0
    219  1.5  christos 
    220  1.5  christos 		# or end at end of line
    221  1.5  christos 		check \
    222  1.5  christos     'x=$(cat <<'"'${end}'${nl}text${nl}${end} ${nl}${end}${nl}"'); echo "$x"' \
    223  1.5  christos 				"text ${end} " 0
    224  1.5  christos 
    225  1.5  christos 		# or something that is correct much of the way, but then...
    226  1.5  christos 
    227  1.5  christos 		case "${#end}" in
    228  1.5  christos 		(0)	;;		# cannot test this one
    229  1.5  christos 		(1)	check \
    230  1.5  christos     'x=$(cat <<'"'${end}'${nl}text${nl}${end}${end}${nl}${end}${nl}"'); echo "$x"' \
    231  1.5  christos 				"text ${end}${end}" 0
    232  1.5  christos 			;;
    233  1.5  christos 		(2-7)	pfx="${end%?}"
    234  1.5  christos 			check \
    235  1.5  christos     'x=$(cat <<'"'${end}'${nl}text${nl}${end}${pfx}${nl}${end}${nl}"'); echo "$x"' \
    236  1.5  christos 				"text ${end}${pfx}" 0
    237  1.5  christos 			check \
    238  1.5  christos     'x=$(cat <<'"'${end}'${nl}text${nl}${pfx}${end}${nl}${end}${nl}"'); echo "$x"' \
    239  1.5  christos 				"text ${pfx}${end}" 0
    240  1.5  christos 			;;
    241  1.5  christos 		(*)	pfx=${end%??????}; sfx=${end#??????}
    242  1.5  christos 			check \
    243  1.5  christos     'x=$(cat <<'"'${end}'${nl}text${nl}${end}${sfx}${nl}${end}${nl}"'); echo "$x"' \
    244  1.5  christos 				"text ${end}${sfx}" 0
    245  1.5  christos 			check \
    246  1.5  christos     'x=$(cat <<'"'${end}'${nl}text${nl}${pfx}${end}${nl}${end}${nl}"'); echo "$x"' \
    247  1.5  christos 				"text ${pfx}${end}" 0
    248  1.5  christos 			check \
    249  1.5  christos     'x=$(cat <<'"'${end}'${nl}text${nl}${pfx}${sfx}${nl}${end}${nl}"'); echo "$x"' \
    250  1.5  christos 				"text ${pfx}${sfx}" 0
    251  1.5  christos 			;;
    252  1.5  christos 		esac
    253  1.5  christos 	done
    254  1.5  christos 
    255  1.5  christos 	# Add striptabs tests (in similar way) here one day...
    256  1.5  christos 
    257  1.5  christos 	results
    258  1.2  christos }
    259  1.2  christos 
    260  1.2  christos atf_test_case incomplete
    261  1.2  christos incomplete_head() {
    262  1.2  christos 	atf_set "descr" "Basic tests for incomplete here documents"
    263  1.2  christos }
    264  1.2  christos incomplete_body() {
    265  1.5  christos 	reset incomplete
    266  1.5  christos 
    267  1.2  christos 	check 'cat <<EOF' '' 2
    268  1.2  christos 	check 'cat <<- EOF' '' 2
    269  1.2  christos 	check 'cat <<\EOF' '' 2
    270  1.2  christos 	check 'cat <<- \EOF' '' 2
    271  1.2  christos 
    272  1.2  christos 	check 'cat <<EOF'"${nl}" '' 2
    273  1.2  christos 	check 'cat <<- EOF'"${nl}" '' 2
    274  1.2  christos 	check 'cat <<'"'EOF'${nl}" '' 2
    275  1.2  christos 	check 'cat <<- "EOF"'"${nl}" '' 2
    276  1.2  christos 
    277  1.2  christos 	check 'cat << EOF'"${nl}${nl}" '' 2
    278  1.2  christos 	check 'cat <<-EOF'"${nl}${nl}" '' 2
    279  1.2  christos 	check 'cat << '"'EOF'${nl}${nl}" '' 2
    280  1.2  christos 	check 'cat <<-"EOF"'"${nl}${nl}" '' 2
    281  1.2  christos 
    282  1.2  christos 	check 'cat << EOF'"${nl}"'line 1'"${nl}" '' 2
    283  1.2  christos 	check 'cat <<-EOF'"${nl}"'	line 1'"${nl}" '' 2
    284  1.2  christos 	check 'cat << EOF'"${nl}"'line 1'"${nl}"'	line 2'"${nl}" '' 2
    285  1.2  christos 	check 'cat <<-EOF'"${nl}"'	line 1'"${nl}"'line 2'"${nl}" '' 2
    286  1.2  christos 
    287  1.2  christos 	check 'cat << EOF'"${nl}line 1${nl}${nl}line3${nl}${nl}5!${nl}" '' 2
    288  1.5  christos 
    289  1.5  christos 	results
    290  1.5  christos }
    291  1.5  christos 
    292  1.5  christos atf_test_case lineends
    293  1.5  christos lineends_head() {
    294  1.5  christos 	atf_set "descr" "Tests for line endings in here documents"
    295  1.5  christos }
    296  1.5  christos lineends_body() {
    297  1.5  christos 	reset lineends
    298  1.5  christos 
    299  1.5  christos 	# note that "check" removes newlines from stdout before comparing.
    300  1.5  christos 	# (they become blanks, provided there is something before & after)
    301  1.5  christos 
    302  1.5  christos 	check 'cat << \echo'"${nl}"'\'"${nl}echo${nl}echo${nl}" '\' 0
    303  1.5  christos 	check 'cat <<  echo'"${nl}"'\'"${nl}echo${nl}echo${nl}" 'echo' 0
    304  1.5  christos 	check 'cat << echo'"${nl}"'\\'"${nl}echo${nl}echo${nl}" '\' 0
    305  1.5  christos 
    306  1.5  christos 	check 'X=3; cat << ec\ho'"${nl}"'$X\'"${nl}echo${nl}echo${nl}" \
    307  1.5  christos 		'$X\'  0
    308  1.5  christos 	check 'X=3; cat <<  echo'"${nl}"'$X'"${nl}echo${nl}echo${nl}" \
    309  1.5  christos 		'3'  0
    310  1.5  christos 	check 'X=3; cat <<  echo'"${nl}"'$X\'"${nl}echo${nl}echo${nl}" \
    311  1.5  christos 		''  0
    312  1.5  christos 	check 'X=3; cat <<  echo'"${nl}"'${X}\'"${nl}echo${nl}echo${nl}" \
    313  1.5  christos 		'3echo'  0
    314  1.5  christos 	check 'X=3; cat <<  echo'"${nl}"'\$X\'"${nl}echo${nl}echo${nl}" \
    315  1.5  christos 		'$Xecho'  0
    316  1.5  christos 	check 'X=3; cat <<  echo'"${nl}"'\\$X \'"${nl}echo${nl}echo${nl}" \
    317  1.5  christos 		'\3 echo'  0
    318  1.5  christos 
    319  1.5  christos 	check \
    320  1.5  christos   'cat << "echo"'"${nl}"'line1\'"${nl}"'line2\'"${nl}echo${nl}echo${nl}" \
    321  1.5  christos 		 'line1\ line2\'  0
    322  1.5  christos 	check \
    323  1.5  christos 	  'cat << echo'"${nl}"'line1\'"${nl}"'line2\'"${nl}echo${nl}echo${nl}" \
    324  1.5  christos 	  'line1line2echo'  0
    325  1.5  christos 
    326  1.5  christos 	results
    327  1.2  christos }
    328  1.2  christos 
    329  1.2  christos atf_test_case multiple
    330  1.2  christos multiple_head() {
    331  1.5  christos 	atf_set "descr" "Tests for multiple here documents on one cmd line"
    332  1.2  christos }
    333  1.2  christos multiple_body() {
    334  1.5  christos 	reset multiple
    335  1.5  christos 
    336  1.2  christos 	check \
    337  1.2  christos     "(cat ; cat <&3) <<EOF0 3<<EOF3${nl}STDIN${nl}EOF0${nl}-3-${nl}EOF3${nl}" \
    338  1.2  christos 		'STDIN -3-' 0
    339  1.2  christos 
    340  1.2  christos 	check "(read line; echo \"\$line\"; cat <<EOF1; echo \"\$line\") <<EOF2
    341  1.2  christos The File
    342  1.2  christos EOF1
    343  1.2  christos The Line
    344  1.2  christos EOF2
    345  1.2  christos "			'The Line The File The Line' 0
    346  1.2  christos 
    347  1.2  christos 	check "(read line; echo \"\$line\"; cat <<EOF; echo \"\$line\") <<EOF
    348  1.2  christos The File
    349  1.2  christos EOF
    350  1.2  christos The Line
    351  1.2  christos EOF
    352  1.2  christos "			'The Line The File The Line' 0
    353  1.2  christos 
    354  1.5  christos 	check "V=1; W=2; cat <<-1; cat <<2; cat <<- 3; cat <<'4';"' cat <<\5
    355  1.5  christos 		$V
    356  1.5  christos 		$W
    357  1.5  christos 		3
    358  1.5  christos 	4
    359  1.5  christos 	5
    360  1.5  christos 			1
    361  1.5  christos 2
    362  1.5  christos 	5
    363  1.5  christos 					4*$W+\$V
    364  1.5  christos 	3
    365  1.5  christos $W
    366  1.5  christos 1
    367  1.5  christos 2
    368  1.5  christos 3
    369  1.5  christos 4
    370  1.5  christos 7+$V
    371  1.5  christos $W+6
    372  1.5  christos 5
    373  1.5  christos '			'1 2 3 4 5 5 4*2+$V $W 1 2 3 7+$V $W+6'	0
    374  1.5  christos 
    375  1.5  christos 	results
    376  1.5  christos }
    377  1.5  christos 
    378  1.5  christos atf_test_case nested
    379  1.5  christos nested_head() {
    380  1.5  christos 	atf_set "descr" "Tests for nested here documents for one cmd"
    381  1.5  christos }
    382  1.5  christos nested_body() {
    383  1.5  christos 	reset nested
    384  1.5  christos 
    385  1.5  christos 	check \
    386  1.5  christos 'cat << EOF1'"${nl}"'$(cat << EOF2'"${nl}LINE${nl}EOF2${nl}"')'"${nl}EOF1${nl}"\
    387  1.5  christos 	'LINE' 0
    388  1.5  christos 
    389  1.5  christos # This next one fails ... and correctly, so we will omit it (bad test)
    390  1.5  christos # Reasoning is that the correct data "$(cat << EOF2)\nLINE\nEOF2\n" is
    391  1.5  christos # collected for the outer (EOF1) heredoc, when that is parsed, it looks
    392  1.5  christos # like
    393  1.5  christos #	$(cat <<EOF2)
    394  1.5  christos #	LINE
    395  1.5  christos #	EOF2
    396  1.5  christos # which looks like a good command - except it is being parsed in "heredoc"
    397  1.5  christos # syntax, which means it is enclosed in double quotes, which means that
    398  1.5  christos # the newline after the ')' in the first line is not a newline token, but
    399  1.5  christos # just a character.  The EOF2 heredoc cannot start until after the next
    400  1.5  christos # newline token, of which there are none here...  LINE and EOF2 are just
    401  1.5  christos # more data in the outer EOF1 heredoc for its "cat" command to read & write.
    402  1.5  christos #
    403  1.5  christos # The previous sub-test works because there the \n comes inside the
    404  1.5  christos # $( ), and in there, the outside quoting rules are suspended, and it
    405  1.5  christos # all starts again - so that \n is a newline token, and the EOF2 heredoc
    406  1.5  christos # is processed.
    407  1.5  christos #
    408  1.5  christos #	check \
    409  1.5  christos #   'cat << EOF1'"${nl}"'$(cat << EOF2 )'"${nl}LINE${nl}EOF2${nl}EOF1${nl}" \
    410  1.5  christos #	'LINE' 0
    411  1.5  christos 
    412  1.5  christos 	L='cat << EOF1'"${nl}"'LINE1$(cat << EOF2'"${nl}"
    413  1.5  christos 	L="${L}"'LINE2$(cat << EOF3'"${nl}"
    414  1.5  christos 	L="${L}"'LINE3$(cat << EOF4'"${nl}"
    415  1.5  christos 	L="${L}"'LINE4$(cat << EOF5'"${nl}"
    416  1.5  christos 	L="${L}LINE5${nl}EOF5${nl})4${nl}EOF4${nl})3${nl}"
    417  1.5  christos 	L="${L}EOF3${nl})2${nl}EOF2${nl})1${nl}EOF1${nl}"
    418  1.5  christos 
    419  1.5  christos 	# That mess is ...
    420  1.5  christos 	#
    421  1.5  christos 	#	cat <<EOF1
    422  1.5  christos 	#	LINE1$(cat << EOF2
    423  1.5  christos 	#	LINE2$(cat << EOF3
    424  1.5  christos 	#	LINE3$(cat << EOF4
    425  1.5  christos 	#	LINE4$(cat << EOF5
    426  1.5  christos 	#	LINE5
    427  1.5  christos 	#	EOF5
    428  1.5  christos 	#	)4
    429  1.5  christos 	#	EOF4
    430  1.5  christos 	#	)3
    431  1.5  christos 	#	EOF3
    432  1.5  christos 	#	)2
    433  1.5  christos 	#	EOF2
    434  1.5  christos 	#	)1
    435  1.5  christos 	#	EOF1
    436  1.5  christos 
    437  1.5  christos 	check "${L}" 'LINE1LINE2LINE3LINE4LINE54321' 0
    438  1.5  christos 
    439  1.5  christos 	results
    440  1.5  christos }
    441  1.5  christos 
    442  1.5  christos atf_test_case quoting
    443  1.5  christos quoting_head() {
    444  1.5  christos 	atf_set "descr" "Tests for use of quotes inside here documents"
    445  1.5  christos }
    446  1.5  christos quoting_body() {
    447  1.5  christos 	reset quoting
    448  1.5  christos 
    449  1.5  christos 	check 'X=!; cat <<- E\0F
    450  1.5  christos 		<'\''"'\'' \\$X\$X  "'\''" \\>
    451  1.5  christos 	E0F
    452  1.5  christos 	'	'<'\''"'\'' \\$X\$X  "'\''" \\>'	0
    453  1.5  christos 
    454  1.5  christos 	check 'X=!; cat <<- E0F
    455  1.5  christos 		<'\''"'\'' \\$X\$X  "'\''" \\>
    456  1.5  christos 	E0F
    457  1.5  christos 	'	'<'\''"'\'' \!$X  "'\''" \>'	0
    458  1.5  christos 
    459  1.5  christos 	check 'cat <<- END
    460  1.5  christos 		$( echo "'\''" ) $( echo '\''"'\'' ) $( echo \\ )
    461  1.5  christos 	END
    462  1.5  christos 	'	"' \" \\"		0
    463  1.5  christos 
    464  1.5  christos 	check 'X=12345; Y="string1 line1?-line2"; Z=; unset W; cat <<-EOF
    465  1.5  christos 		${#X}${Z:-${Y}}${W+junk}${Y%%l*}${Y#*\?}
    466  1.5  christos 		"$Z"'\''$W'\'' ${Y%" "*} $(( X + 54321 ))
    467  1.5  christos 	EOF
    468  1.5  christos 	'	'5string1 line1?-line2string1 -line2 ""'\'\'' string1 66666' 0
    469  1.5  christos 
    470  1.5  christos 	results
    471  1.5  christos }
    472  1.5  christos 
    473  1.5  christos atf_test_case side_effects
    474  1.5  christos side_effects_head() {
    475  1.5  christos 	atf_set "descr" "Tests how side effects in here documents are handled"
    476  1.5  christos }
    477  1.5  christos side_effects_body() {
    478  1.5  christos 
    479  1.5  christos 	atf_check -s exit:0 -o inline:'2\n1\n' -e empty ${TEST_SH} -c '
    480  1.5  christos 		unset X
    481  1.5  christos 		cat <<-EOF
    482  1.5  christos 		${X=2}
    483  1.5  christos 		EOF
    484  1.5  christos 		echo "${X-1}"
    485  1.5  christos 		'
    486  1.2  christos }
    487  1.2  christos 
    488  1.3  christos atf_test_case vicious
    489  1.3  christos vicious_head() {
    490  1.2  christos 	atf_set "descr" "Tests for obscure and obnoxious uses of here docs"
    491  1.2  christos }
    492  1.3  christos vicious_body() {
    493  1.5  christos 	reset
    494  1.2  christos 
    495  1.2  christos 	cat <<- \END_SCRIPT > script
    496  1.2  christos 		cat <<ONE && cat \
    497  1.2  christos 		<<TWO
    498  1.2  christos 		a
    499  1.2  christos 		ONE
    500  1.2  christos 		b
    501  1.2  christos 		TWO
    502  1.2  christos 	END_SCRIPT
    503  1.2  christos 
    504  1.2  christos 	atf_check -s exit:0 -o inline:'a\nb\n' -e empty ${TEST_SH} script
    505  1.2  christos 
    506  1.2  christos 	# This next one is causing discussion currently (late Feb 2016)
    507  1.2  christos 	# amongst stds writers & implementors.   Consequently we
    508  1.2  christos 	# will not check what it produces.   The eventual result
    509  1.2  christos 	# seems unlikely to be what we currently output, which
    510  1.2  christos 	# is:
    511  1.3  christos 	#	A:echo line 1
    512  1.2  christos 	#	B:echo line 2)" && prefix DASH_CODE <<DASH_CODE
    513  1.2  christos 	#	B:echo line 3
    514  1.2  christos 	#	line 4
    515  1.2  christos 	#	line 5
    516  1.2  christos 	#
    517  1.2  christos 	# The likely intended output is ...
    518  1.2  christos 	#
    519  1.2  christos 	#	A:echo line 3
    520  1.2  christos 	#	B:echo line 1
    521  1.2  christos 	#	line 2
    522  1.2  christos 	#	DASH_CODE:echo line 4)"
    523  1.2  christos 	#	DASH_CODE:echo line 5
    524  1.2  christos 	#
    525  1.5  christos 	# The difference is explained by differing opinions on just
    526  1.2  christos 	# when processing of a here doc should start
    527  1.2  christos 
    528  1.2  christos 	cat <<- \END_SCRIPT > script
    529  1.2  christos 		prefix() { sed -e "s/^/$1:/"; }
    530  1.2  christos 		DASH_CODE() { :; }
    531  1.2  christos 
    532  1.2  christos 		prefix A <<XXX && echo "$(prefix B <<XXX
    533  1.2  christos 		echo line 1
    534  1.2  christos 		XXX
    535  1.2  christos 		echo line 2)" && prefix DASH_CODE <<DASH_CODE
    536  1.2  christos 		echo line 3
    537  1.2  christos 		XXX
    538  1.2  christos 		echo line 4)"
    539  1.2  christos 		echo line 5
    540  1.2  christos 		DASH_CODE
    541  1.2  christos 	END_SCRIPT
    542  1.1    jruoho 
    543  1.2  christos 	# we will just verify that the shell can parse the
    544  1.2  christos 	# script somehow, and doesn't fall over completely...
    545  1.1    jruoho 
    546  1.5  christos 	atf_check -s exit:0 -o ignore -e empty ${TEST_SH} script
    547  1.1    jruoho }
    548  1.1    jruoho 
    549  1.1    jruoho atf_init_test_cases() {
    550  1.5  christos 	atf_add_test_case do_simple	# not worthy of a comment
    551  1.5  christos 	atf_add_test_case end_markers	# the mundane, the weird, the bizarre
    552  1.5  christos 	atf_add_test_case incomplete	# where the end marker isn't...
    553  1.5  christos 	atf_add_test_case lineends	# test weird line endings in heredocs
    554  1.2  christos 	atf_add_test_case multiple	# multiple << operators on one cmd
    555  1.5  christos 	atf_add_test_case nested	# here docs inside here docs
    556  1.5  christos 	atf_add_test_case quoting	# stuff quoted inside
    557  1.5  christos 	atf_add_test_case side_effects	# here docs that modify environment
    558  1.3  christos 	atf_add_test_case vicious	# evil test from the austin-l list...
    559  1.1    jruoho }
    560