generated from coulomb/repo-seed
92 lines
2.6 KiB
Python
92 lines
2.6 KiB
Python
from pathlib import Path
|
|
|
|
from click.testing import CliRunner
|
|
|
|
from markitect_tool.cli import main
|
|
from markitect_tool.literate import (
|
|
discover_code_chunks,
|
|
tangle_markdown,
|
|
weave_markdown,
|
|
write_tangle_files,
|
|
)
|
|
|
|
|
|
LITERATE_DOC = """# Literate Example
|
|
|
|
```python {#helpers}
|
|
def helper():
|
|
return "ready"
|
|
```
|
|
|
|
```python {#main tangle="src/app.py"}
|
|
<<helpers>>
|
|
|
|
def main():
|
|
return helper()
|
|
```
|
|
"""
|
|
|
|
|
|
def test_discover_code_chunks_with_references_and_targets():
|
|
chunks = discover_code_chunks(LITERATE_DOC, source_path="example.md")
|
|
|
|
assert [chunk.chunk_id for chunk in chunks] == ["helpers", "main"]
|
|
assert chunks[1].target_path == "src/app.py"
|
|
assert chunks[1].references == ["helpers"]
|
|
|
|
|
|
def test_tangle_expands_named_chunk_references():
|
|
result = tangle_markdown(LITERATE_DOC, source_path="example.md")
|
|
|
|
assert result.valid
|
|
assert len(result.files) == 1
|
|
assert result.files[0].path == "src/app.py"
|
|
assert "def helper" in result.files[0].content
|
|
assert "<<helpers>>" not in result.files[0].content
|
|
assert result.provenance[0].operation == "literate.tangle"
|
|
|
|
|
|
def test_tangle_reports_missing_chunk_reference():
|
|
markdown = """```python {#main tangle="src/app.py"}
|
|
<<missing>>
|
|
```
|
|
"""
|
|
|
|
result = tangle_markdown(markdown, source_path="example.md")
|
|
|
|
assert not result.valid
|
|
assert result.diagnostics[0].code == "literate.missing_chunk"
|
|
|
|
|
|
def test_weave_appends_chunk_index():
|
|
result = weave_markdown(LITERATE_DOC, source_path="example.md")
|
|
|
|
assert "## Code Chunk Index" in result.markdown
|
|
assert "`main` -> `src/app.py`; refs: `helpers`" in result.markdown
|
|
|
|
|
|
def test_write_tangle_files(tmp_path: Path):
|
|
result = tangle_markdown(LITERATE_DOC, source_path="example.md")
|
|
|
|
written = write_tangle_files(result, tmp_path)
|
|
|
|
assert written == [str(tmp_path / "src" / "app.py")]
|
|
assert "def main" in (tmp_path / "src" / "app.py").read_text(encoding="utf-8")
|
|
|
|
|
|
def test_mkt_tangle_and_weave(tmp_path: Path):
|
|
source = tmp_path / "literate.md"
|
|
output_dir = tmp_path / "out"
|
|
woven = tmp_path / "woven.md"
|
|
source.write_text(LITERATE_DOC, encoding="utf-8")
|
|
runner = CliRunner()
|
|
|
|
tangle_result = runner.invoke(main, ["tangle", str(source), "--output-dir", str(output_dir)])
|
|
weave_result = runner.invoke(main, ["weave", str(source), "--output", str(woven)])
|
|
|
|
assert tangle_result.exit_code == 0
|
|
assert "files: 1" in tangle_result.output
|
|
assert (output_dir / "src" / "app.py").exists()
|
|
assert weave_result.exit_code == 0
|
|
assert "## Code Chunk Index" in woven.read_text(encoding="utf-8")
|