add --pytest-test-first convention
This commit is contained in:
parent
621f50e155
commit
412564fa95
|
@ -164,7 +164,7 @@
|
||||||
types: [text]
|
types: [text]
|
||||||
- id: name-tests-test
|
- id: name-tests-test
|
||||||
name: python tests naming
|
name: python tests naming
|
||||||
description: this verifies that test files are named correctly.
|
description: verifies that test files are named correctly.
|
||||||
entry: name-tests-test
|
entry: name-tests-test
|
||||||
language: python
|
language: python
|
||||||
files: (^|/)tests/.+\.py$
|
files: (^|/)tests/.+\.py$
|
||||||
|
|
|
@ -142,8 +142,10 @@ Replaces or checks mixed line ending.
|
||||||
- `no` - Checks if there is any mixed line ending without modifying any file.
|
- `no` - Checks if there is any mixed line ending without modifying any file.
|
||||||
|
|
||||||
#### `name-tests-test`
|
#### `name-tests-test`
|
||||||
Assert that files in tests/ end in `_test.py`.
|
verifies that test files are named correctly.
|
||||||
- Use `args: ['--django']` to match `test*.py` instead.
|
- `--pytest` (the default): ensure tests match `.*_test\.py`
|
||||||
|
- `--pytest-test-first`: ensure tests match `test_.*\.py`
|
||||||
|
- `--django` / `--unittest`: ensure tests match `test.*\.py`
|
||||||
|
|
||||||
#### `no-commit-to-branch`
|
#### `no-commit-to-branch`
|
||||||
Protect specific branches from direct checkins.
|
Protect specific branches from direct checkins.
|
||||||
|
|
|
@ -9,23 +9,42 @@ from typing 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='*')
|
||||||
parser.add_argument(
|
mutex = parser.add_mutually_exclusive_group()
|
||||||
'--django', default=False, action='store_true',
|
mutex.add_argument(
|
||||||
help='Use Django-style test naming pattern (test*.py)',
|
'--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',
|
||||||
)
|
)
|
||||||
args = parser.parse_args(argv)
|
args = parser.parse_args(argv)
|
||||||
|
|
||||||
retcode = 0
|
retcode = 0
|
||||||
test_name_pattern = r'test.*\.py' if args.django else r'.*_test\.py'
|
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)
|
||||||
if (
|
if (
|
||||||
not re.match(test_name_pattern, base) and
|
not reg.fullmatch(base) and
|
||||||
not base == '__init__.py' and
|
not base == '__init__.py' and
|
||||||
not base == 'conftest.py'
|
not base == 'conftest.py'
|
||||||
):
|
):
|
||||||
retcode = 1
|
retcode = 1
|
||||||
print(f'{filename} does not match pattern "{test_name_pattern}"')
|
print(f'{filename} does not match pattern "{args.pattern}"')
|
||||||
|
|
||||||
return retcode
|
return retcode
|
||||||
|
|
||||||
|
|
|
@ -43,3 +43,8 @@ def test_main_not_django_fails():
|
||||||
def test_main_django_fails():
|
def test_main_django_fails():
|
||||||
ret = main(['--django', 'foo_test.py', 'test_bar.py', 'test_baz.py'])
|
ret = main(['--django', 'foo_test.py', 'test_bar.py', 'test_baz.py'])
|
||||||
assert ret == 1
|
assert ret == 1
|
||||||
|
|
||||||
|
|
||||||
|
def test_main_pytest_test_first():
|
||||||
|
assert main(['--pytest-test-first', 'test_foo.py']) == 0
|
||||||
|
assert main(['--pytest-test-first', 'foo_test.py']) == 1
|
||||||
|
|
Loading…
Reference in New Issue