From 7be16a3a8190e447d71034a3b0a0c05046f6ab7d Mon Sep 17 00:00:00 2001 From: ninjinkun Date: Tue, 7 Jun 2022 17:30:07 +0900 Subject: [PATCH 1/4] refactor: remove @memorizer for reducing redundancy. Instead of that, move the regex initializations to the global scope --- pytest_launchable/conftest.py | 15 ++++++--------- 1 file changed, 6 insertions(+), 9 deletions(-) diff --git a/pytest_launchable/conftest.py b/pytest_launchable/conftest.py index a740542..54d8081 100755 --- a/pytest_launchable/conftest.py +++ b/pytest_launchable/conftest.py @@ -4,7 +4,6 @@ from typing import List, Optional, Tuple, Union import pytest -from .memorizer import memorizer from launchable_cli_args import CLIArgs from lxml.builder import E # type: ignore from lxml import etree # type: ignore @@ -15,6 +14,10 @@ TestNameList = Tuple[Optional[str], str, Optional[str]] +testpath_re = re.compile( + "file=(?P([^#]+))(#class=(?P([^#]+)))?#testcase=(?P(.+))$") +pytest_test_file_re = re.compile(".*test_.*\.py$") + class LaunchableTestContext: def __init__(self): @@ -33,10 +36,7 @@ def get_node_from_path(self, path: str) -> "LaunchableTestNode": return node def find_testcase_from_testpath(self, testpath: str) -> "LaunchableTestCase": - @memorizer - def testpath_re(): - return re.compile("file=(?P([^#]+))(#class=(?P([^#]+)))?#testcase=(?P(.+))$") - m = testpath_re().match(testpath) + m = testpath_re.match(testpath) e = m.groupdict() # class is optional @@ -198,10 +198,7 @@ def collect_junit_element(self, array: List) -> None: def is_pytest_test_file(path: str) -> bool: """check the path is pytest test file or not""" - @memorizer - def pytest_test_file_re(): - return re.compile(".*test_.*\.py$") - return pytest_test_file_re().match(path) + return pytest_test_file_re.match(path) def read_test_path_list_file(filename: str) -> List[str]: From e956c1faba990d67d36edc6b4e13311cca195339 Mon Sep 17 00:00:00 2001 From: ninjinkun Date: Tue, 7 Jun 2022 17:38:44 +0900 Subject: [PATCH 2/4] refactor: remove memoizer file --- pytest_launchable/memorizer.py | 16 ---------------- tests/test_memorizer.py | 16 ---------------- 2 files changed, 32 deletions(-) delete mode 100644 pytest_launchable/memorizer.py delete mode 100644 tests/test_memorizer.py diff --git a/pytest_launchable/memorizer.py b/pytest_launchable/memorizer.py deleted file mode 100644 index 85fb3ba..0000000 --- a/pytest_launchable/memorizer.py +++ /dev/null @@ -1,16 +0,0 @@ -# cache the result of function call -# note that arguments are passed as is. - -# The new version python seems to have similar features, we will support the old python as well. - -def memorizer(f): - func_to_value = {} - - def w(*args): - if f in func_to_value: # cache hit - return func_to_value[f] - else: - v = f(*args) - func_to_value[f] = v - return v - return w diff --git a/tests/test_memorizer.py b/tests/test_memorizer.py deleted file mode 100644 index 4fc73df..0000000 --- a/tests/test_memorizer.py +++ /dev/null @@ -1,16 +0,0 @@ -from pytest_launchable.memorizer import memorizer - - -class Test_Memorizer: - @memorizer - def body(self): - self.call_count += 1 - return 100 - - def test_memorize(self): - self.call_count = 0 - a = self.body() - b = self.body() - assert a == 100 - assert b == 100 - assert self.call_count == 1, "must evaluate only once" From 26dc39e2bcc97535a4fbaf96c57cba425148e02e Mon Sep 17 00:00:00 2001 From: ninjinkun Date: Tue, 7 Jun 2022 18:24:42 +0900 Subject: [PATCH 3/4] fix: previously hidden type inconsistencies --- pytest_launchable/conftest.py | 12 ++++++++---- 1 file changed, 8 insertions(+), 4 deletions(-) diff --git a/pytest_launchable/conftest.py b/pytest_launchable/conftest.py index 54d8081..d169af8 100755 --- a/pytest_launchable/conftest.py +++ b/pytest_launchable/conftest.py @@ -35,12 +35,16 @@ def get_node_from_path(self, path: str) -> "LaunchableTestNode": self.test_node_list.append(node) return node - def find_testcase_from_testpath(self, testpath: str) -> "LaunchableTestCase": + def find_testcase_from_testpath(self, testpath: str) -> Optional["LaunchableTestCase"]: m = testpath_re.match(testpath) + if m is None: + return None + e = m.groupdict() + class_name = e.get("class") # class is optional - return self.get_node_from_path(e["file"]).find_test_case(e.get("class"), e["testcase"]) + return self.get_node_from_path(e["file"]).find_test_case(class_name, e["testcase"]) if class_name else None def set_subset_command_request(self, command, input_files: List[str]) -> None: self.subset_command = command @@ -98,7 +102,7 @@ def add_test_case(self, pytest_item: pytest.Function, test_name_tuple: TestNameL def short_str(self): return ",".join(map(lambda c: c.short_str(), self.case_list)) - def find_test_case(self, class_name: str, function_name_and_parameters: str): + def find_test_case(self, class_name: str, function_name_and_parameters: str) -> Optional["LaunchableTestCase"]: for testcase in self.case_list: if testcase.class_name == class_name and testcase.function_name_and_parameters == function_name_and_parameters: return testcase @@ -198,7 +202,7 @@ def collect_junit_element(self, array: List) -> None: def is_pytest_test_file(path: str) -> bool: """check the path is pytest test file or not""" - return pytest_test_file_re.match(path) + return pytest_test_file_re.match(path) is not None def read_test_path_list_file(filename: str) -> List[str]: From d87c72f6ca60774c3a0d10c3737ab2d84de0cb9e Mon Sep 17 00:00:00 2001 From: ninjinkun Date: Tue, 7 Jun 2022 20:02:38 +0900 Subject: [PATCH 4/4] fix: failure case --- pytest_launchable/conftest.py | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/pytest_launchable/conftest.py b/pytest_launchable/conftest.py index d169af8..0c2b86b 100755 --- a/pytest_launchable/conftest.py +++ b/pytest_launchable/conftest.py @@ -44,7 +44,7 @@ def find_testcase_from_testpath(self, testpath: str) -> Optional["LaunchableTest class_name = e.get("class") # class is optional - return self.get_node_from_path(e["file"]).find_test_case(class_name, e["testcase"]) if class_name else None + return self.get_node_from_path(e["file"]).find_test_case(class_name, e["testcase"]) def set_subset_command_request(self, command, input_files: List[str]) -> None: self.subset_command = command @@ -102,7 +102,7 @@ def add_test_case(self, pytest_item: pytest.Function, test_name_tuple: TestNameL def short_str(self): return ",".join(map(lambda c: c.short_str(), self.case_list)) - def find_test_case(self, class_name: str, function_name_and_parameters: str) -> Optional["LaunchableTestCase"]: + def find_test_case(self, class_name: Optional[str], function_name_and_parameters: str) -> Optional["LaunchableTestCase"]: for testcase in self.case_list: if testcase.class_name == class_name and testcase.function_name_and_parameters == function_name_and_parameters: return testcase