xref: /freebsd/usr.bin/indent/tests/functional_test.sh (revision d0b2dbfa0ecf2bbc9709efc5e20baf8e4b44bbbf)
1*7d9ade5dSEnji Cooper#
2*7d9ade5dSEnji Cooper# Copyright 2016 Dell EMC
3*7d9ade5dSEnji Cooper# All rights reserved.
4*7d9ade5dSEnji Cooper#
5*7d9ade5dSEnji Cooper# Redistribution and use in source and binary forms, with or without
6*7d9ade5dSEnji Cooper# modification, are permitted provided that the following conditions are
7*7d9ade5dSEnji Cooper# met:
8*7d9ade5dSEnji Cooper#
9*7d9ade5dSEnji Cooper# * Redistributions of source code must retain the above copyright
10*7d9ade5dSEnji Cooper#   notice, this list of conditions and the following disclaimer.
11*7d9ade5dSEnji Cooper# * Redistributions in binary form must reproduce the above copyright
12*7d9ade5dSEnji Cooper#   notice, this list of conditions and the following disclaimer in the
13*7d9ade5dSEnji Cooper#   documentation and/or other materials provided with the distribution.
14*7d9ade5dSEnji Cooper#
15*7d9ade5dSEnji Cooper# THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
16*7d9ade5dSEnji Cooper# "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
17*7d9ade5dSEnji Cooper# LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
18*7d9ade5dSEnji Cooper# A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
19*7d9ade5dSEnji Cooper# OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
20*7d9ade5dSEnji Cooper# SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
21*7d9ade5dSEnji Cooper# LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
22*7d9ade5dSEnji Cooper# DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
23*7d9ade5dSEnji Cooper# THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
24*7d9ade5dSEnji Cooper# (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
25*7d9ade5dSEnji Cooper# OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
26*7d9ade5dSEnji Cooper#
27*7d9ade5dSEnji Cooper
28*7d9ade5dSEnji CooperSRCDIR=$(atf_get_srcdir)
29*7d9ade5dSEnji Cooper
30*7d9ade5dSEnji Coopercheck()
31*7d9ade5dSEnji Cooper{
32*7d9ade5dSEnji Cooper	local tc=${1}; shift
33*7d9ade5dSEnji Cooper
34*7d9ade5dSEnji Cooper	local indent=$(atf_config_get usr.bin.indent.test_indent /usr/bin/indent)
35*7d9ade5dSEnji Cooper
36*7d9ade5dSEnji Cooper	# All of the files need to be in the ATF sandbox in order for the tests
37*7d9ade5dSEnji Cooper	# to pass.
38*7d9ade5dSEnji Cooper	atf_check cp ${SRCDIR}/${tc}* .
39*7d9ade5dSEnji Cooper
40*7d9ade5dSEnji Cooper	# Remove $FreeBSD$ RCS expansions because they get re-indented, which
41*7d9ade5dSEnji Cooper	# changes the output
42*7d9ade5dSEnji Cooper	local out_file="${tc}.stdout"
43*7d9ade5dSEnji Cooper	if [ -f "${out_file}" ]; then
44*7d9ade5dSEnji Cooper		parsed_file=output_file.parsed
45*7d9ade5dSEnji Cooper
46*7d9ade5dSEnji Cooper		atf_check -o save:$parsed_file sed -e '/\$FreeBSD.*\$/d' \
47*7d9ade5dSEnji Cooper		    ${tc}.stdout
48*7d9ade5dSEnji Cooper		out_flag="-o file:$parsed_file"
49*7d9ade5dSEnji Cooper	fi
50*7d9ade5dSEnji Cooper	local profile_file="${tc}.pro"
51*7d9ade5dSEnji Cooper	if [ -f "${profile_file}" ]; then
52*7d9ade5dSEnji Cooper		profile_flag="-P${profile_file}"
53*7d9ade5dSEnji Cooper	else
54*7d9ade5dSEnji Cooper		# Make sure we don't implicitly use ~/.indent.pro from the test
55*7d9ade5dSEnji Cooper		# host, for determinism purposes.
56*7d9ade5dSEnji Cooper		profile_flag="-npro"
57*7d9ade5dSEnji Cooper	fi
58*7d9ade5dSEnji Cooper	sed -e '/\$FreeBSD.*\$/d' ${tc} > input_file.parsed
59*7d9ade5dSEnji Cooper	atf_check -s exit:${tc##*.} ${out_flag} ${indent} ${profile_flag} < input_file.parsed
60*7d9ade5dSEnji Cooper}
61*7d9ade5dSEnji Cooper
62*7d9ade5dSEnji Cooperadd_testcase()
63*7d9ade5dSEnji Cooper{
64*7d9ade5dSEnji Cooper	local tc=${1}
65*7d9ade5dSEnji Cooper	local tc_escaped word
66*7d9ade5dSEnji Cooper
67*7d9ade5dSEnji Cooper	case "${tc%.*}" in
68*7d9ade5dSEnji Cooper	*-*)
69*7d9ade5dSEnji Cooper		local IFS="-"
70*7d9ade5dSEnji Cooper		for word in ${tc%.*}; do
71*7d9ade5dSEnji Cooper			tc_escaped="${tc_escaped:+${tc_escaped}_}${word}"
72*7d9ade5dSEnji Cooper		done
73*7d9ade5dSEnji Cooper		;;
74*7d9ade5dSEnji Cooper	*)
75*7d9ade5dSEnji Cooper		tc_escaped=${tc%.*}
76*7d9ade5dSEnji Cooper		;;
77*7d9ade5dSEnji Cooper	esac
78*7d9ade5dSEnji Cooper
79*7d9ade5dSEnji Cooper	atf_test_case ${tc_escaped}
80*7d9ade5dSEnji Cooper	eval "${tc_escaped}_body() { check ${tc}; }"
81*7d9ade5dSEnji Cooper	atf_add_test_case ${tc_escaped}
82*7d9ade5dSEnji Cooper}
83*7d9ade5dSEnji Cooper
84*7d9ade5dSEnji Cooperatf_init_test_cases()
85*7d9ade5dSEnji Cooper{
86*7d9ade5dSEnji Cooper	for path in $(find -Es "${SRCDIR}" -regex '.*\.[0-9]+$'); do
87*7d9ade5dSEnji Cooper		add_testcase ${path##*/}
88*7d9ade5dSEnji Cooper	done
89*7d9ade5dSEnji Cooper}
90