From 467250ba5ad2fb616acc08dfb2674f93bb49b614 Mon Sep 17 00:00:00 2001 From: Eric Brown Date: Wed, 23 Oct 2024 14:21:12 -0700 Subject: [PATCH] Change the pattern to match for testing code The existing pattern to match whether the code analyzed was testing code was a little too frequent. At the very least it would always match the example code snippets in the tests of precli itself. And we can't have instructions that inform users to run precli against those examples if they always get ignored. So this change changes the matching algorithm to filter out testing code only if all conditions are met, instead of just one. So the python file needs to be a directory path with "tests", filename beginning with "tests_", and in a function name starting with "test_". In the future, these checks on whether it is test code or not need to be configurable in a precli configuration file of sorts. Signed-off-by: Eric Brown --- precli/parsers/python.py | 9 +++++---- 1 file changed, 5 insertions(+), 4 deletions(-) diff --git a/precli/parsers/python.py b/precli/parsers/python.py index 2f136c5c..ab63eae2 100644 --- a/precli/parsers/python.py +++ b/precli/parsers/python.py @@ -67,12 +67,13 @@ def is_test_code(self) -> bool: """ # TODO: move these constants into configuration options - if self.current_symtab.name().startswith("test_"): - return True - path = pathlib.Path(self.context["artifact"].file_name) - return "tests" in path.parts or path.name.startswith("test_") + return all( + "tests" in path.parts, + path.name.startswith("test_"), + self.current_symtab.name().startswith("test_"), + ) def visit_module(self, nodes: list[Node]): self.suppressions = {}