Home | History | Annotate | Line # | Download | only in sh
t_cmdsub.sh revision 1.1
      1  1.1  christos # $NetBSD: t_cmdsub.sh,v 1.1 2016/03/20 22:57:04 christos Exp $
      2  1.1  christos #
      3  1.1  christos # Copyright (c) 2016 The NetBSD Foundation, Inc.
      4  1.1  christos # All rights reserved.
      5  1.1  christos #
      6  1.1  christos # Redistribution and use in source and binary forms, with or without
      7  1.1  christos # modification, are permitted provided that the following conditions
      8  1.1  christos # are met:
      9  1.1  christos # 1. Redistributions of source code must retain the above copyright
     10  1.1  christos #    notice, this list of conditions and the following disclaimer.
     11  1.1  christos # 2. Redistributions in binary form must reproduce the above copyright
     12  1.1  christos #    notice, this list of conditions and the following disclaimer in the
     13  1.1  christos #    documentation and/or other materials provided with the distribution.
     14  1.1  christos #
     15  1.1  christos # THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
     16  1.1  christos # ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
     17  1.1  christos # TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
     18  1.1  christos # PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
     19  1.1  christos # BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
     20  1.1  christos # CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
     21  1.1  christos # SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
     22  1.1  christos # INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
     23  1.1  christos # CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
     24  1.1  christos # ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
     25  1.1  christos # POSSIBILITY OF SUCH DAMAGE.
     26  1.1  christos #
     27  1.1  christos # the implementation of "sh" to test
     28  1.1  christos : ${TEST_SH:="/bin/sh"}
     29  1.1  christos 
     30  1.1  christos #
     31  1.1  christos # This file tests command substitutions ( `...` and $( ... ) )
     32  1.1  christos #
     33  1.1  christos # CAUTION:
     34  1.1  christos #	Be careful attempting running these tests outside the ATF environment
     35  1.1  christos #	Some of the tests run "rm *" in the current directory to clean up
     36  1.1  christos #	An ATF test directory should be empty already, outside ATF, anything
     37  1.1  christos 
     38  1.1  christos atf_test_case a_basic_cmdsub
     39  1.1  christos a_basic_cmdsub_head() {
     40  1.1  christos 	atf_set "descr" 'Test operation of simple $( ) substitutions'
     41  1.1  christos }
     42  1.1  christos a_basic_cmdsub_body() {
     43  1.1  christos 	atf_check -s exit:0 -o match:'Result is true today' -e empty \
     44  1.1  christos 	    ${TEST_SH} -c \
     45  1.1  christos 		'echo Result is $( true && echo true || echo false ) today'
     46  1.1  christos 
     47  1.1  christos 	atf_check -s exit:0 -o match:'Result is false today' -e empty \
     48  1.1  christos 	    ${TEST_SH} -c \
     49  1.1  christos 		'echo Result is $( false && echo true || echo false ) today'
     50  1.1  christos 
     51  1.1  christos 	atf_check -s exit:0 -o match:'aaabbbccc' -e empty \
     52  1.1  christos 	    ${TEST_SH} -c 'echo aaa$( echo bbb )ccc'
     53  1.1  christos 	atf_check -s exit:0 -o match:'aaabbb cccddd' -e empty \
     54  1.1  christos 	    ${TEST_SH} -c 'echo aaa$( echo bbb ccc )ddd'
     55  1.1  christos 	atf_check -s exit:0 -o inline:'aaabbb cccddd\n' -e empty \
     56  1.1  christos 	    ${TEST_SH} -c 'echo aaa$( echo bbb; echo ccc )ddd'
     57  1.1  christos 	atf_check -s exit:0 -o inline:'aaabbb\ncccddd\n' -e empty \
     58  1.1  christos 	    ${TEST_SH} -c 'echo "aaa$( echo bbb; echo ccc )ddd"'
     59  1.1  christos 
     60  1.1  christos 	atf_check -s exit:0 -o inline:'some string\n' -e empty \
     61  1.1  christos 	    ${TEST_SH} -c 'X=$( echo some string ); echo "$X"'
     62  1.1  christos 	atf_check -s exit:0 -o inline:'weird; string *\n' -e empty \
     63  1.1  christos 	    ${TEST_SH} -c 'X=$( echo "weird; string *" ); echo "$X"'
     64  1.1  christos 
     65  1.1  christos 	rm -f * 2>/dev/null || :
     66  1.1  christos 	for f in file-1 file-2
     67  1.1  christos 	do
     68  1.1  christos 		cp /dev/null "$f"
     69  1.1  christos 	done
     70  1.1  christos 
     71  1.1  christos 	atf_check -s exit:0 -o match:'Found file-1 file-2' -e empty \
     72  1.1  christos 	    ${TEST_SH} -c 'echo Found $( echo * )'
     73  1.1  christos 	atf_check -s exit:0 -o match:'Found file-1 file-2' -e empty \
     74  1.1  christos 	    ${TEST_SH} -c 'echo Found "$( echo * )"'
     75  1.1  christos 	atf_check -s exit:0 -o match:'Found file-1 file-2' -e empty \
     76  1.1  christos 	    ${TEST_SH} -c 'echo Found $('" echo '*' )"
     77  1.1  christos 	atf_check -s exit:0 -o match:'Found \*' -e empty \
     78  1.1  christos 	    ${TEST_SH} -c 'echo Found "$('" echo '*' "')"'
     79  1.1  christos 	atf_check -s exit:0 -o match:'Found file-1 file-2' -e empty \
     80  1.1  christos 	    ${TEST_SH} -c 'echo Found $('" echo \\* )"
     81  1.1  christos 	atf_check -s exit:0 -o match:'Found \*' -e empty \
     82  1.1  christos 	    ${TEST_SH} -c 'echo Found "$('" echo \\* )"\"
     83  1.1  christos }
     84  1.1  christos 
     85  1.1  christos atf_test_case b_basic_backticks
     86  1.1  christos b_basic_backticks_head() {
     87  1.1  christos 	atf_set "descr" 'Test operation of old style ` ` substitutions'
     88  1.1  christos }
     89  1.1  christos b_basic_backticks_body() {
     90  1.1  christos 	atf_check -s exit:0 -o match:'Result is true today' -e empty \
     91  1.1  christos 	    ${TEST_SH} -c \
     92  1.1  christos 		'echo Result is `true && echo true || echo false` today'
     93  1.1  christos 
     94  1.1  christos 	atf_check -s exit:0 -o match:'Result is false today' -e empty \
     95  1.1  christos 	    ${TEST_SH} -c \
     96  1.1  christos 		'echo Result is `false && echo true || echo false` today'
     97  1.1  christos 
     98  1.1  christos 	atf_check -s exit:0 -o match:'aaabbbccc' -e empty \
     99  1.1  christos 	    ${TEST_SH} -c 'echo aaa` echo bbb `ccc'
    100  1.1  christos 	atf_check -s exit:0 -o match:'aaabbb cccddd' -e empty \
    101  1.1  christos 	    ${TEST_SH} -c 'echo aaa` echo bbb ccc `ddd'
    102  1.1  christos 	atf_check -s exit:0 -o inline:'aaabbb cccddd\n' -e empty \
    103  1.1  christos 	    ${TEST_SH} -c 'echo aaa` echo bbb; echo ccc `ddd'
    104  1.1  christos 	atf_check -s exit:0 -o inline:'aaabbb\ncccddd\n' -e empty \
    105  1.1  christos 	    ${TEST_SH} -c 'echo "aaa` echo bbb; echo ccc `ddd"'
    106  1.1  christos 
    107  1.1  christos 	atf_check -s exit:0 -o inline:'some string\n' -e empty \
    108  1.1  christos 	    ${TEST_SH} -c 'X=` echo some string `; echo "$X"'
    109  1.1  christos 	atf_check -s exit:0 -o inline:'weird; string *\n' -e empty \
    110  1.1  christos 	    ${TEST_SH} -c 'X=` echo "weird; string *" `; echo "$X"'
    111  1.1  christos 
    112  1.1  christos 	rm -f * 2>/dev/null || :
    113  1.1  christos 	for f in file-1 file-2
    114  1.1  christos 	do
    115  1.1  christos 		cp /dev/null "$f"
    116  1.1  christos 	done
    117  1.1  christos 
    118  1.1  christos 	atf_check -s exit:0 -o match:'Found file-1 file-2' -e empty \
    119  1.1  christos 	    ${TEST_SH} -c 'echo Found ` echo * `'
    120  1.1  christos 	atf_check -s exit:0 -o match:'Found file-1 file-2' -e empty \
    121  1.1  christos 	    ${TEST_SH} -c 'echo Found "` echo * `"'
    122  1.1  christos 	atf_check -s exit:0 -o match:'Found file-1 file-2' -e empty \
    123  1.1  christos 	    ${TEST_SH} -c 'echo Found `'" echo '*' "'`'
    124  1.1  christos 	atf_check -s exit:0 -o match:'Found \*' -e empty \
    125  1.1  christos 	    ${TEST_SH} -c 'echo Found "`'" echo '*' "'`"'
    126  1.1  christos 	atf_check -s exit:0 -o match:'Found file-1 file-2' -e empty \
    127  1.1  christos 	    ${TEST_SH} -c 'echo Found `'" echo \\* "'`'
    128  1.1  christos 	atf_check -s exit:0 -o match:'Found \*' -e empty \
    129  1.1  christos 	    ${TEST_SH} -c 'echo Found "`'" echo \\* "'`"'
    130  1.1  christos }
    131  1.1  christos 
    132  1.1  christos atf_test_case c_nested_cmdsub
    133  1.1  christos c_nested_cmdsub_head() {
    134  1.1  christos 	atf_set "descr" "Test that cmd substitutions can be nested"
    135  1.1  christos }
    136  1.1  christos c_nested_cmdsub_body() {
    137  1.1  christos 	atf_check -s exit:0 -o match:'__foobarbletch__' -e empty \
    138  1.1  christos 	    ${TEST_SH} -c 'echo __$( echo foo$(echo bar)bletch )__'
    139  1.1  christos 	atf_check -s exit:0 -o match:'_abcde_' -e empty \
    140  1.1  christos 	    ${TEST_SH} -c 'echo _$(echo a$(echo $(echo b)c$(echo d))e )_'
    141  1.1  christos 	atf_check -s exit:0 -o match:'123454321' -e empty \
    142  1.1  christos 	    ${TEST_SH} -c 'echo 1$(echo 2$(echo 3$(echo 4$(echo 5)4)3)2)1'
    143  1.1  christos }
    144  1.1  christos 
    145  1.1  christos atf_test_case d_nested_backticks
    146  1.1  christos d_nested_backticks_head() {
    147  1.1  christos 	atf_set "descr" "Tests that old style backtick cmd subs can be nested"
    148  1.1  christos }
    149  1.1  christos d_nested_backticks_body() {
    150  1.1  christos 	atf_check -s exit:0 -o match:'__foobarbletch__' -e empty \
    151  1.1  christos 	    ${TEST_SH} -c 'echo __` echo foo\`echo bar\`bletch `__'
    152  1.1  christos 	atf_check -s exit:0 -o match:'_abcde_' -e empty \
    153  1.1  christos 	    ${TEST_SH} -c \
    154  1.1  christos 		'echo _`echo a\`echo \\\`echo b\\\`c\\\`echo d\\\`\`e `_'
    155  1.1  christos 	atf_check -s exit:0 -o match:'123454321' -e empty \
    156  1.1  christos 	    ${TEST_SH} -c \
    157  1.1  christos 	    'echo 1`echo 2\`echo 3\\\`echo 4\\\\\\\`echo 5\\\\\\\`4\\\`3\`2`1'
    158  1.1  christos }
    159  1.1  christos 
    160  1.1  christos atf_test_case e_perverse_mixing
    161  1.1  christos e_perverse_mixing_head() {
    162  1.1  christos 	atf_set "descr" \
    163  1.1  christos 		"Checks various mixed new and old style cmd substitutions"
    164  1.1  christos }
    165  1.1  christos e_perverse_mixing_body() {
    166  1.1  christos 	atf_check -s exit:0 -o match:'__foobarbletch__' -e empty \
    167  1.1  christos 	    ${TEST_SH} -c 'echo __$( echo foo`echo bar`bletch )__'
    168  1.1  christos 	atf_check -s exit:0 -o match:'__foobarbletch__' -e empty \
    169  1.1  christos 	    ${TEST_SH} -c 'echo __` echo foo$(echo bar)bletch `__'
    170  1.1  christos 	atf_check -s exit:0 -o match:'_abcde_' -e empty \
    171  1.1  christos 	    ${TEST_SH} -c 'echo _$(echo a`echo $(echo b)c$(echo d)`e )_'
    172  1.1  christos 	atf_check -s exit:0 -o match:'_abcde_' -e empty \
    173  1.1  christos 	    ${TEST_SH} -c 'echo _`echo a$(echo \`echo b\`c\`echo d\`)e `_'
    174  1.1  christos 	atf_check -s exit:0 -o match:'12345654321' -e empty \
    175  1.1  christos 	    ${TEST_SH} -c \
    176  1.1  christos 		'echo 1`echo 2$(echo 3\`echo 4\\\`echo 5$(echo 6)5\\\`4\`3)2`1'
    177  1.1  christos }
    178  1.1  christos 
    179  1.1  christos atf_test_case f_redirect_in_cmdsub
    180  1.1  christos f_redirect_in_cmdsub_head() {
    181  1.1  christos 	atf_set "descr" "Checks that redirects work in command substitutions"
    182  1.1  christos }
    183  1.1  christos f_redirect_in_cmdsub_body() {
    184  1.1  christos 	rm -f file 2>/dev/null || :
    185  1.1  christos 	atf_check -s exit:0 -o match:'_aa_' -e empty \
    186  1.1  christos 	    ${TEST_SH} -c 'echo _$( echo a$( echo b > file )a)_'
    187  1.1  christos 	atf_check -s exit:0 -o match:b -e empty ${TEST_SH} -c 'cat file'
    188  1.1  christos 	atf_check -s exit:0 -o match:'_aba_' -e empty \
    189  1.1  christos 	    ${TEST_SH} -c 'echo _$( echo a$( cat < file )a)_'
    190  1.1  christos 	atf_check -s exit:0 -o match:'_aa_' -e empty \
    191  1.1  christos 	    ${TEST_SH} -c 'echo _$( echo a$( echo d >> file )a)_'
    192  1.1  christos 	atf_check -s exit:0 -o inline:'b\nd\n' -e empty ${TEST_SH} -c 'cat file'
    193  1.1  christos 	atf_check -s exit:0 -o match:'_aa_' -e match:'not error' \
    194  1.1  christos 	    ${TEST_SH} -c 'echo _$( echo a$( echo not error >&2 )a)_'
    195  1.1  christos }
    196  1.1  christos 
    197  1.1  christos atf_test_case g_redirect_in_backticks
    198  1.1  christos g_redirect_in_backticks_head() {
    199  1.1  christos 	atf_set "descr" "Checks that redirects work in old style cmd sub"
    200  1.1  christos }
    201  1.1  christos g_redirect_in_backticks_body() {
    202  1.1  christos 	rm -f file 2>/dev/null || :
    203  1.1  christos 	atf_check -s exit:0 -o match:'_aa_' -e empty \
    204  1.1  christos 	    ${TEST_SH} -c 'echo _` echo a\` echo b > file \`a`_'
    205  1.1  christos 	atf_check -s exit:0 -o match:b -e empty ${TEST_SH} -c 'cat file'
    206  1.1  christos 	atf_check -s exit:0 -o match:'_aba_' -e empty \
    207  1.1  christos 	    ${TEST_SH} -c 'echo _` echo a\` cat < file \`a`_'
    208  1.1  christos 	atf_check -s exit:0 -o match:'_aa_' -e empty \
    209  1.1  christos 	    ${TEST_SH} -c 'echo _` echo a\` echo d >> file \`a`_'
    210  1.1  christos 	atf_check -s exit:0 -o inline:'b\nd\n' -e empty ${TEST_SH} -c 'cat file'
    211  1.1  christos 	atf_check -s exit:0 -o match:'_aa_' -e match:'not error' \
    212  1.1  christos 	    ${TEST_SH} -c 'echo _` echo a\` echo not error >&2 \`a`_'
    213  1.1  christos }
    214  1.1  christos 
    215  1.1  christos atf_test_case h_vars_in_cmdsub
    216  1.1  christos h_vars_in_cmdsub_head() {
    217  1.1  christos 	atf_set "descr" "Check that variables work in command substitutions"
    218  1.1  christos }
    219  1.1  christos h_vars_in_cmdsub_body() {
    220  1.1  christos 	atf_check -s exit:0 -o match:'__abc__' -e empty \
    221  1.1  christos 	    ${TEST_SH} -c 'X=abc; echo __$( echo ${X} )__'
    222  1.1  christos 	atf_check -s exit:0 -o match:'__abc__' -e empty \
    223  1.1  christos 	    ${TEST_SH} -c 'X=abc; echo __$( echo "${X}" )__'
    224  1.1  christos 	atf_check -s exit:0 -o match:'__abc__' -e empty \
    225  1.1  christos 	    ${TEST_SH} -c 'X=abc; echo "__$( echo ${X} )__"'
    226  1.1  christos 	atf_check -s exit:0 -o match:'__abc__' -e empty \
    227  1.1  christos 	    ${TEST_SH} -c 'X=abc; echo "__$( echo "${X}" )__"'
    228  1.1  christos 
    229  1.1  christos 	atf_check -s exit:0 -o inline:'a\n\nb\n\nc\n' -e empty \
    230  1.1  christos 	    ${TEST_SH} -c "for X in a '' b '' c"'; do echo $( echo "$X" ); done'
    231  1.1  christos 
    232  1.1  christos 	atf_check -s exit:0 -o match:'__acd__' -e empty \
    233  1.1  christos 	    ${TEST_SH} -c 'X=; unset Y; echo "__$( echo a${X-b}${Y-c}d)__"'
    234  1.1  christos 	atf_check -s exit:0 -o match:'__abcd__' -e empty \
    235  1.1  christos 	    ${TEST_SH} -c 'X=; unset Y; echo "__$( echo a${X:-b}${Y:-c}d)__"'
    236  1.1  christos 	atf_check -s exit:0 -o match:'__XYX__' -e empty \
    237  1.1  christos 	    ${TEST_SH} -c 'X=X; echo "__${X}$( X=Y; echo ${X} )${X}__"'
    238  1.1  christos 	atf_check -s exit:0 -o match:'__def__' -e empty \
    239  1.1  christos 	    ${TEST_SH} -c 'X=abc; echo "__$(X=def; echo "${X}" )__"'
    240  1.1  christos 	atf_check -s exit:0 -o inline:'abcdef\nabc\n' -e empty \
    241  1.1  christos 	    ${TEST_SH} -c 'X=abc; echo "$X$(X=def; echo ${X} )"; echo $X'
    242  1.1  christos }
    243  1.1  christos 
    244  1.1  christos atf_test_case i_vars_in_backticks
    245  1.1  christos i_vars_in_backticks_head() {
    246  1.1  christos 	atf_set "descr" "Checks that variables work in old style cmd sub"
    247  1.1  christos }
    248  1.1  christos i_vars_in_backticks_body() {
    249  1.1  christos 	atf_check -s exit:0 -o match:'__abc__' -e empty \
    250  1.1  christos 	    ${TEST_SH} -c 'X=abc; echo __` echo ${X} `__'
    251  1.1  christos 	atf_check -s exit:0 -o match:'__abc__' -e empty \
    252  1.1  christos 	    ${TEST_SH} -c 'X=abc; echo __` echo "${X}" `__'
    253  1.1  christos 	atf_check -s exit:0 -o match:'__abc__' -e empty \
    254  1.1  christos 	    ${TEST_SH} -c 'X=abc; echo "__` echo ${X} `__"'
    255  1.1  christos 	atf_check -s exit:0 -o match:'__abc__' -e empty \
    256  1.1  christos 	    ${TEST_SH} -c 'X=abc; echo "__` echo \"${X}\" `__"'
    257  1.1  christos 
    258  1.1  christos 	atf_check -s exit:0 -o inline:'a\n\nb\n\nc\n' -e empty \
    259  1.1  christos 	    ${TEST_SH} -c "for X in a '' b '' c"'; do echo $( echo "$X" ); done'
    260  1.1  christos 
    261  1.1  christos 	atf_check -s exit:0 -o match:'__acd__' -e empty \
    262  1.1  christos 	    ${TEST_SH} -c 'X=; unset Y; echo "__$( echo a${X-b}${Y-c}d)__"'
    263  1.1  christos 	atf_check -s exit:0 -o match:'__abcd__' -e empty \
    264  1.1  christos 	    ${TEST_SH} -c 'X=; unset Y; echo "__$( echo a${X:-b}${Y:-c}d)__"'
    265  1.1  christos 	atf_check -s exit:0 -o match:'__XYX__' -e empty \
    266  1.1  christos 	    ${TEST_SH} -c 'X=X; echo "__${X}$( X=Y; echo ${X} )${X}__"'
    267  1.1  christos 	atf_check -s exit:0 -o inline:'abcdef\nabc\n' -e empty \
    268  1.1  christos 	    ${TEST_SH} -c 'X=abc; echo "$X`X=def; echo \"${X}\" `";echo $X'
    269  1.1  christos 
    270  1.1  christos 	# The following is nonsense, so is not included ...
    271  1.1  christos 	# atf_check -s exit:0 -o match:'__abc__' -e empty \
    272  1.1  christos 	#                              oV             cV   oV   cV
    273  1.1  christos 	#    ${TEST_SH} -c 'X=abc; echo "__`X=def echo "${X}" `__"'
    274  1.1  christos 	#				   `start in " ^ " ends, ` not yet
    275  1.1  christos }
    276  1.1  christos 
    277  1.1  christos atf_test_case j_cmdsub_in_varexpand
    278  1.1  christos j_cmdsub_in_varexpand_head() {
    279  1.1  christos 	atf_set "descr" "Checks that command sub can be used in var expansion"
    280  1.1  christos }
    281  1.1  christos j_cmdsub_in_varexpand_body() {
    282  1.1  christos 	atf_check -s exit:0 -o match:'foo' -e empty \
    283  1.1  christos 	    ${TEST_SH} -c 'X=set; echo ${X+$(echo foo)}'
    284  1.1  christos 	atf_check -s exit:0 -o match:'set' -e empty \
    285  1.1  christos 	    ${TEST_SH} -c 'X=set; echo ${X-$(echo foo)}'
    286  1.1  christos 	rm -f bar 2>/dev/null || :
    287  1.1  christos 	atf_check -s exit:0 -o match:'set' -e empty \
    288  1.1  christos 	    ${TEST_SH} -c 'X=set; echo ${X-$(echo foo > bar)}'
    289  1.1  christos 	test -f bar && atf_fail "bar should not exist, but does"
    290  1.1  christos 	atf_check -s exit:0 -o inline:'\n' -e empty \
    291  1.1  christos 	    ${TEST_SH} -c 'X=set; echo ${X+$(echo foo > bar)}'
    292  1.1  christos 	test -f bar || atf_fail "bar should exist, but does not"
    293  1.1  christos }
    294  1.1  christos 
    295  1.1  christos atf_test_case k_backticks_in_varexpand
    296  1.1  christos k_backticks_in_varexpand_head() {
    297  1.1  christos 	atf_set "descr" "Checks that old style cmd sub works in var expansion"
    298  1.1  christos }
    299  1.1  christos k_backticks_in_varexpand_body() {
    300  1.1  christos 	atf_check -s exit:0 -o match:'foo' -e empty \
    301  1.1  christos 	    ${TEST_SH} -c 'X=set; echo ${X+`echo foo`}'
    302  1.1  christos 	atf_check -s exit:0 -o match:'set' -e empty \
    303  1.1  christos 	    ${TEST_SH} -c 'X=set; echo ${X-`echo foo`}'
    304  1.1  christos 	rm -f bar 2>/dev/null || :
    305  1.1  christos 	atf_check -s exit:0 -o match:'set' -e empty \
    306  1.1  christos 	    ${TEST_SH} -c 'X=set; echo ${X-`echo foo > bar`}'
    307  1.1  christos 	test -f bar && atf_fail "bar should not exist, but does"
    308  1.1  christos 	atf_check -s exit:0 -o inline:'\n' -e empty \
    309  1.1  christos 	    ${TEST_SH} -c 'X=set; echo ${X+`echo foo > bar`}'
    310  1.1  christos 	test -f bar || atf_fail "bar should exist, but does not"
    311  1.1  christos }
    312  1.1  christos 
    313  1.1  christos atf_test_case l_arithmetic_in_cmdsub
    314  1.1  christos l_arithmetic_in_cmdsub_head() {
    315  1.1  christos 	atf_set "descr" "Checks that arithmetic works in cmd substitutions"
    316  1.1  christos }
    317  1.1  christos l_arithmetic_in_cmdsub_body() {
    318  1.1  christos 	atf_check -s exit:0 -o inline:'1 + 1 = 2\n' -e empty \
    319  1.1  christos 	    ${TEST_SH} -c 'echo 1 + 1 = $( echo $(( 1 + 1 )) )'
    320  1.1  christos 	atf_check -s exit:0 -o inline:'X * Y = 6\n' -e empty \
    321  1.1  christos 	    ${TEST_SH} -c 'X=2; Y=3; echo X \* Y = $( echo $(( X * Y )) )'
    322  1.1  christos 	atf_check -s exit:0 -o inline:'Y % X = 1\n' -e empty \
    323  1.1  christos 	    ${TEST_SH} -c 'X=2; Y=3; echo Y % X = $( echo $(( $Y % $X )) )'
    324  1.1  christos }
    325  1.1  christos 
    326  1.1  christos atf_test_case m_arithmetic_in_backticks
    327  1.1  christos m_arithmetic_in_backticks_head() {
    328  1.1  christos 	atf_set "descr" "Checks that arithmetic works in old style cmd sub"
    329  1.1  christos }
    330  1.1  christos m_arithmetic_in_backticks_body() {
    331  1.1  christos 	atf_check -s exit:0 -o inline:'2 + 3 = 5\n' -e empty \
    332  1.1  christos 	    ${TEST_SH} -c 'echo 2 + 3 = ` echo $(( 2 + 3 )) `'
    333  1.1  christos 	atf_check -s exit:0 -o inline:'X * Y = 6\n' -e empty \
    334  1.1  christos 	    ${TEST_SH} -c 'X=2; Y=3; echo X \* Y = ` echo $(( X * Y )) `'
    335  1.1  christos 	atf_check -s exit:0 -o inline:'Y % X = 1\n' -e empty \
    336  1.1  christos 	    ${TEST_SH} -c 'X=2; Y=3; echo Y % X = ` echo $(( $Y % $X )) `'
    337  1.1  christos }
    338  1.1  christos 
    339  1.1  christos atf_test_case n_cmdsub_in_arithmetic
    340  1.1  christos n_cmdsub_in_arithmetic_head() {
    341  1.1  christos 	atf_set "descr" "Tests uses of command substitutions in arithmetic"
    342  1.1  christos }
    343  1.1  christos n_cmdsub_in_arithmetic_body() {
    344  1.1  christos 	atf_check -s exit:0 -o inline:'7\n' -e empty \
    345  1.1  christos 	    ${TEST_SH} -c 'echo $(( $( echo 3 ) $( echo + ) $( echo 4 ) ))'
    346  1.1  christos 	atf_check -s exit:0 -o inline:'11\n7\n18\n4\n1\n' -e empty \
    347  1.1  christos 	    ${TEST_SH} -c \
    348  1.1  christos 		 'for op in + - \* / %
    349  1.1  christos 		  do
    350  1.1  christos 		      echo $(( $( echo 9 ) $( echo "${op}" ) $( echo 2 ) ))
    351  1.1  christos 		  done'
    352  1.1  christos }
    353  1.1  christos 
    354  1.1  christos atf_test_case o_backticks_in_arithmetic
    355  1.1  christos o_backticks_in_arithmetic_head() {
    356  1.1  christos 	atf_set "descr" "Tests old style cmd sub used in arithmetic"
    357  1.1  christos }
    358  1.1  christos o_backticks_in_arithmetic_body() {
    359  1.1  christos 	atf_check -s exit:0 -o inline:'33\n' -e empty \
    360  1.1  christos 	    ${TEST_SH} -c 'echo $(( `echo 77` `echo -` `echo 44`))'
    361  1.1  christos 	atf_check -s exit:0 -o inline:'14\n8\n33\n3\n2\n' -e empty \
    362  1.1  christos 	    ${TEST_SH} -c \
    363  1.1  christos 		 'for op in + - \* / %
    364  1.1  christos 		  do
    365  1.1  christos 		      echo $((`echo 11``echo "${op}"``echo 3`))
    366  1.1  christos 		  done'
    367  1.1  christos }
    368  1.1  christos 
    369  1.1  christos atf_test_case p_cmdsub_in_heredoc
    370  1.1  christos p_cmdsub_in_heredoc_head() {
    371  1.1  christos 	atf_set "descr" "Checks that cmdsubs work inside a here document"
    372  1.1  christos }
    373  1.1  christos p_cmdsub_in_heredoc_body() {
    374  1.1  christos 	atf_check -s exit:0 -o inline:'line 1+1\nline 2\nline 3\n' -e empty \
    375  1.1  christos 	    ${TEST_SH} -c \
    376  1.1  christos 		'cat <<- EOF
    377  1.1  christos 			$( echo line 1 )$( echo +1 )
    378  1.1  christos 			$( echo line 2;echo line 3 )
    379  1.1  christos 		EOF'
    380  1.1  christos }
    381  1.1  christos 
    382  1.1  christos atf_test_case q_backticks_in_heredoc
    383  1.1  christos q_backticks_in_heredoc_head() {
    384  1.1  christos 	atf_set "descr" "Checks that old style cmdsubs work in here docs"
    385  1.1  christos }
    386  1.1  christos q_backticks_in_heredoc_body() {
    387  1.1  christos 	atf_check -s exit:0 -o inline:'Mary had a\nlittle\nlamb\n' -e empty \
    388  1.1  christos 	    ${TEST_SH} -c \
    389  1.1  christos 		'cat <<- EOF
    390  1.1  christos 			`echo Mary ` `echo had a `
    391  1.1  christos 			` echo little; echo lamb `
    392  1.1  christos 		EOF'
    393  1.1  christos }
    394  1.1  christos 
    395  1.1  christos atf_test_case r_heredoc_in_cmdsub
    396  1.1  christos r_heredoc_in_cmdsub_head() {
    397  1.1  christos 	atf_set "descr" "Checks that here docs work inside cmd subs"
    398  1.1  christos }
    399  1.1  christos r_heredoc_in_cmdsub_body() {
    400  1.1  christos 	atf_check -s exit:0 -o inline:'Mary had a\nlittle\nlamb\n' -e empty \
    401  1.1  christos 	    ${TEST_SH} -c 'echo "$( cat <<- \EOF
    402  1.1  christos 				Mary had a
    403  1.1  christos 				little
    404  1.1  christos 				lamb
    405  1.1  christos 			EOF
    406  1.1  christos 			)"'
    407  1.1  christos 
    408  1.1  christos 	atf_check -s exit:0 -e empty \
    409  1.1  christos 	    -o inline:'Mary had 1\nlittle\nlamb\nMary had 4\nlittle\nlambs\n' \
    410  1.1  christos 	    ${TEST_SH} -c 'for N in 1 4; do echo "$( cat <<- EOF
    411  1.1  christos 				Mary had ${N}
    412  1.1  christos 				little
    413  1.1  christos 				lamb$( [ $N -gt 1 ] && echo s )
    414  1.1  christos 			EOF
    415  1.1  christos 			)"; done'
    416  1.1  christos 
    417  1.1  christos 
    418  1.1  christos 	atf_check -s exit:0 -o inline:'A Calculation:\n2 * 7 = 14\n' -e empty \
    419  1.1  christos 	    ${TEST_SH} -c 'echo "$( cat <<- EOF
    420  1.1  christos 				A Calculation:
    421  1.1  christos 					2 * 7 = $(( 2 * 7 ))
    422  1.1  christos 			EOF
    423  1.1  christos 			)"'
    424  1.1  christos 
    425  1.1  christos 	# not all shells permit this syntax ...
    426  1.1  christos 	# But I see nothing in the standard to prohibit it.
    427  1.1  christos 	atf_check -s exit:0 -o inline:'Line 1\nLine 2\n' -e empty \
    428  1.1  christos 	    ${TEST_SH} -c 'echo "$( cat <<- "EOF" )"
    429  1.1  christos 				Line 1
    430  1.1  christos 				Line 2
    431  1.1  christos 				EOF
    432  1.1  christos 			'
    433  1.1  christos }
    434  1.1  christos 
    435  1.1  christos atf_test_case s_heredoc_in_backticks
    436  1.1  christos s_heredoc_in_backticks_head() {
    437  1.1  christos 	atf_set "descr" "Checks that here docs work inside old style cmd subs"
    438  1.1  christos }
    439  1.1  christos s_heredoc_in_backticks_body() {
    440  1.1  christos 	atf_check -s exit:0 -o inline:'Mary had a little lamb\n' -e empty \
    441  1.1  christos 	    ${TEST_SH} -c 'echo ` cat <<- \EOF
    442  1.1  christos 				Mary had a
    443  1.1  christos 				little
    444  1.1  christos 				lamb
    445  1.1  christos 			EOF
    446  1.1  christos 			`'
    447  1.1  christos 
    448  1.1  christos 	atf_check -s exit:0 -o inline:'A Calculation:\n17 / 3 = 5\n' -e empty \
    449  1.1  christos 	    ${TEST_SH} -c 'echo "` cat <<- EOF
    450  1.1  christos 				A Calculation:
    451  1.1  christos 					17 / 3 = $(( 17 / 3 ))
    452  1.1  christos 			EOF
    453  1.1  christos 			`"'
    454  1.1  christos }
    455  1.1  christos 
    456  1.1  christos atf_test_case t_nested_cmdsubs_in_heredoc
    457  1.1  christos t_nested_cmdsubs_in_heredoc_head() {
    458  1.1  christos 	atf_set "descr" "Checks nested command substitutions in here docs"
    459  1.1  christos }
    460  1.1  christos t_nested_cmdsubs_in_heredoc_body() {
    461  1.1  christos }
    462  1.1  christos 
    463  1.1  christos atf_test_case u_nested_backticks_in_heredoc
    464  1.1  christos u_nested_backticks_in_heredoc_head() {
    465  1.1  christos 	atf_set "descr" "Checks nested old style cmd subs in here docs"
    466  1.1  christos }
    467  1.1  christos u_nested_backticks_in_heredoc_body() {
    468  1.1  christos }
    469  1.1  christos 
    470  1.1  christos atf_test_case z_absurd_heredoc_cmdsub_combos
    471  1.1  christos z_absurd_heredoc_cmdsub_combos_head() {
    472  1.1  christos 	atf_set "descr" "perverse and unusual cmd substitutions & more"
    473  1.1  christos }
    474  1.1  christos z_absurd_heredoc_cmdsub_combos_body() {
    475  1.1  christos }
    476  1.1  christos 
    477  1.1  christos atf_init_test_cases() {
    478  1.1  christos 	atf_add_test_case a_basic_cmdsub
    479  1.1  christos 	atf_add_test_case b_basic_backticks
    480  1.1  christos 	atf_add_test_case c_nested_cmdsub
    481  1.1  christos 	atf_add_test_case d_nested_backticks
    482  1.1  christos 	atf_add_test_case e_perverse_mixing
    483  1.1  christos 	atf_add_test_case f_redirect_in_cmdsub
    484  1.1  christos 	atf_add_test_case g_redirect_in_backticks
    485  1.1  christos 	atf_add_test_case h_vars_in_cmdsub
    486  1.1  christos 	atf_add_test_case i_vars_in_backticks
    487  1.1  christos 	atf_add_test_case j_cmdsub_in_varexpand
    488  1.1  christos 	atf_add_test_case k_backticks_in_varexpand
    489  1.1  christos 	atf_add_test_case l_arithmetic_in_cmdsub
    490  1.1  christos 	atf_add_test_case m_arithmetic_in_backticks
    491  1.1  christos 	atf_add_test_case n_cmdsub_in_arithmetic
    492  1.1  christos 	atf_add_test_case o_backticks_in_arithmetic
    493  1.1  christos 	atf_add_test_case p_cmdsub_in_heredoc
    494  1.1  christos 	atf_add_test_case q_backticks_in_heredoc
    495  1.1  christos 	atf_add_test_case r_heredoc_in_cmdsub
    496  1.1  christos 	atf_add_test_case s_heredoc_in_backticks
    497  1.1  christos 	atf_add_test_case t_nested_cmdsubs_in_heredoc
    498  1.1  christos 	atf_add_test_case u_nested_backticks_in_heredoc
    499  1.1  christos 	atf_add_test_case z_absurd_heredoc_cmdsub_combos
    500  1.1  christos }
    501