xref: /netbsd-src/tests/usr.bin/printf/t_command.sh (revision d1e529d31cb3c9952600dac6f3d85aa9e3432af0)
1# $NetBSD: t_command.sh,v 1.1 2018/09/05 21:05:40 kre Exp $
2#
3# Copyright (c) 2018 The NetBSD Foundation, Inc.
4# All rights reserved.
5#
6# Redistribution and use in source and binary forms, with or without
7# modification, are permitted provided that the following conditions
8# are met:
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.
14#
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.
26#
27
28# The printf command to test (if not a full path, $PATH will be examined)
29# Do not use relative (./ or ../) paths when running under ATF.
30: ${TEST_PRINTF:=/usr/bin/printf}
31
32# This tests an external (filesystem) printf command
33
34# For the actual code/tests see printf.sh
35# (shared with tests for the shell builtin printf
36
37# These tests are designed to be able to be run by ATF, or standalone
38#
39# That is, either
40#	atf_run t_command | atf-report		(or whatever is needed)
41# or
42#	sh t_command [sub_test]...	( default is to run all sub_tests )
43#
44# nb: for standalone runs, do not attempt ./t_builtin as the #! line
45# added will force ATF which will complain about the incorrect method
46# of running the test.  Instead use some Bourne shell compatible shell
47# (any will do, caveat any bugs it might have), and run the script explicitly.
48
49do_printf()
50{
51	$Running_under_ATF && atf_require_prog "${PRINTF%% *}"
52
53	unset LANG LC_ALL LC_NUMERIC LC_CTYPE LC_MESSAGES
54
55	${PRINTF} "$@"
56}
57
58No_command()
59{
60	setmsg No_command
61
62	case "${TEST_PRINTF%% *}" in
63	( '' )
64		msg='Configuration error: check $TEST_PRINTF'
65		;;
66	( /* | ./* | ../* )
67		msg="Cannot find/execute ${TEST_PRINTF%% *}"
68		;;
69	( * )
70		msg="No '${TEST_PRINTF%% *}' found in "'$PATH'
71		;;
72	esac
73	atf_skip "${msg}"
74
75	return $RVAL
76}
77
78# See if we have a "printf" command in $PATH to test - pick the first
79
80setup()
81{
82	saveIFS="${IFS-UnSet}"
83	saveOPTS="$(set +o)"
84
85	unset PRINTF
86
87	case "${TEST_PRINTF%% *}" in
88	( /* )		PRINTF="${TEST_PRINTF}" ;;
89	( ./* | ../* )	PRINTF="${PWD}/${TEST_PRINTF}" ;;
90	(*)
91		set -f
92		IFS=:
93		for P in $PATH
94		do
95			case "$P" in
96			('' | . )	D="${PWD}";;
97			( /* )		D="${P}"  ;;
98			( * )		D="${PWD}/${P}";;
99			esac
100
101			test -x "${D}/${TEST_PRINTF%% *}" || continue
102
103			PRINTF="${D}/${TEST_PRINTF}"
104			break
105		done
106		unset IFS
107		eval "${saveOPTS}"
108
109		case "${saveIFS}" in
110		(UnSet)		unset IFS;;
111		(*)		IFS="${saveIFS}";;
112		esac
113		;;
114	esac
115
116	test -x "${PRINTF%% *}" || PRINTF=
117
118	case "${PRINTF}" in
119
120	('')	Tests=
121		define No_command 'Dummy test to skip no printf command'
122		return 1
123		;;
124
125	( * )
126		# nothing here, it all happens below.
127		;;
128
129	esac
130
131	return 0
132}
133
134setmsg()
135{
136	MSG="${PRINTF}"
137	CurrentTest="$1"
138	RVAL=0
139}
140
141BUILTIN_TEST=false
142
143# All the code to actually run the test comes from printf.sh ...
144
145