1 # $NetBSD: t_exit.sh,v 1.6 2016/05/07 23:51:30 kre Exp $
3 # Copyright (c) 2007 The NetBSD Foundation, Inc.
6 # Redistribution and use in source and binary forms, with or without
7 # modification, are permitted provided that the following conditions
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.
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.
27 # the implementation of "sh" to test
28 : ${TEST_SH:="/bin/sh"}
31 atf_test_case background
33 atf_set "descr" "Tests that sh(1) sets '$?' properly when running " \
34 "a command in the background (PR bin/46327)"
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 $?'
42 atf_test_case function
44 atf_set "descr" "Tests that \$? is correctly updated inside " \
48 atf_check -s exit:0 -o match:STATUS=1-0 -e empty \
64 atf_set "descr" "Tests that \$? is correctly updated in a " \
68 atf_check -s exit:0 -o match:0 -e empty \
69 ${TEST_SH} -c 'true && ! true | false; echo $?'
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"
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 )'
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"
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
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"
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 \
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
108 atf_test_case simple_exit
110 atf_set "descr" "Tests that various values for exit status work"
112 # Note: ATF will not allow tests of exit values > 255, even if they would work
114 for N in 0 1 2 3 4 5 6 42 99 101 125 126 127 128 129 200 254 255
116 atf_check -s exit:$N -o empty -e empty \
117 ${TEST_SH} -c "exit $N; echo FOO; echo BAR >&2"
121 atf_test_case subshell_exit
122 subshell_exit_head() {
123 atf_set "descr" "Tests that subshell exit status works and \$? gets it"
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
129 atf_check -s exit:0 -o empty -e empty \
130 ${TEST_SH} -c "(exit $N); test \$? -eq $N"
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"
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 $?'
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