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