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