import ast import re import subprocess import tempfile import unittest from pathlib import Path ROOT = Path(__file__).resolve().parents[1] def _parse_toml_string_array(text: str, key: str) -> list[str]: match = re.search(rf"(?ms)^\s*{re.escape(key)}\s*=\s*\[(.*?)^\s*\]", text) if not match: raise AssertionError(f"{key} array not found") return ast.literal_eval("[" + match.group(1) + "]") class PackagingMetadataTests(unittest.TestCase): def test_py_modules_matches_top_level_src_modules(self): text = (ROOT / "pyproject.toml").read_text(encoding="utf-8") py_modules = sorted(_parse_toml_string_array(text, "py-modules")) discovered = sorted(path.stem for path in (ROOT / "src").glob("*.py")) self.assertEqual(py_modules, discovered) def test_project_dependencies_exclude_native_gui_bindings(self): text = (ROOT / "pyproject.toml").read_text(encoding="utf-8") dependencies = _parse_toml_string_array(text, "dependencies") self.assertNotIn("PyGObject", dependencies) self.assertNotIn("python-xlib", dependencies) def test_runtime_requirements_follow_project_dependency_contract(self): with tempfile.TemporaryDirectory() as td: output_path = Path(td) / "requirements.txt" script = ( f'source "{ROOT / "scripts" / "package_common.sh"}"\n' f'write_runtime_requirements "{output_path}"\n' ) subprocess.run( ["bash", "-lc", script], cwd=ROOT, text=True, capture_output=True, check=True, ) requirements = output_path.read_text(encoding="utf-8").splitlines() self.assertIn("faster-whisper", requirements) self.assertIn("llama-cpp-python", requirements) self.assertNotIn("PyGObject", requirements) self.assertNotIn("python-xlib", requirements) if __name__ == "__main__": unittest.main()