diff --git a/CHANGES b/CHANGES index e57c49c..008aad0 100644 --- a/CHANGES +++ b/CHANGES @@ -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` diff --git a/pyproject.toml b/pyproject.toml index 4c679e6..b8f9441 100644 --- a/pyproject.toml +++ b/pyproject.toml @@ -58,6 +58,7 @@ dev = [ "sphinx-autobuild", # Testing "gp-libs>=0.0.19", + "hatchling", "pytest", "pytest-asyncio", "pytest-rerunfailures>=16.2", @@ -84,6 +85,7 @@ docs = [ ] testing = [ "gp-libs>=0.0.19", + "hatchling", "pytest", "pytest-asyncio", "pytest-rerunfailures>=16.2", @@ -116,6 +118,7 @@ include = [ "src/libvt", "tests", "docs", + "examples", "CHANGES", "MIGRATION", "README.md", diff --git a/tests/test_libvt.py b/tests/test_libvt.py index db89739..9b8573c 100644 --- a/tests/test_libvt.py +++ b/tests/test_libvt.py @@ -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 @@ -14,7 +16,9 @@ import re import subprocess import sys +import tarfile import tomllib +import zipfile import pytest @@ -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 diff --git a/uv.lock b/uv.lock index ac33e1f..1a131d1 100644 --- a/uv.lock +++ b/uv.lock @@ -453,6 +453,22 @@ wheels = [ { url = "https://files.pythonhosted.org/packages/04/4b/29cac41a4d98d144bf5f6d33995617b185d14b22401f75ca86f384e87ff1/h11-0.16.0-py3-none-any.whl", hash = "sha256:63cf8bbe7522de3bf65932fda1d9c2772064ffb3dae62d55932da54b31cb6c86", size = 37515, upload-time = "2025-04-24T03:35:24.344Z" }, ] +[[package]] +name = "hatchling" +version = "1.33.0" +source = { registry = "https://pypi.org/simple" } +dependencies = [ + { name = "packaging" }, + { name = "pathspec" }, + { name = "pluggy" }, + { name = "tomlkit" }, + { name = "trove-classifiers" }, +] +sdist = { url = "https://files.pythonhosted.org/packages/7a/f1/e47b937226ae3b093bbd0132eb0b7ee5bfad12c2b5324afd10ae49946b05/hatchling-1.33.0.tar.gz", hash = "sha256:4c422bd4d554fcff5de5b73e9596f85e41e9f97f7e553d8f7595ecb31a627bef", size = 58079, upload-time = "2026-10-28T02:27:30.694Z" } +wheels = [ + { url = "https://files.pythonhosted.org/packages/32/64/4afe5dd77b2939a93e8c9513f8082dbd049ecba9c1484a379a0f6ae29d6b/hatchling-1.33.0-py3-none-any.whl", hash = "sha256:415290b84f464d2884303fca8974d38e7b85579069fab02a15db0828fbb1446d", size = 78750, upload-time = "2026-10-28T02:27:29.033Z" }, +] + [[package]] name = "idna" version = "3.20" @@ -516,6 +532,7 @@ dev = [ { name = "coverage" }, { name = "gp-libs" }, { name = "gp-sphinx" }, + { name = "hatchling" }, { name = "pydantic" }, { name = "pytest" }, { name = "pytest-asyncio" }, @@ -544,6 +561,7 @@ lint = [ ] testing = [ { name = "gp-libs" }, + { name = "hatchling" }, { name = "pytest" }, { name = "pytest-asyncio" }, { name = "pytest-rerunfailures" }, @@ -570,6 +588,7 @@ dev = [ { name = "coverage" }, { name = "gp-libs", specifier = ">=0.0.19" }, { name = "gp-sphinx", specifier = "==0.1.0a39" }, + { name = "hatchling" }, { name = "pydantic", specifier = ">=2.12" }, { name = "pytest" }, { name = "pytest-asyncio" }, @@ -598,6 +617,7 @@ lint = [ ] testing = [ { name = "gp-libs", specifier = ">=0.0.19" }, + { name = "hatchling" }, { name = "pytest" }, { name = "pytest-asyncio" }, { name = "pytest-rerunfailures", specifier = ">=16.2" }, @@ -787,6 +807,15 @@ wheels = [ { url = "https://files.pythonhosted.org/packages/ba/7b/5504f91c9c2733f693d7759e50ede5d27119d4da7e5836c46d3b5ff81d56/packaging-26.4-py3-none-any.whl", hash = "sha256:81383611a09c1bc0b08b76e0fd5829d7c6824c65b27a6aee339da133f4233ea2", size = 140049, upload-time = "2026-11-23T21:44:09.531Z" }, ] +[[package]] +name = "pathspec" +version = "1.1.1" +source = { registry = "https://pypi.org/simple" } +sdist = { url = "https://files.pythonhosted.org/packages/5a/82/42f767fc1c1143d6fd36efb827202a2d997a375e160a71eb2888a925aac1/pathspec-1.1.1.tar.gz", hash = "sha256:17db5ecd524104a120e173814c90367a96a98d07c45b2e10c2f3919fff91bf5a", size = 135180, upload-time = "2026-04-27T01:46:08.907Z" } +wheels = [ + { url = "https://files.pythonhosted.org/packages/f1/d9/7fb5aa316bc299258e68c73ba3bddbc499654a07f151cba08f6153988714/pathspec-1.1.1-py3-none-any.whl", hash = "sha256:a00ce642f577bf7f473932318056212bc4f8bfdf53128c78bbd5af0b9b20b189", size = 57328, upload-time = "2026-04-27T01:46:07.06Z" }, +] + [[package]] name = "pluggy" version = "1.7.0" @@ -1486,6 +1515,24 @@ wheels = [ { url = "https://files.pythonhosted.org/packages/98/e0/83f27009f6c99a6b61d99c2d311cab080f43917bdad5d0bf974edf754b70/syrupy-7.0.3-py3-none-any.whl", hash = "sha256:020b4688b46887accb7365fdef7983e6a157f1e6984d1e00571d3d40cfebce51", size = 69112, upload-time = "2026-11-21T16:07:01.582Z" }, ] +[[package]] +name = "tomlkit" +version = "0.15.3" +source = { registry = "https://pypi.org/simple" } +sdist = { url = "https://files.pythonhosted.org/packages/c9/85/4a7c68111c057a6498f5bc721ef93b388b420bb31d7020759f18a5f0c2ec/tomlkit-0.15.3.tar.gz", hash = "sha256:b3c3020ec1a4bd469d12e31fb4da938e7f6ba758cdbec3fd9047db63120eaf81", size = 204435, upload-time = "2026-11-24T07:21:10.422Z" } +wheels = [ + { url = "https://files.pythonhosted.org/packages/c3/49/6b0a22e69c844c0b18cb66c206bdd791693d0cf845badc5f84e6448f2192/tomlkit-0.15.3-py3-none-any.whl", hash = "sha256:a83ce6de3b648e68dd9b50aabadad749ba823eb58b1d026f3c0a283c4767a9c1", size = 55467, upload-time = "2026-11-24T07:21:11.239Z" }, +] + +[[package]] +name = "trove-classifiers" +version = "2026.9.21.13" +source = { registry = "https://pypi.org/simple" } +sdist = { url = "https://files.pythonhosted.org/packages/bf/93/af436dfaa845cab5d96f0adbc1e4f3730532d37fa249e4eb796fb1d7fc82/trove_classifiers-2026.9.21.13.tar.gz", hash = "sha256:0a9ebc8d4e2f3e8a22848c5258033035bec17a3012ac3fea16dbaa764489eb71", size = 17085, upload-time = "2026-09-21T13:29:07.301Z" } +wheels = [ + { url = "https://files.pythonhosted.org/packages/30/81/0da8afb52a71d0a4f2bd3152357b1a441e393b286374802b9d3addab4ab5/trove_classifiers-2026.9.21.13-py3-none-any.whl", hash = "sha256:8b1ff4f9c191b1040b71c37f1e445ab99732911e3cd91de52838453a854d7a17", size = 14232, upload-time = "2026-09-21T13:29:06.123Z" }, +] + [[package]] name = "ty" version = "1.2.0"