check_consistent.py: Allow having weird filenames in the test_cases directory (#9239)

This commit is contained in:
Alex Waygood
2022-11-21 17:40:11 +00:00
committed by GitHub
parent a948ef02d5
commit 4588dd6c74

View File

@@ -23,7 +23,9 @@ extension_descriptions = {".pyi": "stub", ".py": ".py"}
supported_stubtest_platforms = {"win32", "darwin", "linux"}
def assert_consistent_filetypes(directory: Path, *, kind: str, allowed: set[str]) -> None:
def assert_consistent_filetypes(
directory: Path, *, kind: str, allowed: set[str], allow_nonidentifier_filenames: bool = False
) -> None:
"""Check that given directory contains only valid Python files of a certain kind."""
allowed_paths = {Path(f) for f in allowed}
contents = list(directory.iterdir())
@@ -36,8 +38,9 @@ def assert_consistent_filetypes(directory: Path, *, kind: str, allowed: set[str]
# Note if a subdirectory is allowed, we will not check its contents
continue
if entry.is_file():
assert entry.stem.isidentifier(), f'Files must be valid modules, got: "{entry}"'
bad_filetype = f'Only {extension_descriptions[kind]!r} files allowed in the "{directory}" directory; got: {entry}'
if not allow_nonidentifier_filenames:
assert entry.stem.isidentifier(), f'Files must be valid modules, got: "{entry}"'
bad_filetype = f'Only {extension_descriptions[kind]!r} files allowed in the "{directory}" directory; got: {entry}'
assert entry.suffix == kind, bad_filetype
else:
assert entry.name.isidentifier(), f"Directories must be valid packages, got: {entry}"
@@ -67,7 +70,7 @@ def check_stubs() -> None:
def check_test_cases() -> None:
for package_name, testcase_dir in get_all_testcase_directories():
assert_consistent_filetypes(testcase_dir, kind=".py", allowed={"README.md"})
assert_consistent_filetypes(testcase_dir, kind=".py", allowed={"README.md"}, allow_nonidentifier_filenames=True)
bad_test_case_filename = 'Files in a `test_cases` directory must have names starting with "check_"; got "{}"'
for file in testcase_dir.rglob("*.py"):
assert file.stem.startswith("check_"), bad_test_case_filename.format(file)