|
| 1 | +"""Tests for Issue #251: Report circular includes explicitly instead of as orphaned files. |
| 2 | +
|
| 3 | +When two AsciiDoc files include each other circularly (A includes B, B includes A), |
| 4 | +the validation should report them as "circular_include" errors, not as "orphaned_file" |
| 5 | +warnings. |
| 6 | +""" |
| 7 | + |
| 8 | +from pathlib import Path |
| 9 | + |
| 10 | +import pytest |
| 11 | +from click.testing import CliRunner |
| 12 | + |
| 13 | +from dacli.cli import cli |
| 14 | +from dacli.mcp_app import create_mcp_server |
| 15 | + |
| 16 | + |
| 17 | +@pytest.fixture |
| 18 | +def temp_circular_include(tmp_path: Path) -> Path: |
| 19 | + """Create a temporary directory with two files that include each other.""" |
| 20 | + file_a = tmp_path / "file_a.adoc" |
| 21 | + file_b = tmp_path / "file_b.adoc" |
| 22 | + |
| 23 | + file_a.write_text( |
| 24 | + """= File A |
| 25 | +
|
| 26 | +Some content in A. |
| 27 | +
|
| 28 | +include::file_b.adoc[] |
| 29 | +""", |
| 30 | + encoding="utf-8", |
| 31 | + ) |
| 32 | + |
| 33 | + file_b.write_text( |
| 34 | + """= File B |
| 35 | +
|
| 36 | +Some content in B. |
| 37 | +
|
| 38 | +include::file_a.adoc[] |
| 39 | +""", |
| 40 | + encoding="utf-8", |
| 41 | + ) |
| 42 | + |
| 43 | + return tmp_path |
| 44 | + |
| 45 | + |
| 46 | +@pytest.fixture |
| 47 | +def temp_self_circular_include(tmp_path: Path) -> Path: |
| 48 | + """Create a temporary directory with a file that includes itself.""" |
| 49 | + file_a = tmp_path / "self_ref.adoc" |
| 50 | + file_a.write_text( |
| 51 | + """= Self Referencing Doc |
| 52 | +
|
| 53 | +include::self_ref.adoc[] |
| 54 | +""", |
| 55 | + encoding="utf-8", |
| 56 | + ) |
| 57 | + return tmp_path |
| 58 | + |
| 59 | + |
| 60 | +class TestCircularIncludeValidation: |
| 61 | + """Test that circular includes are reported as errors, not orphaned files.""" |
| 62 | + |
| 63 | + def test_circular_include_reported_as_error( |
| 64 | + self, temp_circular_include: Path |
| 65 | + ): |
| 66 | + """Issue #251: Circular includes should be reported as circular_include errors.""" |
| 67 | + mcp = create_mcp_server(temp_circular_include) |
| 68 | + |
| 69 | + # Access validate_structure tool |
| 70 | + tools = {t.name: t for t in mcp._tool_manager._tools.values()} |
| 71 | + result = tools["validate_structure"].fn() |
| 72 | + |
| 73 | + # Should NOT be valid due to circular include |
| 74 | + assert result["valid"] is False, ( |
| 75 | + f"Expected valid=False for circular include. Result: {result}" |
| 76 | + ) |
| 77 | + |
| 78 | + # Should have a circular_include error |
| 79 | + error_types = [e["type"] for e in result["errors"]] |
| 80 | + assert "circular_include" in error_types, ( |
| 81 | + f"Expected 'circular_include' error. Errors: {result['errors']}" |
| 82 | + ) |
| 83 | + |
| 84 | + def test_circular_include_not_reported_as_orphaned( |
| 85 | + self, temp_circular_include: Path |
| 86 | + ): |
| 87 | + """Issue #251: Files involved in circular includes should NOT be reported as orphaned.""" |
| 88 | + mcp = create_mcp_server(temp_circular_include) |
| 89 | + |
| 90 | + tools = {t.name: t for t in mcp._tool_manager._tools.values()} |
| 91 | + result = tools["validate_structure"].fn() |
| 92 | + |
| 93 | + # Check that no orphaned_file warnings exist for the circular files |
| 94 | + orphaned_warnings = [ |
| 95 | + w for w in result["warnings"] if w["type"] == "orphaned_file" |
| 96 | + ] |
| 97 | + orphaned_paths = [w["path"] for w in orphaned_warnings] |
| 98 | + |
| 99 | + assert "file_a.adoc" not in orphaned_paths, ( |
| 100 | + f"file_a.adoc should not be orphaned. Warnings: {result['warnings']}" |
| 101 | + ) |
| 102 | + assert "file_b.adoc" not in orphaned_paths, ( |
| 103 | + f"file_b.adoc should not be orphaned. Warnings: {result['warnings']}" |
| 104 | + ) |
| 105 | + |
| 106 | + def test_circular_include_error_contains_chain( |
| 107 | + self, temp_circular_include: Path |
| 108 | + ): |
| 109 | + """Circular include error should include the include chain.""" |
| 110 | + mcp = create_mcp_server(temp_circular_include) |
| 111 | + |
| 112 | + tools = {t.name: t for t in mcp._tool_manager._tools.values()} |
| 113 | + result = tools["validate_structure"].fn() |
| 114 | + |
| 115 | + circular_errors = [ |
| 116 | + e for e in result["errors"] if e["type"] == "circular_include" |
| 117 | + ] |
| 118 | + assert len(circular_errors) >= 1 |
| 119 | + |
| 120 | + # The error message should mention the files involved |
| 121 | + error = circular_errors[0] |
| 122 | + assert "message" in error |
| 123 | + assert "circular" in error["message"].lower() or "Circular" in error["message"] |
| 124 | + |
| 125 | + def test_self_circular_include_reported_as_error( |
| 126 | + self, temp_self_circular_include: Path |
| 127 | + ): |
| 128 | + """A file that includes itself should be reported as circular_include error.""" |
| 129 | + mcp = create_mcp_server(temp_self_circular_include) |
| 130 | + |
| 131 | + tools = {t.name: t for t in mcp._tool_manager._tools.values()} |
| 132 | + result = tools["validate_structure"].fn() |
| 133 | + |
| 134 | + # Should NOT be valid |
| 135 | + assert result["valid"] is False |
| 136 | + |
| 137 | + # Should have a circular_include error |
| 138 | + error_types = [e["type"] for e in result["errors"]] |
| 139 | + assert "circular_include" in error_types, ( |
| 140 | + f"Expected 'circular_include' error for self-include. Errors: {result['errors']}" |
| 141 | + ) |
| 142 | + |
| 143 | + # Should NOT have orphaned_file warning for the file |
| 144 | + orphaned_paths = [ |
| 145 | + w["path"] for w in result["warnings"] if w["type"] == "orphaned_file" |
| 146 | + ] |
| 147 | + assert "self_ref.adoc" not in orphaned_paths |
| 148 | + |
| 149 | + |
| 150 | +class TestCLICircularIncludeValidation: |
| 151 | + """Test CLI validate command with circular includes.""" |
| 152 | + |
| 153 | + def test_cli_validate_reports_circular_include( |
| 154 | + self, temp_circular_include: Path |
| 155 | + ): |
| 156 | + """CLI validate should report circular includes as errors.""" |
| 157 | + runner = CliRunner() |
| 158 | + result = runner.invoke( |
| 159 | + cli, |
| 160 | + ["--docs-root", str(temp_circular_include), "validate"], |
| 161 | + ) |
| 162 | + |
| 163 | + assert "circular_include" in result.output |
| 164 | + # Exit code 4 = EXIT_VALIDATION_ERROR (validation found errors) |
| 165 | + assert result.exit_code == 4 |
0 commit comments