import textwrap from pathlib import Path from check_markdown_links import find_broken_links def write(path: Path, content: str) -> None: path.parent.mkdir(parents=True, exist_ok=True) path.write_text(textwrap.dedent(content).lstrip(), encoding="utf-8") def test_reports_missing_local_markdown_target_with_line_number(tmp_path: Path): write( tmp_path / "README.md", """ # Repo See [status](docs/status.md). """, ) broken = find_broken_links(tmp_path) assert len(broken) == 1 assert broken[0]["source"].endswith("README.md") assert broken[0]["line"] == 3 assert broken[0]["target"] == "docs/status.md" def test_allows_existing_relative_targets(tmp_path: Path): write(tmp_path / "docs" / "status.md", "# Status\n") write( tmp_path / "README.md", """ # Repo See [status](docs/status.md). """, ) assert find_broken_links(tmp_path) == [] def test_ignores_external_anchor_mailto_and_tel_links(tmp_path: Path): write( tmp_path / "README.md", """ [external](https://example.com) [anchor](#section) [mail](mailto:test@example.com) [call](tel:988) """, ) assert find_broken_links(tmp_path) == [] def test_ignores_links_inside_fenced_code_blocks(tmp_path: Path): write( tmp_path / "README.md", """ ```md [broken](docs/missing.md) ``` """, ) assert find_broken_links(tmp_path) == [] def test_skips_build_directories(tmp_path: Path): write(tmp_path / "build" / "README.md", "[broken](missing.md)\n") assert find_broken_links(tmp_path) == []