Anthony Sottile | 8f61529 | 2022-01-15 19:24:05 -0500 | [diff] [blame] | 1 | from __future__ import annotations |
| 2 | |
Anthony Sottile | 63f01e9 | 2015-01-04 15:03:56 -0800 | [diff] [blame] | 3 | import argparse |
Anthony Sottile | 030bfac | 2019-01-31 19:19:10 -0800 | [diff] [blame] | 4 | import os.path |
Guy Kisel | db4b8f0 | 2015-03-11 17:44:59 -0700 | [diff] [blame] | 5 | import re |
Anthony Sottile | 030bfac | 2019-01-31 19:19:10 -0800 | [diff] [blame] | 6 | from typing import Sequence |
Anthony Sottile | ab35cd3 | 2014-03-14 15:42:24 -0700 | [diff] [blame] | 7 | |
| 8 | |
Anthony Sottile | 8f61529 | 2022-01-15 19:24:05 -0500 | [diff] [blame] | 9 | def main(argv: Sequence[str] | None = None) -> int: |
Anthony Sottile | 63f01e9 | 2015-01-04 15:03:56 -0800 | [diff] [blame] | 10 | parser = argparse.ArgumentParser() |
| 11 | parser.add_argument('filenames', nargs='*') |
Anthony Sottile | 412564f | 2022-06-07 09:10:42 -0700 | [diff] [blame] | 12 | 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 Kisel | db4b8f0 | 2015-03-11 17:44:59 -0700 | [diff] [blame] | 34 | ) |
Anthony Sottile | 63f01e9 | 2015-01-04 15:03:56 -0800 | [diff] [blame] | 35 | args = parser.parse_args(argv) |
| 36 | |
Anthony Sottile | ab35cd3 | 2014-03-14 15:42:24 -0700 | [diff] [blame] | 37 | retcode = 0 |
Anthony Sottile | 412564f | 2022-06-07 09:10:42 -0700 | [diff] [blame] | 38 | reg = re.compile(args.pattern) |
Anthony Sottile | 63f01e9 | 2015-01-04 15:03:56 -0800 | [diff] [blame] | 39 | for filename in args.filenames: |
Anthony Sottile | 030bfac | 2019-01-31 19:19:10 -0800 | [diff] [blame] | 40 | base = os.path.basename(filename) |
Anthony Sottile | ab35cd3 | 2014-03-14 15:42:24 -0700 | [diff] [blame] | 41 | if ( |
Anthony Sottile | 412564f | 2022-06-07 09:10:42 -0700 | [diff] [blame] | 42 | not reg.fullmatch(base) and |
phoxelua | 58edfc8 | 2015-11-19 00:18:38 -0800 | [diff] [blame] | 43 | not base == '__init__.py' and |
| 44 | not base == 'conftest.py' |
Anthony Sottile | ab35cd3 | 2014-03-14 15:42:24 -0700 | [diff] [blame] | 45 | ): |
| 46 | retcode = 1 |
Anthony Sottile | 412564f | 2022-06-07 09:10:42 -0700 | [diff] [blame] | 47 | print(f'{filename} does not match pattern "{args.pattern}"') |
Anthony Sottile | ab35cd3 | 2014-03-14 15:42:24 -0700 | [diff] [blame] | 48 | |
| 49 | return retcode |
| 50 | |
| 51 | |
Anthony Sottile | ab35cd3 | 2014-03-14 15:42:24 -0700 | [diff] [blame] | 52 | if __name__ == '__main__': |
Anthony Sottile | 39ab2ed | 2021-10-23 13:23:50 -0400 | [diff] [blame] | 53 | raise SystemExit(main()) |