|
| 1 | +"""Unit tests for scripts/check_doc_links.py.""" |
| 2 | + |
| 3 | +import sys |
| 4 | +from pathlib import Path |
| 5 | + |
| 6 | +import pytest |
| 7 | + |
| 8 | +sys.path.append(str(Path(__file__).resolve().parent.parent / "scripts")) |
| 9 | + |
| 10 | +from check_doc_links import LINK_RE, _normalize, check |
| 11 | + |
| 12 | +SITEMAP_XML = """<?xml version='1.0' encoding='utf-8'?> |
| 13 | +<urlset xmlns="https://www.sitemaps.org/schemas/sitemap/0.9"> |
| 14 | + <url><loc>http://localhost:3000/getting-started/basics/</loc></url> |
| 15 | + <url><loc>http://localhost:3000/library/disclosure/</loc></url> |
| 16 | +</urlset> |
| 17 | +""" |
| 18 | + |
| 19 | +SITEMAP_XML_WITH_DOCS_PREFIX = """<?xml version='1.0' encoding='utf-8'?> |
| 20 | +<urlset xmlns="https://www.sitemaps.org/schemas/sitemap/0.9"> |
| 21 | + <url><loc>http://localhost:3000/docs/getting-started/basics/</loc></url> |
| 22 | + <url><loc>http://localhost:3000/docs/library/disclosure/</loc></url> |
| 23 | +</urlset> |
| 24 | +""" |
| 25 | + |
| 26 | + |
| 27 | +@pytest.fixture |
| 28 | +def docs_tree(tmp_path: Path) -> tuple[Path, Path]: |
| 29 | + """Create a tmp docs root + sitemap.xml and return their paths.""" |
| 30 | + sitemap = tmp_path / "sitemap.xml" |
| 31 | + sitemap.write_text(SITEMAP_XML) |
| 32 | + md_root = tmp_path / "docs" |
| 33 | + md_root.mkdir() |
| 34 | + return md_root, sitemap |
| 35 | + |
| 36 | + |
| 37 | +def test_normalize_strips_fragment_query_and_trailing_slash(): |
| 38 | + assert _normalize("/foo/bar/") == "/foo/bar" |
| 39 | + assert _normalize("/foo/bar#section") == "/foo/bar" |
| 40 | + assert _normalize("/foo/bar?x=1") == "/foo/bar" |
| 41 | + assert _normalize("/") == "/" |
| 42 | + |
| 43 | + |
| 44 | +def test_link_re_matches_basic_link(): |
| 45 | + matches = LINK_RE.findall("see [basics](/docs/getting-started/basics) here") |
| 46 | + assert matches == ["/docs/getting-started/basics"] |
| 47 | + |
| 48 | + |
| 49 | +def test_link_re_does_not_match_docs_prefix_without_separator(): |
| 50 | + """`/docsfoo` and `/docs-foo` must not be treated as /docs links.""" |
| 51 | + assert LINK_RE.findall("[x](/docsfoo/bar)") == [] |
| 52 | + assert LINK_RE.findall("[x](/docs-foo/bar)") == [] |
| 53 | + |
| 54 | + |
| 55 | +def test_link_re_keeps_fragment_and_query(): |
| 56 | + assert LINK_RE.findall("[x](/docs/foo#anchor)") == ["/docs/foo#anchor"] |
| 57 | + assert LINK_RE.findall("[x](/docs/foo?q=1)") == ["/docs/foo?q=1"] |
| 58 | + |
| 59 | + |
| 60 | +def test_check_passes_for_valid_link(docs_tree): |
| 61 | + md_root, sitemap = docs_tree |
| 62 | + (md_root / "page.md").write_text("[ok](/docs/getting-started/basics)\n") |
| 63 | + assert check(md_root, sitemap) == [] |
| 64 | + |
| 65 | + |
| 66 | +def test_check_flags_missing_link(docs_tree): |
| 67 | + md_root, sitemap = docs_tree |
| 68 | + (md_root / "page.md").write_text("[bad](/docs/no-such-page)\n") |
| 69 | + errors = check(md_root, sitemap) |
| 70 | + assert len(errors) == 1 |
| 71 | + assert "not found in sitemap" in errors[0] |
| 72 | + |
| 73 | + |
| 74 | +def test_check_flags_underscore_and_missing(docs_tree): |
| 75 | + """Underscore link is reported twice: once for the underscore, once for missing.""" |
| 76 | + md_root, sitemap = docs_tree |
| 77 | + (md_root / "page.md").write_text("[under](/docs/getting_started/basics)\n") |
| 78 | + errors = check(md_root, sitemap) |
| 79 | + assert len(errors) == 2 |
| 80 | + assert any("underscore" in e for e in errors) |
| 81 | + assert any("not found in sitemap" in e for e in errors) |
| 82 | + |
| 83 | + |
| 84 | +def test_check_ignores_fragment_for_sitemap_lookup(docs_tree): |
| 85 | + md_root, sitemap = docs_tree |
| 86 | + (md_root / "page.md").write_text("[anchor](/docs/getting-started/basics#section)\n") |
| 87 | + assert check(md_root, sitemap) == [] |
| 88 | + |
| 89 | + |
| 90 | +def test_check_ignores_query_for_sitemap_lookup(docs_tree): |
| 91 | + md_root, sitemap = docs_tree |
| 92 | + (md_root / "page.md").write_text("[q](/docs/library/disclosure?x=1)\n") |
| 93 | + assert check(md_root, sitemap) == [] |
| 94 | + |
| 95 | + |
| 96 | +def test_check_ignores_docs_prefix_lookalikes(docs_tree): |
| 97 | + """`/docsfoo` should not even be treated as a /docs link.""" |
| 98 | + md_root, sitemap = docs_tree |
| 99 | + (md_root / "page.md").write_text("[x](/docsfoo/bar)\n") |
| 100 | + assert check(md_root, sitemap) == [] |
| 101 | + |
| 102 | + |
| 103 | +def test_check_skips_build_dirs(docs_tree): |
| 104 | + md_root, sitemap = docs_tree |
| 105 | + skipped = md_root / "node_modules" / "vendor" |
| 106 | + skipped.mkdir(parents=True) |
| 107 | + (skipped / "README.md").write_text("[bad](/docs/no-such-page)\n") |
| 108 | + assert check(md_root, sitemap) == [] |
| 109 | + |
| 110 | + |
| 111 | +def test_check_returns_helpful_message_when_sitemap_missing(tmp_path): |
| 112 | + errors = check(tmp_path, tmp_path / "missing.xml") |
| 113 | + assert len(errors) == 1 |
| 114 | + assert "sitemap.xml not found" in errors[0] |
| 115 | + |
| 116 | + |
| 117 | +def test_check_works_when_sitemap_has_docs_prefix(tmp_path: Path): |
| 118 | + """Both deployment styles (with or without /docs prefix in sitemap) work.""" |
| 119 | + sitemap = tmp_path / "sitemap.xml" |
| 120 | + sitemap.write_text(SITEMAP_XML_WITH_DOCS_PREFIX) |
| 121 | + md_root = tmp_path / "docs" |
| 122 | + md_root.mkdir() |
| 123 | + (md_root / "page.md").write_text( |
| 124 | + "[ok](/docs/getting-started/basics)\n[bad](/docs/no-such-page)\n" |
| 125 | + ) |
| 126 | + errors = check(md_root, sitemap) |
| 127 | + assert len(errors) == 1 |
| 128 | + assert "no-such-page" in errors[0] |
0 commit comments