Skip to content

Commit

Permalink
chore: add exception handler for non-dict input to declarative hook
Browse files Browse the repository at this point in the history
  • Loading branch information
robcxyz committed Dec 2, 2022
1 parent 55aca15 commit 08bae60
Show file tree
Hide file tree
Showing 3 changed files with 20 additions and 5 deletions.
14 changes: 11 additions & 3 deletions tackle/parser.py
Original file line number Diff line number Diff line change
Expand Up @@ -1196,16 +1196,24 @@ def create_function_model(
context: 'Context', func_name: str, func_dict: dict
) -> Type[BaseFunction]:
"""Create a model from the function input dict."""
if func_name.endswith(('<-', '<_')):
func_name = func_name[:-2]

if func_dict is None:
raise exceptions.EmptyFunctionException(
"Can't have an empty function", context=context, function_name=func_name
)
if not isinstance(func_dict, dict):
raise exceptions.MalformedFunctionFieldException(
"Input to a declarative hook must be a map. This could change in future"
" versions.",
context=context,
function_name=func_name,
) from None

# Macro to expand all keys properly so that a field's default can be parsed
func_dict = function_field_to_parseable_macro(func_dict, context, func_name)

if func_name.endswith(('<-', '<_')):
func_name = func_name[:-2]

# Implement inheritance
if 'extends' in func_dict and func_dict['extends'] is not None:
base_hook = get_hook(func_dict['extends'], context)
Expand Down
2 changes: 2 additions & 0 deletions tests/parser/functions/exceptions/str-value.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@

s<-: stuff
9 changes: 7 additions & 2 deletions tests/parser/functions/test_functions_exceptions.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
import pytest

from tackle import tackle
from tackle.cli import main
from tackle import exceptions


Expand Down Expand Up @@ -57,8 +58,12 @@ def test_parser_functions_raises_hook_kwarg_missing_default(chdir):


def test_parser_functions_raises_validation_missing_field(chdir):
from tackle.cli import main

chdir('exceptions')
with pytest.raises(exceptions.MalformedFunctionFieldException):
main(["missing-field.yaml", "stuff"])


def test_parser_functions_raises_(chdir):
chdir('exceptions')
with pytest.raises(exceptions.MalformedFunctionFieldException):
main(["str-value.yaml", "stuff"])

0 comments on commit 08bae60

Please sign in to comment.