herdr/scripts/test_vendor_libghostty_vt.py

78 lines
3.1 KiB
Python

from __future__ import annotations
import io
import tarfile
import tempfile
import unittest
from pathlib import Path
from unittest import mock
from scripts.vendor_libghostty_vt import ensure_dist_archive, parse_archive_root
class VendorLibghosttyVtTests(unittest.TestCase):
def test_parse_archive_root_returns_single_top_level_directory(self) -> None:
with tempfile.TemporaryDirectory() as temp_dir:
archive = Path(temp_dir) / "libghostty-vt.tar.gz"
with tarfile.open(archive, "w:gz") as tar:
data = b"hello"
info = tarfile.TarInfo("libghostty-vt-1.0.0/README.md")
info.size = len(data)
tar.addfile(info, io.BytesIO(data))
self.assertEqual(parse_archive_root(archive), "libghostty-vt-1.0.0")
def test_ensure_dist_archive_refuses_stale_archives_without_head_match(self) -> None:
with tempfile.TemporaryDirectory() as temp_dir:
repo = Path(temp_dir)
dist = repo / "zig-out" / "dist"
dist.mkdir(parents=True)
(dist / "libghostty-vt-1.3.2-main-+deadbeef0.tar.gz").write_bytes(b"stale")
with (
mock.patch("scripts.vendor_libghostty_vt.subprocess.run"),
mock.patch(
"scripts.vendor_libghostty_vt.subprocess.check_output",
return_value="0123456789abcdef\n",
),
):
with self.assertRaisesRegex(FileNotFoundError, "HEAD 012345678"):
ensure_dist_archive(repo)
def test_vendored_tree_contains_required_upstream_files(self) -> None:
root = Path(__file__).resolve().parent.parent / "vendor" / "libghostty-vt"
required = [
root / "build.zig",
root / "build.zig.zon",
root / "CMakeLists.txt",
root / "dist" / "cmake" / "ghostty-vt-config.cmake.in",
root / "include" / "ghostty" / "vt.h",
root / "include" / "ghostty" / "vt" / "render.h",
root / "src" / "lib_vt.zig",
]
missing = [str(path.relative_to(root)) for path in required if not path.exists()]
self.assertEqual(missing, [])
def test_vendor_metadata_exists_and_points_at_vendored_tree(self) -> None:
project_root = Path(__file__).resolve().parent.parent
metadata = project_root / "vendor" / "libghostty-vt.vendor.json"
self.assertTrue(metadata.exists())
text = metadata.read_text()
self.assertIn('"source_commit"', text)
self.assertIn('"dist_archive"', text)
self.assertIn('"extracted_dir"', text)
def test_embedded_libghostty_logging_is_silenced(self) -> None:
root = Path(__file__).resolve().parent.parent / "vendor" / "libghostty-vt"
lib_vt = root / "src" / "lib_vt.zig"
sys_zig = root / "src" / "terminal" / "c" / "sys.zig"
lib_text = lib_vt.read_text()
sys_text = sys_zig.read_text()
self.assertIn('.logFn = @import("terminal/c/sys.zig").logFn', lib_text)
self.assertIn("if (global.log == null) return;", sys_text)
if __name__ == "__main__":
unittest.main()