xref: /freebsd/contrib/atf/atf-sh/atf_check_test.sh (revision 4436b51dff5736e74da464946049ea6899a88938)
1# Copyright (c) 2007 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# TODO: Bring in the checks in the bootstrap testsuite for atf_check.
27
28atf_test_case info_ok
29info_ok_head()
30{
31    atf_set "descr" "Verifies that atf_check prints an informative" \
32                    "message even when the command is successful"
33}
34info_ok_body()
35{
36    h="$(atf_get_srcdir)/misc_helpers -s $(atf_get_srcdir)"
37
38    atf_check -s eq:0 -o save:stdout -e save:stderr -x \
39              "${h} atf_check_info_ok"
40    grep 'Executing command.*true' stdout >/dev/null || \
41        atf_fail "atf_check does not print an informative message"
42
43    atf_check -s eq:0 -o save:stdout -e save:stderr -x \
44              "${h} atf_check_info_fail"
45    grep 'Executing command.*false' stdout >/dev/null || \
46        atf_fail "atf_check does not print an informative message"
47}
48
49atf_test_case expout_mismatch
50expout_mismatch_head()
51{
52    atf_set "descr" "Verifies that atf_check prints a diff of the" \
53                    "stdout and the expected stdout if the two do not" \
54                    "match"
55}
56expout_mismatch_body()
57{
58    h="$(atf_get_srcdir)/misc_helpers -s $(atf_get_srcdir)"
59
60    atf_check -s eq:1 -o save:stdout -e save:stderr -x \
61              "${h} atf_check_expout_mismatch"
62    grep 'Executing command.*echo bar' stdout >/dev/null || \
63        atf_fail "atf_check does not print an informative message"
64    grep 'stdout does not match golden output' stderr >/dev/null || \
65        atf_fail "atf_check does not print the stdout header"
66    grep 'stderr' stderr >/dev/null && \
67        atf_fail "atf_check prints the stderr header"
68    grep '^-foo' stderr >/dev/null || \
69        atf_fail "atf_check does not print the stdout's diff"
70    grep '^+bar' stderr >/dev/null || \
71        atf_fail "atf_check does not print the stdout's diff"
72}
73
74atf_test_case experr_mismatch
75experr_mismatch_head()
76{
77    atf_set "descr" "Verifies that atf_check prints a diff of the" \
78                    "stderr and the expected stderr if the two do not" \
79                    "match"
80}
81experr_mismatch_body()
82{
83    h="$(atf_get_srcdir)/misc_helpers -s $(atf_get_srcdir)"
84
85    atf_check -s eq:1 -o save:stdout -e save:stderr -x \
86              "${h} atf_check_experr_mismatch"
87    grep 'Executing command.*echo bar' stdout >/dev/null || \
88        atf_fail "atf_check does not print an informative message"
89    grep 'stdout' stderr >/dev/null && \
90        atf_fail "atf_check prints the stdout header"
91    grep 'stderr does not match golden output' stderr >/dev/null || \
92        atf_fail "atf_check does not print the stderr header"
93    grep '^-foo' stderr >/dev/null || \
94        atf_fail "atf_check does not print the stderr's diff"
95    grep '^+bar' stderr >/dev/null || \
96        atf_fail "atf_check does not print the stderr's diff"
97}
98
99atf_test_case null_stdout
100null_stdout_head()
101{
102    atf_set "descr" "Verifies that atf_check prints a the stdout it got" \
103                    "when it was supposed to be null"
104}
105null_stdout_body()
106{
107    h="$(atf_get_srcdir)/misc_helpers -s $(atf_get_srcdir)"
108
109    atf_check -s eq:1 -o save:stdout -e save:stderr -x \
110              "${h} atf_check_null_stdout"
111    grep 'Executing command.*echo.*These.*contents' stdout >/dev/null || \
112        atf_fail "atf_check does not print an informative message"
113    grep 'stdout not empty' stderr >/dev/null || \
114        atf_fail "atf_check does not print the stdout header"
115    grep 'stderr' stderr >/dev/null && \
116        atf_fail "atf_check prints the stderr header"
117    grep 'These are the contents' stderr >/dev/null || \
118        atf_fail "atf_check does not print stdout's contents"
119}
120
121atf_test_case null_stderr
122null_stderr_head()
123{
124    atf_set "descr" "Verifies that atf_check prints a the stderr it got" \
125                    "when it was supposed to be null"
126}
127null_stderr_body()
128{
129    h="$(atf_get_srcdir)/misc_helpers -s $(atf_get_srcdir)"
130
131    atf_check -s eq:1 -o save:stdout -e save:stderr -x \
132              "${h} atf_check_null_stderr"
133    grep 'Executing command.*echo.*These.*contents' stdout >/dev/null || \
134        atf_fail "atf_check does not print an informative message"
135    grep 'stdout' stderr >/dev/null && \
136        atf_fail "atf_check prints the stdout header"
137    grep 'stderr not empty' stderr >/dev/null || \
138        atf_fail "atf_check does not print the stderr header"
139    grep 'These are the contents' stderr >/dev/null || \
140        atf_fail "atf_check does not print stderr's contents"
141}
142
143atf_test_case equal
144equal_head()
145{
146    atf_set "descr" "Verifies that atf_check_equal works"
147}
148equal_body()
149{
150    h="$(atf_get_srcdir)/misc_helpers -s $(atf_get_srcdir)"
151
152    atf_check -s eq:0 -o ignore -e ignore -x "${h} atf_check_equal_ok"
153
154    atf_check -s eq:1 -o ignore -e ignore -x \
155        "${h} -r resfile atf_check_equal_fail"
156    atf_check -s eq:0 -o ignore -e empty grep '^failed: a != b (a != b)$' \
157        resfile
158
159    atf_check -s eq:0 -o ignore -e ignore -x "${h} atf_check_equal_eval_ok"
160
161    atf_check -s eq:1 -o ignore -e ignore -x \
162        "${h} -r resfile atf_check_equal_eval_fail"
163    atf_check -s eq:0 -o ignore -e empty \
164        grep '^failed: \${x} != \${y} (a != b)$' resfile
165}
166
167atf_test_case flush_stdout_on_death
168flush_stdout_on_death_body()
169{
170    CONTROL_FILE="$(pwd)/done" "$(atf_get_srcdir)/misc_helpers" \
171        -s "$(atf_get_srcdir)" atf_check_flush_stdout >out 2>err &
172    pid="${!}"
173    while [ ! -f ./done ]; do
174        echo "Still waiting for helper to create control file"
175        ls
176        sleep 1
177    done
178    kill -9 "${pid}"
179
180    grep 'Executing command.*true' out \
181        || atf_fail 'First command not in output'
182    grep 'Executing command.*false' out \
183        || atf_fail 'Second command not in output'
184}
185
186atf_init_test_cases()
187{
188    atf_add_test_case info_ok
189    atf_add_test_case expout_mismatch
190    atf_add_test_case experr_mismatch
191    atf_add_test_case null_stdout
192    atf_add_test_case null_stderr
193    atf_add_test_case equal
194    atf_add_test_case flush_stdout_on_death
195}
196
197# vim: syntax=sh:expandtab:shiftwidth=4:softtabstop=4
198