Skip to content

Commit

Permalink
Improve output for check-tests-unittest-testcase check (apache#37950)
Browse files Browse the repository at this point in the history
  • Loading branch information
Taragolis authored Mar 7, 2024
1 parent d37d18e commit 1189579
Showing 1 changed file with 11 additions and 4 deletions.
15 changes: 11 additions & 4 deletions scripts/ci/pre_commit/pre_commit_unittest_testcase.py
Original file line number Diff line number Diff line change
Expand Up @@ -27,20 +27,27 @@ def check_test_file(file: str) -> int:

found = 0
classes = [c for c in node.body if isinstance(c, ast.ClassDef)]
known_classes = {"TestCase"}
for c in classes:
# Some classes are returned as an ast.Attribute, some as an ast.Name object. Not quite sure why
if any(
(isinstance(base, ast.Attribute) and base.attr == "TestCase")
or (isinstance(base, ast.Name) and base.id == "TestCase")
(isinstance(base, ast.Attribute) and base.attr in known_classes)
or (isinstance(base, ast.Name) and base.id in known_classes)
for base in c.bases
):
found += 1
print(f"The class {c.name} inherits from TestCase, please use pytest instead")
prefix = f"{file}:{c.lineno}:"
print(f"{prefix} The class {c.name!r} inherits from TestCase, please use pytest instead")
known_classes.add(c.name) # Also use to found inherited classes in the same module
return found


def main(*args: str) -> int:
return sum(check_test_file(file) for file in args[1:])
errors = sum(check_test_file(file) for file in args[1:])
if not errors:
return 0
print(f"Found {errors} error{'s' if errors > 1 else ''}.")
return 1


if __name__ == "__main__":
Expand Down

0 comments on commit 1189579

Please sign in to comment.