new regex

This commit is contained in:
Uros 2024-10-17 16:43:32 +02:00
parent 8215312c10
commit 68581c54e5

View file

@ -1,11 +1,8 @@
from __future__ import annotations from __future__ import annotations
import argparse import argparse
import os.path import os.path
import re import re
from collections.abc import Sequence from collections.abc import Sequence
def main(argv: Sequence[str] | None = None) -> int: def main(argv: Sequence[str] | None = None) -> int:
parser = argparse.ArgumentParser() parser = argparse.ArgumentParser()
parser.add_argument('filenames', nargs='*') parser.add_argument('filenames', nargs='*')
@ -14,27 +11,23 @@ def main(argv: Sequence[str] | None = None) -> int:
'--pytest', '--pytest',
dest='pattern', dest='pattern',
action='store_const', action='store_const',
const=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$', default=r'^tests\/(?:[a-zA-Z0-9_]+\/)*tests_[a-zA-Z0-9_]*\.py$',
help='(the default) ensure tests match %(const)s', help='(the default) ensure tests match %(const)s',
) )
args = parser.parse_args(argv) args = parser.parse_args(argv)
retcode = 0 retcode = 0
reg = re.compile(args.pattern) reg = re.compile(args.pattern)
for filename in args.filenames: for filename in args.filenames:
base = os.path.basename(filename) base = os.path.basename(filename)
print(base) print(base)
if ( # Check for files that should be ignored
not reg.fullmatch(base) and if base in ('__init__.py', 'conftest.py', 'models.py'):
not base == '.*/__init__.py' and continue
not base == '.*/conftest.py' and # Raise an exception if filename doesn't start with 'tests_' and doesn't match the pattern
not base == '.*/models.py' if not reg.fullmatch(filename):
):
retcode = 1 retcode = 1
print(f'{filename} does not match pattern "{args.pattern}"') print(f'{filename} does not match pattern "{args.pattern}"')
return retcode return retcode