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