2022-01-16 08:24:05 +08:00
|
|
|
from __future__ import annotations
|
|
|
|
|
2015-01-05 07:03:56 +08:00
|
|
|
import argparse
|
2019-02-01 11:19:10 +08:00
|
|
|
import os.path
|
2015-03-12 08:44:59 +08:00
|
|
|
import re
|
2024-10-12 07:30:07 +08:00
|
|
|
from collections.abc import Sequence
|
2014-03-15 06:42:24 +08:00
|
|
|
|
|
|
|
|
2022-01-16 08:24:05 +08:00
|
|
|
def main(argv: Sequence[str] | None = None) -> int:
|
2015-01-05 07:03:56 +08:00
|
|
|
parser = argparse.ArgumentParser()
|
|
|
|
parser.add_argument('filenames', nargs='*')
|
2022-06-08 00:10:42 +08:00
|
|
|
mutex = parser.add_mutually_exclusive_group()
|
|
|
|
mutex.add_argument(
|
|
|
|
'--pytest',
|
|
|
|
dest='pattern',
|
|
|
|
action='store_const',
|
|
|
|
const=r'.*_test\.py',
|
|
|
|
default=r'.*_test\.py',
|
|
|
|
help='(the default) ensure tests match %(const)s',
|
|
|
|
)
|
|
|
|
mutex.add_argument(
|
|
|
|
'--pytest-test-first',
|
|
|
|
dest='pattern',
|
|
|
|
action='store_const',
|
|
|
|
const=r'test_.*\.py',
|
|
|
|
help='ensure tests match %(const)s',
|
|
|
|
)
|
|
|
|
mutex.add_argument(
|
|
|
|
'--django', '--unittest',
|
|
|
|
dest='pattern',
|
|
|
|
action='store_const',
|
|
|
|
const=r'test.*\.py',
|
|
|
|
help='ensure tests match %(const)s',
|
2015-03-12 08:44:59 +08:00
|
|
|
)
|
2015-01-05 07:03:56 +08:00
|
|
|
args = parser.parse_args(argv)
|
|
|
|
|
2014-03-15 06:42:24 +08:00
|
|
|
retcode = 0
|
2022-06-08 00:10:42 +08:00
|
|
|
reg = re.compile(args.pattern)
|
2015-01-05 07:03:56 +08:00
|
|
|
for filename in args.filenames:
|
2019-02-01 11:19:10 +08:00
|
|
|
base = os.path.basename(filename)
|
2014-03-15 06:42:24 +08:00
|
|
|
if (
|
2022-06-08 00:10:42 +08:00
|
|
|
not reg.fullmatch(base) and
|
2015-11-19 16:18:38 +08:00
|
|
|
not base == '__init__.py' and
|
|
|
|
not base == 'conftest.py'
|
2014-03-15 06:42:24 +08:00
|
|
|
):
|
|
|
|
retcode = 1
|
2022-06-08 00:10:42 +08:00
|
|
|
print(f'{filename} does not match pattern "{args.pattern}"')
|
2014-03-15 06:42:24 +08:00
|
|
|
|
|
|
|
return retcode
|
|
|
|
|
|
|
|
|
|
|
|
if __name__ == '__main__':
|
2021-10-24 01:23:50 +08:00
|
|
|
raise SystemExit(main())
|