Skip to content

Commit

Permalink
Merge pull request #212 from dbt-labs/more-jinja-block-escapes
Browse files Browse the repository at this point in the history
More jinja block escapes
  • Loading branch information
dave-connors-3 authored Jul 11, 2024
2 parents 9a1efa1 + 92a500b commit 83c29ee
Show file tree
Hide file tree
Showing 2 changed files with 65 additions and 1 deletion.
6 changes: 5 additions & 1 deletion dbt_meshify/storage/jinja_blocks.py
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,11 @@ def find_block_range(file_content: str, block_type: str, name: str) -> Tuple[int
end_line = None

for match in re.finditer(
r"{%-?\s*" + block_type + r"\s*" + name + r"\s*([(a-zA-Z0-9=,_ \'\")]*)\s*-?%}",
r"{%-?[\s\n]*"
+ block_type
+ r"[\s\n]*"
+ name
+ r"([(a-zA-Z0-9=,_ \[\]{}\"'\s\n)]*)[\s\n]*-?%}",
file_content,
re.MULTILINE,
):
Expand Down
60 changes: 60 additions & 0 deletions tests/unit/test_jinja_blocks.py
Original file line number Diff line number Diff line change
Expand Up @@ -116,6 +116,44 @@
{% endmacro %}
"""

simple_macro_empty_list_defaults = """\
{% macro test_macro(num=[]) %}
{{ num }}
{% endmacro %}
"""

simple_macro_empty_bracket_defaults = """\
{% macro test_macro(num={}) %}
{{ num }}
{% endmacro %}
"""

simple_macro_empty_string_defaults = """\
{% macro test_macro(num='') %}
{{ num }}
{% endmacro %}
"""

simple_macro_new_line_args = """\
{% macro test_macro(
num='',
list=[],
dict={},
int=8,
string='dave'
) %}
{{ num }}
{% endmacro %}
"""


class TestJinjaBlock:
def test_from_file_detects_block_range(self):
Expand Down Expand Up @@ -168,6 +206,28 @@ def test_from_file_detects_block_range_simple_macro_int_defaults(self):
range = JinjaBlock.find_block_range(simple_macro_int_defaults, "macro", "test_macro")
assert range == (2, 58)

def test_from_file_detects_block_range_simple_macro_empty_list_defaults(self):
range = JinjaBlock.find_block_range(
simple_macro_empty_list_defaults, "macro", "test_macro"
)
assert range == (2, 59)

def test_from_file_detects_block_range_simple_macro_empty_bracket_defaults(self):
range = JinjaBlock.find_block_range(
simple_macro_empty_bracket_defaults, "macro", "test_macro"
)
assert range == (2, 59)

def test_from_file_detects_block_range_simple_macro_empty_string_defaults(self):
range = JinjaBlock.find_block_range(
simple_macro_empty_string_defaults, "macro", "test_macro"
)
assert range == (2, 59)

def test_from_file_detects_block_range_simple_macro_new_line_args(self):
range = JinjaBlock.find_block_range(simple_macro_new_line_args, "macro", "test_macro")
assert range == (2, 125)

def test_from_file_extracts_content(self):
content = JinjaBlock.isolate_content(string, 2, 72)
assert (
Expand Down

0 comments on commit 83c29ee

Please sign in to comment.