Skip to content

Commit 68581c5

Browse files
committed
new regex
1 parent 8215312 commit 68581c5

File tree

1 file changed

+7
-14
lines changed

1 file changed

+7
-14
lines changed

pre_commit_hooks/tests_should_end_in_test.py

Lines changed: 7 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,8 @@
11
from __future__ import annotations
2-
32
import argparse
43
import os.path
54
import re
65
from collections.abc import Sequence
7-
8-
96
def main(argv: Sequence[str] | None = None) -> int:
107
parser = argparse.ArgumentParser()
118
parser.add_argument('filenames', nargs='*')
@@ -14,27 +11,23 @@ def main(argv: Sequence[str] | None = None) -> int:
1411
'--pytest',
1512
dest='pattern',
1613
action='store_const',
17-
const=r'^tests\/(?:[a-zA-Z0-9_]+\/)*tests[a-zA-Z0-9_]*\.py$',
18-
default=r'^tests\/(?:[a-zA-Z0-9_]+\/)*tests[a-zA-Z0-9_]*\.py$',
14+
const=r'^tests\/(?:[a-zA-Z0-9_]+\/)*tests_[a-zA-Z0-9_]*\.py$',
15+
default=r'^tests\/(?:[a-zA-Z0-9_]+\/)*tests_[a-zA-Z0-9_]*\.py$',
1916
help='(the default) ensure tests match %(const)s',
2017
)
2118
args = parser.parse_args(argv)
22-
2319
retcode = 0
2420
reg = re.compile(args.pattern)
2521
for filename in args.filenames:
2622
base = os.path.basename(filename)
2723
print(base)
28-
if (
29-
not reg.fullmatch(base) and
30-
not base == '.*/__init__.py' and
31-
not base == '.*/conftest.py' and
32-
not base == '.*/models.py'
33-
34-
):
24+
# Check for files that should be ignored
25+
if base in ('__init__.py', 'conftest.py', 'models.py'):
26+
continue
27+
# Raise an exception if filename doesn't start with 'tests_' and doesn't match the pattern
28+
if not reg.fullmatch(filename):
3529
retcode = 1
3630
print(f'{filename} does not match pattern "{args.pattern}"')
37-
3831
return retcode
3932

4033

0 commit comments

Comments
 (0)