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

More jinja block escapes #212

Merged
merged 2 commits into from
Jul 11, 2024
Merged
Show file tree
Hide file tree
Changes from 1 commit
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
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
Loading