run_unit_tests.sh 1.5 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253
  1. #!/bin/bash
  2. # Builds the Catch2 `tests` target and runs it with no display, a private runtime dir and no core dumps.
  3. # usage: tools/run_unit_tests.sh [--build-dir <dir>] [--jobs <n>] [--no-build] [-- <Catch2 arguments>]
  4. # Exit status is the test binary's (or the build's), so a release prep can gate on it.
  5. set -u
  6. root=$(cd "$(dirname "$0")/.." && pwd)
  7. build="$root/build"
  8. jobs=$(( $(nproc) > 4 ? 4 : $(nproc) ))
  9. compile=1
  10. while [ $# -gt 0 ]; do
  11. case "$1" in
  12. --build-dir) build=$(realpath -m "$2"); shift 2 ;;
  13. --jobs) jobs=$2; shift 2 ;;
  14. --no-build) compile=0; shift ;;
  15. --) shift; break ;;
  16. *) echo "unknown argument: $1" >&2; exit 2 ;;
  17. esac
  18. done
  19. if [ "$compile" = 1 ]; then
  20. if ! grep -qs '^BUILD_TESTING:.*=\(1\|ON\)$' "$build/CMakeCache.txt"; then
  21. cmake -S "$root" -B "$build" -DBUILD_TESTING=1 || exit 1
  22. fi
  23. nice cmake --build "$build" --target tests -j "$jobs" || { echo "unit tests: BUILD FAILED"; exit 1; }
  24. fi
  25. binary="$build/output/tests"
  26. if [ ! -x "$binary" ]; then
  27. echo "unit tests: $binary does not exist" >&2
  28. exit 1
  29. fi
  30. runtime=$(mktemp -d)
  31. trap 'rm -rf "$runtime"' EXIT
  32. chmod 700 "$runtime"
  33. ulimit -c 0
  34. (cd "$build/output" && env -u DISPLAY -u WAYLAND_DISPLAY -u WAYLAND_SOCKET -u DBUS_SESSION_BUS_ADDRESS \
  35. XDG_RUNTIME_DIR="$runtime" timeout 1800 "$binary" "$@")
  36. status=$?
  37. if [ $status -eq 0 ]; then
  38. echo "unit tests: PASS"
  39. else
  40. echo "unit tests: FAIL (exit $status)"
  41. fi
  42. exit $status