mkdocs cli
This commit is contained in:
140
tests/cli/test_mkdocs.py
Normal file
140
tests/cli/test_mkdocs.py
Normal file
@@ -0,0 +1,140 @@
|
||||
from pathlib import Path
|
||||
|
||||
import yaml
|
||||
from click.testing import CliRunner
|
||||
|
||||
from docforge.cli.main import cli
|
||||
|
||||
|
||||
def _write_nav_spec(path: Path) -> None:
|
||||
path.write_text(
|
||||
"""
|
||||
home: openapi_first/index.md
|
||||
groups:
|
||||
Core:
|
||||
- openapi_first/app.md
|
||||
- openapi_first/client.md
|
||||
""".strip(),
|
||||
encoding="utf-8",
|
||||
)
|
||||
|
||||
|
||||
def _write_docs(docs: Path) -> None:
|
||||
files = [
|
||||
"openapi_first/index.md",
|
||||
"openapi_first/app.md",
|
||||
"openapi_first/client.md",
|
||||
]
|
||||
for rel in files:
|
||||
p = docs / rel
|
||||
p.parent.mkdir(parents=True, exist_ok=True)
|
||||
p.write_text(f"# {rel}", encoding="utf-8")
|
||||
|
||||
|
||||
def test_mkdocs_uses_builtin_template(tmp_path: Path) -> None:
|
||||
docs = tmp_path / "docs"
|
||||
docs.mkdir()
|
||||
|
||||
nav_file = tmp_path / "docforge.nav.yml"
|
||||
out_file = tmp_path / "mkdocs.yml"
|
||||
|
||||
_write_docs(docs)
|
||||
_write_nav_spec(nav_file)
|
||||
|
||||
runner = CliRunner()
|
||||
result = runner.invoke(
|
||||
cli,
|
||||
[
|
||||
"mkdocs",
|
||||
"--docs-dir",
|
||||
str(docs),
|
||||
"--nav",
|
||||
str(nav_file),
|
||||
"--out",
|
||||
str(out_file),
|
||||
],
|
||||
)
|
||||
|
||||
assert result.exit_code == 0
|
||||
assert out_file.exists()
|
||||
|
||||
data = yaml.safe_load(out_file.read_text(encoding="utf-8"))
|
||||
|
||||
# Nav should be injected
|
||||
assert "nav" in data
|
||||
assert data["nav"] == [
|
||||
{"Home": "openapi_first/index.md"},
|
||||
{
|
||||
"Core": [
|
||||
"openapi_first/app.md",
|
||||
"openapi_first/client.md",
|
||||
]
|
||||
},
|
||||
]
|
||||
|
||||
# Template content should still exist
|
||||
assert "theme" in data
|
||||
assert "plugins" in data
|
||||
|
||||
|
||||
def test_mkdocs_overrides_template(tmp_path: Path) -> None:
|
||||
docs = tmp_path / "docs"
|
||||
docs.mkdir()
|
||||
|
||||
nav_file = tmp_path / "docforge.nav.yml"
|
||||
template = tmp_path / "custom.yml"
|
||||
out_file = tmp_path / "mkdocs.yml"
|
||||
|
||||
_write_docs(docs)
|
||||
_write_nav_spec(nav_file)
|
||||
|
||||
template.write_text(
|
||||
"""
|
||||
site_name: Custom Site
|
||||
theme:
|
||||
name: readthedocs
|
||||
""".strip(),
|
||||
encoding="utf-8",
|
||||
)
|
||||
|
||||
runner = CliRunner()
|
||||
result = runner.invoke(
|
||||
cli,
|
||||
[
|
||||
"mkdocs",
|
||||
"--docs-dir",
|
||||
str(docs),
|
||||
"--nav",
|
||||
str(nav_file),
|
||||
"--template",
|
||||
str(template),
|
||||
"--out",
|
||||
str(out_file),
|
||||
],
|
||||
)
|
||||
|
||||
assert result.exit_code == 0
|
||||
|
||||
data = yaml.safe_load(out_file.read_text(encoding="utf-8"))
|
||||
|
||||
assert data["site_name"] == "Custom Site"
|
||||
assert data["theme"]["name"] == "readthedocs"
|
||||
assert "nav" in data
|
||||
|
||||
|
||||
def test_mkdocs_missing_nav_fails(tmp_path: Path) -> None:
|
||||
docs = tmp_path / "docs"
|
||||
docs.mkdir()
|
||||
|
||||
runner = CliRunner()
|
||||
result = runner.invoke(
|
||||
cli,
|
||||
[
|
||||
"mkdocs",
|
||||
"--docs-dir",
|
||||
str(docs),
|
||||
],
|
||||
)
|
||||
|
||||
assert result.exit_code != 0
|
||||
assert "Nav spec not found" in result.output
|
||||
@@ -25,13 +25,13 @@ def test_emit_mkdocs_nav():
|
||||
{"Home": "openapi_first/index.md"},
|
||||
{
|
||||
"Core": [
|
||||
{"App": "openapi_first/app.md"},
|
||||
{"Client": "openapi_first/client.md"},
|
||||
"openapi_first/app.md",
|
||||
"openapi_first/client.md",
|
||||
]
|
||||
},
|
||||
{
|
||||
"CLI": [
|
||||
{"Cli": "openapi_first/cli.md"},
|
||||
"openapi_first/cli.md",
|
||||
]
|
||||
},
|
||||
]
|
||||
|
||||
Reference in New Issue
Block a user