|
| 1 | +""" |
| 2 | +Tests for circular <include> detection in pdd/preprocess.py (Issue #521). |
| 3 | +
|
| 4 | +The preprocessor has no cycle detection — circular includes recurse ~82 times |
| 5 | +until Python's recursion limit, then the broad `except Exception` swallows |
| 6 | +the RecursionError and returns corrupted output with exit code 0. |
| 7 | +
|
| 8 | +These tests verify that circular includes produce an error (exception or |
| 9 | +error marker in output), NOT silently corrupted content. |
| 10 | +""" |
| 11 | + |
| 12 | +import os |
| 13 | +import pytest |
| 14 | +from unittest.mock import patch, mock_open, MagicMock |
| 15 | +from pdd.preprocess import preprocess |
| 16 | + |
| 17 | +# Store original so we can restore |
| 18 | +_original_pdd_path = os.environ.get('PDD_PATH') |
| 19 | + |
| 20 | + |
| 21 | +def set_pdd_path(path: str) -> None: |
| 22 | + os.environ['PDD_PATH'] = path |
| 23 | + |
| 24 | + |
| 25 | +def _make_mock_open(file_map: dict): |
| 26 | + """Create a mock open that returns content based on filename.""" |
| 27 | + def side_effect(file_name, *args, **kwargs): |
| 28 | + mock_file = MagicMock() |
| 29 | + for key, content in file_map.items(): |
| 30 | + if key in str(file_name): |
| 31 | + mock_file.read.return_value = content |
| 32 | + mock_file.__enter__ = lambda s: s |
| 33 | + mock_file.__exit__ = MagicMock(return_value=False) |
| 34 | + return mock_file |
| 35 | + raise FileNotFoundError(f"No mock for {file_name}") |
| 36 | + return side_effect |
| 37 | + |
| 38 | + |
| 39 | +class TestCircularIncludes: |
| 40 | + """Issue #521: Circular <include> tags must be detected, not silently expanded.""" |
| 41 | + |
| 42 | + def setup_method(self): |
| 43 | + set_pdd_path('/mock/path') |
| 44 | + |
| 45 | + def teardown_method(self): |
| 46 | + if _original_pdd_path is not None: |
| 47 | + os.environ['PDD_PATH'] = _original_pdd_path |
| 48 | + elif 'PDD_PATH' in os.environ: |
| 49 | + del os.environ['PDD_PATH'] |
| 50 | + |
| 51 | + def test_circular_xml_includes_must_error(self): |
| 52 | + """A→B→A circular include must raise or return error, not silently corrupt.""" |
| 53 | + file_map = { |
| 54 | + 'a.txt': 'Hello\n<include>b.txt</include>', |
| 55 | + 'b.txt': 'World\n<include>a.txt</include>', |
| 56 | + } |
| 57 | + with patch('builtins.open', mock_open()) as m: |
| 58 | + m.side_effect = _make_mock_open(file_map) |
| 59 | + |
| 60 | + # The bug: preprocess silently returns corrupted output with |
| 61 | + # "Hello" repeated ~82 times. A correct implementation must |
| 62 | + # either raise an error OR return output containing an error marker. |
| 63 | + try: |
| 64 | + result = preprocess( |
| 65 | + '<include>a.txt</include>', |
| 66 | + recursive=True, |
| 67 | + double_curly_brackets=False, |
| 68 | + ) |
| 69 | + except (RecursionError, ValueError, RuntimeError): |
| 70 | + # Any of these exceptions is acceptable — cycle was detected |
| 71 | + return |
| 72 | + |
| 73 | + # If no exception, the output must NOT contain duplicated content. |
| 74 | + # The buggy behavior produces "Hello" 82+ times. |
| 75 | + hello_count = result.count('Hello') |
| 76 | + world_count = result.count('World') |
| 77 | + assert hello_count <= 2 and world_count <= 2, ( |
| 78 | + f"Circular include silently produced corrupted output: " |
| 79 | + f"'Hello' appeared {hello_count} times, 'World' appeared {world_count} times. " |
| 80 | + f"Expected an error or at most 2 occurrences each." |
| 81 | + ) |
| 82 | + |
| 83 | + def test_circular_backtick_includes_must_error(self): |
| 84 | + """Circular backtick-style includes must also be detected.""" |
| 85 | + file_map = { |
| 86 | + 'x.txt': 'Foo\n```<y.txt>```', |
| 87 | + 'y.txt': 'Bar\n```<x.txt>```', |
| 88 | + } |
| 89 | + with patch('builtins.open', mock_open()) as m: |
| 90 | + m.side_effect = _make_mock_open(file_map) |
| 91 | + |
| 92 | + try: |
| 93 | + result = preprocess( |
| 94 | + '```<x.txt>```', |
| 95 | + recursive=True, |
| 96 | + double_curly_brackets=False, |
| 97 | + ) |
| 98 | + except (RecursionError, ValueError, RuntimeError): |
| 99 | + return |
| 100 | + |
| 101 | + foo_count = result.count('Foo') |
| 102 | + bar_count = result.count('Bar') |
| 103 | + assert foo_count <= 2 and bar_count <= 2, ( |
| 104 | + f"Circular backtick include silently produced corrupted output: " |
| 105 | + f"'Foo' appeared {foo_count} times, 'Bar' appeared {bar_count} times." |
| 106 | + ) |
| 107 | + |
| 108 | + def test_self_referencing_include_must_error(self): |
| 109 | + """A file that includes itself must be detected as circular.""" |
| 110 | + file_map = { |
| 111 | + 'self.txt': 'Content\n<include>self.txt</include>', |
| 112 | + } |
| 113 | + with patch('builtins.open', mock_open()) as m: |
| 114 | + m.side_effect = _make_mock_open(file_map) |
| 115 | + |
| 116 | + try: |
| 117 | + result = preprocess( |
| 118 | + '<include>self.txt</include>', |
| 119 | + recursive=True, |
| 120 | + double_curly_brackets=False, |
| 121 | + ) |
| 122 | + except (RecursionError, ValueError, RuntimeError): |
| 123 | + return |
| 124 | + |
| 125 | + content_count = result.count('Content') |
| 126 | + assert content_count <= 2, ( |
| 127 | + f"Self-referencing include silently produced corrupted output: " |
| 128 | + f"'Content' appeared {content_count} times." |
| 129 | + ) |
| 130 | + |
| 131 | + def test_three_file_cycle_must_error(self): |
| 132 | + """A→B→C→A three-file cycle must be detected.""" |
| 133 | + file_map = { |
| 134 | + 'a.txt': 'AAA\n<include>b.txt</include>', |
| 135 | + 'b.txt': 'BBB\n<include>c.txt</include>', |
| 136 | + 'c.txt': 'CCC\n<include>a.txt</include>', |
| 137 | + } |
| 138 | + with patch('builtins.open', mock_open()) as m: |
| 139 | + m.side_effect = _make_mock_open(file_map) |
| 140 | + |
| 141 | + try: |
| 142 | + result = preprocess( |
| 143 | + '<include>a.txt</include>', |
| 144 | + recursive=True, |
| 145 | + double_curly_brackets=False, |
| 146 | + ) |
| 147 | + except (RecursionError, ValueError, RuntimeError): |
| 148 | + return |
| 149 | + |
| 150 | + aaa_count = result.count('AAA') |
| 151 | + assert aaa_count <= 2, ( |
| 152 | + f"Three-file circular include silently produced corrupted output: " |
| 153 | + f"'AAA' appeared {aaa_count} times." |
| 154 | + ) |
| 155 | + |
| 156 | + def test_non_circular_recursive_includes_still_work(self): |
| 157 | + """Non-circular recursive includes (A→B→C, no cycle) must still work.""" |
| 158 | + file_map = { |
| 159 | + 'top.txt': 'Top\n<include>mid.txt</include>', |
| 160 | + 'mid.txt': 'Mid\n<include>leaf.txt</include>', |
| 161 | + 'leaf.txt': 'Leaf', |
| 162 | + } |
| 163 | + with patch('builtins.open', mock_open()) as m: |
| 164 | + m.side_effect = _make_mock_open(file_map) |
| 165 | + |
| 166 | + result = preprocess( |
| 167 | + '<include>top.txt</include>', |
| 168 | + recursive=True, |
| 169 | + double_curly_brackets=False, |
| 170 | + ) |
| 171 | + |
| 172 | + assert 'Top' in result |
| 173 | + assert 'Mid' in result |
| 174 | + assert 'Leaf' in result |
| 175 | + |
| 176 | + def test_diamond_includes_not_falsely_flagged(self): |
| 177 | + """Diamond pattern (A→B, A→C, B→D, C→D) is NOT circular and must work.""" |
| 178 | + file_map = { |
| 179 | + 'a.txt': '<include>b.txt</include>\n<include>c.txt</include>', |
| 180 | + 'b.txt': 'B\n<include>d.txt</include>', |
| 181 | + 'c.txt': 'C\n<include>d.txt</include>', |
| 182 | + 'd.txt': 'Shared', |
| 183 | + } |
| 184 | + with patch('builtins.open', mock_open()) as m: |
| 185 | + m.side_effect = _make_mock_open(file_map) |
| 186 | + |
| 187 | + result = preprocess( |
| 188 | + '<include>a.txt</include>', |
| 189 | + recursive=True, |
| 190 | + double_curly_brackets=False, |
| 191 | + ) |
| 192 | + |
| 193 | + assert 'B' in result |
| 194 | + assert 'C' in result |
| 195 | + # D is included twice (via B and via C) — that's fine, not circular |
| 196 | + assert result.count('Shared') == 2 |
0 commit comments