Home | History | Annotate | Line # | Download | only in printf
      1  1.1  kre # $NetBSD: t_command.sh,v 1.1 2018/09/05 21:05:40 kre Exp $
      2  1.1  kre #
      3  1.1  kre # Copyright (c) 2018 The NetBSD Foundation, Inc.
      4  1.1  kre # All rights reserved.
      5  1.1  kre #
      6  1.1  kre # Redistribution and use in source and binary forms, with or without
      7  1.1  kre # modification, are permitted provided that the following conditions
      8  1.1  kre # are met:
      9  1.1  kre # 1. Redistributions of source code must retain the above copyright
     10  1.1  kre #    notice, this list of conditions and the following disclaimer.
     11  1.1  kre # 2. Redistributions in binary form must reproduce the above copyright
     12  1.1  kre #    notice, this list of conditions and the following disclaimer in the
     13  1.1  kre #    documentation and/or other materials provided with the distribution.
     14  1.1  kre #
     15  1.1  kre # THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
     16  1.1  kre # ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
     17  1.1  kre # TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
     18  1.1  kre # PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
     19  1.1  kre # BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
     20  1.1  kre # CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
     21  1.1  kre # SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
     22  1.1  kre # INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
     23  1.1  kre # CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
     24  1.1  kre # ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
     25  1.1  kre # POSSIBILITY OF SUCH DAMAGE.
     26  1.1  kre #
     27  1.1  kre 
     28  1.1  kre # The printf command to test (if not a full path, $PATH will be examined)
     29  1.1  kre # Do not use relative (./ or ../) paths when running under ATF.
     30  1.1  kre : ${TEST_PRINTF:=/usr/bin/printf}
     31  1.1  kre 
     32  1.1  kre # This tests an external (filesystem) printf command
     33  1.1  kre 
     34  1.1  kre # For the actual code/tests see printf.sh
     35  1.1  kre # (shared with tests for the shell builtin printf
     36  1.1  kre 
     37  1.1  kre # These tests are designed to be able to be run by ATF, or standalone
     38  1.1  kre #
     39  1.1  kre # That is, either
     40  1.1  kre #	atf_run t_command | atf-report		(or whatever is needed)
     41  1.1  kre # or
     42  1.1  kre #	sh t_command [sub_test]...	( default is to run all sub_tests )
     43  1.1  kre #
     44  1.1  kre # nb: for standalone runs, do not attempt ./t_builtin as the #! line
     45  1.1  kre # added will force ATF which will complain about the incorrect method
     46  1.1  kre # of running the test.  Instead use some Bourne shell compatible shell
     47  1.1  kre # (any will do, caveat any bugs it might have), and run the script explicitly.
     48  1.1  kre 
     49  1.1  kre do_printf()
     50  1.1  kre {
     51  1.1  kre 	$Running_under_ATF && atf_require_prog "${PRINTF%% *}"
     52  1.1  kre 
     53  1.1  kre 	unset LANG LC_ALL LC_NUMERIC LC_CTYPE LC_MESSAGES
     54  1.1  kre 
     55  1.1  kre 	${PRINTF} "$@"
     56  1.1  kre }
     57  1.1  kre 
     58  1.1  kre No_command()
     59  1.1  kre {
     60  1.1  kre 	setmsg No_command
     61  1.1  kre 
     62  1.1  kre 	case "${TEST_PRINTF%% *}" in
     63  1.1  kre 	( '' )
     64  1.1  kre 		msg='Configuration error: check $TEST_PRINTF'
     65  1.1  kre 		;;
     66  1.1  kre 	( /* | ./* | ../* )
     67  1.1  kre 		msg="Cannot find/execute ${TEST_PRINTF%% *}"
     68  1.1  kre 		;;
     69  1.1  kre 	( * )
     70  1.1  kre 		msg="No '${TEST_PRINTF%% *}' found in "'$PATH'
     71  1.1  kre 		;;
     72  1.1  kre 	esac
     73  1.1  kre 	atf_skip "${msg}"
     74  1.1  kre 
     75  1.1  kre 	return $RVAL
     76  1.1  kre }
     77  1.1  kre 
     78  1.1  kre # See if we have a "printf" command in $PATH to test - pick the first
     79  1.1  kre 
     80  1.1  kre setup()
     81  1.1  kre {
     82  1.1  kre 	saveIFS="${IFS-UnSet}"
     83  1.1  kre 	saveOPTS="$(set +o)"
     84  1.1  kre 
     85  1.1  kre 	unset PRINTF
     86  1.1  kre 
     87  1.1  kre 	case "${TEST_PRINTF%% *}" in
     88  1.1  kre 	( /* )		PRINTF="${TEST_PRINTF}" ;;
     89  1.1  kre 	( ./* | ../* )	PRINTF="${PWD}/${TEST_PRINTF}" ;;
     90  1.1  kre 	(*)
     91  1.1  kre 		set -f
     92  1.1  kre 		IFS=:
     93  1.1  kre 		for P in $PATH
     94  1.1  kre 		do
     95  1.1  kre 			case "$P" in
     96  1.1  kre 			('' | . )	D="${PWD}";;
     97  1.1  kre 			( /* )		D="${P}"  ;;
     98  1.1  kre 			( * )		D="${PWD}/${P}";;
     99  1.1  kre 			esac
    100  1.1  kre 
    101  1.1  kre 			test -x "${D}/${TEST_PRINTF%% *}" || continue
    102  1.1  kre 
    103  1.1  kre 			PRINTF="${D}/${TEST_PRINTF}"
    104  1.1  kre 			break
    105  1.1  kre 		done
    106  1.1  kre 		unset IFS
    107  1.1  kre 		eval "${saveOPTS}"
    108  1.1  kre 
    109  1.1  kre 		case "${saveIFS}" in
    110  1.1  kre 		(UnSet)		unset IFS;;
    111  1.1  kre 		(*)		IFS="${saveIFS}";;
    112  1.1  kre 		esac
    113  1.1  kre 		;;
    114  1.1  kre 	esac
    115  1.1  kre 
    116  1.1  kre 	test -x "${PRINTF%% *}" || PRINTF=
    117  1.1  kre 
    118  1.1  kre 	case "${PRINTF}" in
    119  1.1  kre 
    120  1.1  kre 	('')	Tests=
    121  1.1  kre 		define No_command 'Dummy test to skip no printf command'
    122  1.1  kre 		return 1
    123  1.1  kre 		;;
    124  1.1  kre 
    125  1.1  kre 	( * )
    126  1.1  kre 		# nothing here, it all happens below.
    127  1.1  kre 		;;
    128  1.1  kre 
    129  1.1  kre 	esac
    130  1.1  kre 
    131  1.1  kre 	return 0
    132  1.1  kre }
    133  1.1  kre 
    134  1.1  kre setmsg()
    135  1.1  kre {
    136  1.1  kre 	MSG="${PRINTF}"
    137  1.1  kre 	CurrentTest="$1"
    138  1.1  kre 	RVAL=0
    139  1.1  kre }
    140  1.1  kre 
    141  1.1  kre BUILTIN_TEST=false
    142  1.1  kre 
    143  1.1  kre # All the code to actually run the test comes from printf.sh ...
    144  1.1  kre 
    145