Skip to content

Commit 80d40a3

Browse files
committed
skip converting empty yaml objects
1 parent 45f7743 commit 80d40a3

2 files changed

Lines changed: 59 additions & 3 deletions

File tree

iib/workers/tasks/fbc_utils.py

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@
1111

1212
import ruamel.yaml
1313

14-
from iib.exceptions import IIBError
14+
from iib.exceptions import IIBError, ValidationError
1515
from iib.workers.config import get_worker_config
1616
from iib.common.tracing import instrument_tracing
1717

@@ -159,7 +159,10 @@ def enforce_json_config_dir(config_dir: str) -> None:
159159
for dirpath, _, filenames in os.walk(config_dir):
160160
for file in filenames:
161161
in_file = os.path.join(dirpath, file)
162-
if in_file.lower().endswith(".yaml"):
162+
if in_file.lower().endswith(".yaml") or in_file.lower().endswith(".yml"):
163+
if os.path.getsize(in_file) == 0:
164+
raise ValidationError(f"Empty YAML file found: {in_file}")
165+
163166
out_file = os.path.join(dirpath, f"{Path(in_file).stem}.json")
164167
log.debug(f"Converting {in_file} to {out_file}.")
165168
# Make sure the output file doesn't exist before opening in append mode
@@ -169,5 +172,8 @@ def enforce_json_config_dir(config_dir: str) -> None:
169172
with open(in_file, 'r') as yaml_in, open(out_file, 'a') as json_out:
170173
data = yaml.load_all(yaml_in)
171174
for chunk in data:
175+
# Ignore if it is an empty yaml object
176+
if chunk is None:
177+
continue
172178
json.dump(chunk, json_out, default=_serialize_datetime)
173179
os.remove(in_file)

tests/test_workers/test_tasks/test_fbc_utils.py

Lines changed: 51 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@
99
import pytest
1010
import ruamel.yaml
1111

12-
from iib.exceptions import IIBError
12+
from iib.exceptions import IIBError, ValidationError
1313
from iib.workers.config import get_worker_config
1414
from iib.workers.tasks.fbc_utils import (
1515
is_image_fbc,
@@ -300,3 +300,53 @@ def test__serialize_datetime():
300300
def test__serialize_datetime_raise():
301301
with pytest.raises(TypeError, match="Type <class 'int'> is not serializable."):
302302
_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

Comments
 (0)