Debian Codegraph @

util/run-gnu-test.sh

raw
1 #!/usr/bin/env bash
2 # `run-gnu-test.bash [TEST]`
3 # run GNU test (or all tests if TEST is missing/null)
4
5 # spell-checker:ignore (env/vars) GNULIB SRCDIR SUBDIRS OSTYPE MAKEFLAGS; (utils) shellcheck greadlink ginstall
6
7 # ref: [How the GNU coreutils are tested](https://www.pixelbeat.org/docs/coreutils-testing.html) @@ <https://archive.is/p2ITW>
8 # * note: to run a single test => `make check TESTS=PATH/TO/TEST/SCRIPT SUBDIRS=. VERBOSE=yes`
9
10 # Use GNU make, readlink on *BSD
11 MAKE=$(command -v gmake||command -v make)
12 READLINK=$(command -v greadlink||command -v readlink) # Use our readlink to remove a dependency
13
14 ME_dir="$(dirname -- "$("${READLINK}" -fm -- "$0")")"
15 REPO_main_dir="$(dirname -- "${ME_dir}")"
16
17 echo "ME_dir='${ME_dir}'"
18 echo "REPO_main_dir='${REPO_main_dir}'"
19
20 set -e
21
22 ### * config (from environment with fallback defaults); note: GNU and GNULIB are expected to be sibling repo directories
23
24 path_UUTILS=${path_UUTILS:-${REPO_main_dir}}
25 path_GNU="$("${READLINK}" -fm -- "${path_GNU:-${path_UUTILS}/../gnu}")"
26
27 echo "path_UUTILS='${path_UUTILS}'"
28 echo "path_GNU='${path_GNU}'"
29
30 # Use GNU nproc for *BSD
31 NPROC=$(command -v ${path_GNU}/src/nproc||command -v nproc)
32 MAKEFLAGS="${MAKEFLAGS} -j ${NPROC}"
33 export MAKEFLAGS
34 ###
35
36 cd "${path_GNU}" && echo "[ pwd:'${PWD}' ]"
37
38 # --- Self-heal: make the shared GNU harness point at THIS checkout ----------
39 # ${path_GNU} is shared between sibling uutils checkouts; whichever one last ran
40 # build-gnu.sh leaves its own target dir patched into the Makefile/local.mk
41 # PATH, so the harness can silently run a stale/other build. Re-point the PATH
42 # (and re-create the multicall hardlinks) here so that running this script is
43 # enough to test the current tree - no full rebuild just because another
44 # checkout stole the PATH.
45 if [ -n "${CARGO_TARGET_DIR}" ]; then
46 UU_TARGET_BASE="${CARGO_TARGET_DIR}"
47 else
48 UU_TARGET_BASE="${path_UUTILS}/target"
49 fi
50 # Locate the multicall binary. Honor PROFILE when set; otherwise probe the usual
51 # profiles (build-gnu.sh defaults to debug, CI uses release-small) and fall back
52 # to whatever 'target/*/coreutils' actually exists so the harness adapts to the
53 # build it is given.
54 UU_BUILD_DIR=""
55 for p in ${PROFILE:+"${PROFILE}"} debug release release-small; do
56 if [ -x "${UU_TARGET_BASE}/${p}/coreutils" ]; then
57 UU_BUILD_DIR="${UU_TARGET_BASE}/${p}"
58 break
59 fi
60 done
61 if [ -z "${UU_BUILD_DIR}" ]; then
62 for candidate in "${UU_TARGET_BASE}"/*/coreutils; do
63 if [ -x "${candidate}" ]; then UU_BUILD_DIR="$(dirname -- "${candidate}")"; break; fi
64 done
65 fi
66 if [ -z "${UU_BUILD_DIR}" ]; then
67 # No build to re-point at; leave whatever build-gnu.sh already patched in place.
68 echo "warning: no coreutils binary under '${UU_TARGET_BASE}'; skipping re-point." >&2
69 else
70 echo "Re-pointing GNU harness at '${UU_BUILD_DIR}'"
71 for binary in $("${UU_BUILD_DIR}/coreutils" --list); do
72 tgt="${UU_BUILD_DIR}/${binary}"
73 [ "${tgt}" -ef "${UU_BUILD_DIR}/coreutils" ] || ln -f "${UU_BUILD_DIR}/coreutils" "${tgt}"
74 done
75 # The GNU tests invoke `ginstall` rather than `install`.
76 [ -e "${UU_BUILD_DIR}/ginstall" ] || [ ! -e "${UU_BUILD_DIR}/install" ] || ln -f "${UU_BUILD_DIR}/install" "${UU_BUILD_DIR}/ginstall"
77 uu_path_sed="s/^[[:blank:]]*PATH=.*/ PATH='${UU_BUILD_DIR//\//\\/}\$(PATH_SEPARATOR)'\"\$\$PATH\" \\\/"
78 for f in Makefile tests/local.mk; do
79 if [ -f "${f}" ]; then sed -i "${uu_path_sed}" "${f}"; fi
80 done
81 # Keep Makefile.in newer than the local.mk we just edited (and Makefile newer
82 # than Makefile.in) so make won't re-run automake/config.status - which would
83 # both undo the patch and choke on GNU's factor-test continuation lines that
84 # build-gnu.sh strips (automake 1.18: "blank line following trailing backslash").
85 for f in Makefile.in Makefile; do
86 if [ -f "${f}" ]; then touch "${f}"; fi
87 done
88 fi
89 # ---------------------------------------------------------------------------
90
91 export RUST_BACKTRACE=1
92
93 # The GNU tests compare stderr byte for byte: never let a dev shell force a
94 # caret report into their pipes.
95 unset UUTILS_DIAG
96
97 # Determine if we have SELinux tests
98 has_selinux_tests=false
99 if test $# -ge 1; then
100 for t in "$@"; do
101 if [[ "$t" == *"selinux"* ]]; then
102 has_selinux_tests=true
103 break
104 fi
105 done
106 fi
107
108 if [[ "$1" == "run-tty" ]]; then
109 # Handle TTY tests - dynamically find tests requiring TTY and run each individually
110 shift
111 TTY_TESTS=$(grep -r "require_controlling_input_terminal" tests --include="*.sh" --include="*.pl" -l 2>/dev/null)
112 echo "Running TTY tests individually:"
113 # 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
114 for test in $TTY_TESTS; do
115 echo " Running: $test"
116 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 || :
117 done
118 exit 0
119 elif [[ "$1" == "run-root" && "$has_selinux_tests" == true ]]; then
120 # Handle SELinux root tests separately
121 shift
122 if test -n "$CI"; then
123 echo "Running SELinux tests as root"
124 # Don't use check-root here as the upstream root tests is hardcoded
125 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" || :
126 fi
127 exit 0
128 elif test "$1" != "run-root" && test "$1" != "run-tty"; then
129 if test $# -ge 1; then
130 # if set, run only the tests passed
131 SPECIFIC_TESTS=""
132 for t in "$@"; do
133
134 # Construct the full path
135 full_path="$path_GNU/$t"
136
137 # Check if the file exists with .sh, .pl extension or without any extension in the $path_GNU directory
138 if [ -f "$full_path" ] || [ -f "$full_path.sh" ] || [ -f "$full_path.pl" ]; then
139 SPECIFIC_TESTS="$SPECIFIC_TESTS $t"
140 else
141 echo "Error: Test file $full_path, $full_path.sh, or $full_path.pl does not exist!"
142 exit 1
143 fi
144 done
145 # trim it
146 SPECIFIC_TESTS=$(echo "$SPECIFIC_TESTS" | xargs)
147 echo "Running specific tests: $SPECIFIC_TESTS"
148 fi
149 fi
150
151 # * timeout used to kill occasionally errant/"stuck" processes (note: 'release' testing takes ~1 hour; 'debug' testing takes ~2.5 hours)
152 # * `gl_public_submodule_commit=` disables testing for use of a "public" gnulib commit (which will fail when using shallow gnulib checkouts)
153 # * `srcdir=..` specifies the GNU source directory for tests (fixing failing/confused 'tests/factor/tNN.sh' tests and causing no harm to other tests)
154 #shellcheck disable=SC2086
155
156 if test "$1" != "run-root" && test "$1" != "run-tty"; then
157 # run the regular tests
158 if test $# -ge 1; then
159 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
160 else
161 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
162 fi
163 else
164 # in case we would like to run tests requiring root
165 if test -z "$1" -o "$1" == "run-root"; then
166 if test -n "$CI"; then
167 if test $# -ge 2; then
168 echo "Running check-root to run only root tests"
169 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" || :
170 else
171 echo "Running check-root to run only root tests"
172 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" || :
173 fi
174 fi
175 fi
176 fi