Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Macros can include other macros #120

Merged
merged 7 commits into from
Sep 15, 2020
Merged
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
24 changes: 19 additions & 5 deletions snowfakery/parse_recipe_yaml.py
Original file line number Diff line number Diff line change
Expand Up @@ -233,30 +233,44 @@ def parse_count_expression(yaml_sobj: Dict, sobj_def: Dict, context: ParseContex


def include_macro(
name: str, context: ParseContext
name: str, context: ParseContext, parent_macros=()
) -> Tuple[List[FieldFactory], List[TemplateLike]]:
macro = context.macros.get(name)
if not macro:
raise exc.DataGenNameError(
f"Cannot find macro named {name}", **context.line_num()
)
parsed_macro = parse_element(
macro, "macro", {}, {"fields": Dict, "friends": List}, context
macro, "macro", {}, {"fields": Dict, "friends": List, "include": str}, context
)
fields = parsed_macro.fields or {}
friends = parsed_macro.friends or []
return parse_fields(fields, context), parse_friends(friends, context)
fields, friends = parse_fields(fields, context), parse_friends(friends, context)
if name in parent_macros:
idx = parent_macros.index(name)
raise exc.DataGenError(
f"Macro `{name}` calls `{'` which calls `'.join(parent_macros[idx+1:])}` which calls `{name}`",
**context.line_num(macro),
)
parse_inclusions(macro, fields, friends, context, parent_macros + (name,))
return fields, friends


def parse_inclusions(
yaml_sobj: Dict, fields: List, friends: List, context: ParseContext
yaml_sobj: Dict,
fields: List,
friends: List,
context: ParseContext,
parent_macros=(),
) -> None:
inclusions: Iterable[str] = [
x.strip() for x in yaml_sobj.get("include", "").split(",")
]
inclusions = filter(None, inclusions)
for inclusion in inclusions:
include_fields, include_friends = include_macro(inclusion, context)
include_fields, include_friends = include_macro(
inclusion, context, parent_macros
)
fields.extend(include_fields)
friends.extend(include_friends)

Expand Down
11 changes: 11 additions & 0 deletions tests/macros-include-macros.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
- macro: foo
fields:
foobar: FOOBAR

- macro: bar
include: foo
fields:
barbar: BARBAR

- object: Bar
include: bar
17 changes: 17 additions & 0 deletions tests/macros-recurse-unbounded.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
- macro: baz
include: bar
fields:
foobar: FOOBAR

- macro: foo
include: baz
fields:
foobar: FOOBAR

- macro: bar
include: foo
fields:
barbar: BARBAR

- object: Bar
include: bar
43 changes: 43 additions & 0 deletions tests/test_macros.py
Original file line number Diff line number Diff line change
@@ -1,8 +1,10 @@
import unittest
from unittest import mock
from io import StringIO
import pytest

from snowfakery.data_generator import generate
import snowfakery.data_gen_exceptions as exc

write_row_path = "snowfakery.output_streams.DebugOutputStream.write_row"

Expand Down Expand Up @@ -77,3 +79,44 @@ def test_friend_includes_and_references(self, write_row):
assert write_row.mock_calls[0] == mock.call("foo", {"id": 1})
assert write_row.mock_calls[1][1][0] == "bar"
assert write_row.mock_calls[1][1][1]["myfoo"].id == 1

@mock.patch("snowfakery.output_streams.DebugOutputStream.write_row")
def test_macros_include_macros(self, write_row):
yaml = """
- macro: foo
fields:
foobar: FOOBAR

- macro: bar
include: foo
fields:
barbar: BARBAR

- object: Bar
include: bar
"""
generate(StringIO(yaml))
assert write_row.mock_calls[0] == mock.call(
"Bar", {"id": 1, "barbar": "BARBAR", "foobar": "FOOBAR"}
)

@mock.patch("snowfakery.output_streams.DebugOutputStream.write_row")
def test_macros_include_themselves(self, write_row):
yaml = """
- macro: foo
include: bar
fields:
foobar: FOOBAR

- macro: bar
include: foo
fields:
barbar: BARBAR

- object: Bar
include: bar
"""
with pytest.raises(exc.DataGenError) as e:
generate(StringIO(yaml))
assert "foo" in str(e.value)
assert "bar" in str(e.value)