Files
vx/tests/test_which.sh

78 lines
2.1 KiB
Bash
Executable File

#!/bin/sh
# tests/test_which.sh - Tests for which applet
_test_name="which"
. "$(dirname "$0")/harness.sh"
setup_tmpdir
WHICH="$VX which"
echo "Running which tests..."
# --- basic: find a known binary ---
_got="$($WHICH sh 2>/dev/null)"
# sh should resolve to something in PATH
if [ -n "$_got" ] && [ -x "$_got" ]; then
pass "basic: finds sh"
else
fail "basic: finds sh" "got: $_got"
fi
# --- basic: nonexistent command ---
check_exit "basic: nonexistent exits 1" 1 $WHICH this_command_does_not_exist_xyz
# --- -s (silent) ---
_got="$($WHICH -s sh 2>/dev/null)"
check_eq "$_got" "" "silent: -s produces no output"
check_exit "silent: -s sh exits 0" 0 $WHICH -s sh
check_exit "silent: -s nonexistent exits 1" 1 $WHICH -s this_command_does_not_exist_xyz
# --- -a (all matches) ---
# Create a temp dir with a duplicate "testprog" and prepend it to PATH
mkdir -p bin1 bin2
printf '#!/bin/sh\n' > bin1/testprog
printf '#!/bin/sh\n' > bin2/testprog
chmod +x bin1/testprog bin2/testprog
_old_path="$PATH"
PATH="$(pwd)/bin1:$(pwd)/bin2:$PATH"
export PATH
# Without -a, should find exactly 1
_count="$($WHICH testprog 2>/dev/null | wc -l | tr -d ' ')"
check_eq "$_count" "1" "allpaths: default finds 1"
# With -a, should find at least 2
_count="$($WHICH -a testprog 2>/dev/null | wc -l | tr -d ' ')"
if [ "$_count" -ge 2 ]; then
pass "allpaths: -a finds multiple"
else
fail "allpaths: -a finds multiple" "got $_count matches"
fi
PATH="$_old_path"
export PATH
# --- multiple arguments ---
$WHICH sh cat > multi_out 2>/dev/null
_lines=$(wc -l < multi_out | tr -d ' ')
check_eq "$_lines" "2" "multi: two args produce two lines"
# --- absolute path passthrough ---
_got="$($WHICH /bin/sh 2>/dev/null)"
if [ "$_got" = "/bin/sh" ] || [ "$_got" = "" ]; then
# FreeBSD which passes through absolute paths if executable
if [ -x /bin/sh ]; then
check_eq "$_got" "/bin/sh" "absolute: /bin/sh passes through"
else
pass "absolute: /bin/sh not executable (skip)"
fi
else
fail "absolute: /bin/sh passes through" "got: $_got"
fi
# --- no args prints usage and exits 1 ---
$WHICH >/dev/null 2>&1
_rc=$?
check_eq "$_rc" "1" "usage: no args exits 1"
summary