Home | History | Annotate | Line # | Download | only in sh
t_exit.sh revision 1.4
      1  1.4  christos # $NetBSD: t_exit.sh,v 1.4 2016/02/24 14:42:06 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.4  christos # the implementation of "sh" to test
     28  1.4  christos : ${TEST_SH:="/bin/sh"}
     29  1.1    jruoho 
     30  1.1    jruoho 
     31  1.3    jruoho atf_test_case background
     32  1.3    jruoho background_head() {
     33  1.3    jruoho 	atf_set "descr" "Tests that sh(1) sets '$?' properly when running " \
     34  1.4  christos 			"a command in the background (PR bin/46327)"
     35  1.3    jruoho }
     36  1.3    jruoho background_body() {
     37  1.4  christos 	atf_check -o match:0 -e empty "${TEST_SH}" -c 'true; true & echo $?'
     38  1.4  christos 	# atf_expect_fail "PR bin/46327" (now fixed?)
     39  1.4  christos 	atf_check -o match:0 -e empty "${TEST_SH}" -c 'false; true & echo $?'
     40  1.3    jruoho }
     41  1.3    jruoho 
     42  1.1    jruoho atf_test_case function
     43  1.1    jruoho function_head() {
     44  1.4  christos 	atf_set "descr" "Tests that \$? is correctly updated inside " \
     45  1.4  christos 			"a function"
     46  1.1    jruoho }
     47  1.1    jruoho function_body() {
     48  1.4  christos 	atf_check -s exit:0 -o match:STATUS=1-0 -e empty \
     49  1.4  christos 		"${TEST_SH}" -c '
     50  1.4  christos 			crud() {
     51  1.4  christos 				test yes = no
     52  1.4  christos 
     53  1.4  christos 				cat <<-EOF
     54  1.4  christos 				STATUS=$?
     55  1.4  christos 				EOF
     56  1.4  christos 			}
     57  1.4  christos 			foo=$(crud)
     58  1.4  christos 			echo "${foo}-$?"
     59  1.4  christos 		'
     60  1.1    jruoho }
     61  1.1    jruoho 
     62  1.1    jruoho atf_test_case readout
     63  1.1    jruoho readout_head() {
     64  1.4  christos 	atf_set "descr" "Tests that \$? is correctly updated in a " \
     65  1.4  christos 			"compound expression"
     66  1.1    jruoho }
     67  1.1    jruoho readout_body() {
     68  1.4  christos 	atf_check -s exit:0 -o match:0 -e empty \
     69  1.4  christos 		"${TEST_SH}" -c 'true && ! true | false; echo $?'
     70  1.1    jruoho }
     71  1.1    jruoho 
     72  1.1    jruoho atf_test_case trap_subshell
     73  1.1    jruoho trap_subshell_head() {
     74  1.4  christos 	atf_set "descr" "Tests that the trap statement in a subshell " \
     75  1.4  christos 			"works when the subshell exits"
     76  1.1    jruoho }
     77  1.1    jruoho trap_subshell_body() {
     78  1.4  christos 	atf_check -s exit:0 -o inline:'exiting\n' -e empty \
     79  1.4  christos 	    ${TEST_SH} -c '( trap "echo exiting" EXIT; /usr/bin/true )'
     80  1.1    jruoho }
     81  1.1    jruoho 
     82  1.1    jruoho atf_test_case trap_zero__implicit_exit
     83  1.4  christos trap_zero__implicit_exit_head() {
     84  1.4  christos 	atf_set "descr" "Tests that the trap statement in a subshell in a " \
     85  1.4  christos 		"script works when the subshell simply runs out of commands"
     86  1.4  christos }
     87  1.1    jruoho trap_zero__implicit_exit_body() {
     88  1.4  christos 	# PR bin/6764: sh works but ksh does not
     89  1.1    jruoho 	echo '( trap "echo exiting" 0 )' >helper.sh
     90  1.4  christos 	atf_check -s exit:0 -o match:exiting -e empty "${TEST_SH}" helper.sh
     91  1.4  christos 	# test ksh by setting TEST_SH to /bin/ksh and run the entire set...
     92  1.4  christos 	# atf_check -s exit:0 -o match:exiting -e empty /bin/ksh helper.sh
     93  1.1    jruoho }
     94  1.1    jruoho 
     95  1.1    jruoho atf_test_case trap_zero__explicit_exit
     96  1.4  christos trap_zero__explicit_exit_head() {
     97  1.4  christos 	atf_set "descr" "Tests that the trap statement in a subshell in a " \
     98  1.4  christos 		"script works when the subshell executes an explicit exit"
     99  1.4  christos }
    100  1.1    jruoho trap_zero__explicit_exit_body() {
    101  1.4  christos 	echo '( trap "echo exiting" 0; exit; echo NO_NO_NO )' >helper.sh
    102  1.4  christos 	atf_check -s exit:0 -o match:exiting -o not-match:NO_NO -e empty \
    103  1.4  christos 		"${TEST_SH}" helper.sh
    104  1.4  christos 	# test ksh by setting TEST_SH to /bin/ksh and run the entire set...
    105  1.4  christos 	# atf_check -s exit:0 -o match:exiting -e empty /bin/ksh helper.sh
    106  1.1    jruoho }
    107  1.1    jruoho 
    108  1.4  christos # Is return really defined to operate other than in functions (& '.') ??
    109  1.1    jruoho atf_test_case trap_zero__explicit_return
    110  1.4  christos trap_zero__explicit_return_head() {
    111  1.4  christos 	atf_set "descr" "Tests that the trap statement in a subshell in a " \
    112  1.4  christos 			"script works when the subshell executes a return"
    113  1.4  christos }
    114  1.1    jruoho trap_zero__explicit_return_body() {
    115  1.4  christos 	echo '( trap "echo exiting" 0; return; echo NO_NO_NO )' >helper.sh
    116  1.4  christos 	atf_expect_fail "return from a sub-shell not defined and does not work"
    117  1.4  christos 	atf_check -s exit:0 -o match:exiting -o not-match:NO_NO -e empty \
    118  1.4  christos 		"${TEST_SH}" helper.sh
    119  1.4  christos 	# test ksh by setting TEST_SH to /bin/ksh and run the entire set...
    120  1.4  christos 	# atf_check -s exit:0 -o match:exiting -e empty /bin/ksh helper.sh
    121  1.4  christos }
    122  1.4  christos 
    123  1.4  christos atf_test_case simple_exit
    124  1.4  christos simple_exit_head() {
    125  1.4  christos 	atf_set "descr" "Tests that various values for exit status work"
    126  1.4  christos }
    127  1.4  christos # Note: ATF will not allow tests of exit values > 255, even if they would work
    128  1.4  christos simple_exit_body() {
    129  1.4  christos 	for N in 0 1 2 3 4 5 6 42 99 101 125 126 127 128 129 200 254 255
    130  1.4  christos 	do
    131  1.4  christos 		atf_check -s exit:$N -o empty -e empty \
    132  1.4  christos 			"${TEST_SH}" -c "exit $N; echo FOO; echo BAR >&2"
    133  1.4  christos 	done
    134  1.4  christos }
    135  1.4  christos 
    136  1.4  christos atf_test_case subshell_exit
    137  1.4  christos subshell_exit_head() {
    138  1.4  christos 	atf_set "descr" "Tests that subshell exit status works and \$? gets it"
    139  1.4  christos }
    140  1.4  christos # Note: ATF will not allow tests of exit values > 255, even if they would work
    141  1.4  christos subshell_exit_body() {
    142  1.4  christos 	for N in 0 1 2 3 4 5 6 42 99 101 125 126 127 128 129 200 254 255
    143  1.4  christos 	do
    144  1.4  christos 		atf_check -s exit:0 -o empty -e empty \
    145  1.4  christos 			"${TEST_SH}" -c "(exit $N); test \$? -eq $N"
    146  1.4  christos 	done
    147  1.4  christos }
    148  1.4  christos 
    149  1.4  christos atf_test_case subshell_background
    150  1.4  christos subshell_background_head() {
    151  1.4  christos 	atf_set "descr" "Tests that sh(1) sets '$?' properly when running " \
    152  1.4  christos 			"a subshell in the background"
    153  1.4  christos }
    154  1.4  christos subshell_background_body() {
    155  1.4  christos 	atf_check -o match:0 -e empty \
    156  1.4  christos 		"${TEST_SH}" -c 'true; (false || true) & echo $?'
    157  1.4  christos 	# atf_expect_fail "PR bin/46327" (now fixed?)
    158  1.4  christos 	atf_check -o match:0 -e empty \
    159  1.4  christos 		"${TEST_SH}" -c 'false; (false || true) & echo $?'
    160  1.1    jruoho }
    161  1.1    jruoho 
    162  1.1    jruoho atf_init_test_cases() {
    163  1.3    jruoho 	atf_add_test_case background
    164  1.1    jruoho 	atf_add_test_case function
    165  1.1    jruoho 	atf_add_test_case readout
    166  1.1    jruoho 	atf_add_test_case trap_subshell
    167  1.1    jruoho 	atf_add_test_case trap_zero__implicit_exit
    168  1.1    jruoho 	atf_add_test_case trap_zero__explicit_exit
    169  1.1    jruoho 	atf_add_test_case trap_zero__explicit_return
    170  1.4  christos 	atf_add_test_case simple_exit
    171  1.4  christos 	atf_add_test_case subshell_exit
    172  1.4  christos 	atf_add_test_case subshell_background
    173  1.1    jruoho }
    174