"""用 `uv lock --check` 证明锁文件与依赖声明一致,并模拟漂移。""" from __future__ import annotations import shutil import subprocess import tempfile from pathlib import Path INJECTED = 'dependencies = ["idna>=3"]' def uv_check(project: Path) -> tuple[int, str]: completed = subprocess.run( ["uv", "lock", "--check", "--offline"], cwd=project, capture_output=True, text=True, check=False, ) return completed.returncode, (completed.stderr or completed.stdout).strip() def make_drifted_copy(project: Path, target: Path) -> Path: for name in ("pyproject.toml", "uv.lock"): shutil.copy(project / name, target / name) pyproject = target / "pyproject.toml" text = pyproject.read_text(encoding="utf-8") marker = "dependencies = []" if marker not in text: raise RuntimeError("pyproject.toml has no empty dependencies list to mutate") pyproject.write_text(text.replace(marker, INJECTED, 1), encoding="utf-8") return target def main() -> None: project = Path(__file__).resolve().parents[3] clean_code, _ = uv_check(project) with tempfile.TemporaryDirectory() as tmp: drifted = make_drifted_copy(project, Path(tmp)) drift_code, drift_message = uv_check(drifted) print(f"clean_exit: {clean_code}") print(f"drift_exit: {drift_code}") print(f"drift_mentions_lock: {'needs to be updated' in drift_message}") print(f"drift_hint: {'uv lock' in drift_message}") if __name__ == "__main__": main()