From eff5bd226a4354140a6b568b5414d7968be714bb Mon Sep 17 00:00:00 2001 From: Luong NGUYEN Date: Sat, 25 Apr 2026 00:33:21 +0200 Subject: [PATCH] test(scripts): cover repo-root boundary in check_cross_references (#95) * test(scripts): cover repo-root boundary in check_cross_references Locks in the boundary behavior added in #91: links resolving outside the repo root are silently skipped, in-repo broken links still error, and valid in-repo links still pass. * test(scripts): cover missing-README detection in numbered lesson dirs --- scripts/tests/test_check_cross_references.py | 61 ++++++++++++++++++++ 1 file changed, 61 insertions(+) create mode 100644 scripts/tests/test_check_cross_references.py diff --git a/scripts/tests/test_check_cross_references.py b/scripts/tests/test_check_cross_references.py new file mode 100644 index 0000000..7dbae29 --- /dev/null +++ b/scripts/tests/test_check_cross_references.py @@ -0,0 +1,61 @@ +"""Tests for check_cross_references.py — focus on repo-root boundary.""" + +from __future__ import annotations + +import os +import sys +from pathlib import Path + +import pytest + +sys.path.insert(0, str(Path(__file__).parent.parent)) + +import check_cross_references + + +@pytest.fixture +def repo(tmp_path: Path, monkeypatch: pytest.MonkeyPatch) -> Path: + monkeypatch.chdir(tmp_path) + return tmp_path + + +def test_links_escaping_repo_root_are_skipped( + repo: Path, capsys: pytest.CaptureFixture[str] +) -> None: + outside = repo.parent / "outside.md" + outside.write_text("# Outside") + + rel = os.path.relpath(outside, repo) + (repo / "README.md").write_text(f"# Doc\n\n[escape]({rel})\n") + + assert check_cross_references.main() == 0 + assert "broken cross-reference" not in capsys.readouterr().out + + +def test_broken_in_repo_link_is_reported( + repo: Path, capsys: pytest.CaptureFixture[str] +) -> None: + (repo / "README.md").write_text("# Doc\n\n[missing](does-not-exist.md)\n") + + assert check_cross_references.main() == 1 + assert "broken cross-reference" in capsys.readouterr().out + + +def test_valid_in_repo_link_passes( + repo: Path, capsys: pytest.CaptureFixture[str] +) -> None: + (repo / "other.md").write_text("# Other") + (repo / "README.md").write_text("# Doc\n\n[ok](other.md)\n") + + assert check_cross_references.main() == 0 + assert "All cross-references valid" in capsys.readouterr().out + + +def test_numbered_lesson_dir_missing_readme_is_reported( + repo: Path, capsys: pytest.CaptureFixture[str] +) -> None: + (repo / "README.md").write_text("# Doc") + (repo / "01-intro").mkdir() + + assert check_cross_references.main() == 1 + assert "01-intro: missing README.md" in capsys.readouterr().out