nav submodule

This commit is contained in:
2026-01-20 21:22:28 +05:30
parent 869b1730c4
commit 726e7ca6d2
12 changed files with 493 additions and 0 deletions

0
tests/nav/__init__.py Normal file
View File

37
tests/nav/test_mkdocs.py Normal file
View File

@@ -0,0 +1,37 @@
from pathlib import Path
from docforge.nav import ResolvedNav
from docforge.nav import MkDocsNavEmitter
def test_emit_mkdocs_nav():
nav = ResolvedNav(
home="openapi_first/index.md",
groups={
"Core": [
Path("openapi_first/app.md"),
Path("openapi_first/client.md"),
],
"CLI": [
Path("openapi_first/cli.md"),
],
},
)
emitter = MkDocsNavEmitter()
mkdocs_nav = emitter.emit(nav)
assert mkdocs_nav == [
{"Home": "openapi_first/index.md"},
{
"Core": [
{"App": "openapi_first/app.md"},
{"Client": "openapi_first/client.md"},
]
},
{
"CLI": [
{"Cli": "openapi_first/cli.md"},
]
},
]

104
tests/nav/test_resolver.py Normal file
View File

@@ -0,0 +1,104 @@
from pathlib import Path
import pytest
from docforge.nav import NavSpec
from docforge.nav import resolve_nav
def _write_docs(root: Path, paths: list[str]) -> None:
for p in paths:
full = root / p
full.parent.mkdir(parents=True, exist_ok=True)
full.write_text(f"# {p}", encoding="utf-8")
def test_resolve_simple_nav(tmp_path: Path):
docs = tmp_path / "docs"
_write_docs(
docs,
[
"openapi_first/index.md",
"openapi_first/app.md",
"openapi_first/client.md",
],
)
spec = NavSpec(
home="openapi_first/index.md",
groups={
"Core": [
"openapi_first/app.md",
"openapi_first/client.md",
]
},
)
resolved = resolve_nav(spec, docs)
assert resolved.home == "openapi_first/index.md"
assert list(resolved.groups["Core"]) == [
docs / "openapi_first/app.md",
docs / "openapi_first/client.md",
]
def test_wildcard_expansion_preserves_order(tmp_path: Path):
docs = tmp_path / "docs"
_write_docs(
docs,
[
"pkg/a.md",
"pkg/b.md",
"pkg/c.md",
],
)
spec = NavSpec(
home=None,
groups={"All": ["pkg/*.md"]},
)
resolved = resolve_nav(spec, docs)
paths = [p.name for p in resolved.groups["All"]]
assert paths == ["a.md", "b.md", "c.md"]
def test_missing_doc_file_raises(tmp_path: Path):
docs = tmp_path / "docs"
docs.mkdir()
spec = NavSpec(
home="missing.md",
groups={},
)
with pytest.raises(FileNotFoundError):
resolve_nav(spec, docs)
def test_all_files_returns_flat_sequence(tmp_path: Path):
docs = tmp_path / "docs"
_write_docs(
docs,
[
"a.md",
"b.md",
"c.md",
],
)
spec = NavSpec(
home="a.md",
groups={"G": ["b.md", "c.md"]},
)
resolved = resolve_nav(spec, docs)
files = list(resolved.all_files())
assert files == [
docs / "a.md",
docs / "b.md",
docs / "c.md",
]

71
tests/nav/test_spec.py Normal file
View File

@@ -0,0 +1,71 @@
from pathlib import Path
import pytest
from docforge.nav import NavSpec
def test_load_valid_nav_spec(tmp_path: Path):
nav_yml = tmp_path / "docforge.nav.yml"
nav_yml.write_text(
"""
home: openapi_first/index.md
groups:
Core:
- openapi_first/app.md
- openapi_first/client.md
CLI:
- openapi_first/cli.md
""",
encoding="utf-8",
)
spec = NavSpec.load(nav_yml)
assert spec.home == "openapi_first/index.md"
assert "Core" in spec.groups
assert spec.groups["Core"] == [
"openapi_first/app.md",
"openapi_first/client.md",
]
def test_missing_file_raises(tmp_path: Path):
with pytest.raises(FileNotFoundError):
NavSpec.load(tmp_path / "missing.yml")
def test_invalid_schema_raises(tmp_path: Path):
nav_yml = tmp_path / "docforge.nav.yml"
nav_yml.write_text(
"""
groups:
- not_a_mapping
""",
encoding="utf-8",
)
with pytest.raises(ValueError):
NavSpec.load(nav_yml)
def test_all_patterns_includes_home_and_groups(tmp_path: Path):
nav_yml = tmp_path / "docforge.nav.yml"
nav_yml.write_text(
"""
home: a.md
groups:
X:
- b.md
- c/*.md
""",
encoding="utf-8",
)
spec = NavSpec.load(nav_yml)
patterns = spec.all_patterns()
assert "a.md" in patterns
assert "b.md" in patterns
assert "c/*.md" in patterns