|
| 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 shutil |
| 7 | +import subprocess |
| 8 | +import sys |
| 9 | +from pathlib import Path |
| 10 | + |
| 11 | +import pytest |
| 12 | + |
| 13 | +sys.path.insert(0, str(Path(__file__).parent.parent)) |
| 14 | +from preprocess_repo_links import preprocess_links |
| 15 | + |
| 16 | +SCRIPT = Path(__file__).resolve().parents[1] / "preprocess_repo_links.py" |
| 17 | + |
| 18 | + |
| 19 | +@pytest.mark.parametrize( |
| 20 | + "template", |
| 21 | + [ |
| 22 | + "[Directory]({directory})\n[File]({file})\n[Relative](guide.rst)\n", |
| 23 | + "`Directory <{directory}>`_\n`File <{file}>`_\n`Relative <guide.rst>`_\n", |
| 24 | + '<a href="{directory}">Directory</a><a href="{file}">File</a><a href="guide.rst">Relative</a>\n', |
| 25 | + ], |
| 26 | +) |
| 27 | +@pytest.mark.agent_authored(model="gpt-6.1-sol") |
| 28 | +def test_preprocess_links_preserves_format_relative_links_and_url_encoding(tmp_path, template): |
| 29 | + root = tmp_path / "checkout space$#" |
| 30 | + content = template.format( |
| 31 | + directory="https://github.com/NVIDIA/cuda-python/tree/main/new_directory/", |
| 32 | + file="https://github.com/NVIDIA/cuda-python/blob/main/docs/a%20guide.md?plain=1#some-heading", |
| 33 | + ) |
| 34 | + |
| 35 | + assert preprocess_links(content, root) == template.format( |
| 36 | + directory=f"{root.as_uri()}/new_directory/", |
| 37 | + file=f"{root.as_uri()}/docs/a%20guide.md?plain=1#some-heading", |
| 38 | + ) |
| 39 | + |
| 40 | + |
| 41 | +@pytest.mark.parametrize( |
| 42 | + "url", |
| 43 | + [ |
| 44 | + "https://github.com/NVIDIA/cuda-python/blob/12.9.x/README.md", |
| 45 | + "https://github.com/NVIDIA/cuda-python/tree/v13.0.0/cuda_bindings", |
| 46 | + "https://github.com/NVIDIA/cuda-python/blob/0123456789abcdef/README.md", |
| 47 | + "https://github.com/NVIDIA/cuda-python/blob/mainly/README.md", |
| 48 | + "https://github.com/NVIDIA/cuda-python/tree/main", |
| 49 | + "https://github.com/rwgk/cuda-python/blob/main/README.md", |
| 50 | + "https://github.com/NVIDIA/other-repository/tree/main/docs", |
| 51 | + "http://github.com/NVIDIA/cuda-python/blob/main/README.md", |
| 52 | + "https://raw.githubusercontent.com/NVIDIA/cuda-python/main/README.md", |
| 53 | + "https://nvidia.github.io/cuda-python/latest/", |
| 54 | + ], |
| 55 | +) |
| 56 | +@pytest.mark.agent_authored(model="gpt-6.1-sol") |
| 57 | +def test_preprocess_links_leaves_other_targets_unchanged(tmp_path, url): |
| 58 | + assert preprocess_links(f"[Link]({url})\n", tmp_path) == f"[Link]({url})\n" |
| 59 | + |
| 60 | + |
| 61 | +@pytest.mark.agent_authored(model="gpt-6.1-sol") |
| 62 | +def test_cli_uses_its_checkout_root_and_leaves_input_unchanged(tmp_path): |
| 63 | + checkout = tmp_path / "checkout space$#" |
| 64 | + script = checkout / "ci" / "tools" / SCRIPT.name |
| 65 | + script.parent.mkdir(parents=True) |
| 66 | + shutil.copyfile(SCRIPT, script) |
| 67 | + source = tmp_path / "source guide.rst" |
| 68 | + content = b"`Repository <https://github.com/NVIDIA/cuda-python/tree/main/new_directory>`_\r\n" |
| 69 | + source.write_bytes(content) |
| 70 | + |
| 71 | + result = subprocess.run( # noqa: S603 |
| 72 | + [sys.executable, str(script), str(source)], |
| 73 | + cwd=tmp_path, |
| 74 | + check=True, |
| 75 | + capture_output=True, |
| 76 | + text=True, |
| 77 | + ) |
| 78 | + |
| 79 | + assert result.stdout == f"`Repository <{checkout.as_uri()}/new_directory>`_\n" |
| 80 | + assert result.stderr == "" |
| 81 | + assert source.read_bytes() == content |
| 82 | + |
| 83 | + |
| 84 | +@pytest.mark.agent_authored(model="gpt-6.1-sol") |
| 85 | +def test_cli_missing_input_fails_without_success_output(tmp_path): |
| 86 | + source = tmp_path / "missing.md" |
| 87 | + |
| 88 | + result = subprocess.run([sys.executable, str(SCRIPT), str(source)], capture_output=True, text=True) # noqa: S603 |
| 89 | + |
| 90 | + assert result.returncode != 0 |
| 91 | + assert result.stdout == "" |
| 92 | + assert "FileNotFoundError" in result.stderr |
| 93 | + assert not source.exists() |
0 commit comments