diff --git a/README.md b/README.md index f0d94b6..00dfb8e 100644 --- a/README.md +++ b/README.md @@ -25,6 +25,7 @@ pip3 install seddy Install extra packages for further functionality * Coloured logging: ``coloredlogs`` * YAML workflows specs file: ``pyyaml`` or ``ruamel.yaml`` +* TOML workflows specs file: ``toml`` ## Usage Get the CLI usage diff --git a/src/seddy/_util.py b/src/seddy/_util.py index 3f1b047..bbfbc0e 100644 --- a/src/seddy/_util.py +++ b/src/seddy/_util.py @@ -16,6 +16,10 @@ from ruamel import yaml except ImportError: yaml = e +try: + import toml +except ImportError as e: # pragma: no cover + toml = e from . import decisions as seddy_decisions @@ -172,4 +176,8 @@ def load_workflows(workflows_file: pathlib.Path) -> t.Dict[str, t.Any]: if isinstance(yaml, Exception): raise yaml return yaml.safe_load(workflows_text) + elif workflows_file.suffix == ".toml": + if isinstance(toml, Exception): + raise toml + return toml.loads(workflows_text) raise ValueError("Unknown extension: %s" % workflows_file.suffix) diff --git a/tests/requirements.txt b/tests/requirements.txt index 8c65f14..f883602 100644 --- a/tests/requirements.txt +++ b/tests/requirements.txt @@ -3,3 +3,4 @@ pytest-cov coloredlogs moto pyyaml +toml diff --git a/tests/test_util.py b/tests/test_util.py index 830a59f..ff876b8 100644 --- a/tests/test_util.py +++ b/tests/test_util.py @@ -6,6 +6,7 @@ from seddy import _util as seddy_util import pytest import yaml +import toml def test_list_paginated(): @@ -88,6 +89,38 @@ def test_load_workflows_yaml_raises(tmp_path, workflows_spec): assert e.value == yaml_exc +def test_load_workflows_toml(tmp_path, workflows_spec): + """Test workflows specs loading from TOML.""" + # Build input + workflows_file = tmp_path / "workflows.yml" + workflows_file.write_text(toml.dumps(workflows_spec)) + print(workflows_file.read_text()) + + # Run function + res = seddy_util.load_workflows(workflows_file) + print(workflows_spec) + print(res) + print(json.dumps(workflows_spec, indent=4)) + print(json.dumps(res, indent=4)) + assert res == workflows_spec + + +def test_load_workflows_toml_raises(tmp_path, workflows_spec): + """Test workflows specs loading from TOML raises when unavailable.""" + # Setup environment + toml_exc = ModuleNotFoundError("toml") + toml_patch = mock.patch.object(seddy_util, "toml", toml_exc) + + # Build input + workflows_file = tmp_path / "workflows.toml" + workflows_file.write_text(toml.dumps(workflows_spec)) + + # Run function + with pytest.raises(ModuleNotFoundError) as e, toml_patch: + seddy_util.load_workflows(workflows_file) + assert e.value == toml_exc + + def test_load_workflows_with_incorrect_suffix(tmp_path, workflows_spec): """Test workflows specs loading raises for incorrect suffix.""" # Build input