Home | History | Annotate | Line # | Download | only in printf
t_builtin.sh revision 1.1.2.2
      1 # $NetBSD: t_builtin.sh,v 1.1.2.2 2018/09/06 06:56:49 pgoyette 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 shell to use for tests of the builtin printf (or at least try)
     29 : ${TEST_SH:=/bin/sh}
     30 
     31 # This tests the builtin printf command in ${TEST_SH}
     32 
     33 # For the actual code/tests see printf.sh
     34 # (shared with tests for the external (filesystem) command printf
     35 
     36 # These tests are designed to be able to be run by ATF, or standalone
     37 #
     38 # That is, either
     39 #	atf_run t_builtin | atf-report		(or whatever is needed)
     40 # or
     41 #	sh t_builtin [sub_test]...	(default is to run all sub_tests)
     42 #
     43 # nb: for standalone runs, do not attempt ./t_builtin as the #! line
     44 # added will force ATF which will complain about the test being run
     45 # in the wrong way.  Instead use some Bourne shell compatible shell
     46 # (any will do, caveat any bugs it might have, it need not be TEST_SH,
     47 # but it can be) and run the script explicitly.
     48 
     49 do_printf()
     50 {
     51 	$Running_under_ATF && atf_require_prog "${TEST_SH%% *}"
     52 
     53 	unset LANG LC_ALL LC_NUMERIC LC_CTYPE LC_MESSAGES
     54 
     55 	case "$*" in
     56 	*\'*)
     57 		$Running_under_ATF && atf_require_prog printf
     58 		$Running_under_ATF && atf_require_pfog sed
     59 		;;
     60 	esac
     61 
     62 	COMMAND=printf
     63 	for ARG
     64 	do
     65 		case "${ARG}" in
     66 		(*\'*)
     67 			# This is kind of odd, we need a working
     68 			# printf in order to test printf ...
     69 			# nb: do not use echo here, an arg might be "-n'x"
     70 			COMMAND="${COMMAND} '$(
     71 			    printf '%s\n' "${ARG}" |
     72 				sed "s/'/'\\\\''/g"
     73 			)'"
     74 			;;
     75 		(*)
     76 			COMMAND="${COMMAND} '${ARG}'"
     77 			;;
     78 		esac
     79 	done
     80 	${TEST_SH} -c "${COMMAND}"
     81 }
     82 
     83 Not_builtin()
     84 {
     85 	if $Running_under_ATF
     86 	then
     87 		atf_skip "${TEST_SH%% *} does not have printf built in"
     88 	else
     89 		echo >&2 "No builtin printf in ${TEST_SH}"
     90 		exit 1
     91 	fi
     92 }
     93 
     94 # See if we have a builtin "printf" command to test
     95 
     96 setup()
     97 {
     98 	case "$( ${TEST_SH} -c 'type printf' 2>&1 )" in
     99 
    100 	( *[Bb]uiltin* | *[Bb]uilt[-\ ][Ii]n* )
    101 		# nothing here, it all happens below.
    102 		;;
    103 
    104 	(*)	Tests=
    105 		define Not_builtin 'Dummy test to skip when no printf builtin'
    106 		return 1
    107 		;;
    108 	esac
    109 
    110 	return 0
    111 }
    112 
    113 setmsg()
    114 {
    115 	MSG="${TEST_SH%% *} builtin printf"
    116 	CurrentTest="$1"
    117 	RVAL=0
    118 }
    119 
    120 BUILTIN_TEST=true
    121 
    122 # All the code to actually run the test comes from printf.sh ...
    123 
    124