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_CNT=10 20 TST_SETUP=setup 21 TST_TESTFUNC=do_test 22 TST_NEEDS_TMPDIR=1 23 TST_NEEDS_CMDS="which" 24 . tst_test.sh 25 26 setup() 27 { 28 touch pname 29 chmod +x pname 30 PATH=$PATH:. 31 32 mkdir bin 33 touch bin/pname 34 chmod +x bin/pname 35 PATH=$PATH:./bin 36 37 alias pname='pname -i' 38 } 39 40 which_verify() 41 { 42 until [ -z "$1" ] 43 do 44 found="no" 45 for i in $1; do 46 if grep -q "$i" temp; then 47 found="yes" 48 fi 49 done 50 if [ "$found" != "yes" ]; then 51 echo "'$1' not found in:" 52 cat temp 53 echo 54 return 1 55 fi 56 shift 57 done 58 } 59 60 which_test() 61 { 62 local which_op=$1 63 local prog_name=$2 64 65 local which_cmd="which $which_op $prog_name" 66 67 if [ "$which_op" = "--read-alias" ] || [ "$which_op" = "-i" ] || \ 68 [ "$which_op" = "--skip-alias" ]; then 69 which_cmd="alias | $which_cmd" 70 fi 71 72 eval ${which_cmd} >temp 2>&1 73 if [ $? -ne 0 ]; then 74 grep -q -E "unknown option|invalid option|Usage" temp 75 if [ $? -eq 0 ]; then 76 tst_res TCONF "'${which_cmd}' not supported." 77 return 78 fi 79 80 tst_res TFAIL "'${which_cmd}' failed." 81 cat temp 82 return 83 fi 84 85 if [ $# -gt 2 ]; then 86 shift 2 87 which_verify "$@" 88 if [ $? -ne 0 ]; then 89 tst_res TFAIL "'${which_cmd}' failed, not expected." 90 return 91 fi 92 fi 93 94 tst_res TPASS "'${which_cmd}' passed." 95 } 96 97 do_test() 98 { 99 case $1 in 100 1) which_test "" "pname" "$PWD/pname ./pname";; 101 2) which_test "--all" "pname" "$PWD/bin/pname" "$PWD/pname";; 102 3) which_test "-a" "pname" "$PWD/bin/pname ./bin/pname" "$PWD/pname ./pname";; 103 4) which_test "--read-alias" "pname" "pname='pname -i'" "$PWD/pname";; 104 5) which_test "-i" "pname" "pname='pname -i'" "$PWD/pname";; 105 6) alias which='which --read-alias'; 106 which_test "--skip-alias" "pname" "$PWD/pname"; 107 unalias which;; 108 7) which_test "--version";; 109 8) which_test "-v";; 110 9) which_test "-V";; 111 10) which_test "--help";; 112 esac 113 } 114 115 tst_run 116