1#!/bin/sh 2# SPDX-License-Identifier: GPL-2.0-or-later 3# Copyright (c) 2015 Fujitsu Ltd. 4# Author: Guangwen Feng <[email protected]> 5# 6# Test which command with some basic options. 7 8TST_CNT=10 9TST_SETUP=setup 10TST_TESTFUNC=do_test 11TST_NEEDS_TMPDIR=1 12TST_NEEDS_CMDS="which" 13 14setup() 15{ 16 touch pname 17 chmod +x pname 18 PATH=$PATH:. 19 20 mkdir bin 21 touch bin/pname 22 chmod +x bin/pname 23 PATH=$PATH:./bin 24 25 alias pname='pname -i' 26} 27 28which_verify() 29{ 30 local IFS i j 31 IFS="$IFS_FIRST_LEVEL" 32 for i in $1; do 33 found="no" 34 IFS="$IFS_SECOND_LEVEL" 35 for j in $i; do 36 if grep -F -q "$j" temp; then 37 found="yes" 38 fi 39 done 40 if [ "$found" != "yes" ]; then 41 echo "'$i' not found in:" 42 cat temp 43 echo 44 return 1 45 fi 46 done 47} 48 49which_test() 50{ 51 local which_op=$1 52 local prog_name=$2 53 54 local which_cmd="which $which_op $prog_name" 55 56 if [ "$which_op" = "--read-alias" ] || [ "$which_op" = "-i" ] || \ 57 [ "$which_op" = "--skip-alias" ]; then 58 which_cmd="alias | $which_cmd" 59 fi 60 61 eval ${which_cmd} >temp 2>&1 62 if [ $? -ne 0 ]; then 63 grep -q -E "unknown option|invalid option|Usage" temp 64 if [ $? -eq 0 ]; then 65 tst_res TCONF "'${which_cmd}' not supported." 66 return 67 fi 68 69 tst_res TFAIL "'${which_cmd}' failed." 70 cat temp 71 return 72 fi 73 74 if [ $# -gt 2 ]; then 75 shift 2 76 which_verify "$@" 77 if [ $? -ne 0 ]; then 78 tst_res TFAIL "'${which_cmd}' failed, not expected." 79 return 80 fi 81 fi 82 83 tst_res TPASS "'${which_cmd}' passed." 84} 85 86IFS_FIRST_LEVEL='^' 87IFS_SECOND_LEVEL='|' 88do_test() 89{ 90 case $1 in 91 1) which_test "" "pname" "$PWD/pname|./pname";; 92 2) which_test "-all" "pname" "$PWD/bin/pname|./bin/pname^$PWD/pname|./pname";; 93 3) which_test "-a" "pname" "$PWD/bin/pname|./bin/pname^$PWD/pname|./pname";; 94 4) which_test "--read-alias" "pname" "pname='pname -i'^$PWD/pname";; 95 5) which_test "-i" "pname" "pname='pname -i'^$PWD/pname";; 96 6) alias which='which --read-alias'; 97 which_test "--skip-alias" "pname" "$PWD/pname"; 98 unalias which;; 99 7) which_test "--version";; 100 8) which_test "-v";; 101 9) which_test "-V";; 102 10) which_test "--help";; 103 esac 104} 105 106. tst_test.sh 107tst_run 108