Skip to content

Commit

Permalink
fix: error passing context to jinja hook calls
Browse files Browse the repository at this point in the history
  • Loading branch information
robcxyz committed Aug 23, 2022
1 parent d9cbf11 commit cfdda49
Show file tree
Hide file tree
Showing 3 changed files with 31 additions and 8 deletions.
21 changes: 14 additions & 7 deletions tackle/models.py
Original file line number Diff line number Diff line change
Expand Up @@ -448,12 +448,6 @@ class LazyBaseFunction(BaseModel):
description="List of fields used to 1, enrich functions without exec method, "
"and 2, inherit base attributes into methods. Basically a helper.",
)
function_methods: list = Field(
None,
description="List of methods so that when we use a function as a jinja hook, "
"we can easily enrich them without having to iterate over all of "
"its fields to detect if it is a callable.",
)


class JinjaHook(BaseModel):
Expand All @@ -478,7 +472,20 @@ def wrapped_exec(self, *args, **kwargs):
evaluate_args(
args=args_list, hook_dict=kwargs, Hook=self.hook, context=self.context
)
output = self.hook(**kwargs, **self.context.dict()).exec()
# Can't simply do self.hook(**kwargs, **self.context.dict()) because the
# references might not be copied over properly.
output = self.hook(
**kwargs,
input_context=self.context.input_context,
public_context=self.context.public_context,
existing_context=self.context.existing_context,
no_input=self.context.no_input,
calling_directory=self.context.calling_directory,
calling_file=self.context.calling_file,
provider_hooks=self.context.provider_hooks,
key_path=self.context.key_path,
verbose=self.context.verbose,
).exec()
return output

def set_method(self, key: str, method: Callable):
Expand Down
2 changes: 1 addition & 1 deletion tackle/parser.py
Original file line number Diff line number Diff line change
Expand Up @@ -956,7 +956,7 @@ def create_function_model(
)

# fmt: on
new_func = {'hook_type': func_name, 'function_fields': [], 'function_methods': []}
new_func = {'hook_type': func_name, 'function_fields': []}
literals = ('str', 'int', 'float', 'bool', 'dict', 'list') # strings to match
# Create function fields from anything left over in the function dict
for k, v in func_dict.items():
Expand Down
16 changes: 16 additions & 0 deletions tests/models/test_models.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,10 @@
import sys

from tackle.models import (
BaseContext,
BaseHook,
Context,
JinjaHook,
)


Expand Down Expand Up @@ -69,8 +71,22 @@ def test_models_latest():
assert c.checkout == 'latest'


# TODO: RM
def test_models_lazy_base_function():
from tackle.models import LazyBaseFunction

c = LazyBaseFunction(function_dict={}, function_fields=[], hook_type="foo")
assert c


def test_models_jinja_hook():
"""
Test that wrapped_exec properly passes references to context vars such as
public_context between a hook call.
"""
context = BaseContext()
context.provider_hooks = Context().provider_hooks
c = JinjaHook(context=context, hook=context.provider_hooks['set'])
c.context.public_context = {}
c.wrapped_exec(path="foo", value=1)
assert c.context.public_context == {"foo": 1}

0 comments on commit cfdda49

Please sign in to comment.