blob: e7842af754e4d650676165bdb22a1649f05f08d7 [file] [log] [blame]
Anthony Sottile8f615292022-01-15 19:24:05 -05001from __future__ import annotations
2
Anthony Sottile63f01e92015-01-04 15:03:56 -08003import argparse
Anthony Sottile030bfac2019-01-31 19:19:10 -08004import os.path
Guy Kiseldb4b8f02015-03-11 17:44:59 -07005import re
Anthony Sottile030bfac2019-01-31 19:19:10 -08006from typing import Sequence
Anthony Sottileab35cd32014-03-14 15:42:24 -07007
8
Anthony Sottile8f615292022-01-15 19:24:05 -05009def main(argv: Sequence[str] | None = None) -> int:
Anthony Sottile63f01e92015-01-04 15:03:56 -080010 parser = argparse.ArgumentParser()
11 parser.add_argument('filenames', nargs='*')
Anthony Sottile412564f2022-06-07 09:10:42 -070012 mutex = parser.add_mutually_exclusive_group()
13 mutex.add_argument(
14 '--pytest',
15 dest='pattern',
16 action='store_const',
17 const=r'.*_test\.py',
18 default=r'.*_test\.py',
19 help='(the default) ensure tests match %(const)s',
20 )
21 mutex.add_argument(
22 '--pytest-test-first',
23 dest='pattern',
24 action='store_const',
25 const=r'test_.*\.py',
26 help='ensure tests match %(const)s',
27 )
28 mutex.add_argument(
29 '--django', '--unittest',
30 dest='pattern',
31 action='store_const',
32 const=r'test.*\.py',
33 help='ensure tests match %(const)s',
Guy Kiseldb4b8f02015-03-11 17:44:59 -070034 )
Anthony Sottile63f01e92015-01-04 15:03:56 -080035 args = parser.parse_args(argv)
36
Anthony Sottileab35cd32014-03-14 15:42:24 -070037 retcode = 0
Anthony Sottile412564f2022-06-07 09:10:42 -070038 reg = re.compile(args.pattern)
Anthony Sottile63f01e92015-01-04 15:03:56 -080039 for filename in args.filenames:
Anthony Sottile030bfac2019-01-31 19:19:10 -080040 base = os.path.basename(filename)
Anthony Sottileab35cd32014-03-14 15:42:24 -070041 if (
Anthony Sottile412564f2022-06-07 09:10:42 -070042 not reg.fullmatch(base) and
phoxelua58edfc82015-11-19 00:18:38 -080043 not base == '__init__.py' and
44 not base == 'conftest.py'
Anthony Sottileab35cd32014-03-14 15:42:24 -070045 ):
46 retcode = 1
Anthony Sottile412564f2022-06-07 09:10:42 -070047 print(f'{filename} does not match pattern "{args.pattern}"')
Anthony Sottileab35cd32014-03-14 15:42:24 -070048
49 return retcode
50
51
Anthony Sottileab35cd32014-03-14 15:42:24 -070052if __name__ == '__main__':
Anthony Sottile39ab2ed2021-10-23 13:23:50 -040053 raise SystemExit(main())