|
5 | 5 | from pathlib import Path |
6 | 6 |
|
7 | 7 | from aws_doc_sdk_examples_tools import snippets |
| 8 | +from aws_doc_sdk_examples_tools.fs import RecordFs |
| 9 | +from aws_doc_sdk_examples_tools.metadata import Example, Language, Version, Excerpt |
| 10 | +from aws_doc_sdk_examples_tools.metadata_errors import MetadataErrors |
8 | 11 |
|
9 | 12 |
|
10 | 13 | @pytest.mark.parametrize( |
@@ -102,3 +105,159 @@ def test_strip_spdx_header(): |
102 | 105 | ) |
103 | 106 |
|
104 | 107 | assert [] == snippets.strip_spdx_header([]) |
| 108 | + |
| 109 | + |
| 110 | +class TestFindSnippetsFs: |
| 111 | + """Test find_snippets with filesystem abstraction.""" |
| 112 | + |
| 113 | + def test_find_snippets_with_recordfs(self): |
| 114 | + """Test find_snippets using RecordFs.""" |
| 115 | + fs = RecordFs( |
| 116 | + { |
| 117 | + Path( |
| 118 | + "/project/test.py" |
| 119 | + ): """# snippet-start:[example.hello] |
| 120 | +def hello(): |
| 121 | + print("Hello, World!") |
| 122 | +# snippet-end:[example.hello] |
| 123 | +""" |
| 124 | + } |
| 125 | + ) |
| 126 | + |
| 127 | + snippet_dict, errors = snippets.find_snippets( |
| 128 | + Path("/project/test.py"), "", fs=fs |
| 129 | + ) |
| 130 | + |
| 131 | + assert len(errors) == 0 |
| 132 | + assert len(snippet_dict) == 1 |
| 133 | + assert "example.hello" in snippet_dict |
| 134 | + snippet = snippet_dict["example.hello"] |
| 135 | + assert snippet.id == "example.hello" |
| 136 | + assert "def hello():" in snippet.code |
| 137 | + |
| 138 | + def test_find_snippets_missing_file_graceful(self): |
| 139 | + """Test find_snippets behavior with missing files.""" |
| 140 | + fs = RecordFs({}) |
| 141 | + |
| 142 | + snippet_dict, errors = snippets.find_snippets( |
| 143 | + Path("/project/missing.py"), "", fs=fs |
| 144 | + ) |
| 145 | + |
| 146 | + # Missing files generate errors (not handled gracefully) |
| 147 | + assert len(snippet_dict) == 0 |
| 148 | + assert len(errors) == 1 # Should have a FileReadError |
| 149 | + |
| 150 | + |
| 151 | +class TestCollectSnippetsFs: |
| 152 | + """Test collect_snippets with filesystem abstraction.""" |
| 153 | + |
| 154 | + def test_collect_snippets_with_recordfs(self): |
| 155 | + """Test collect_snippets using RecordFs.""" |
| 156 | + fs = RecordFs( |
| 157 | + { |
| 158 | + Path( |
| 159 | + "/project/src/file1.py" |
| 160 | + ): """# snippet-start:[example1] |
| 161 | +def example1(): |
| 162 | + pass |
| 163 | +# snippet-end:[example1] |
| 164 | +""", |
| 165 | + Path( |
| 166 | + "/project/src/file2.py" |
| 167 | + ): """# snippet-start:[example2] |
| 168 | +def example2(): |
| 169 | + pass |
| 170 | +# snippet-end:[example2] |
| 171 | +""", |
| 172 | + } |
| 173 | + ) |
| 174 | + |
| 175 | + snippet_dict, errors = snippets.collect_snippets(Path("/project/src"), fs=fs) |
| 176 | + |
| 177 | + assert len(errors) == 0 |
| 178 | + assert len(snippet_dict) == 2 |
| 179 | + assert "example1" in snippet_dict |
| 180 | + assert "example2" in snippet_dict |
| 181 | + |
| 182 | + |
| 183 | +class TestCollectSnippetFilesFs: |
| 184 | + """Test collect_snippet_files with filesystem abstraction.""" |
| 185 | + |
| 186 | + def test_collect_snippet_files_with_recordfs(self): |
| 187 | + """Test collect_snippet_files using RecordFs.""" |
| 188 | + fs = RecordFs({Path("/project/example.py"): "print('Hello, World!')\n"}) |
| 189 | + |
| 190 | + example = Example( |
| 191 | + id="test_example", |
| 192 | + file=None, |
| 193 | + languages={ |
| 194 | + "python": Language( |
| 195 | + name="python", |
| 196 | + property="python", |
| 197 | + versions=[ |
| 198 | + Version( |
| 199 | + sdk_version="3", |
| 200 | + excerpts=[ |
| 201 | + Excerpt( |
| 202 | + description="Test excerpt", |
| 203 | + snippet_tags=[], |
| 204 | + snippet_files=["example.py"], |
| 205 | + ) |
| 206 | + ], |
| 207 | + ) |
| 208 | + ], |
| 209 | + ) |
| 210 | + }, |
| 211 | + ) |
| 212 | + |
| 213 | + snippet_dict = {} |
| 214 | + errors = MetadataErrors() |
| 215 | + |
| 216 | + snippets.collect_snippet_files( |
| 217 | + [example], snippet_dict, "", errors, Path("/project"), fs=fs |
| 218 | + ) |
| 219 | + |
| 220 | + assert len(errors) == 0 |
| 221 | + assert len(snippet_dict) == 1 |
| 222 | + assert "example.py" in snippet_dict |
| 223 | + snippet = snippet_dict["example.py"] |
| 224 | + assert snippet.file == "example.py" |
| 225 | + assert "Hello, World!" in snippet.code |
| 226 | + |
| 227 | + def test_collect_snippet_files_missing_file_error(self): |
| 228 | + """Test collect_snippet_files properly reports missing files as errors.""" |
| 229 | + fs = RecordFs({}) # Empty filesystem |
| 230 | + |
| 231 | + example = Example( |
| 232 | + id="test_example", |
| 233 | + file=None, |
| 234 | + languages={ |
| 235 | + "python": Language( |
| 236 | + name="python", |
| 237 | + property="python", |
| 238 | + versions=[ |
| 239 | + Version( |
| 240 | + sdk_version="3", |
| 241 | + excerpts=[ |
| 242 | + Excerpt( |
| 243 | + description="Test excerpt", |
| 244 | + snippet_tags=[], |
| 245 | + snippet_files=["missing.py"], |
| 246 | + ) |
| 247 | + ], |
| 248 | + ) |
| 249 | + ], |
| 250 | + ) |
| 251 | + }, |
| 252 | + ) |
| 253 | + |
| 254 | + snippet_dict = {} |
| 255 | + errors = MetadataErrors() |
| 256 | + |
| 257 | + snippets.collect_snippet_files( |
| 258 | + [example], snippet_dict, "", errors, Path("/project"), fs=fs |
| 259 | + ) |
| 260 | + |
| 261 | + # Missing snippet files should generate errors (unlike find_snippets) |
| 262 | + assert len(errors) == 1 |
| 263 | + assert len(snippet_dict) == 0 |
0 commit comments