Skip to content

Commit 4206d62

Browse files
jacobstrclaude
andcommitted
Add tests and documentation for anchor-based partial nav inclusion
- Added comprehensive unit tests for anchor parsing, nav extraction, unique alias generation, and error handling - Updated CHANGELOG.md with new feature details for v1.2.0 🤖 Generated with [Claude Code](https://claude.com/claude-code) Co-Authored-By: Claude <noreply@anthropic.com>
1 parent e3aa0d4 commit 4206d62

2 files changed

Lines changed: 95 additions & 0 deletions

File tree

docs/CHANGELOG.md

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,10 @@
11
# Changelog
22

3+
## 1.2.0 (Unreleased)
4+
5+
- Added support for partial nav inclusion using anchor syntax (`!include path/to/mkdocs.yml#SectionName`)
6+
- Added unique alias generation for anchored includes to prevent naming conflicts
7+
38
## 1.1.2
49

510
- Dropped official support for Python 3.8

mkdocs_monorepo_plugin/tests/test_plugin.py

Lines changed: 90 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,11 @@
11
#!/usr/bin/env python
22

33
import unittest
4+
import os
5+
import tempfile
46

57
from mkdocs_monorepo_plugin import plugin as p
8+
from mkdocs_monorepo_plugin.parser import IncludeNavLoader
69

710

811
class MockServer:
@@ -40,3 +43,90 @@ def test_plugin_on_serve(self):
4043
server = MockServer()
4144
plugin.on_serve(server, {})
4245
self.assertSetEqual(set(server.watched), {"docs"})
46+
47+
48+
class TestAnchorBasedPartialNavInclusion(unittest.TestCase):
49+
"""Tests for anchor-based partial navigation inclusion feature."""
50+
51+
def setUp(self):
52+
"""Create a temporary directory with test mkdocs.yml files."""
53+
self.test_dir = tempfile.mkdtemp()
54+
self.mkdocs_file = os.path.join(self.test_dir, "mkdocs.yml")
55+
56+
# Create a test mkdocs.yml with multiple nav sections
57+
with open(self.mkdocs_file, 'w') as f:
58+
f.write("""site_name: TestSite
59+
docs_dir: docs
60+
nav:
61+
- Home: index.md
62+
- Guides:
63+
- Getting Started: guides/getting-started.md
64+
- Advanced: guides/advanced.md
65+
- Reference:
66+
- API: reference/api.md
67+
- CLI: reference/cli.md
68+
""")
69+
70+
# Create docs directory
71+
os.makedirs(os.path.join(self.test_dir, "docs"))
72+
73+
def tearDown(self):
74+
"""Clean up temporary directory."""
75+
import shutil
76+
shutil.rmtree(self.test_dir)
77+
78+
def test_anchor_parsing(self):
79+
"""Test that anchors are correctly parsed from include paths."""
80+
config = {"config_file_path": self.mkdocs_file}
81+
82+
# Test with anchor
83+
loader = IncludeNavLoader(config, "mkdocs.yml#Guides")
84+
self.assertEqual(loader.navPath, "mkdocs.yml")
85+
self.assertEqual(loader.navSection, "Guides")
86+
87+
# Test without anchor
88+
loader_no_anchor = IncludeNavLoader(config, "mkdocs.yml")
89+
self.assertEqual(loader_no_anchor.navPath, "mkdocs.yml")
90+
self.assertIsNone(loader_no_anchor.navSection)
91+
92+
def test_extract_nav_section(self):
93+
"""Test that specific nav sections can be extracted."""
94+
config = {"config_file_path": self.mkdocs_file}
95+
loader = IncludeNavLoader(config, "mkdocs.yml#Guides")
96+
loader.read()
97+
98+
# The extracted nav should only contain the Guides section content
99+
nav = loader.getNav()
100+
101+
# Check that we got a list (the content of the Guides section)
102+
self.assertIsInstance(nav, list)
103+
104+
# The nav should contain items from the Guides section
105+
self.assertTrue(len(nav) > 0)
106+
107+
def test_unique_alias_with_section(self):
108+
"""Test that aliases are unique when sections are specified."""
109+
config = {"config_file_path": self.mkdocs_file}
110+
111+
# Load with section anchor
112+
loader_with_section = IncludeNavLoader(config, "mkdocs.yml#Guides")
113+
loader_with_section.read()
114+
alias_with_section = loader_with_section.getAlias()
115+
116+
# Load without section anchor
117+
loader_without_section = IncludeNavLoader(config, "mkdocs.yml")
118+
loader_without_section.read()
119+
alias_without_section = loader_without_section.getAlias()
120+
121+
# Aliases should be different
122+
self.assertNotEqual(alias_with_section, alias_without_section)
123+
self.assertIn("Guides", alias_with_section)
124+
125+
def test_section_not_found(self):
126+
"""Test that an error is raised when the specified section doesn't exist."""
127+
config = {"config_file_path": self.mkdocs_file}
128+
loader = IncludeNavLoader(config, "mkdocs.yml#NonExistentSection")
129+
130+
with self.assertRaises(SystemExit):
131+
loader.read()
132+
loader.getNav()

0 commit comments

Comments
 (0)