FazBrowse GitHub Viewer
|
Trending
|
URL:
|
Home
Tools:
[Download Repo ZIP]
[View Raw Code]
[Original HTTPS Page]
pyrite/scripts/check_fix_commit_has_tests.py at dev · markramm/pyrite · GitHub
markramm
/
pyrite
Public
Notifications
You must be signed in to change notification settings
Fork
0
Star
3
Code
Issues
0
Pull requests
0
Discussions
Actions
Projects
Security and quality
0
Insights
Additional navigation options
Code
Issues
Pull requests
Discussions
Actions
Projects
Security and quality
Insights
Expand file tree
Breadcrumbs
pyrite
/
scripts
/
check_fix_commit_has_tests.py
Copy path
More file actions
More file actions
Latest commit
History
History
History
72 lines (55 loc) · 2.35 KB
Breadcrumbs
pyrite
/
scripts
/
check_fix_commit_has_tests.py
Copy path
File metadata and controls
72 lines (55 loc) · 2.35 KB
Raw
Copy raw file
Download raw file
Open symbols panel
Edit and raw actions
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
#!/usr/bin/env python3
"""Require `fix:`-prefixed commits to touch tests/ or extensions/*/tests/.
Mechanizes Iron Law 1 (no production code without a failing test first) at
the commit boundary -- a `fix:` commit with zero test lines is exactly the
class of regression the Iron Law exists to prevent (see 40e7a39, the fix
that shipped with zero test lines and this ticket's own motivation).
Used as a pre-commit `commit-msg` stage hook: receives the commit message
file path as argv[1], diffs the staged tree against HEAD, and exits
non-zero if the message starts with `fix:` but no staged file lives under
a tests/ directory.
"""
from
__future__
import
annotations
import
subprocess
import
sys
from
pathlib
import
Path
def
is_fix_commit
(
message
:
str
)
->
bool
:
"""True if the commit message's subject line starts with `fix:`."""
subject
=
message
.
strip
().
splitlines
()[
0
]
if
message
.
strip
()
else
""
return
subject
.
startswith
(
"fix:"
)
def
touches_tests
(
changed_paths
:
list
[
str
])
->
bool
:
"""True if any changed path lives under a tests/ directory (top-level
tests/ or extensions/*/tests/)."""
return
any
(
"tests"
in
Path
(
path
).
parts
for
path
in
changed_paths
)
def
get_staged_paths
()
->
list
[
str
]:
"""Paths staged for this commit, relative to the repo root."""
result
=
subprocess
.
run
(
[
"git"
,
"diff"
,
"--cached"
,
"--name-only"
],
capture_output
=
True
,
text
=
True
,
check
=
True
,
)
return
[
line
for
line
in
result
.
stdout
.
splitlines
()
if
line
]
def
main
()
->
int
:
if
len
(
sys
.
argv
)
<
2
:
print
(
"check_fix_commit_has_tests: missing commit-msg file argument"
,
file
=
sys
.
stderr
)
return
1
message
=
Path
(
sys
.
argv
[
1
]).
read_text
()
if
not
is_fix_commit
(
message
):
return
0
changed_paths
=
get_staged_paths
()
if
touches_tests
(
changed_paths
):
return
0
print
(
"ERROR: commit message starts with 'fix:' but no staged file is "
"under a tests/ directory.
\n
"
" Iron Law 1: no production code without a failing test first.
\n
"
" If this fix genuinely has no testable behavior change (e.g. a
\n
"
" pure docs/config fix), reword the subject to drop the 'fix:'
\n
"
" prefix, or add the regression test the fix should have shipped
\n
"
" with."
,
file
=
sys
.
stderr
,
)
return
1
if
__name__
==
"__main__"
:
sys
.
exit
(
main
())
Back
|
FazBrowse Home
|
New Git URL