init.cfg (cmd_exe): Provide a function to find the command under test,
whether in the $PATH or in the standard build directory.
* tests/env/env0-from.sh: Use it instead of `command -v`.
* tests/misc/coreutils.sh: Likewise.
* tests/nproc/nproc-quota.sh: Likewise.
* tests/env/env.sh: Likewise.  Also remove non robust use of sed.
---
 init.cfg                   | 14 ++++++++++++++
 tests/env/env.sh           |  2 +-
 tests/env/env0-from.sh     |  2 +-
 tests/misc/coreutils.sh    |  2 +-
 tests/nproc/nproc-quota.sh |  2 +-
 5 files changed, 18 insertions(+), 4 deletions(-)

diff --git a/init.cfg b/init.cfg
index 08eb26824..8bacb5d68 100644
--- a/init.cfg
+++ b/init.cfg
@@ -350,6 +350,20 @@ require_built_()
   test $skip_ = yes && skip_ "required program(s) not built"
 }
 
+# Return the file for a command.
+# Warn and fail if not found.
+cmd_exe()
+{
+  # Give precedence to $PATH to allow overriding command under test
+  local found_cmd=$(command -v "$1$EXEEXT") # May be a builtin
+  local found_dir=$(dirname "$(command -v "env$EXEEXT")") # Assume env is not
+  local built_cmd="$abs_top_builddir/src/$1$EXEEXT"
+  { test -x "$found_cmd" && printf '%s\n' "$found_cmd"; } ||
+  { test -x "$found_dir/$1" && printf '%s\n' "$found_dir/$1"; } ||
+  { test -x "$built_cmd" && printf '%s\n' "$built_cmd"; } ||
+  { warn_ "unable to find $1 command"; return 1; }
+}
+
 require_file_system_bytes_free_()
 {
   local req=$1
diff --git a/tests/env/env.sh b/tests/env/env.sh
index a6eb6260b..65b2e7d6f 100755
--- a/tests/env/env.sh
+++ b/tests/env/env.sh
@@ -21,7 +21,7 @@
 print_ver_ env pwd nice
 
 # A simple shebang program to call "echo" from symlinks like "./-u" or "./--".
-echo '#!'"$(command -v env | sed 's|/env|/echo|')"' foo' > show_args \
+echo '#!'"$(cmd_exe echo)"' foo' > show_args \
   || framework_failure_
 chmod a+x show_args || framework_failure_
 
diff --git a/tests/env/env0-from.sh b/tests/env/env0-from.sh
index 66d46de4c..0dcb488fa 100755
--- a/tests/env/env0-from.sh
+++ b/tests/env/env0-from.sh
@@ -20,7 +20,7 @@
 print_ver_ env
 
 # reference the env(1) to test before $PATH is adjusted
-envexe=$(command -v "env$EXEEXT") || framework_failure_
+envexe=$(cmd_exe 'env') || framework_failure_
 
 CU_ENV0_FROM_PARENT=parent
 CU_ENV0_FROM_REPLACE=parent
diff --git a/tests/misc/coreutils.sh b/tests/misc/coreutils.sh
index 36829360e..d6745cd15 100755
--- a/tests/misc/coreutils.sh
+++ b/tests/misc/coreutils.sh
@@ -20,7 +20,7 @@
 . "${srcdir=.}/tests/init.sh"; path_prepend_ ./src
 print_ver_ coreutils
 
-cp -s "$(command -v coreutils)" blah || skip_ 'multicall binary is disabled'
+cp -s "$(cmd_exe coreutils)" blah || skip_ 'multicall binary is disabled'
 
 # Yes outputs all its params so is good to verify argv manipulations
 echo 'y' > exp &&
diff --git a/tests/nproc/nproc-quota.sh b/tests/nproc/nproc-quota.sh
index c950ecae9..f591365d0 100755
--- a/tests/nproc/nproc-quota.sh
+++ b/tests/nproc/nproc-quota.sh
@@ -44,7 +44,7 @@ ROOT=cgroup
   echo 'max 100000' > $ROOT/sys/fs/cgroup/foo/cpu.max  # ignored
 } || framework_failure_
 
-nproc=$(command -v nproc)
+nproc=$(cmd_exe 'nproc')
 cp --parents $(ldd "$nproc" | grep -o '/[^ ]*') $ROOT ||
   skip_ 'Failed to copy nproc libs to chroot'
 cp "$nproc" $ROOT || framework_failure_
-- 
2.55.0


Reply via email to