Files
prowler/scripts/check_test_init_files.py
T
Hugo P.Brito 167bcca67d chore: remove __init__.py from test directories
- delete test package markers across the repository
- add a guard script and PR workflow to block regressions
- preserve custom check folder fixtures with neutral placeholder files
2026-04-06 13:46:00 +01:00

47 lines
1.2 KiB
Python

#!/usr/bin/env python3
"""Fail when __init__.py files are present inside test directories."""
from __future__ import annotations
import sys
from argparse import ArgumentParser
from pathlib import Path
def is_test_init_file(path: Path) -> bool:
"""Return True when the file is a test __init__.py."""
return path.name == "__init__.py" and "tests" in path.parts
def find_test_init_files(root: Path) -> list[Path]:
"""Return sorted __init__.py files found under test directories."""
return sorted(path for path in root.rglob("__init__.py") if is_test_init_file(path))
def main(argv: list[str] | None = None) -> int:
parser = ArgumentParser(description=__doc__)
parser.add_argument(
"root",
nargs="?",
default=".",
help="Repository root to scan. Defaults to the current directory.",
)
args = parser.parse_args(argv)
root = Path(args.root).resolve()
matches = find_test_init_files(root)
if not matches:
print("No __init__.py files found in test directories.")
return 0
print("Remove __init__.py files from test directories:")
for path in matches:
print(path.relative_to(root))
return 1
if __name__ == "__main__":
sys.exit(main())