Skip to content

Commit

Permalink
[Resolve #1484] Gracefully handle invalid parameters (#1485)
Browse files Browse the repository at this point in the history
* [Resolve #1484] Gracefully handle invalid parameters

This adds logic to _cast_parameters to also handle the case that the
`parameters` dict itself is not a dict but some other invalid type.
  • Loading branch information
alex-harvey-z3q authored Jul 6, 2024
1 parent c06b3b5 commit 3d315c3
Show file tree
Hide file tree
Showing 2 changed files with 19 additions and 0 deletions.
5 changes: 5 additions & 0 deletions sceptre/stack.py
Original file line number Diff line number Diff line change
Expand Up @@ -305,6 +305,11 @@ def is_valid(value: Any) -> bool:
or isinstance(value, Resolver)
)

if not isinstance(parameters, dict):
raise InvalidConfigFileError(
f"{self.name}: parameters must be a dictionary of key-value pairs, got {parameters}"
)

casted_parameters = {k: cast_value(v) for k, v in parameters.items()}

if not all(is_valid(value) for value in casted_parameters.values()):
Expand Down
14 changes: 14 additions & 0 deletions tests/test_stack.py
Original file line number Diff line number Diff line change
Expand Up @@ -202,6 +202,20 @@ def test_init__invalid_parameters_raise_invalid_config_file_error(self, paramete
parameters=parameters,
)

@pytest.mark.parametrize(
"parameters",
[["this", "is", "a", "list"], "a_string"],
)
def test_init__invalid_parameters__parameters_a_list(self, parameters):
with pytest.raises(InvalidConfigFileError):
Stack(
name="stack_name",
project_code="project_code",
template_handler_config={"type": "file"},
region="region",
parameters=parameters,
)

@pytest.mark.parametrize(
"parameters",
[
Expand Down

0 comments on commit 3d315c3

Please sign in to comment.