|
1 | 1 | #!/usr/bin/env python |
2 | 2 |
|
3 | 3 | import unittest |
| 4 | +import os |
| 5 | +import tempfile |
4 | 6 |
|
5 | 7 | from mkdocs_monorepo_plugin import plugin as p |
| 8 | +from mkdocs_monorepo_plugin.parser import IncludeNavLoader |
6 | 9 |
|
7 | 10 |
|
8 | 11 | class MockServer: |
@@ -40,3 +43,90 @@ def test_plugin_on_serve(self): |
40 | 43 | server = MockServer() |
41 | 44 | plugin.on_serve(server, {}) |
42 | 45 | 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