Home | History | Annotate | Line # | Download | only in sh
      1 # $NetBSD: t_exit.sh,v 1.6 2016/05/07 23:51:30 kre Exp $
      2 #
      3 # Copyright (c) 2007 The NetBSD Foundation, Inc.
      4 # All rights reserved.
      5 #
      6 # Redistribution and use in source and binary forms, with or without
      7 # modification, are permitted provided that the following conditions
      8 # are met:
      9 # 1. Redistributions of source code must retain the above copyright
     10 #    notice, this list of conditions and the following disclaimer.
     11 # 2. Redistributions in binary form must reproduce the above copyright
     12 #    notice, this list of conditions and the following disclaimer in the
     13 #    documentation and/or other materials provided with the distribution.
     14 #
     15 # THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
     16 # ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
     17 # TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
     18 # PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
     19 # BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
     20 # CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
     21 # SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
     22 # INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
     23 # CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
     24 # ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
     25 # POSSIBILITY OF SUCH DAMAGE.
     26 #
     27 # the implementation of "sh" to test
     28 : ${TEST_SH:="/bin/sh"}
     29 
     30 
     31 atf_test_case background
     32 background_head() {
     33 	atf_set "descr" "Tests that sh(1) sets '$?' properly when running " \
     34 			"a command in the background (PR bin/46327)"
     35 }
     36 background_body() {
     37 	atf_check -o match:0 -e empty ${TEST_SH} -c 'true; true & echo $?'
     38 	# atf_expect_fail "PR bin/46327" (now fixed?)
     39 	atf_check -o match:0 -e empty ${TEST_SH} -c 'false; true & echo $?'
     40 }
     41 
     42 atf_test_case function
     43 function_head() {
     44 	atf_set "descr" "Tests that \$? is correctly updated inside " \
     45 			"a function"
     46 }
     47 function_body() {
     48 	atf_check -s exit:0 -o match:STATUS=1-0 -e empty \
     49 		${TEST_SH} -c '
     50 			crud() {
     51 				test yes = no
     52 
     53 				cat <<-EOF
     54 				STATUS=$?
     55 				EOF
     56 			}
     57 			foo=$(crud)
     58 			echo "${foo}-$?"
     59 		'
     60 }
     61 
     62 atf_test_case readout
     63 readout_head() {
     64 	atf_set "descr" "Tests that \$? is correctly updated in a " \
     65 			"compound expression"
     66 }
     67 readout_body() {
     68 	atf_check -s exit:0 -o match:0 -e empty \
     69 		${TEST_SH} -c 'true && ! true | false; echo $?'
     70 }
     71 
     72 atf_test_case trap_subshell
     73 trap_subshell_head() {
     74 	atf_set "descr" "Tests that the trap statement in a subshell " \
     75 			"works when the subshell exits"
     76 }
     77 trap_subshell_body() {
     78 	atf_check -s exit:0 -o inline:'exiting\n' -e empty \
     79 	    ${TEST_SH} -c '( trap "echo exiting" EXIT; /usr/bin/true )'
     80 }
     81 
     82 atf_test_case trap_zero__implicit_exit
     83 trap_zero__implicit_exit_head() {
     84 	atf_set "descr" "Tests that the trap statement in a subshell in a " \
     85 		"script works when the subshell simply runs out of commands"
     86 }
     87 trap_zero__implicit_exit_body() {
     88 	# PR bin/6764: sh works but ksh does not
     89 	echo '( trap "echo exiting" 0 )' >helper.sh
     90 	atf_check -s exit:0 -o match:exiting -e empty ${TEST_SH} helper.sh
     91 	# test ksh by setting TEST_SH to /bin/ksh and run the entire set...
     92 	# atf_check -s exit:0 -o match:exiting -e empty /bin/ksh helper.sh
     93 }
     94 
     95 atf_test_case trap_zero__explicit_exit
     96 trap_zero__explicit_exit_head() {
     97 	atf_set "descr" "Tests that the trap statement in a subshell in a " \
     98 		"script works when the subshell executes an explicit exit"
     99 }
    100 trap_zero__explicit_exit_body() {
    101 	echo '( trap "echo exiting" 0; exit; echo NO_NO_NO )' >helper.sh
    102 	atf_check -s exit:0 -o match:exiting -o not-match:NO_NO -e empty \
    103 		${TEST_SH} helper.sh
    104 	# test ksh by setting TEST_SH to /bin/ksh and run the entire set...
    105 	# atf_check -s exit:0 -o match:exiting -e empty /bin/ksh helper.sh
    106 }
    107 
    108 atf_test_case simple_exit
    109 simple_exit_head() {
    110 	atf_set "descr" "Tests that various values for exit status work"
    111 }
    112 # Note: ATF will not allow tests of exit values > 255, even if they would work
    113 simple_exit_body() {
    114 	for N in 0 1 2 3 4 5 6 42 99 101 125 126 127 128 129 200 254 255
    115 	do
    116 		atf_check -s exit:$N -o empty -e empty \
    117 			${TEST_SH} -c "exit $N; echo FOO; echo BAR >&2"
    118 	done
    119 }
    120 
    121 atf_test_case subshell_exit
    122 subshell_exit_head() {
    123 	atf_set "descr" "Tests that subshell exit status works and \$? gets it"
    124 }
    125 # Note: ATF will not allow tests of exit values > 255, even if they would work
    126 subshell_exit_body() {
    127 	for N in 0 1 2 3 4 5 6 42 99 101 125 126 127 128 129 200 254 255
    128 	do
    129 		atf_check -s exit:0 -o empty -e empty \
    130 			${TEST_SH} -c "(exit $N); test \$? -eq $N"
    131 	done
    132 }
    133 
    134 atf_test_case subshell_background
    135 subshell_background_head() {
    136 	atf_set "descr" "Tests that sh(1) sets '$?' properly when running " \
    137 			"a subshell in the background"
    138 }
    139 subshell_background_body() {
    140 	atf_check -o match:0 -e empty \
    141 		${TEST_SH} -c 'true; (false || true) & echo $?'
    142 	# atf_expect_fail "PR bin/46327" (now fixed?)
    143 	atf_check -o match:0 -e empty \
    144 		${TEST_SH} -c 'false; (false || true) & echo $?'
    145 }
    146 
    147 atf_init_test_cases() {
    148 	atf_add_test_case background
    149 	atf_add_test_case function
    150 	atf_add_test_case readout
    151 	atf_add_test_case trap_subshell
    152 	atf_add_test_case trap_zero__implicit_exit
    153 	atf_add_test_case trap_zero__explicit_exit
    154 	atf_add_test_case simple_exit
    155 	atf_add_test_case subshell_exit
    156 	atf_add_test_case subshell_background
    157 }
    158