Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 5 additions & 0 deletions CHANGES
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,11 @@ returned by {meth}`~libvt.snapshot.Snapshot.to_dict`.

### Fixes

#### Include `examples/` in source distributions (#196)

`pyproject.toml` source distribution builds include `examples/` files
referenced by tests and documentation. Wheels continue shipping only `libvt`.

#### Reduce full-display erase overhead (#199)

Full-row display erases (`ED 0`, `ED 1`, `ED 2`, and `ED 22`) and `DECCOLM`
Expand Down
3 changes: 3 additions & 0 deletions pyproject.toml
Original file line number Diff line number Diff line change
Expand Up @@ -58,6 +58,7 @@ dev = [
"sphinx-autobuild",
# Testing
"gp-libs>=0.0.19",
"hatchling",
"pytest",
"pytest-asyncio",
"pytest-rerunfailures>=16.2",
Expand All @@ -84,6 +85,7 @@ docs = [
]
testing = [
"gp-libs>=0.0.19",
"hatchling",
"pytest",
"pytest-asyncio",
"pytest-rerunfailures>=16.2",
Expand Down Expand Up @@ -116,6 +118,7 @@ include = [
"src/libvt",
"tests",
"docs",
"examples",
"CHANGES",
"MIGRATION",
"README.md",
Expand Down
200 changes: 200 additions & 0 deletions tests/test_libvt.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,10 @@
from __future__ import annotations

import ast
import contextlib
import dataclasses
import doctest
import email.parser
import importlib
import importlib.metadata
import importlib.util
Expand All @@ -14,7 +16,9 @@
import re
import subprocess
import sys
import tarfile
import tomllib
import zipfile

import pytest

Expand Down Expand Up @@ -529,3 +533,199 @@ def test_every_public_module_has_api_doc_page() -> None:

for aio_page in ("index.md", "core.md", "stream.md", "pty.md"):
assert (api_root / "aio" / aio_page).is_file(), f"docs/api/aio/{aio_page}"


def _build_sdist(
project_dir: pathlib.Path,
out_dir: pathlib.Path,
) -> pathlib.Path:
"""Build one sdist for ``project_dir`` into ``out_dir`` via Hatchling."""
hatchling_build = pytest.importorskip("hatchling.build")
out_dir.mkdir(parents=True, exist_ok=True)
with contextlib.chdir(project_dir):
built_name = hatchling_build.build_sdist(str(out_dir))
sdist_files = list(out_dir.glob("*.tar.gz"))
assert len(sdist_files) == 1
assert sdist_files[0].name == built_name
return sdist_files[0]


def _build_wheel(
project_dir: pathlib.Path,
out_dir: pathlib.Path,
) -> pathlib.Path:
"""Build one wheel for ``project_dir`` into ``out_dir`` via Hatchling."""
hatchling_build = pytest.importorskip("hatchling.build")
out_dir.mkdir(parents=True, exist_ok=True)
with contextlib.chdir(project_dir):
built_name = hatchling_build.build_wheel(str(out_dir))
wheel_files = list(out_dir.glob("*.whl"))
assert len(wheel_files) == 1
assert wheel_files[0].name == built_name
return wheel_files[0]


def _build_distributions(
project_dir: pathlib.Path,
out_dir: pathlib.Path,
) -> tuple[pathlib.Path, pathlib.Path]:
"""Build an sdist and wheel for ``project_dir`` into ``out_dir`` via Hatchling."""
return (
_build_sdist(project_dir, out_dir),
_build_wheel(project_dir, out_dir),
)


def _sdist_relative_paths(sdist_path: pathlib.Path) -> set[str]:
"""Return archive member paths stripped of the top-level sdist directory."""
with tarfile.open(sdist_path, "r:gz") as archive:
return {
member.name.split("/", 1)[1]
for member in archive.getmembers()
if member.isfile() and "/" in member.name
}


def _assert_clean_wheel(wheel_path: pathlib.Path) -> set[str]:
"""Assert ``wheel_path`` ships only ``libvt`` and no unconditional runtime deps."""
with zipfile.ZipFile(wheel_path) as archive:
wheel_names = set(archive.namelist())
meta_names = [
name for name in wheel_names if name.endswith(".dist-info/METADATA")
]
assert len(meta_names) == 1
metadata = email.parser.Parser().parsestr(
archive.read(meta_names[0]).decode("utf-8"),
)
assert {"libvt/__init__.py", "libvt/py.typed"} <= wheel_names
assert all(path.startswith(("libvt/", "libvt-")) for path in wheel_names)
assert all(
path.startswith("libvt/") or ".dist-info/" in path for path in wheel_names
)
assert not any(
"__pycache__" in pathlib.PurePosixPath(path).parts
or path.endswith((".pyc", ".pyo", ".pyd"))
for path in wheel_names
)
requires_dist = metadata.get_all("Requires-Dist") or []
assert all("; extra ==" in dep for dep in requires_dist)
return wheel_names


def test_packaging_config_includes_examples_only_in_sdist() -> None:
"""``pyproject.toml`` includes ``examples`` in sdist and ``src/libvt`` in wheel."""
repo_root = pathlib.Path(__file__).resolve().parent.parent
pyproject_path = repo_root / "pyproject.toml"
assert pyproject_path.is_file()
data = tomllib.loads(pyproject_path.read_text(encoding="utf-8"))

assert data["build-system"]["build-backend"] == "hatchling.build"
assert data["project"]["dependencies"] == []

hatch_targets = data["tool"]["hatch"]["build"]["targets"]
sdist_include = hatch_targets["sdist"]["include"]
assert "examples" in sdist_include
assert {"src/libvt", "tests", "docs", "examples"} <= set(sdist_include)

wheel_config = hatch_targets["wheel"]
assert wheel_config["packages"] == ["src/libvt"]
wheel_include = wheel_config.get("include", [])
wheel_force_include = wheel_config.get("force-include", {})
assert not any("examples" in entry or "tests" in entry for entry in wheel_include)
assert not any(
"examples" in src or "examples" in dst or "tests" in src or "tests" in dst
for src, dst in wheel_force_include.items()
)


def test_sdist_build_includes_examples_and_keeps_wheel_clean(
tmp_path: pathlib.Path,
) -> None:
"""Sdists include ``examples/`` without bytecode; wheels ship only ``libvt``."""
repo_root = pathlib.Path(__file__).resolve().parent.parent
project = tmp_path / "project"
project.mkdir()
for config_name in ("pyproject.toml", ".gitignore"):
src_path = repo_root / config_name
assert src_path.is_file(), config_name
(project / config_name).write_text(
src_path.read_text(encoding="utf-8"),
encoding="utf-8",
)
for rel_file, body in (
("README.md", "# libvt\n"),
("LICENSE", "MIT\n"),
("CHANGES", "# Changelog\n"),
("MIGRATION", "# Migration\n"),
("conftest.py", ""),
("src/libvt/__init__.py", ""),
("src/libvt/py.typed", ""),
("tests/test_smoke.py", "def test_ok() -> None:\n pass\n"),
("docs/index.md", "# Docs\n"),
):
target = project / rel_file
target.parent.mkdir(parents=True, exist_ok=True)
target.write_text(body, encoding="utf-8")

sdist_without, wheel_without = _build_distributions(
project,
tmp_path / "out_without_examples",
)
sdist_without_files = _sdist_relative_paths(sdist_without)
assert {
"src/libvt/__init__.py",
"src/libvt/py.typed",
"tests/test_smoke.py",
"docs/index.md",
"conftest.py",
} <= sdist_without_files
assert not any(path.startswith("examples/") for path in sdist_without_files)
_assert_clean_wheel(wheel_without)

expected_examples = {
"examples/replay_prompt.py": "print('replay')\n",
"examples/tmux_screens.py": "print('tmux')\n",
"examples/last_beacon/game.py": "print('game')\n",
"examples/last_beacon/tour.py": "print('tour')\n",
"examples/last_beacon/walkthrough.json": '{"steps": []}\n',
}
for rel_file, body in expected_examples.items():
target = project / rel_file
target.parent.mkdir(parents=True, exist_ok=True)
target.write_text(body, encoding="utf-8")
for bytecode_rel in (
"examples/__pycache__/replay_prompt.cpython-312.pyc",
"examples/last_beacon/__pycache__/game.cpython-312.pyc",
"examples/stale.pyc",
"examples/stale.pyo",
"examples/stale.pyd",
):
target = project / bytecode_rel
target.parent.mkdir(parents=True, exist_ok=True)
target.write_bytes(b"pyc")

sdist_with, wheel_with = _build_distributions(
project,
tmp_path / "out_with_examples",
)
sdist_with_files = _sdist_relative_paths(sdist_with)
assert set(expected_examples) <= sdist_with_files
assert not any(
"__pycache__" in pathlib.PurePosixPath(path).parts
or path.endswith((".pyc", ".pyo", ".pyd"))
for path in sdist_with_files
)
wheel_with_names = _assert_clean_wheel(wheel_with)

extracted_dir = tmp_path / "extracted_sdist"
extracted_dir.mkdir()
with tarfile.open(sdist_with, "r:gz") as archive:
archive.extractall(extracted_dir, filter="data")
extracted_roots = list(extracted_dir.iterdir())
assert len(extracted_roots) == 1
assert extracted_roots[0].is_dir()
wheel_from_sdist = _build_wheel(
extracted_roots[0],
tmp_path / "out_wheel_from_sdist",
)
assert _assert_clean_wheel(wheel_from_sdist) == wheel_with_names
47 changes: 47 additions & 0 deletions uv.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Loading