xref: /freebsd/contrib/atf/atf-sh/integration_test.sh (revision af23369a6deaaeb612ab266eb88b8bb8d560c322)
1# Copyright (c) 2010 The NetBSD Foundation, Inc.
2# All rights reserved.
3#
4# Redistribution and use in source and binary forms, with or without
5# modification, are permitted provided that the following conditions
6# are met:
7# 1. Redistributions of source code must retain the above copyright
8#    notice, this list of conditions and the following disclaimer.
9# 2. Redistributions in binary form must reproduce the above copyright
10#    notice, this list of conditions and the following disclaimer in the
11#    documentation and/or other materials provided with the distribution.
12#
13# THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND
14# CONTRIBUTORS ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES,
15# INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF
16# MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
17# IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS BE LIABLE FOR ANY
18# DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
19# DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE
20# GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
21# INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER
22# IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR
23# OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN
24# IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
25
26: ${ATF_SH:="__ATF_SH__"}
27
28create_test_program() {
29    local output="${1}"; shift
30    echo "#! ${ATF_SH} ${*}" >"${output}"
31    cat >>"${output}"
32    chmod +x "${output}"
33}
34
35atf_test_case no_args
36no_args_body()
37{
38    cat >experr <<EOF
39atf-sh: ERROR: No test program provided
40atf-sh: See atf-sh(1) for usage details.
41EOF
42    atf_check -s eq:1 -o ignore -e file:experr "${ATF_SH}"
43}
44
45atf_test_case missing_script
46missing_script_body()
47{
48    cat >experr <<EOF
49atf-sh: ERROR: The test program 'non-existent' does not exist
50EOF
51    atf_check -s eq:1 -o ignore -e file:experr "${ATF_SH}" non-existent
52}
53
54atf_test_case arguments
55arguments_body()
56{
57    create_test_program tp <<EOF
58main() {
59    echo ">>>\${0}<<<"
60    while test \${#} -gt 0; do
61        echo ">>>\${1}<<<"
62        shift
63    done
64    true
65}
66EOF
67
68    cat >expout <<EOF
69>>>./tp<<<
70>>> a b <<<
71>>>foo<<<
72EOF
73    atf_check -s eq:0 -o file:expout -e empty ./tp ' a b ' foo
74
75    cat >expout <<EOF
76>>>tp<<<
77>>> hello bye <<<
78>>>foo bar<<<
79EOF
80    atf_check -s eq:0 -o file:expout -e empty "${ATF_SH}" tp \
81        ' hello bye ' 'foo bar'
82}
83
84atf_test_case custom_shell__command_line
85custom_shell__command_line_body()
86{
87    cat >expout <<EOF
88This is the custom shell
89This is the test program
90EOF
91
92    cat >custom-shell <<EOF
93#! /bin/sh
94echo "This is the custom shell"
95exec /bin/sh "\${@}"
96EOF
97    chmod +x custom-shell
98
99    echo 'main() { echo "This is the test program"; }' | create_test_program tp
100    atf_check -s eq:0 -o file:expout -e empty "${ATF_SH}" -s ./custom-shell tp
101}
102
103atf_test_case custom_shell__shebang
104custom_shell__shebang_body()
105{
106    cat >expout <<EOF
107This is the custom shell
108This is the test program
109EOF
110
111    cat >custom-shell <<EOF
112#! /bin/sh
113echo "This is the custom shell"
114exec /bin/sh "\${@}"
115EOF
116    chmod +x custom-shell
117
118    echo 'main() { echo "This is the test program"; }' | create_test_program \
119        tp "-s$(pwd)/custom-shell"
120    atf_check -s eq:0 -o file:expout -e empty ./tp
121}
122
123atf_test_case set_e
124set_e_head()
125{
126    atf_set "descr" "Simple test to validate that atf-sh works even when" \
127        "set -e is enabled"
128}
129set_e_body()
130{
131    cat >custom-shell <<EOF
132#! /bin/sh
133exec /bin/sh -e "\${@}"
134EOF
135    chmod +x custom-shell
136
137    cat >tp <<EOF
138atf_test_case helper
139helper_body() {
140    atf_skip "reached"
141}
142atf_init_test_cases() {
143    atf_add_test_case helper
144}
145EOF
146    atf_check -s eq:0 -o match:skipped.*reached \
147        "${ATF_SH}" -s ./custom-shell tp helper
148}
149
150atf_init_test_cases()
151{
152    atf_add_test_case no_args
153    atf_add_test_case missing_script
154    atf_add_test_case arguments
155    atf_add_test_case custom_shell__command_line
156    atf_add_test_case custom_shell__shebang
157    atf_add_test_case set_e
158}
159
160# vim: syntax=sh:expandtab:shiftwidth=4:softtabstop=4
161