From fd8b34935808c1c24d5f0ccca80902f17404554f Mon Sep 17 00:00:00 2001 From: Alessandro Martini <18130319+martini97@users.noreply.github.com> Date: Sun, 18 Jun 2023 22:57:26 -0300 Subject: [PATCH] fix: handle nested functions in pytest --- test/assets/test_python_pytest.py | 10 ++++++++++ test/testrun-pytest-test.el | 8 +++++++- testrun-pytest.el | 9 +++++++-- 3 files changed, 24 insertions(+), 3 deletions(-) diff --git a/test/assets/test_python_pytest.py b/test/assets/test_python_pytest.py index 0ff5950..23a98a5 100644 --- a/test/assets/test_python_pytest.py +++ b/test/assets/test_python_pytest.py @@ -13,3 +13,13 @@ def test_inside_namespace(self): for i in range(5): assert i < 5 assert True + + def test_nested_function(self): + def foo(): + return 5 + + assert foo() == 5 + + @pytest.mark.django_db + def test_decorated(self): + assert 1 == 1.0 diff --git a/test/testrun-pytest-test.el b/test/testrun-pytest-test.el index 6a362bd..37b16ac 100644 --- a/test/testrun-pytest-test.el +++ b/test/testrun-pytest-test.el @@ -48,7 +48,13 @@ If it's outside of a class it should return the path to the file." "test/assets/test_python_pytest.py::TestWithNamespace")) (goto-char 205) (should (equal (testrun-pytest-get-test "nearest") - "test/assets/test_python_pytest.py::TestWithNamespace::test_inside_namespace"))))) + "test/assets/test_python_pytest.py::TestWithNamespace::test_inside_namespace")) + (goto-char 305) + (should (equal (testrun-pytest-get-test "nearest") + "test/assets/test_python_pytest.py::TestWithNamespace::test_nested_function")) + (goto-char 407) + (should (equal (testrun-pytest-get-test "nearest") + "test/assets/test_python_pytest.py::TestWithNamespace::test_decorated"))))) (ert-deftest test-testrun-pytest-get-test-namespace () "Verify expected test paths with the namespace scope. diff --git a/testrun-pytest.el b/testrun-pytest.el index 5fd73ba..d5b15b1 100644 --- a/testrun-pytest.el +++ b/testrun-pytest.el @@ -30,6 +30,8 @@ ;;; Code: +(require 'rx) +(require 'seq) (require 'testrun-core) (require 'testrun-treesit) @@ -45,10 +47,13 @@ "function_definition") "List of node types relevant for the \"nearest\" scope.") +(defvar testrun-pytest-node-regex (rx bol (or "test" "Test") (1+ (or word "_")) eol)) + (defun testrun-pytest--get-nodes (node-types) "Return name of nodes with NODE-TYPES." - (mapcar #'testrun-treesit--get-node-name - (testrun-treesit--get-nodes-by-type node-types))) + (seq-filter (lambda (n) (string-match-p testrun-pytest-node-regex n)) + (mapcar #'testrun-treesit--get-node-name + (testrun-treesit--get-nodes-by-type node-types)))) ;;;###autoload (defun testrun-pytest-get-test (scope)