|
| 1 | +"""Tests for dotted filenames producing unique paths (Issue #266).""" |
| 2 | + |
| 3 | +from dacli.markdown_parser import MarkdownStructureParser |
| 4 | +from dacli.structure_index import StructureIndex |
| 5 | + |
| 6 | + |
| 7 | +class TestDottedFilenames: |
| 8 | + """Files with dots in names (e.g. version numbers) must have unique paths.""" |
| 9 | + |
| 10 | + def test_version_numbered_files_have_unique_paths(self, tmp_path): |
| 11 | + """DACLI_TEST_RESULTS_v0.4.27.md and v0.4.28.md must not collide.""" |
| 12 | + f1 = tmp_path / "RESULTS_v0.4.27.md" |
| 13 | + f2 = tmp_path / "RESULTS_v0.4.28.md" |
| 14 | + f1.write_text("# Results v0.4.27\n\nContent.\n") |
| 15 | + f2.write_text("# Results v0.4.28\n\nContent.\n") |
| 16 | + |
| 17 | + parser = MarkdownStructureParser(base_path=tmp_path) |
| 18 | + doc1 = parser.parse_file(f1) |
| 19 | + doc2 = parser.parse_file(f2) |
| 20 | + |
| 21 | + index = StructureIndex() |
| 22 | + warnings = index.build_from_documents([doc1, doc2]) |
| 23 | + |
| 24 | + assert len(warnings) == 0, f"Unexpected warnings: {warnings}" |
| 25 | + |
| 26 | + structure = index.get_structure() |
| 27 | + paths = [s["path"] for s in structure["sections"]] |
| 28 | + assert len(paths) == len(set(paths)), f"Duplicate paths found: {paths}" |
| 29 | + |
| 30 | + def test_cli_context_passes_base_path_to_markdown_parser(self, tmp_path): |
| 31 | + """CliContext must pass docs_root as base_path to MarkdownStructureParser.""" |
| 32 | + from dacli.cli import CliContext |
| 33 | + |
| 34 | + f1 = tmp_path / "test_v1.2.3.md" |
| 35 | + f1.write_text("# Test v1.2.3\n") |
| 36 | + |
| 37 | + ctx = CliContext( |
| 38 | + docs_root=tmp_path, |
| 39 | + output_format="json", |
| 40 | + pretty=False, |
| 41 | + ) |
| 42 | + # The markdown parser should have base_path set |
| 43 | + assert ctx.markdown_parser.base_path == tmp_path |
| 44 | + |
| 45 | + def test_file_prefix_without_base_path_strips_only_known_extensions(self, tmp_path): |
| 46 | + """Without base_path, only .md extension should be stripped, not version dots.""" |
| 47 | + f1 = tmp_path / "report_v2.1.5.md" |
| 48 | + f1.write_text("# Report\n") |
| 49 | + |
| 50 | + # BUG #266: Without base_path, Path(stem).with_suffix("") strips ".5" |
| 51 | + parser = MarkdownStructureParser() # No base_path! |
| 52 | + doc = parser.parse_file(f1) |
| 53 | + |
| 54 | + # The path should preserve the full version number |
| 55 | + assert doc.sections[0].path == "report_v2.1.5" |
| 56 | + |
| 57 | + def test_get_file_prefix_preserves_version_dots(self, tmp_path): |
| 58 | + """_get_file_prefix must not strip version-like suffixes.""" |
| 59 | + parser = MarkdownStructureParser(base_path=tmp_path) |
| 60 | + prefix = parser._get_file_prefix(tmp_path / "data_v3.2.1.md") |
| 61 | + assert prefix == "data_v3.2.1" |
| 62 | + |
| 63 | + def test_subdirectory_file_with_dots(self, tmp_path): |
| 64 | + """Files with dots in subdirectories also get correct paths.""" |
| 65 | + sub = tmp_path / "reports" |
| 66 | + sub.mkdir() |
| 67 | + f1 = sub / "sprint_2.0.1.md" |
| 68 | + f1.write_text("# Sprint 2.0.1\n\nNotes.\n") |
| 69 | + |
| 70 | + parser = MarkdownStructureParser(base_path=tmp_path) |
| 71 | + doc = parser.parse_file(f1) |
| 72 | + |
| 73 | + assert doc.sections[0].path == "reports/sprint_2.0.1" |
| 74 | + |
| 75 | + |
| 76 | +class TestDottedFilenamesAsciiDoc: |
| 77 | + """AsciiDoc files with dots in names must also have unique paths.""" |
| 78 | + |
| 79 | + def test_asciidoc_file_with_version_dots(self, tmp_path): |
| 80 | + """AsciiDoc _get_file_prefix must preserve version dots.""" |
| 81 | + from dacli.asciidoc_parser import AsciidocStructureParser |
| 82 | + |
| 83 | + f1 = tmp_path / "release_v1.2.3.adoc" |
| 84 | + f1.write_text("= Release v1.2.3\n\nContent.\n") |
| 85 | + |
| 86 | + parser = AsciidocStructureParser(base_path=tmp_path) |
| 87 | + doc = parser.parse_file(f1) |
| 88 | + |
| 89 | + assert doc.sections[0].path == "release_v1.2.3" |
0 commit comments