mirror of
https://github.com/prowler-cloud/prowler.git
synced 2026-04-09 19:28:23 +00:00
Compare commits
12 Commits
improve-co
...
chore/remo
| Author | SHA1 | Date | |
|---|---|---|---|
|
|
6a34ba051c | ||
|
|
e7b915d6d3 | ||
|
|
167bcca67d | ||
|
|
0debfba4e8 | ||
|
|
e5fd366ea9 | ||
|
|
d218e87209 | ||
|
|
58d2ba81c4 | ||
|
|
f55839797c | ||
|
|
a40b6dd51b | ||
|
|
a6cba5af58 | ||
|
|
c71abf0c59 | ||
|
|
dccfcf2848 |
33
.github/workflows/check-test-init-files.yml
vendored
Normal file
33
.github/workflows/check-test-init-files.yml
vendored
Normal file
@@ -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 .
|
||||
62
scripts/check_test_init_files.py
Normal file
62
scripts/check_test_init_files.py
Normal file
@@ -0,0 +1,62 @@
|
||||
#!/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
|
||||
|
||||
EXCLUDED_TEST_INIT_ROOTS = {
|
||||
Path("tests/lib/check/fixtures/checks_folder"),
|
||||
}
|
||||
|
||||
|
||||
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 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."""
|
||||
return sorted(
|
||||
path
|
||||
for path in root.rglob("__init__.py")
|
||||
if is_test_init_file(path) and not is_excluded_test_init_file(path, root)
|
||||
)
|
||||
|
||||
|
||||
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())
|
||||
68
tests/lib/check_test_init_files_test.py
Normal file
68
tests/lib/check_test_init_files_test.py
Normal file
@@ -0,0 +1,68 @@
|
||||
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_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 / "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("api/tests/performance/__init__.py"),
|
||||
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) == []
|
||||
@@ -1 +0,0 @@
|
||||
# OCI Provider Tests
|
||||
Some files were not shown because too many files have changed in this diff Show More
Reference in New Issue
Block a user