2024-01-02 19:11:34 +01:00
#!/usr/bin/env bash
2022-02-06 17:23:02 -06:00
# `run-gnu-test.bash [TEST]`
2022-02-03 13:42:13 -06:00
# run GNU test (or all tests if TEST is missing/null)
2022-02-06 17:23:02 -06:00
2025-12-17 02:49:26 +09:00
# spell-checker:ignore (env/vars) GNULIB SRCDIR SUBDIRS OSTYPE MAKEFLAGS; (utils) shellcheck greadlink
2022-02-03 13:42:13 -06:00
2022-02-22 08:00:37 -06:00
# ref: [How the GNU coreutils are tested](https://www.pixelbeat.org/docs/coreutils-testing.html) @@ <https://archive.is/p2ITW>
# * note: to run a single test => `make check TESTS=PATH/TO/TEST/SCRIPT SUBDIRS=. VERBOSE=yes`
2025-12-17 02:49:26 +09:00
# Use GNU make, readlink on *BSD
MAKE = $( command -v gmake|| command -v make)
READLINK = $( command -v greadlink|| command -v readlink) # Use our readlink to remove a dependency
2024-01-02 19:11:34 +01:00
ME_dir = " $( dirname -- " $( " ${ READLINK } " -fm -- " $0 " ) " ) "
2022-02-03 13:42:13 -06:00
REPO_main_dir = " $( dirname -- " ${ ME_dir } " ) "
2022-02-06 17:23:02 -06:00
echo "ME_dir=' ${ ME_dir } '"
echo "REPO_main_dir=' ${ REPO_main_dir } '"
2021-05-21 18:25:23 +02:00
set -e
2022-02-03 13:42:13 -06:00
2022-02-06 17:23:02 -06:00
### * config (from environment with fallback defaults); note: GNU and GNULIB are expected to be sibling repo directories
2022-02-03 13:42:13 -06:00
path_UUTILS = ${ path_UUTILS :- ${ REPO_main_dir }}
2024-01-02 19:11:34 +01:00
path_GNU = " $( " ${ READLINK } " -fm -- " ${ path_GNU :- ${ path_UUTILS } /../gnu } " ) "
2022-02-06 17:23:02 -06:00
echo "path_UUTILS=' ${ path_UUTILS } '"
echo "path_GNU=' ${ path_GNU } '"
2022-02-03 13:42:13 -06:00
2025-12-17 02:49:26 +09:00
# Use GNU nproc for *BSD
2025-12-20 18:41:57 +09:00
NPROC = $( command -v ${ path_GNU } /src/nproc|| command -v nproc)
MAKEFLAGS = " ${ MAKEFLAGS } -j ${ NPROC } "
2025-12-17 02:49:26 +09:00
export MAKEFLAGS
2022-02-03 13:42:13 -06:00
###
2022-02-06 17:23:02 -06:00
cd " ${ path_GNU } " && echo "[ pwd:' ${ PWD } ' ]"
2021-05-21 18:25:23 +02:00
2021-06-02 20:59:53 +02:00
export RUST_BACKTRACE = 1
2021-06-02 21:21:12 +02:00
2025-03-15 12:15:16 +01:00
# Determine if we have SELinux tests
has_selinux_tests = false
if test $# -ge 1; then
for t in " $@ " ; do
if [[ " $t " == *"selinux" * ]] ; then
has_selinux_tests = true
break
fi
done
fi
2025-12-07 16:03:25 -05:00
if [[ " $1 " == "run-tty" ]] ; then
# Handle TTY tests - dynamically find tests requiring TTY and run each individually
shift
TTY_TESTS = $( grep -r "require_controlling_input_terminal" tests --include= "*.sh" --include= "*.pl" -l 2>/dev/null)
echo "Running TTY tests individually:"
# If a test fails, it can break the implementation of the other tty tests. By running them separately this stops the different tests from being able to break each other
for test in $TTY_TESTS ; do
echo " Running: $test "
script -qec "timeout -sKILL 5m ' ${ MAKE } ' check TESTS=' $test ' SUBDIRS=. RUN_EXPENSIVE_TESTS=yes VERBOSE=no gl_public_submodule_commit='' srcdir=' ${ path_GNU } '" /dev/null || :
done
exit 0
elif [[ " $1 " == "run-root" && " $has_selinux_tests " == true ]] ; then
2025-03-15 12:15:16 +01:00
# Handle SELinux root tests separately
shift
if test -n " $CI " ; then
echo "Running SELinux tests as root"
# Don't use check-root here as the upstream root tests is hardcoded
2025-12-17 02:49:26 +09:00
sudo " ${ MAKE } " check TESTS = " $* " SUBDIRS = . RUN_EXPENSIVE_TESTS = yes RUN_VERY_EXPENSIVE_TESTS = yes VERBOSE = no gl_public_submodule_commit = "" srcdir = " ${ path_GNU } " TEST_SUITE_LOG = "tests/test-suite-root.log" || :
2025-03-15 12:15:16 +01:00
fi
exit 0
2025-12-07 16:03:25 -05:00
elif test " $1 " != "run-root" && test " $1 " != "run-tty" ; then
2023-09-26 09:33:14 +02:00
if test $# -ge 1; then
# if set, run only the tests passed
SPECIFIC_TESTS = ""
for t in " $@ " ; do
2023-09-24 22:35:27 +02:00
2023-09-26 09:33:14 +02:00
# Construct the full path
full_path = " $path_GNU / $t "
2023-09-24 22:35:27 +02:00
2023-09-26 09:33:14 +02:00
# Check if the file exists with .sh, .pl extension or without any extension in the $path_GNU directory
if [ -f " $full_path " ] || [ -f " $full_path .sh" ] || [ -f " $full_path .pl" ] ; then
SPECIFIC_TESTS = " $SPECIFIC_TESTS $t "
else
echo "Error: Test file $full_path , $full_path .sh, or $full_path .pl does not exist!"
exit 1
fi
done
# trim it
2024-01-02 19:11:34 +01:00
SPECIFIC_TESTS = $( echo " $SPECIFIC_TESTS " | xargs)
2023-09-26 09:33:14 +02:00
echo "Running specific tests: $SPECIFIC_TESTS "
fi
2021-06-02 21:21:12 +02:00
fi
2022-02-06 17:23:02 -06:00
# * timeout used to kill occasionally errant/"stuck" processes (note: 'release' testing takes ~1 hour; 'debug' testing takes ~2.5 hours)
# * `gl_public_submodule_commit=` disables testing for use of a "public" gnulib commit (which will fail when using shallow gnulib checkouts)
# * `srcdir=..` specifies the GNU source directory for tests (fixing failing/confused 'tests/factor/tNN.sh' tests and causing no harm to other tests)
2022-02-01 06:55:11 +00:00
#shellcheck disable=SC2086
2022-02-20 18:29:18 +08:00
2025-12-07 16:03:25 -05:00
if test " $1 " != "run-root" && test " $1 " != "run-tty" ; then
2023-07-17 10:53:44 +08:00
# run the regular tests
2022-09-17 11:08:12 +02:00
if test $# -ge 1; then
2025-12-17 02:49:26 +09:00
timeout -sKILL 4h " ${ MAKE } " check TESTS = " $SPECIFIC_TESTS " SUBDIRS = . RUN_EXPENSIVE_TESTS = yes RUN_VERY_EXPENSIVE_TESTS = yes VERBOSE = no gl_public_submodule_commit = "" srcdir = " ${ path_GNU } " || : # Kill after 4 hours in case something gets stuck in make
2022-09-17 11:08:12 +02:00
else
2025-12-17 02:49:26 +09:00
timeout -sKILL 4h " ${ MAKE } " check SUBDIRS = . RUN_EXPENSIVE_TESTS = yes RUN_VERY_EXPENSIVE_TESTS = yes VERBOSE = no gl_public_submodule_commit = "" srcdir = " ${ path_GNU } " || : # Kill after 4 hours in case something gets stuck in make
2022-09-17 11:08:12 +02:00
fi
else
2023-07-17 10:53:44 +08:00
# in case we would like to run tests requiring root
2022-09-17 11:08:12 +02:00
if test -z " $1 " -o " $1 " == "run-root" ; then
if test -n " $CI " ; then
2025-03-15 12:15:16 +01:00
if test $# -ge 2; then
echo "Running check-root to run only root tests"
2025-12-17 02:49:26 +09:00
sudo " ${ MAKE } " check-root TESTS = " $2 " SUBDIRS = . RUN_EXPENSIVE_TESTS = yes RUN_VERY_EXPENSIVE_TESTS = yes VERBOSE = no gl_public_submodule_commit = "" srcdir = " ${ path_GNU } " TEST_SUITE_LOG = "tests/test-suite-root.log" || :
2025-03-15 12:15:16 +01:00
else
echo "Running check-root to run only root tests"
2025-12-17 02:49:26 +09:00
sudo " ${ MAKE } " check-root SUBDIRS = . RUN_EXPENSIVE_TESTS = yes RUN_VERY_EXPENSIVE_TESTS = yes VERBOSE = no gl_public_submodule_commit = "" srcdir = " ${ path_GNU } " TEST_SUITE_LOG = "tests/test-suite-root.log" || :
2025-03-15 12:15:16 +01:00
fi
2022-09-17 11:08:12 +02:00
fi
fi
2023-04-13 16:47:05 +02:00
fi