|
9 | 9 | import pytest |
10 | 10 | import ruamel.yaml |
11 | 11 |
|
12 | | -from iib.exceptions import IIBError |
| 12 | +from iib.exceptions import IIBError, ValidationError |
13 | 13 | from iib.workers.config import get_worker_config |
14 | 14 | from iib.workers.tasks.fbc_utils import ( |
15 | 15 | is_image_fbc, |
@@ -300,3 +300,53 @@ def test__serialize_datetime(): |
300 | 300 | def test__serialize_datetime_raise(): |
301 | 301 | with pytest.raises(TypeError, match="Type <class 'int'> is not serializable."): |
302 | 302 | _serialize_datetime(2025) |
| 303 | + |
| 304 | + |
| 305 | +def test_enforce_json_config_dir_skips_empty_yaml_documents(tmpdir): |
| 306 | + yaml_with_empty_doc = """\ |
| 307 | + --- |
| 308 | + foo: bar |
| 309 | + --- |
| 310 | + --- |
| 311 | + another: data |
| 312 | + """ |
| 313 | + |
| 314 | + expected_result = '{"foo": "bar"}{"another": "data"}' |
| 315 | + |
| 316 | + input_file = os.path.join(tmpdir, "test_file.yaml") |
| 317 | + output_file = os.path.join(tmpdir, "test_file.json") |
| 318 | + with open(input_file, 'w') as w: |
| 319 | + w.write(dedent(yaml_with_empty_doc)) |
| 320 | + |
| 321 | + enforce_json_config_dir(tmpdir) |
| 322 | + |
| 323 | + with open(output_file, 'r') as f: |
| 324 | + assert f.read() == expected_result |
| 325 | + |
| 326 | + |
| 327 | +def test_enforce_json_config_dir_raises_on_empty_yaml(tmpdir): |
| 328 | + """Ensure a 0-byte YAML file raises a ValidationError.""" |
| 329 | + empty_yaml = os.path.join(tmpdir, "empty.yaml") |
| 330 | + |
| 331 | + # Create a 0-byte file |
| 332 | + open(empty_yaml, 'w').close() |
| 333 | + |
| 334 | + with pytest.raises(ValidationError) as exc_info: |
| 335 | + enforce_json_config_dir(str(tmpdir)) |
| 336 | + |
| 337 | + assert "Empty YAML file found" in str(exc_info.value) |
| 338 | + assert "empty.yaml" in str(exc_info.value) |
| 339 | + |
| 340 | + |
| 341 | +def test_enforce_json_config_dir_handles_yml_extension(tmpdir): |
| 342 | + """Ensure files with the .yml extension are also processed.""" |
| 343 | + input_file = os.path.join(tmpdir, "test_file.yml") |
| 344 | + output_file = os.path.join(tmpdir, "test_file.json") |
| 345 | + |
| 346 | + with open(input_file, 'w') as w: |
| 347 | + w.write("key: value") |
| 348 | + |
| 349 | + enforce_json_config_dir(str(tmpdir)) |
| 350 | + |
| 351 | + assert os.path.exists(output_file) |
| 352 | + assert not os.path.exists(input_file) |
0 commit comments