Skip site navigation (1)Skip section navigation (2)
Date:      Sat, 1 Apr 2023 19:44:54 GMT
From:      "Alexander V. Chernikov" <melifaro@FreeBSD.org>
To:        src-committers@FreeBSD.org, dev-commits-src-all@FreeBSD.org, dev-commits-src-main@FreeBSD.org
Subject:   git: d9af4219d668 - main - tests: refactor atf_python a bit
Message-ID:  <202304011944.331Jis1P029380@gitrepo.freebsd.org>

next in thread | raw e-mail | index | archive | help
The branch main has been updated by melifaro:

URL: https://cgit.FreeBSD.org/src/commit/?id=d9af4219d668f4f16574f757d6fc4c7971a4ef2c

commit d9af4219d668f4f16574f757d6fc4c7971a4ef2c
Author:     Alexander V. Chernikov <melifaro@FreeBSD.org>
AuthorDate: 2023-04-01 19:40:54 +0000
Commit:     Alexander V. Chernikov <melifaro@FreeBSD.org>
CommitDate: 2023-04-01 19:44:37 +0000

    tests: refactor atf_python a bit
    
    * Move more logic from conftest.py to the actual atf_pytest handler
    * Move nodeid_to_method_name() to the utils.py so it can be shared
    
    MFC after:      2 weeks
---
 tests/atf_python/atf_pytest.py | 21 +++++++++++++++------
 tests/atf_python/utils.py      |  5 +++++
 tests/conftest.py              | 10 ++--------
 3 files changed, 22 insertions(+), 14 deletions(-)

diff --git a/tests/atf_python/atf_pytest.py b/tests/atf_python/atf_pytest.py
index db7244d3234b..0dd3a225b73d 100644
--- a/tests/atf_python/atf_pytest.py
+++ b/tests/atf_python/atf_pytest.py
@@ -6,15 +6,12 @@ from typing import NamedTuple
 from typing import Optional
 from typing import Tuple
 
+from atf_python.utils import nodeid_to_method_name
+
 import pytest
 import os
 
 
-def nodeid_to_method_name(nodeid: str) -> str:
-    """file_name.py::ClassName::method_name[parametrize] -> method_name"""
-    return nodeid.split("::")[-1].split("[")[0]
-
-
 class ATFCleanupItem(pytest.Item):
     def runtest(self):
         """Runs cleanup procedure for the test instead of the test itself"""
@@ -73,7 +70,6 @@ class ATFTestObj(object):
         else:
             ret["require.user"] = username
 
-
     def _convert_marks(self, obj) -> Dict[str, Any]:
         wj_func = lambda x: " ".join(x)  # noqa: E731
         _map: Dict[str, Dict] = {
@@ -158,6 +154,19 @@ class ATFHandler(object):
                 return True
         return False
 
+    def _generate_test_cleanups(self, items):
+        new_items = []
+        for obj in items:
+            if self.has_object_cleanup(obj):
+                self.override_runtest(obj)
+                new_items.append(obj)
+        items.clear()
+        items.extend(new_items)
+
+    def modify_tests(self, items, config):
+        if config.option.atf_cleanup:
+            self._generate_test_cleanups(items)
+
     def list_tests(self, tests: List[str]):
         print('Content-Type: application/X-atf-tp; version="1"')
         print()
diff --git a/tests/atf_python/utils.py b/tests/atf_python/utils.py
index c8146b943ce9..591a532ca476 100644
--- a/tests/atf_python/utils.py
+++ b/tests/atf_python/utils.py
@@ -11,6 +11,11 @@ from typing import Optional
 import pytest
 
 
+def nodeid_to_method_name(nodeid: str) -> str:
+    """file_name.py::ClassName::method_name[parametrize] -> method_name"""
+    return nodeid.split("::")[-1].split("[")[0]
+
+
 class LibCWrapper(object):
     def __init__(self):
         path: Optional[str] = find_library("c")
diff --git a/tests/conftest.py b/tests/conftest.py
index 687f6bde375e..5d319863af73 100644
--- a/tests/conftest.py
+++ b/tests/conftest.py
@@ -101,15 +101,9 @@ def pytest_configure(config):
 
 def pytest_collection_modifyitems(session, config, items):
     """If cleanup is requested, replace collected tests with their cleanups (if any)"""
-    if PLUGIN_ENABLED and config.option.atf_cleanup:
-        new_items = []
+    if PLUGIN_ENABLED:
         handler = get_handler()
-        for obj in items:
-            if handler.has_object_cleanup(obj):
-                handler.override_runtest(obj)
-                new_items.append(obj)
-        items.clear()
-        items.extend(new_items)
+        handler.modify_tests(items, config)
 
 
 def pytest_collection_finish(session):



Want to link to this message? Use this URL: <https://mail-archive.FreeBSD.org/cgi/mid.cgi?202304011944.331Jis1P029380>