|
| 1 | +from pathlib import Path |
| 2 | + |
| 3 | +from scripts.check_internal_links import check_site |
| 4 | + |
| 5 | + |
| 6 | +def write(tmp_path: Path, relative_path: str, content: str) -> None: |
| 7 | + target = tmp_path / relative_path |
| 8 | + target.parent.mkdir(parents=True, exist_ok=True) |
| 9 | + target.write_text(content, encoding="utf-8") |
| 10 | + |
| 11 | + |
| 12 | +def test_check_site_accepts_valid_internal_links(tmp_path: Path) -> None: |
| 13 | + write( |
| 14 | + tmp_path, |
| 15 | + "index.html", |
| 16 | + """ |
| 17 | + <html><body> |
| 18 | + <a href="/docs/fr/docs/demarrage/">French getting started</a> |
| 19 | + <a href="/docs/fr/docs/guide-langage/toutes-langues/#equivalents">All languages</a> |
| 20 | + </body></html> |
| 21 | + """, |
| 22 | + ) |
| 23 | + write( |
| 24 | + tmp_path, |
| 25 | + "fr/docs/demarrage/index.html", |
| 26 | + """ |
| 27 | + <html><body> |
| 28 | + <a href="/docs/fr/docs/guide-langage/toutes-langues/#equivalents">Guide</a> |
| 29 | + </body></html> |
| 30 | + """, |
| 31 | + ) |
| 32 | + write( |
| 33 | + tmp_path, |
| 34 | + "fr/docs/guide-langage/toutes-langues/index.html", |
| 35 | + """ |
| 36 | + <html><body> |
| 37 | + <h1 id="equivalents">Equivalents</h1> |
| 38 | + </body></html> |
| 39 | + """, |
| 40 | + ) |
| 41 | + |
| 42 | + assert check_site(tmp_path, base_path="/docs") == [] |
| 43 | + |
| 44 | + |
| 45 | +def test_check_site_rejects_root_relative_links_without_base_path(tmp_path: Path) -> None: |
| 46 | + write( |
| 47 | + tmp_path, |
| 48 | + "fr/docs/demarrage/demarrage-rapide/index.html", |
| 49 | + """ |
| 50 | + <html><body> |
| 51 | + <a href="/fr/docs/guide-langage/toutes-langues/">Broken link</a> |
| 52 | + </body></html> |
| 53 | + """, |
| 54 | + ) |
| 55 | + write( |
| 56 | + tmp_path, |
| 57 | + "fr/docs/guide-langage/toutes-langues/index.html", |
| 58 | + "<html><body></body></html>", |
| 59 | + ) |
| 60 | + |
| 61 | + errors = check_site(tmp_path, base_path="/docs") |
| 62 | + |
| 63 | + assert len(errors) == 1 |
| 64 | + assert "bypasses base path /docs" in errors[0] |
| 65 | + |
| 66 | + |
| 67 | +def test_check_site_rejects_missing_anchor(tmp_path: Path) -> None: |
| 68 | + write( |
| 69 | + tmp_path, |
| 70 | + "fr/docs/demarrage/index.html", |
| 71 | + """ |
| 72 | + <html><body> |
| 73 | + <a href="/docs/fr/docs/guide-langage/#missing-anchor">Guide</a> |
| 74 | + </body></html> |
| 75 | + """, |
| 76 | + ) |
| 77 | + write( |
| 78 | + tmp_path, |
| 79 | + "fr/docs/guide-langage/index.html", |
| 80 | + """ |
| 81 | + <html><body> |
| 82 | + <h1 id="existing-anchor">Guide</h1> |
| 83 | + </body></html> |
| 84 | + """, |
| 85 | + ) |
| 86 | + |
| 87 | + errors = check_site(tmp_path, base_path="/docs") |
| 88 | + |
| 89 | + assert len(errors) == 1 |
| 90 | + assert "missing anchor #missing-anchor" in errors[0] |
0 commit comments