Skip to content

Commit

Permalink
provider: allow yaml hook to read documents as lists
Browse files Browse the repository at this point in the history
  • Loading branch information
robcxyz committed Dec 4, 2022
1 parent 7c73c31 commit f2fa091
Show file tree
Hide file tree
Showing 3 changed files with 17 additions and 2 deletions.
11 changes: 9 additions & 2 deletions tackle/providers/yaml/hooks/yamls.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
from ruamel.yaml import YAML
from ruamel.yaml.composer import ComposerError
import os
from typing import Union

Expand Down Expand Up @@ -33,8 +34,14 @@ def exec(self) -> Union[dict, str]:
return self.path

else:
with open(self.path, 'r') as f:
data = yaml.load(f)
try:
with open(self.path, 'r') as f:
data = yaml.load(f)
except ComposerError:
data = []
with open(self.path, 'r') as f:
for doc in yaml.load_all(f):
data.append(doc)

# TODO: Improve this - https://github.com/robcxyz/tackle/issues/56
import json
Expand Down
2 changes: 2 additions & 0 deletions tackle/providers/yaml/tests/list_yaml_read.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@

y->: yaml list_yaml.yaml
6 changes: 6 additions & 0 deletions tackle/providers/yaml/tests/test_provider_system_yaml.py
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,12 @@ def test_provider_system_hook_yaml_read(change_dir, clean_outputs):
assert read['stuff'] == 'things'


def test_provider_system_hook_list_yaml(change_dir, clean_outputs):
output = tackle('list_yaml_read.yaml', no_input=True)

assert len(output['y']) > 1


def test_provider_system_hook_yaml_write(change_dir, clean_outputs):
tackle('write.yaml', no_input=True)
yaml = YAML()
Expand Down

0 comments on commit f2fa091

Please sign in to comment.