Skip to content

Commit 5e4fe2c

Browse files
committed
Add new logic
1 parent 17cb164 commit 5e4fe2c

File tree

2 files changed

+104
-0
lines changed

2 files changed

+104
-0
lines changed
Lines changed: 48 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,48 @@
1+
#!/usr/bin/env python3
2+
3+
import subprocess
4+
import sys
5+
from pathlib import Path
6+
7+
FORBIDDEN = {"a", "an", "the"}
8+
9+
10+
def git_ls_python_files():
11+
result = subprocess.run(
12+
["git", "ls-files", "*.py"],
13+
capture_output=True,
14+
text=True,
15+
check=True,
16+
)
17+
return [Path(p) for p in result.stdout.splitlines()]
18+
19+
20+
def is_test_file(path: Path) -> bool:
21+
name = path.name
22+
return (
23+
name.startswith("test_")
24+
or name.startswith("tests_")
25+
or name.endswith("_test.py")
26+
)
27+
28+
29+
def has_forbidden_article(path: Path) -> bool:
30+
parts = path.stem.split("_")
31+
return any(part in FORBIDDEN for part in parts)
32+
33+
34+
def main() -> int:
35+
for path in git_ls_python_files():
36+
if not is_test_file(path):
37+
continue
38+
39+
if has_forbidden_article(path):
40+
print("ERROR: Forbidden article in test filename:")
41+
print(path)
42+
return 1
43+
44+
return 0
45+
46+
47+
if __name__ == "__main__":
48+
sys.exit(main())
Lines changed: 56 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,56 @@
1+
import subprocess
2+
import sys
3+
from pathlib import Path
4+
5+
6+
HOOK = Path(__file__).parents[1] / "pre_commit_hooks" / "forbid_articles_in_test_filenames.py"
7+
8+
9+
def run_hook(repo_path: Path):
10+
"""Run the hook in a temporary git repo and return (exit_code, stdout)."""
11+
result = subprocess.run(
12+
[sys.executable, str(HOOK)],
13+
cwd=repo_path,
14+
capture_output=True,
15+
text=True,
16+
)
17+
return result.returncode, result.stdout.strip()
18+
19+
20+
def init_git_repo(tmp_path: Path):
21+
subprocess.run(["git", "init"], cwd=tmp_path, check=True)
22+
subprocess.run(["git", "config", "user.email", "[email protected]"], cwd=tmp_path, check=True)
23+
subprocess.run(["git", "config", "user.name", "Test User"], cwd=tmp_path, check=True)
24+
25+
26+
def git_add_all(tmp_path: Path):
27+
subprocess.run(["git", "add", "."], cwd=tmp_path, check=True)
28+
29+
30+
def test_fails_on_forbidden_article_in_test_filename(tmp_path: Path):
31+
init_git_repo(tmp_path)
32+
33+
bad_test = tmp_path / "tests_create_an_address.py"
34+
bad_test.write_text("def test_something(): pass\n")
35+
36+
git_add_all(tmp_path)
37+
38+
code, output = run_hook(tmp_path)
39+
40+
assert code == 1
41+
assert "ERROR: Forbidden article in test filename:" in output
42+
assert "tests_create_an_address.py" in output
43+
44+
45+
def test_passes_on_valid_test_filename(tmp_path: Path):
46+
init_git_repo(tmp_path)
47+
48+
good_test = tmp_path / "tests_create_address.py"
49+
good_test.write_text("def test_something(): pass\n")
50+
51+
git_add_all(tmp_path)
52+
53+
code, output = run_hook(tmp_path)
54+
55+
assert code == 0
56+
assert output == ""

0 commit comments

Comments
 (0)