Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
9 changes: 8 additions & 1 deletion iib/workers/tasks/fbc_utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -154,12 +154,16 @@ def enforce_json_config_dir(config_dir: str) -> None:
It will walk recursively and convert any YAML files to the JSON format.

:param str config_dir: The config dir to walk recursively converting any YAML to JSON.
:raises IIBError: If the yaml content is empty.
"""
log.info("Enforcing JSON content on config_dir: %s", config_dir)
for dirpath, _, filenames in os.walk(config_dir):
for file in filenames:
in_file = os.path.join(dirpath, file)
if in_file.lower().endswith(".yaml"):
if in_file.lower().endswith(".yaml") or in_file.lower().endswith(".yml"):
Comment thread
ashwgit marked this conversation as resolved.
Comment thread
ashwgit marked this conversation as resolved.
if os.path.getsize(in_file) == 0:
Comment thread
ashwgit marked this conversation as resolved.
raise IIBError(f"Empty YAML file found: {in_file}")

Comment thread
ashwgit marked this conversation as resolved.
out_file = os.path.join(dirpath, f"{Path(in_file).stem}.json")
log.debug(f"Converting {in_file} to {out_file}.")
# Make sure the output file doesn't exist before opening in append mode
Expand All @@ -169,5 +173,8 @@ def enforce_json_config_dir(config_dir: str) -> None:
with open(in_file, 'r') as yaml_in, open(out_file, 'a') as json_out:
data = yaml.load_all(yaml_in)
for chunk in data:
# Ignore if it is an empty yaml object
Comment thread
ashwgit marked this conversation as resolved.
Comment thread
ashwgit marked this conversation as resolved.
Comment thread
ashwgit marked this conversation as resolved.
if chunk is None:
continue
json.dump(chunk, json_out, default=_serialize_datetime)
os.remove(in_file)
50 changes: 50 additions & 0 deletions tests/test_workers/test_tasks/test_fbc_utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -300,3 +300,53 @@ def test__serialize_datetime():
def test__serialize_datetime_raise():
with pytest.raises(TypeError, match="Type <class 'int'> is not serializable."):
_serialize_datetime(2025)

Comment thread
ashwgit marked this conversation as resolved.
Comment thread
ashwgit marked this conversation as resolved.

def test_enforce_json_config_dir_skips_empty_yaml_documents(tmpdir):
yaml_with_empty_doc = """\
---
foo: bar
---
---
another: data
"""

expected_result = '{"foo": "bar"}{"another": "data"}'

input_file = os.path.join(tmpdir, "test_file.yaml")
output_file = os.path.join(tmpdir, "test_file.json")
with open(input_file, 'w') as w:
w.write(dedent(yaml_with_empty_doc))

enforce_json_config_dir(tmpdir)

with open(output_file, 'r') as f:
assert f.read() == expected_result


def test_enforce_json_config_dir_raises_on_empty_yaml(tmpdir):
"""Ensure a 0-byte YAML file raises a IIBError."""
empty_yaml = os.path.join(tmpdir, "empty.yaml")

# Create a 0-byte file
open(empty_yaml, 'w').close()

with pytest.raises(IIBError) as exc_info:
enforce_json_config_dir(str(tmpdir))

assert "Empty YAML file found" in str(exc_info.value)
assert "empty.yaml" in str(exc_info.value)


def test_enforce_json_config_dir_handles_yml_extension(tmpdir):
"""Ensure files with the .yml extension are also processed."""
input_file = os.path.join(tmpdir, "test_file.yml")
output_file = os.path.join(tmpdir, "test_file.json")

with open(input_file, 'w') as w:
w.write("key: value")

enforce_json_config_dir(str(tmpdir))

assert os.path.exists(output_file)
assert not os.path.exists(input_file)
Loading