mirror of
https://github.com/pre-commit/pre-commit-hooks.git
synced 2026-03-30 02:16:52 +00:00
new regex
This commit is contained in:
parent
8215312c10
commit
68581c54e5
1 changed files with 7 additions and 14 deletions
|
|
@ -1,11 +1,8 @@
|
|||
from __future__ import annotations
|
||||
|
||||
import argparse
|
||||
import os.path
|
||||
import re
|
||||
from collections.abc import Sequence
|
||||
|
||||
|
||||
def main(argv: Sequence[str] | None = None) -> int:
|
||||
parser = argparse.ArgumentParser()
|
||||
parser.add_argument('filenames', nargs='*')
|
||||
|
|
@ -14,27 +11,23 @@ def main(argv: Sequence[str] | None = None) -> int:
|
|||
'--pytest',
|
||||
dest='pattern',
|
||||
action='store_const',
|
||||
const=r'^tests\/(?:[a-zA-Z0-9_]+\/)*tests[a-zA-Z0-9_]*\.py$',
|
||||
default=r'^tests\/(?:[a-zA-Z0-9_]+\/)*tests[a-zA-Z0-9_]*\.py$',
|
||||
const=r'^tests\/(?:[a-zA-Z0-9_]+\/)*tests_[a-zA-Z0-9_]*\.py$',
|
||||
default=r'^tests\/(?:[a-zA-Z0-9_]+\/)*tests_[a-zA-Z0-9_]*\.py$',
|
||||
help='(the default) ensure tests match %(const)s',
|
||||
)
|
||||
args = parser.parse_args(argv)
|
||||
|
||||
retcode = 0
|
||||
reg = re.compile(args.pattern)
|
||||
for filename in args.filenames:
|
||||
base = os.path.basename(filename)
|
||||
print(base)
|
||||
if (
|
||||
not reg.fullmatch(base) and
|
||||
not base == '.*/__init__.py' and
|
||||
not base == '.*/conftest.py' and
|
||||
not base == '.*/models.py'
|
||||
|
||||
):
|
||||
# Check for files that should be ignored
|
||||
if base in ('__init__.py', 'conftest.py', 'models.py'):
|
||||
continue
|
||||
# Raise an exception if filename doesn't start with 'tests_' and doesn't match the pattern
|
||||
if not reg.fullmatch(filename):
|
||||
retcode = 1
|
||||
print(f'{filename} does not match pattern "{args.pattern}"')
|
||||
|
||||
return retcode
|
||||
|
||||
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue