1#! /bin/sh
2#
3# @(#)command.sh	1.7 20/03/30 2017-2020 J. Schilling
4#
5
6# Read test core functions
7. ../../common/test-common
8
9#
10# Basic tests to check whether the builtin "command" work
11# IRIX has ls in /sbin/, so we need to be careful
12#
13docommand command01 "$SHELL -c 'command'" 0 "" ""
14docommand command02 "$SHELL -c 'command -v ls | grep bin/ls'" 0 NONEMPTY ""
15docommand command03 "$SHELL -c 'command -V ls | grep \"ls.*bin/ls\"'" 0 NONEMPTY ""
16docommand command04 "$SHELL -c 'command -p ls | grep command.sh'" 0 NONEMPTY ""
17docommand command05 "$SHELL -c 'command env | grep ZzZzZ'" 1 "" ""
18docommand command06 "$SHELL -c 'ZzZzZ=bla command env | grep ZzZzZ=bla'" 0 NONEMPTY ""
19
20docommand command10 "$SHELL -c 'ls() { :; }; ls'" 0 "" ""
21docommand command11 "$SHELL -c 'ls() { :; }; command ls'" 0 NONEMPTY ""
22
23docommand command20 "$SHELL -c 'command expr 1 \< 2'" 0 "1\n" ""
24
25#
26# Check whether command -p switches "echo" into POSIX mode.
27#
28# THese tests may only work with bosh
29#
30if [ "$is_bosh" = true ]; then
31docommand command50 "$SHELL -c 'command echo -n bla'" 0 "-n bla\n" ""
32docommand command51 "$SHELL -c 'PATH=\"/usr/ucb:\$PATH\"; command echo -n bla'" 0 "bla" ""
33docommand command52 "$SHELL -c 'PATH=\"/usr/ucb:\$PATH\"; command -p echo -n bla'" 0 "-n bla\n" ""
34docommand command53 "$SHELL -c 'SYSV3=true; export SYSV3; command echo -n \"bla\\\\t\"'" 0 "bla\t" ""
35docommand command54 "$SHELL -c 'SYSV3=true; export SYSV3; command -p echo -n \"bla\\\\t\"'" 0 "-n bla\t\n" ""
36else
37echo "Skipping tests 50..54 as this shell is $shell and not bosh and the behavior is unspecified"
38fi
39
40docommand command80 "$SHELL -c 'echo() { ls com*; }; command echo OK'" 0 "OK\n" ""
41docommand command81 "$SHELL -c 'echo() { ls com*; }; command -p echo OK'" 0 "OK\n" ""
42
43#
44# Check whether the hash for cmd is clobbered after we called "command cmd"
45#
46cat > x  <<"XEOF"
47LC_ALL=C
48true() {
49	# dummy override
50	command true "$@"
51}
52type true
53true
54type true
55XEOF
56docommand -noremove command100 "$SHELL ./x" 0 NONEMPTY ""
57if grep hash got.stdout >/dev/null ; then
58	fail "Test $cmd_label failed: 'hashed' not expected"
59fi
60if grep / got.stdout >/dev/null ; then
61	fail "Test $cmd_label failed: path name not permitted"
62fi
63remove x
64
65success
66