1 #!/bin/sh 2 # 3 # Copyright (c) 2015 Fujitsu Ltd. 4 # Author: Guangwen Feng <fenggw-fnst (at] cn.fujitsu.com> 5 # 6 # This program is free software; you can redistribute it and/or modify 7 # it under the terms of the GNU General Public License as published by 8 # the Free Software Foundation; either version 2 of the License, or 9 # (at your option) any later version. 10 # 11 # This program is distributed in the hope that it will be useful, 12 # but WITHOUT ANY WARRANTY; without even the implied warranty of 13 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See 14 # the GNU General Public License for more details. 15 # 16 # Test which command with some basic options. 17 # 18 19 TST_ID="which01" 20 TST_CNT=10 21 TST_SETUP=setup 22 TST_TESTFUNC=do_test 23 TST_NEEDS_TMPDIR=1 24 TST_NEEDS_CMDS="which" 25 . tst_test.sh 26 27 setup() 28 { 29 touch pname 30 chmod +x pname 31 PATH=$PATH:. 32 33 mkdir bin 34 touch bin/pname 35 chmod +x bin/pname 36 PATH=$PATH:./bin 37 38 alias pname='pname -i' 39 } 40 41 which_verify() 42 { 43 until [ -z "$1" ] 44 do 45 found="no" 46 for i in $1; do 47 if grep -q "$i" temp; then 48 found="yes" 49 fi 50 done 51 if [ "$found" != "yes" ]; then 52 echo "'$1' not found in:" 53 cat temp 54 echo 55 return 1 56 fi 57 shift 58 done 59 } 60 61 which_test() 62 { 63 local which_op=$1 64 local prog_name=$2 65 66 local which_cmd="which $which_op $prog_name" 67 68 if [ "$which_op" = "--read-alias" ] || [ "$which_op" = "-i" ] || \ 69 [ "$which_op" = "--skip-alias" ]; then 70 which_cmd="alias | $which_cmd" 71 fi 72 73 eval ${which_cmd} >temp 2>&1 74 if [ $? -ne 0 ]; then 75 grep -q -E "unknown option|invalid option|Usage" temp 76 if [ $? -eq 0 ]; then 77 tst_res TCONF "'${which_cmd}' not supported." 78 return 79 fi 80 81 tst_res TFAIL "'${which_cmd}' failed." 82 cat temp 83 return 84 fi 85 86 if [ $# -gt 2 ]; then 87 shift 2 88 which_verify "$@" 89 if [ $? -ne 0 ]; then 90 tst_res TFAIL "'${which_cmd}' failed, not expected." 91 return 92 fi 93 fi 94 95 tst_res TPASS "'${which_cmd}' passed." 96 } 97 98 do_test() 99 { 100 case $1 in 101 1) which_test "" "pname" "$PWD/pname ./pname";; 102 2) which_test "--all" "pname" "$PWD/bin/pname" "$PWD/pname";; 103 3) which_test "-a" "pname" "$PWD/bin/pname ./bin/pname" "$PWD/pname ./pname";; 104 4) which_test "--read-alias" "pname" "pname='pname -i'" "$PWD/pname";; 105 5) which_test "-i" "pname" "pname='pname -i'" "$PWD/pname";; 106 6) alias which='which --read-alias'; 107 which_test "--skip-alias" "pname" "$PWD/pname"; 108 unalias which;; 109 7) which_test "--version";; 110 8) which_test "-v";; 111 9) which_test "-V";; 112 10) which_test "--help";; 113 esac 114 } 115 116 tst_run 117