1#!/bin/sh 2# 3# Copyright (c) 2015 Fujitsu Ltd. 4# Author: Guangwen Feng <fenggw-fnst@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 19TST_CNT=10 20TST_SETUP=setup 21TST_TESTFUNC=do_test 22TST_NEEDS_TMPDIR=1 23TST_NEEDS_CMDS="which" 24. tst_test.sh 25 26setup() 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 40which_verify() 41{ 42 local IFS i j 43 IFS="$IFS_FIRST_LEVEL" 44 for i in $1; do 45 found="no" 46 IFS="$IFS_SECOND_LEVEL" 47 for j in $i; do 48 if grep -F -q "$j" temp; then 49 found="yes" 50 fi 51 done 52 if [ "$found" != "yes" ]; then 53 echo "'$i' not found in:" 54 cat temp 55 echo 56 return 1 57 fi 58 done 59} 60 61which_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 98IFS_FIRST_LEVEL='^' 99IFS_SECOND_LEVEL='|' 100do_test() 101{ 102 case $1 in 103 1) which_test "" "pname" "$PWD/pname|./pname";; 104 2) which_test "-all" "pname" "$PWD/bin/pname|./bin/pname^$PWD/pname|./pname";; 105 3) which_test "-a" "pname" "$PWD/bin/pname|./bin/pname^$PWD/pname|./pname";; 106 4) which_test "--read-alias" "pname" "pname='pname -i'^$PWD/pname";; 107 5) which_test "-i" "pname" "pname='pname -i'^$PWD/pname";; 108 6) alias which='which --read-alias'; 109 which_test "--skip-alias" "pname" "$PWD/pname"; 110 unalias which;; 111 7) which_test "--version";; 112 8) which_test "-v";; 113 9) which_test "-V";; 114 10) which_test "--help";; 115 esac 116} 117 118tst_run 119