|
| 1 | +#!/usr/bin/env python3 |
| 2 | +"""Regression tests for locale-pair freshness and survival.""" |
| 3 | +from __future__ import annotations |
| 4 | + |
| 5 | +import json |
| 6 | +import shutil |
| 7 | +import subprocess |
| 8 | +import sys |
| 9 | +import tempfile |
| 10 | +from pathlib import Path |
| 11 | + |
| 12 | + |
| 13 | +ROOT = Path(__file__).resolve().parents[1] |
| 14 | +CHECKER = ROOT / "tests" / "test_locale_freshness.py" |
| 15 | + |
| 16 | + |
| 17 | +def git(repository: Path, *args: str) -> str: |
| 18 | + result = subprocess.run( |
| 19 | + ["git", *args], |
| 20 | + cwd=repository, |
| 21 | + text=True, |
| 22 | + capture_output=True, |
| 23 | + check=False, |
| 24 | + ) |
| 25 | + if result.returncode != 0: |
| 26 | + raise AssertionError(f"git {' '.join(args)} failed:\n{result.stdout}\n{result.stderr}") |
| 27 | + return result.stdout.strip() |
| 28 | + |
| 29 | + |
| 30 | +def init_repository(repository: Path) -> str: |
| 31 | + (repository / "tests").mkdir() |
| 32 | + (repository / "locales" / "zh-CN").mkdir(parents=True) |
| 33 | + shutil.copy2(CHECKER, repository / "tests" / CHECKER.name) |
| 34 | + (repository / "tests" / "locale_review_acknowledgements.json").write_text( |
| 35 | + json.dumps({"schema_version": 1, "acknowledgements": []}) + "\n", |
| 36 | + encoding="utf-8", |
| 37 | + ) |
| 38 | + (repository / "README.md").write_text("English source\n", encoding="utf-8") |
| 39 | + (repository / "locales" / "zh-CN" / "README.md").write_text( |
| 40 | + "Chinese counterpart\n", encoding="utf-8" |
| 41 | + ) |
| 42 | + git(repository, "init", "--quiet") |
| 43 | + git(repository, "config", "user.name", "Fixture") |
| 44 | + git(repository, "config", "user.email", "fixture@example.invalid") |
| 45 | + git(repository, "add", ".") |
| 46 | + git(repository, "commit", "--quiet", "-m", "initial pair") |
| 47 | + return git(repository, "rev-parse", "HEAD") |
| 48 | + |
| 49 | + |
| 50 | +def commit(repository: Path, message: str) -> str: |
| 51 | + git(repository, "add", "-A") |
| 52 | + git(repository, "commit", "--quiet", "-m", message) |
| 53 | + return git(repository, "rev-parse", "HEAD") |
| 54 | + |
| 55 | + |
| 56 | +def run_checker(repository: Path, base: str, head: str) -> subprocess.CompletedProcess[str]: |
| 57 | + return subprocess.run( |
| 58 | + [sys.executable, "tests/test_locale_freshness.py", base, head], |
| 59 | + cwd=repository, |
| 60 | + text=True, |
| 61 | + capture_output=True, |
| 62 | + check=False, |
| 63 | + ) |
| 64 | + |
| 65 | + |
| 66 | +def expect_failure(result: subprocess.CompletedProcess[str], label: str) -> None: |
| 67 | + if result.returncode == 0: |
| 68 | + raise AssertionError(f"{label} was not rejected") |
| 69 | + |
| 70 | + |
| 71 | +def main() -> int: |
| 72 | + with tempfile.TemporaryDirectory() as temporary: |
| 73 | + repository = Path(temporary) |
| 74 | + base = init_repository(repository) |
| 75 | + (repository / "locales" / "zh-CN" / "README.md").unlink() |
| 76 | + head = commit(repository, "delete localized counterpart") |
| 77 | + expect_failure(run_checker(repository, base, head), "localized deletion") |
| 78 | + |
| 79 | + with tempfile.TemporaryDirectory() as temporary: |
| 80 | + repository = Path(temporary) |
| 81 | + base = init_repository(repository) |
| 82 | + git(repository, "mv", "README.md", "GUIDE.md") |
| 83 | + head = commit(repository, "rename canonical source only") |
| 84 | + expect_failure(run_checker(repository, base, head), "unpaired source rename") |
| 85 | + |
| 86 | + with tempfile.TemporaryDirectory() as temporary: |
| 87 | + repository = Path(temporary) |
| 88 | + base = init_repository(repository) |
| 89 | + git(repository, "mv", "README.md", "GUIDE.md") |
| 90 | + git(repository, "mv", "locales/zh-CN/README.md", "locales/zh-CN/GUIDE.md") |
| 91 | + head = commit(repository, "rename both sides of pair") |
| 92 | + result = run_checker(repository, base, head) |
| 93 | + if result.returncode != 0: |
| 94 | + raise AssertionError( |
| 95 | + f"paired rename was rejected:\n{result.stdout}\n{result.stderr}" |
| 96 | + ) |
| 97 | + |
| 98 | + print("locale freshness regression tests passed") |
| 99 | + return 0 |
| 100 | + |
| 101 | + |
| 102 | +if __name__ == "__main__": |
| 103 | + raise SystemExit(main()) |
0 commit comments