From 4174f6ed5c8250b9a2aace23541a9e01bedc9760 Mon Sep 17 00:00:00 2001 From: Yueh-Shun Li Date: Sun, 2 Mar 2025 18:19:18 +0800 Subject: [PATCH] python3Packages.pytestCheckHook: support enabledTestPaths --- .../interpreters/python/hooks/default.nix | 49 +++++++++++++++++++ .../python/hooks/pytest-check-hook.sh | 28 ++++++++++- .../python/mk-python-derivation.nix | 14 ++++++ 3 files changed, 90 insertions(+), 1 deletion(-) diff --git a/pkgs/development/interpreters/python/hooks/default.nix b/pkgs/development/interpreters/python/hooks/default.nix index 8d6c26e7b7f7..f0207b570b94 100644 --- a/pkgs/development/interpreters/python/hooks/default.nix +++ b/pkgs/development/interpreters/python/hooks/default.nix @@ -204,6 +204,55 @@ in ] ++ previousPythonAttrs.disabledTestPaths or [ ]; }) ); + enabledTestPaths = objprint.overridePythonAttrs (previousPythonAttrs: { + pname = "test-pytestCheckHook-enabledTestPaths-${previousPythonAttrs.pname}"; + enabledTestPaths = [ + "tests/test_basic.py" + ] ++ previousPythonAttrs.enabledTestPaths or [ ]; + }); + enabledTestPaths-nonexistent = testers.testBuildFailure ( + objprint.overridePythonAttrs (previousPythonAttrs: { + pname = "test-pytestCheckHook-enabledTestPaths-nonexistent-${previousPythonAttrs.pname}"; + enabledTestPaths = [ + "tests/test_foo.py" + ] ++ previousPythonAttrs.enabledTestPaths or [ ]; + }) + ); + enabledTestPaths-dir = objprint.overridePythonAttrs (previousPythonAttrs: { + pname = "test-pytestCheckHook-enabledTestPaths-dir-${previousPythonAttrs.pname}"; + enabledTestPaths = [ + "tests" + ] ++ previousPythonAttrs.enabledTestPaths or [ ]; + }); + enabledTestPaths-dir-disabledTestPaths = objprint.overridePythonAttrs (previousPythonAttrs: { + pname = "test-pytestCheckHook-enabledTestPaths-dir-disabledTestPaths-${previousPythonAttrs.pname}"; + enabledTestPaths = [ + "tests" + ] ++ previousPythonAttrs.enabledTestPaths or [ ]; + disabledTestPaths = [ + "tests/test_basic.py" + ] ++ previousPythonAttrs.disabledTestPaths or [ ]; + }); + enabledTestPaths-glob = objprint.overridePythonAttrs (previousPythonAttrs: { + pname = "test-pytestCheckHook-enabledTestPaths-glob-${previousPythonAttrs.pname}"; + enabledTestPaths = [ + "tests/test_obj*.py" + ] ++ previousPythonAttrs.enabledTestPaths or [ ]; + }); + enabledTestPaths-glob-nonexistent = testers.testBuildFailure ( + objprint.overridePythonAttrs (previousPythonAttrs: { + pname = "test-pytestCheckHook-enabledTestPaths-glob-nonexistent-${previousPythonAttrs.pname}"; + enabledTestPaths = [ + "tests/test_foo*.py" + ] ++ previousPythonAttrs.enabledTestPaths or [ ]; + }) + ); + enabledTestPaths-item = objprint.overridePythonAttrs (previousPythonAttrs: { + pname = "test-pytestCheckHook-enabledTestPaths-item-${previousPythonAttrs.pname}"; + enabledTestPaths = [ + "tests/test_basic.py::TestBasic" + ] ++ previousPythonAttrs.enabledTestPaths or [ ]; + }); }; }; } ./pytest-check-hook.sh diff --git a/pkgs/development/interpreters/python/hooks/pytest-check-hook.sh b/pkgs/development/interpreters/python/hooks/pytest-check-hook.sh index 50757ec5b7ae..afe2c0656798 100644 --- a/pkgs/development/interpreters/python/hooks/pytest-check-hook.sh +++ b/pkgs/development/interpreters/python/hooks/pytest-check-hook.sh @@ -10,7 +10,33 @@ function pytestCheckPhase() { # Compose arguments local -a flagsArray=(-m pytest) - local -a _pathsArray=() + local -a _pathsArray + local path + + _pathsArray=() + concatTo _pathsArray enabledTestPaths + for path in "${_pathsArray[@]}"; do + if [[ "$path" =~ "::" ]]; then + flagsArray+=("$path") + else + # The `|| kill "$$"` trick propagates the errors from the process substitutiton subshell, + # which is suggested by a StackOverflow answer: https://unix.stackexchange.com/a/217643 + readarray -t -O"${#flagsArray[@]}" flagsArray < <(@pythonCheckInterpreter@ - "$path" <