Skip to content

Commit

Permalink
Change the pattern to match for testing code (#654)
Browse files Browse the repository at this point in the history
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 <eric.brown@securesauce.dev>
  • Loading branch information
ericwb authored Oct 23, 2024
1 parent 62fe24f commit 9f2bdc0
Showing 1 changed file with 5 additions and 4 deletions.
9 changes: 5 additions & 4 deletions precli/parsers/python.py
Original file line number Diff line number Diff line change
Expand Up @@ -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 = {}
Expand Down

0 comments on commit 9f2bdc0

Please sign in to comment.