diff --git a/tackle/models.py b/tackle/models.py index 55165387a..8a6e4e5df 100644 --- a/tackle/models.py +++ b/tackle/models.py @@ -38,8 +38,8 @@ class BaseContext(BaseModel): verbose: bool = False input_context: Union[dict, list] = None - public_context: Union[dict, list] = None - private_context: Union[dict, list] = None + public_context: Union[dict, list] = {} + private_context: Union[dict, list] = {} temporary_context: Union[dict, list] = None existing_context: dict = {} override_context: dict = Field({}, description="A dict to override inputs with.") diff --git a/tackle/render.py b/tackle/render.py index ad19ccad7..ca38e02d6 100644 --- a/tackle/render.py +++ b/tackle/render.py @@ -215,14 +215,33 @@ def render_string(context: 'Context', raw: str): f"doesn't have arguments. Consider changing." ) - # Check for ambiguous globals like `namespace` tackle/issues/19 + # Check for ambiguous globals like `namespace` + # TODO: tackle/issues/19 -> This is hacky af... Need to redo this with visitor match = re.search(r'\', rendered_template) if match: ambiguous_key = match.group(1).split('.')[-1].lower() - if ambiguous_key in context.public_context: - rendered_template = context.public_context[ambiguous_key] - elif match.group(1) in context.existing_context: - rendered_template = context.existing_context[ambiguous_key] + if context.temporary_context and ambiguous_key in context.temporary_context: + ambiguous_key_rendered = context.temporary_context[ambiguous_key] + elif ambiguous_key in context.public_context: + ambiguous_key_rendered = context.public_context[ambiguous_key] + elif context.private_context and ambiguous_key in context.private_context: + ambiguous_key_rendered = context.private_context[ambiguous_key] + elif context.existing_context and ambiguous_key in context.existing_context: + ambiguous_key_rendered = context.existing_context[ambiguous_key] + else: + raise UnknownTemplateVariableException( + f"Unknown ambiguous key {ambiguous_key}. Tracking issue at " + f"tackle/issues/19", + context=context, + ) from None + if isinstance(ambiguous_key_rendered, str): + rendered_template = ( + rendered_template[: match.regs[0][0]] + + ambiguous_key_rendered + + rendered_template[match.regs[0][1] :] + ) + else: + rendered_template = ambiguous_key_rendered except TypeError as e: # Raised when the wrong type is provided to a hook diff --git a/tests/render/fixtures/render-with-filters-output.yaml b/tests/render/fixtures/render-with-filters-output.yaml new file mode 100644 index 000000000..35da52d26 --- /dev/null +++ b/tests/render/fixtures/render-with-filters-output.yaml @@ -0,0 +1,2 @@ +contract_name: Big Deal Token +project_slug: Big_Deal_Token diff --git a/tests/render/fixtures/render-with-filters.yaml b/tests/render/fixtures/render-with-filters.yaml new file mode 100644 index 000000000..79325a84c --- /dev/null +++ b/tests/render/fixtures/render-with-filters.yaml @@ -0,0 +1,3 @@ + +contract_name: Big Deal Token +project_slug->: var contract_name|replace(' ','_')|replace('-','_')|replace('.','_')|trim() diff --git a/tests/render/test_render.py b/tests/render/test_render.py index 4c1ff6bd7..30a886a0a 100644 --- a/tests/render/test_render.py +++ b/tests/render/test_render.py @@ -12,6 +12,7 @@ # https://github.com/robcxyz/tackle/issues/19 ({'dict': {'stuff': 'things'}}, '{{dict}}', {'stuff': 'things'}), ({'namespace': {'stuff': 'things'}}, '{{namespace}}', {'stuff': 'things'}), + ({'namespace': 'foo'}, '{{namespace}}bar', 'foobar'), # Normal ({'adict': {'stuff': 'things'}}, '{{adict}}', {'stuff': 'things'}), ({'list': ['stuff', 'things']}, '{{list}}', ['stuff', 'things']), @@ -20,9 +21,9 @@ ] -@pytest.mark.parametrize("context,raw,expected_output", RENDERABLES) -def test_render_string_inputs(context, raw, expected_output): - context = Context(public_context=context) +@pytest.mark.parametrize("input_context,raw,expected_output", RENDERABLES) +def test_render_string_inputs(input_context, raw, expected_output): + context = Context(public_context=input_context) output = render_string(context=context, raw=raw) assert output == expected_output @@ -32,13 +33,14 @@ def test_render_string_inputs(context, raw, expected_output): # TODO: Not asserting - RM # ('multi-line-block.yaml', 'multi-line-block-output.yaml'), ('call-function.yaml', 'call-function-output.yaml'), + ('render-with-filters.yaml', 'render-with-filters-output.yaml'), ] @pytest.mark.parametrize("file,expected_output", FILES) def test_render_files(change_curdir_fixtures, file, expected_output): o = tackle(file) - expected_output = read_config_file(expected_output) + expected_output = dict(read_config_file(expected_output)) assert o == expected_output