diff --git a/setup.cfg b/setup.cfg index 22d70c1..84561a3 100644 --- a/setup.cfg +++ b/setup.cfg @@ -1,6 +1,6 @@ [metadata] name = seddy -version = 0.2.0a0.dev2 +version = 0.2.0a0.dev3 url = https://github.com/EpicWink/seddy project_urls = Documentation = https://seddy.readthedocs.io/en/latest/ diff --git a/src/seddy/_util.py b/src/seddy/_util.py index c4b8265..9c4cc09 100644 --- a/src/seddy/_util.py +++ b/src/seddy/_util.py @@ -9,14 +9,6 @@ import boto3 -try: - import yaml -except ImportError as e: # pragma: no cover - try: - from ruamel import yaml - except ImportError: - yaml = e - from . import decisions as seddy_decisions logger = lg.getLogger(__package__) @@ -174,7 +166,12 @@ def load_workflows(workflows_file: pathlib.Path) -> t.Dict[str, t.Any]: if workflows_file.suffix == ".json": return json.loads(workflows_text) elif workflows_file.suffix in (".yml", ".yaml"): - if isinstance(yaml, Exception): - raise yaml + try: + import yaml + except ImportError as e: + try: + from ruamel import yaml + except ImportError as er: + raise e from er return yaml.safe_load(workflows_text) raise ValueError("Unknown extension: %s" % workflows_file.suffix) diff --git a/tests/test_util.py b/tests/test_util.py index 830a59f..19ba15e 100644 --- a/tests/test_util.py +++ b/tests/test_util.py @@ -1,5 +1,6 @@ """Test ``seddy._util``.""" +import sys import json from unittest import mock @@ -75,17 +76,15 @@ def test_load_workflows_yaml(tmp_path, workflows_spec): def test_load_workflows_yaml_raises(tmp_path, workflows_spec): """Test workflows specs loading from YAML raises when unavailable.""" # Setup environment - yaml_exc = ModuleNotFoundError("yaml") - yaml_patch = mock.patch.object(seddy_util, "yaml", yaml_exc) + yaml_patch = mock.patch.dict(sys.modules, {"yaml": None, "ruamel.yaml": None}) # Build input workflows_file = tmp_path / "workflows.yml" workflows_file.write_text(yaml.safe_dump(workflows_spec)) # Run function - with pytest.raises(ModuleNotFoundError) as e, yaml_patch: + with pytest.raises(ModuleNotFoundError), yaml_patch: seddy_util.load_workflows(workflows_file) - assert e.value == yaml_exc def test_load_workflows_with_incorrect_suffix(tmp_path, workflows_spec):