nixpkgs/pkgs/development/interpreters/python/hooks/pytest-check-hook.sh
Yueh-Shun Li 2e512e7cf7 python3Packages.pytestCheckHook: fix disabledTestPaths glob matching assertion
Errors in Bash's process substitution doesn't get propagated outside.

Replace the process substitution with Python's `-` as /dev/stdin and
Bash's heredoc stdin redirection.
2025-03-07 07:14:56 +08:00

47 lines
1.4 KiB
Bash

# Setup hook for pytest
# shellcheck shell=bash
echo "Sourcing pytest-check-hook"
function pytestCheckPhase() {
echo "Executing pytestCheckPhase"
runHook preCheck
# Compose arguments
local -a flagsArray=(-m pytest)
if [ -n "${disabledTests[*]-}" ]; then
disabledTestsString="not $(concatStringsSep " and not " disabledTests)"
flagsArray+=(-k "$disabledTestsString")
fi
local -a _pathsArray=()
concatTo _pathsArray disabledTestPaths
for path in "${_pathsArray[@]}"; do
# Check if every path glob matches at least one path
@pythonCheckInterpreter@ - "$path" <<EOF
import glob
import sys
path_glob=sys.argv[1]
if not len(path_glob):
sys.exit('Got an empty disabled tests path glob. Aborting')
if next(glob.iglob(path_glob), None) is None:
sys.exit('Disabled tests path glob "{}" does not match any paths. Aborting'.format(path_glob))
EOF
flagsArray+=("--ignore-glob=$path")
done
# Compatibility layer to the obsolete pytestFlagsArray
eval "flagsArray+=(${pytestFlagsArray[*]-})"
concatTo flagsArray pytestFlags
echoCmd 'pytest flags' "${flagsArray[@]}"
@pythonCheckInterpreter@ "${flagsArray[@]}"
runHook postCheck
echo "Finished executing pytestCheckPhase"
}
if [ -z "${dontUsePytestCheck-}" ] && [ -z "${installCheckPhase-}" ]; then
echo "Using pytestCheckPhase"
appendToVar preDistPhases pytestCheckPhase
fi