From f7aeb5a9b72806b9b765490990375de5dc20d8f4 Mon Sep 17 00:00:00 2001 From: Yueh-Shun Li Date: Sun, 2 Mar 2025 21:31:34 +0800 Subject: [PATCH] python3Packages.pytestCheckHook: support enabledTests --- .../interpreters/python/hooks/default.nix | 23 +++++++++++++++++++ .../python/hooks/pytest-check-hook.sh | 23 +++++++++++++++---- .../python/mk-python-derivation.nix | 8 ++++--- 3 files changed, 47 insertions(+), 7 deletions(-) diff --git a/pkgs/development/interpreters/python/hooks/default.nix b/pkgs/development/interpreters/python/hooks/default.nix index 1d5eb5c2c720..d80013fa212d 100644 --- a/pkgs/development/interpreters/python/hooks/default.nix +++ b/pkgs/development/interpreters/python/hooks/default.nix @@ -212,6 +212,29 @@ in ] ++ previousPythonAttrs.disabledTestPaths or [ ]; }) ); + enabledTests = objprint.overridePythonAttrs (previousPythonAttrs: { + pname = "test-pytestCheckHook-enabledTests-${previousPythonAttrs.pname}"; + enabledTests = [ + "TestBasic" + ] ++ previousPythonAttrs.disabledTests or [ ]; + }); + enabledTests-expression = objprint.overridePythonAttrs (previousPythonAttrs: { + __structuredAttrs = true; + pname = "test-pytestCheckHook-enabledTests-expression-${previousPythonAttrs.pname}"; + enabledTests = [ + "TestBasic and test_print" + "test_str" + ] ++ previousPythonAttrs.disabledTests or [ ]; + }); + enabledTests-disabledTests = objprint.overridePythonAttrs (previousPythonAttrs: { + pname = "test-pytestCheckHook-enabledTests-disabledTests-${previousPythonAttrs.pname}"; + enabledTests = [ + "TestBasic" + ] ++ previousPythonAttrs.disabledTests or [ ]; + disabledTests = [ + "test_print" + ] ++ previousPythonAttrs.disabledTests or [ ]; + }); enabledTestPaths = objprint.overridePythonAttrs (previousPythonAttrs: { pname = "test-pytestCheckHook-enabledTestPaths-${previousPythonAttrs.pname}"; enabledTestPaths = [ diff --git a/pkgs/development/interpreters/python/hooks/pytest-check-hook.sh b/pkgs/development/interpreters/python/hooks/pytest-check-hook.sh index 61082a771c0f..f728e9e9b26b 100644 --- a/pkgs/development/interpreters/python/hooks/pytest-check-hook.sh +++ b/pkgs/development/interpreters/python/hooks/pytest-check-hook.sh @@ -3,6 +3,23 @@ echo "Sourcing pytest-check-hook" +function _pytestIncludeExcludeExpr() { + local includeListName="$1" + local -n includeListRef="$includeListName" + local excludeListName="$2" + local -n excludeListRef="$excludeListName" + local includeString excludeString + if [[ -n "${includeListRef[*]-}" ]]; then + # ((element1) or (element2)) + includeString="(($(concatStringsSep ") or (" "$includeListName")))" + fi + if [[ -n "${excludeListRef[*]-}" ]]; then + # and not (element1) and not (element2) + excludeString="${includeString:+ and }not ($(concatStringsSep ") and not (" "$excludeListName"))" + fi + echo "$includeString$excludeString" +} + function pytestCheckPhase() { echo "Executing pytestCheckPhase" runHook preCheck @@ -56,10 +73,8 @@ EOF fi done - if [ -n "${disabledTests[*]-}" ]; then - # not (keyword1) and not (keyword2) - disabledTestsString="not ($(concatStringsSep ") and not (" disabledTests))" - flagsArray+=(-k "$disabledTestsString") + if [[ -n "${enabledTests[*]-}" ]] || [[ -n "${disabledTests[*]-}" ]]; then + flagsArray+=(-k "$(_pytestIncludeExcludeExpr enabledTests disabledTests)") fi # Compatibility layer to the obsolete pytestFlagsArray diff --git a/pkgs/development/interpreters/python/mk-python-derivation.nix b/pkgs/development/interpreters/python/mk-python-derivation.nix index b2f47b75e027..43bcaa59fc37 100644 --- a/pkgs/development/interpreters/python/mk-python-derivation.nix +++ b/pkgs/development/interpreters/python/mk-python-derivation.nix @@ -109,6 +109,7 @@ let "disabledTestPaths" "disabledTests" "enabledTestPaths" + "enabledTests" "pytestFlags" "pytestFlagsArray" "unittestFlags" @@ -451,13 +452,14 @@ let lib.mapAttrs ( name: value: - lib.throwIf (attrs.${name} == [ ]) - "${lib.getName finalAttrs}: ${name} must be unspecified, null or a non-empty list." - attrs.${name} + lib.throwIf ( + attrs.${name} == [ ] + ) "${lib.getName finalAttrs}: ${name} must be unspecified, null or a non-empty list." attrs.${name} ) ( getOptionalAttrs [ "enabledTestPaths" + "enabledTests" ] attrs ) )