chore: remove __init__.py from test directories (#10582)

This commit is contained in:
Hugo Pereira Brito
2026-07-13 15:09:42 +01:00
committed by GitHub
parent 4242297e72
commit c6dae3a711
184 changed files with 216 additions and 1 deletions
@@ -0,0 +1,33 @@
name: 'Tools: Check Test Init Files'
on:
pull_request:
branches:
- 'master'
- 'v5.*'
concurrency:
group: ${{ github.workflow }}-${{ github.ref }}
cancel-in-progress: true
jobs:
check-test-init-files:
if: github.repository == 'prowler-cloud/prowler'
runs-on: ubuntu-latest
timeout-minutes: 10
permissions:
contents: read
steps:
- name: Harden the runner (Audit all outbound calls)
uses: step-security/harden-runner@fa2e9d605c4eeb9fcad4c99c224cee0c6c7f3594 # v2.16.0
with:
egress-policy: audit
- name: Checkout repository
uses: actions/checkout@de0fac2e4500dabe0009e67214ff5f5447ce83dd # v6.0.2
with:
persist-credentials: false
- name: Check for __init__.py files in test directories
run: python3 scripts/check_test_init_files.py .
+90
View File
@@ -0,0 +1,90 @@
#!/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 os import walk
from pathlib import Path
EXCLUDED_TEST_INIT_ROOTS = {
Path("tests/lib/check/fixtures/checks_folder"),
}
IGNORED_DIRECTORY_NAMES = {
".git",
".mypy_cache",
".nox",
".pytest_cache",
".tox",
".venv",
"__pycache__",
"node_modules",
"venv",
}
def is_test_init_file(path: Path) -> bool:
"""Return True when the file is a root tests __init__.py."""
return path.name == "__init__.py" and path.parts[0] == "tests"
def is_excluded_test_init_file(path: Path, root: Path) -> bool:
"""Return True when the file belongs to an allowed fixture directory."""
relative_path = path.relative_to(root)
return any(
relative_path.is_relative_to(excluded) for excluded in EXCLUDED_TEST_INIT_ROOTS
)
def find_test_init_files(root: Path) -> list[Path]:
"""Return sorted __init__.py files found under test directories."""
matches = []
for current_root, directories, filenames in walk(root):
directories[:] = [
directory
for directory in directories
if directory not in IGNORED_DIRECTORY_NAMES
]
if "__init__.py" in filenames:
path = Path(current_root) / "__init__.py"
else:
continue
relative_path = path.relative_to(root)
if is_test_init_file(relative_path) and not is_excluded_test_init_file(
path, root
):
matches.append(path)
return sorted(matches)
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())
View File
View File
View File
View File
View File
+93
View File
@@ -0,0 +1,93 @@
from importlib.util import module_from_spec, spec_from_file_location
from pathlib import Path
SCRIPT_PATH = (
Path(__file__).resolve().parents[2] / "scripts" / "check_test_init_files.py"
)
def load_guard_module():
spec = spec_from_file_location("check_test_init_files", SCRIPT_PATH)
assert spec is not None
assert spec.loader is not None
module = module_from_spec(spec)
spec.loader.exec_module(module)
return module
def test_find_test_init_files_detects_only_root_test_directories(tmp_path):
guard = load_guard_module()
(tmp_path / "tests" / "providers" / "aws").mkdir(parents=True)
(tmp_path / "tests" / "providers" / "aws" / "__init__.py").write_text("")
(tmp_path / "api" / "tests" / "performance").mkdir(parents=True)
(tmp_path / "api" / "tests" / "performance" / "__init__.py").write_text("")
(tmp_path / "mcp_server" / "tests").mkdir(parents=True)
(tmp_path / "mcp_server" / "tests" / "__init__.py").write_text("")
(tmp_path / "prowler" / "providers" / "aws").mkdir(parents=True)
(tmp_path / "prowler" / "providers" / "aws" / "__init__.py").write_text("")
(
tmp_path / "tests" / "lib" / "check" / "fixtures" / "checks_folder" / "check11"
).mkdir(parents=True)
(
tmp_path
/ "tests"
/ "lib"
/ "check"
/ "fixtures"
/ "checks_folder"
/ "check11"
/ "__init__.py"
).write_text("")
matches = guard.find_test_init_files(tmp_path)
assert [path.relative_to(tmp_path) for path in matches] == [
Path("tests/providers/aws/__init__.py"),
]
def test_find_test_init_files_ignores_virtualenv_test_packages(tmp_path):
guard = load_guard_module()
virtualenv_tests = (
tmp_path
/ ".venv"
/ "lib"
/ "python3.12"
/ "site-packages"
/ "package"
/ "tests"
)
virtualenv_tests.mkdir(parents=True)
(virtualenv_tests / "__init__.py").write_text("")
(tmp_path / "tests" / "providers" / "aws").mkdir(parents=True)
(tmp_path / "tests" / "providers" / "aws" / "__init__.py").write_text("")
matches = guard.find_test_init_files(tmp_path)
assert [path.relative_to(tmp_path) for path in matches] == [
Path("tests/providers/aws/__init__.py"),
]
def test_main_returns_error_when_test_init_files_exist(tmp_path, capsys):
guard = load_guard_module()
(tmp_path / "tests" / "config").mkdir(parents=True)
(tmp_path / "tests" / "config" / "__init__.py").write_text("")
assert guard.main([str(tmp_path)]) == 1
captured = capsys.readouterr()
assert "Remove __init__.py files from test directories" in captured.out
assert "tests/config/__init__.py" in captured.out
def test_repository_has_no_test_init_files():
guard = load_guard_module()
repo_root = Path(__file__).resolve().parents[2]
assert guard.find_test_init_files(repo_root) == []
View File
View File
View File

Some files were not shown because too many files have changed in this diff Show More