1272343Sngie# $NetBSD: t_exit.sh,v 1.3 2012/04/13 06:12:32 jruoho Exp $
2272343Sngie#
3272343Sngie# Copyright (c) 2007 The NetBSD Foundation, Inc.
4272343Sngie# All rights reserved.
5272343Sngie#
6272343Sngie# Redistribution and use in source and binary forms, with or without
7272343Sngie# modification, are permitted provided that the following conditions
8272343Sngie# are met:
9272343Sngie# 1. Redistributions of source code must retain the above copyright
10272343Sngie#    notice, this list of conditions and the following disclaimer.
11272343Sngie# 2. Redistributions in binary form must reproduce the above copyright
12272343Sngie#    notice, this list of conditions and the following disclaimer in the
13272343Sngie#    documentation and/or other materials provided with the distribution.
14272343Sngie#
15272343Sngie# THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
16272343Sngie# ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
17272343Sngie# TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
18272343Sngie# PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
19272343Sngie# BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
20272343Sngie# CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
21272343Sngie# SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
22272343Sngie# INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
23272343Sngie# CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
24272343Sngie# ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
25272343Sngie# POSSIBILITY OF SUCH DAMAGE.
26272343Sngie#
27272343Sngie
28272343Sngiecrud() {
29272343Sngie	test yes = no
30272343Sngie
31272343Sngie	cat <<EOF
32272343Sngie$?
33272343SngieEOF
34272343Sngie}
35272343Sngie
36272343Sngieatf_test_case background
37272343Sngiebackground_head() {
38272343Sngie	atf_set "descr" "Tests that sh(1) sets '$?' properly when running " \
39272343Sngie	                "a command in the background (PR bin/46327)"
40272343Sngie}
41272343Sngiebackground_body() {
42272343Sngie	atf_check -s exit:0 -o ignore -e ignore -x "true; true & echo $?"
43272343Sngie	atf_check -s exit:0 -o ignore -e ignore -x "false; true & echo $?"
44272343Sngie}
45272343Sngie
46272343Sngieatf_test_case function
47272343Sngiefunction_head() {
48272343Sngie	atf_set "descr" "Tests that \$? is correctly updated inside" \
49272343Sngie	                "a function"
50272343Sngie}
51272343Sngiefunction_body() {
52272343Sngie	foo=`crud`
53272343Sngie	atf_check_equal 'x$foo' 'x1'
54272343Sngie}
55272343Sngie
56272343Sngieatf_test_case readout
57272343Sngiereadout_head() {
58272343Sngie	atf_set "descr" "Tests that \$? is correctly updated in a" \
59272343Sngie	                "compound expression"
60272343Sngie}
61272343Sngiereadout_body() {
62272343Sngie	atf_check_equal '$( true && ! true | false; echo $? )' '0'
63272343Sngie}
64272343Sngie
65272343Sngieatf_test_case trap_subshell
66272343Sngietrap_subshell_head() {
67272343Sngie	atf_set "descr" "Tests that the trap statement in a subshell" \
68272343Sngie	    "works when the subshell exits"
69272343Sngie}
70272343Sngietrap_subshell_body() {
71272343Sngie	atf_check -s eq:0 -o inline:'exiting\n' -x \
72272343Sngie	    '( trap "echo exiting" EXIT; /usr/bin/true )'
73272343Sngie}
74272343Sngie
75272343Sngieatf_test_case trap_zero__implicit_exit
76272343Sngietrap_zero__implicit_exit_body() {
77272343Sngie	# PR bin/6764: sh works but ksh does not"
78272343Sngie	echo '( trap "echo exiting" 0 )' >helper.sh
79272343Sngie	atf_check -s eq:0 -o match:exiting -e empty /bin/sh helper.sh
80272343Sngie	atf_check -s eq:0 -o match:exiting -e empty /bin/ksh helper.sh
81272343Sngie}
82272343Sngie
83272343Sngieatf_test_case trap_zero__explicit_exit
84272343Sngietrap_zero__explicit_exit_body() {
85272343Sngie	echo '( trap "echo exiting" 0; exit )' >helper.sh
86272343Sngie	atf_check -s eq:0 -o match:exiting -e empty /bin/sh helper.sh
87272343Sngie	atf_check -s eq:0 -o match:exiting -e empty /bin/ksh helper.sh
88272343Sngie}
89272343Sngie
90272343Sngieatf_test_case trap_zero__explicit_return
91272343Sngietrap_zero__explicit_return_body() {
92272343Sngie	echo '( trap "echo exiting" 0; return )' >helper.sh
93272343Sngie	atf_check -s eq:0 -o match:exiting -e empty /bin/sh helper.sh
94272343Sngie	atf_check -s eq:0 -o match:exiting -e empty /bin/ksh helper.sh
95272343Sngie}
96272343Sngie
97272343Sngieatf_init_test_cases() {
98272343Sngie	atf_add_test_case background
99272343Sngie	atf_add_test_case function
100272343Sngie	atf_add_test_case readout
101272343Sngie	atf_add_test_case trap_subshell
102272343Sngie	atf_add_test_case trap_zero__implicit_exit
103272343Sngie	atf_add_test_case trap_zero__explicit_exit
104272343Sngie	atf_add_test_case trap_zero__explicit_return
105272343Sngie}
106