Skip to content

Commit

Permalink
Support YAML workflows specs file
Browse files Browse the repository at this point in the history
Closes #20

Took 21 minutes
  • Loading branch information
EpicWink committed Feb 26, 2020
1 parent 755a4a8 commit bdb57ab
Show file tree
Hide file tree
Showing 6 changed files with 46 additions and 6 deletions.
2 changes: 1 addition & 1 deletion Dockerfile
Original file line number Diff line number Diff line change
@@ -1,3 +1,3 @@
FROM python:alpine
RUN pip install seddy coloredlogs
RUN pip install seddy coloredlogs pyyaml
ENTRYPOINT ["seddy"]
7 changes: 3 additions & 4 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -22,10 +22,9 @@ Features:
pip3 install seddy
```

For coloured logging
```bash
pip3 install coloredlogs
```
Install extra packages for further functionality
* Coloured logging: ``coloredlogs``
* YAML workflows specs file: ``pyyaml`` or ``ruamel.yaml``

## Usage
Get the CLI usage
Expand Down
2 changes: 1 addition & 1 deletion setup.cfg
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
[metadata]
name = seddy
version = 0.2.0a0.dev1
version = 0.2.0a0.dev2
url = https://github.com/EpicWink/seddy
project_urls =
Documentation = https://seddy.readthedocs.io/en/latest/
Expand Down
12 changes: 12 additions & 0 deletions src/seddy/_util.py
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,14 @@

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__)
Expand Down Expand Up @@ -160,4 +168,8 @@ def load_workflows(workflows_file: pathlib.Path) -> t.Dict[str, t.Any]:
workflows_text = workflows_file.read_text()
if workflows_file.suffix == ".json":
return json.loads(workflows_text)
elif workflows_file.suffix in (".yml", ".yaml"):
if isinstance(yaml, Exception):
raise yaml
return yaml.safe_load(workflows_text)
raise ValueError("Unknown extension: %s" % workflows_file.suffix)
1 change: 1 addition & 0 deletions tests/requirements.txt
Original file line number Diff line number Diff line change
Expand Up @@ -2,3 +2,4 @@ pytest
pytest-cov
coloredlogs
moto
pyyaml
28 changes: 28 additions & 0 deletions tests/test_util.py
Original file line number Diff line number Diff line change
@@ -1,9 +1,11 @@
"""Test ``seddy._util``."""

import json
from unittest import mock

from seddy import _util as seddy_util
import pytest
import yaml


def test_list_paginated():
Expand Down Expand Up @@ -60,6 +62,32 @@ def test_load_workflows_json(tmp_path, workflows_spec):
assert seddy_util.load_workflows(workflows_file) == workflows_spec


def test_load_workflows_yaml(tmp_path, workflows_spec):
"""Test workflows specs loading from YAML."""
# Build input
workflows_file = tmp_path / "workflows.yml"
workflows_file.write_text(yaml.safe_dump(workflows_spec))

# Run function
assert seddy_util.load_workflows(workflows_file) == 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)

# 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:
seddy_util.load_workflows(workflows_file)
assert e.value == yaml_exc


def test_load_workflows_with_incorrect_suffix(tmp_path, workflows_spec):
"""Test workflows specs loading raises for incorrect suffix."""
# Build input
Expand Down

0 comments on commit bdb57ab

Please sign in to comment.