|
| 1 | +# SPDX-FileCopyrightText: Copyright (c) 2026 NVIDIA CORPORATION & AFFILIATES. All rights reserved. |
| 2 | +# SPDX-License-Identifier: Apache-2.0 |
| 3 | + |
| 4 | +from __future__ import annotations |
| 5 | + |
| 6 | +import os |
| 7 | +import sys |
| 8 | + |
| 9 | +import pytest |
| 10 | + |
| 11 | +sys.path.insert(0, os.path.join(os.path.dirname(__file__), "..")) |
| 12 | +from check_mempool_hygiene import DEFAULT_TREE, main, violations_in |
| 13 | + |
| 14 | + |
| 15 | +def write(tmp_path, source): |
| 16 | + path = tmp_path / "test_sample.py" |
| 17 | + path.write_text(source, encoding="utf-8") |
| 18 | + return path |
| 19 | + |
| 20 | + |
| 21 | +UNCAPPED = [ |
| 22 | + pytest.param("DeviceMemoryResource(dev, DeviceMemoryResourceOptions(ipc_enabled=True))", id="options-kwarg"), |
| 23 | + pytest.param("PinnedMemoryResource(PinnedMemoryResourceOptions())", id="options-empty"), |
| 24 | + pytest.param('DeviceMemoryResource(dev, {"ipc_enabled": True})', id="options-dict"), |
| 25 | +] |
| 26 | + |
| 27 | +CAPPED = [ |
| 28 | + pytest.param("DeviceMemoryResource(dev, DeviceMemoryResourceOptions(max_size=POOL_SIZE))", id="capped-kwarg"), |
| 29 | + pytest.param('DeviceMemoryResource(dev, {"max_size": POOL_SIZE})', id="capped-dict"), |
| 30 | + pytest.param("DeviceMemoryResource(dev, DeviceMemoryResourceOptions(**opts))", id="opaque-kwargs"), |
| 31 | + # No options at all wraps the device's default pool and reserves nothing, so |
| 32 | + # capping it would convert a free wrapper into a new pool. |
| 33 | + pytest.param("DeviceMemoryResource(dev)", id="default-pool-wrapper"), |
| 34 | + # cuMemPoolCreate requires maxSize == 0 for managed pools, so these have no |
| 35 | + # max_size to set. |
| 36 | + pytest.param("ManagedMemoryResource(ManagedMemoryResourceOptions(preferred_location=0))", id="managed-exempt"), |
| 37 | +] |
| 38 | + |
| 39 | + |
| 40 | +@pytest.mark.agent_authored(model="claude-opus-5") |
| 41 | +@pytest.mark.parametrize("source", UNCAPPED) |
| 42 | +def test_uncapped_pool_is_reported(tmp_path, source): |
| 43 | + assert violations_in(write(tmp_path, source)) |
| 44 | + |
| 45 | + |
| 46 | +@pytest.mark.agent_authored(model="claude-opus-5") |
| 47 | +@pytest.mark.parametrize("source", CAPPED) |
| 48 | +def test_acceptable_construction_is_not_reported(tmp_path, source): |
| 49 | + assert violations_in(write(tmp_path, source)) == [] |
| 50 | + |
| 51 | + |
| 52 | +@pytest.mark.agent_authored(model="claude-opus-5") |
| 53 | +@pytest.mark.parametrize("comment_line", [0, 1], ids=["marker-above", "marker-inline"]) |
| 54 | +def test_marker_opts_a_call_out(tmp_path, comment_line): |
| 55 | + # The escape hatch exists mainly for pytest.raises cases, where validation |
| 56 | + # rejects the arguments before any pool is created. |
| 57 | + call = "PinnedMemoryResource(PinnedMemoryResourceOptions())" |
| 58 | + marker = "# uncapped-pool-ok: raises before the pool is created" |
| 59 | + source = f"{marker}\n{call}" if comment_line == 0 else f"{call} {marker}" |
| 60 | + |
| 61 | + assert violations_in(write(tmp_path, source)) == [] |
| 62 | + |
| 63 | + |
| 64 | +@pytest.mark.agent_authored(model="claude-opus-5") |
| 65 | +def test_reported_message_names_file_line_and_symbol(tmp_path): |
| 66 | + path = write(tmp_path, "x = 1\nDeviceMemoryResource(dev, DeviceMemoryResourceOptions())\n") |
| 67 | + |
| 68 | + (violation,) = violations_in(path) |
| 69 | + |
| 70 | + assert violation.startswith(path.as_posix()) |
| 71 | + assert ":2:" in violation |
| 72 | + assert "DeviceMemoryResourceOptions without max_size" in violation |
| 73 | + |
| 74 | + |
| 75 | +@pytest.mark.agent_authored(model="claude-opus-5") |
| 76 | +def test_main_reports_failure_for_the_files_it_is_given(tmp_path, capsys): |
| 77 | + path = write(tmp_path, "DeviceMemoryResource(dev, DeviceMemoryResourceOptions())") |
| 78 | + |
| 79 | + assert main([str(path)]) == 1 |
| 80 | + assert "must set max_size" in capsys.readouterr().err |
| 81 | + |
| 82 | + |
| 83 | +@pytest.mark.agent_authored(model="claude-opus-5") |
| 84 | +def test_main_ignores_non_python_files(tmp_path): |
| 85 | + unrelated = tmp_path / "notes.txt" |
| 86 | + unrelated.write_text("DeviceMemoryResourceOptions()", encoding="utf-8") |
| 87 | + |
| 88 | + assert main([str(unrelated)]) == 0 |
| 89 | + |
| 90 | + |
| 91 | +@pytest.mark.agent_authored(model="claude-opus-5") |
| 92 | +def test_the_live_test_suite_is_clean(): |
| 93 | + # Without a default the hook would only ever see changed files, so a |
| 94 | + # violation could ride in on a rename or a merge. |
| 95 | + assert DEFAULT_TREE.is_dir() |
| 96 | + assert main([]) == 0 |
0 commit comments