|
| 1 | +"""Regression tests for project naming consistency in repository text files.""" |
| 2 | + |
| 3 | +from pathlib import Path |
| 4 | + |
| 5 | + |
| 6 | +FORBIDDEN_NAME_FRAGMENT = "code" + "-" + "ass" |
| 7 | +EXCLUDED_DIRECTORIES = { |
| 8 | + ".git", |
| 9 | + ".mypy_cache", |
| 10 | + ".pytest_cache", |
| 11 | + "__pycache__", |
| 12 | + "dist", |
| 13 | + "htmlcov", |
| 14 | +} |
| 15 | + |
| 16 | + |
| 17 | +def iter_repository_files(root: Path): |
| 18 | + """Yield repository files that should be checked for stale naming.""" |
| 19 | + for path in root.rglob("*"): |
| 20 | + if not path.is_file(): |
| 21 | + continue |
| 22 | + if EXCLUDED_DIRECTORIES.intersection(path.relative_to(root).parts): |
| 23 | + continue |
| 24 | + yield path |
| 25 | + |
| 26 | + |
| 27 | +def read_text_if_possible(path: Path): |
| 28 | + """Return file text when it is UTF-8 text, otherwise skip binary files.""" |
| 29 | + try: |
| 30 | + return path.read_text(encoding="utf-8") |
| 31 | + except UnicodeDecodeError: |
| 32 | + return None |
| 33 | + |
| 34 | + |
| 35 | +def find_forbidden_name_fragment(root: Path): |
| 36 | + """Return path:line matches for stale case-insensitive project name fragments.""" |
| 37 | + matches = [] |
| 38 | + for path in iter_repository_files(root): |
| 39 | + text = read_text_if_possible(path) |
| 40 | + if text is None: |
| 41 | + continue |
| 42 | + |
| 43 | + relative_path = path.relative_to(root) |
| 44 | + for line_number, line in enumerate(text.splitlines(), start=1): |
| 45 | + if FORBIDDEN_NAME_FRAGMENT in line.lower(): |
| 46 | + matches.append(f"{relative_path}:{line_number}: {line}") |
| 47 | + return matches |
| 48 | + |
| 49 | + |
| 50 | +def test_repository_text_files_do_not_use_stale_project_name_prefix(): |
| 51 | + """Prevent regressions to the old hyphenated project-name prefix.""" |
| 52 | + repository_root = Path(__file__).resolve().parents[1] |
| 53 | + |
| 54 | + matches = find_forbidden_name_fragment(repository_root) |
| 55 | + |
| 56 | + assert matches == [], "Found stale project-name references:\n" + "\n".join(matches) |
0 commit comments