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