pre-commit-hooks/pre_commit_hooks/tests_should_end_in_test.py

54 lines
1.4 KiB
Python
Raw Normal View History

from __future__ import annotations
2015-01-05 07:03:56 +08:00
import argparse
import os.path
2015-03-12 08:44:59 +08:00
import re
from collections.abc import Sequence
2014-03-15 06:42:24 +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:
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__':
raise SystemExit(main())