diff --git a/ci/tools/tests/test_rebase_sphinx_inventory.py b/ci/tools/tests/test_rebase_sphinx_inventory.py index a7a07a14c62..560d820ebea 100644 --- a/ci/tools/tests/test_rebase_sphinx_inventory.py +++ b/ci/tools/tests/test_rebase_sphinx_inventory.py @@ -8,21 +8,22 @@ from cuda_python.docs.copy_rebased_sphinx_inventory_one_level_up import copy_rebased_inventory_one_level_up +_INVENTORY_HEADER = ( + b"# Sphinx inventory version 2\n" + b"# Project: cuda.bindings\n" + b"# Version: 13.5.0\n" + b"# The remainder of this file is compressed using zlib.\n" +) + @pytest.mark.agent_authored(model="gpt-6") def test_copy_rebases_relative_inventory_targets(tmp_path, monkeypatch): - header = ( - b"# Sphinx inventory version 2\n" - b"# Project: cuda.bindings\n" - b"# Version: 13.5.0\n" - b"# The remainder of this file is compressed using zlib.\n" - ) entries = ( b"cuda.bindings.driver.cuInit py:function 1 module/driver.html#$ -\n" b"figure 1 std:label -1 overview.html#figure-1 Example with spaces\n" b"external std:label -1 https://example.org/overview.html#target -\n" ) - original = header + zlib.compress(entries) + original = _INVENTORY_HEADER + zlib.compress(entries) source = tmp_path / "latest" / "objects.inv" source.parent.mkdir() source.write_bytes(original) @@ -32,10 +33,23 @@ def test_copy_rebases_relative_inventory_targets(tmp_path, monkeypatch): copy_rebased_inventory_one_level_up(Path("objects.inv")) rebased = destination.read_bytes() - assert rebased.startswith(header) - assert zlib.decompress(rebased[len(header) :]) == ( + assert rebased.startswith(_INVENTORY_HEADER) + assert zlib.decompress(rebased[len(_INVENTORY_HEADER) :]) == ( b"cuda.bindings.driver.cuInit py:function 1 latest/module/driver.html#$ -\n" b"figure 1 std:label -1 latest/overview.html#figure-1 Example with spaces\n" b"external std:label -1 https://example.org/overview.html#target -\n" ) assert source.read_bytes() == original + + +@pytest.mark.agent_authored(model="gpt-6") +def test_copy_rejects_malformed_inventory_entries(tmp_path): + entries = b"cuda.bindings.driver.cuInit py:function 1 module/driver.html#$ -\nunexpected entry\n" + source = tmp_path / "latest" / "objects.inv" + source.parent.mkdir() + source.write_bytes(_INVENTORY_HEADER + zlib.compress(entries)) + + with pytest.raises(ValueError, match="1 of 2 inventory entries did not match the expected format"): + copy_rebased_inventory_one_level_up(source) + + assert not (tmp_path / "objects.inv").exists() diff --git a/cuda_python/docs/copy_rebased_sphinx_inventory_one_level_up.py b/cuda_python/docs/copy_rebased_sphinx_inventory_one_level_up.py index 3b428fee029..1523813862a 100644 --- a/cuda_python/docs/copy_rebased_sphinx_inventory_one_level_up.py +++ b/cuda_python/docs/copy_rebased_sphinx_inventory_one_level_up.py @@ -34,7 +34,12 @@ def rebase_entry(match: re.Match[str]) -> str: header = b"\n".join(parts[:4]) + b"\n" entries = zlib.decompress(parts[4]).decode() - rebased = _ENTRY.sub(rebase_entry, entries) + rebased, matched = _ENTRY.subn(rebase_entry, entries) + expected = sum(1 for line in entries.split("\n") if line.strip()) + if matched != expected: + raise ValueError( + f"{source}: {expected - matched} of {expected} inventory entries did not match the expected format" + ) destination.write_bytes(header + zlib.compress(rebased.encode())) diff --git a/pytest.ini b/pytest.ini index fce1f3f2173..e3a4521370d 100644 --- a/pytest.ini +++ b/pytest.ini @@ -3,6 +3,7 @@ [pytest] addopts = --showlocals --durations=20 +pythonpath = . norecursedirs = samples