Skip to content

Commit

Permalink
fix: Don't run hook if help text option is provided (#5863)
Browse files Browse the repository at this point in the history
* fix: Don't run hook if help text option is provided

* Format
  • Loading branch information
mildaniel authored Aug 30, 2023
1 parent 16d95ca commit c1b1664
Show file tree
Hide file tree
Showing 2 changed files with 39 additions and 1 deletion.
8 changes: 7 additions & 1 deletion samcli/commands/_utils/custom_options/hook_name_option.py
Original file line number Diff line number Diff line change
Expand Up @@ -35,13 +35,19 @@ class HookNameOption(click.Option):

def __init__(self, *args, **kwargs):
self.hook_name_option_name = "hook_name"
self.help_option_name = "help"
self._force_prepare = kwargs.pop("force_prepare", True)
self._invalid_coexist_options = kwargs.pop("invalid_coexist_options", [])
super().__init__(*args, **kwargs)

def handle_parse_result(self, ctx, opts, args):
opt_name = self.hook_name_option_name.replace("_", "-")
if self.hook_name_option_name not in opts and self.hook_name_option_name not in ctx.default_map:
if (
self.hook_name_option_name not in opts
and self.hook_name_option_name not in ctx.default_map
or self.help_option_name in opts
or self.help_option_name in ctx.default_map
):
return super().handle_parse_result(ctx, opts, args)
command_name = ctx.command.name
if command_name in ["invoke", "start-lambda", "start-api"]:
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -159,6 +159,38 @@ def test_valid_hook_package_with_other_options(
self.assertEqual(opts.get("template_file"), self.metadata_path)
record_hook_telemetry_mock.assert_called_once()

@patch("samcli.commands._utils.custom_options.hook_name_option.record_hook_telemetry")
@patch("samcli.commands._utils.custom_options.hook_name_option.prompt_experimental")
@patch("samcli.commands._utils.custom_options.hook_name_option.os.getcwd")
@patch("samcli.commands._utils.custom_options.hook_name_option.IacHookWrapper")
def test_skips_hook_package_with_help_option(
self,
iac_hook_wrapper_mock,
getcwd_mock,
prompt_experimental_mock,
record_hook_telemetry_mock,
):
iac_hook_wrapper_mock.return_value = self.iac_hook_wrapper_instance_mock
prompt_experimental_mock.return_value = True

getcwd_mock.return_value = self.cwd_path

hook_name_option = HookNameOption(
param_decls=(self.name, self.opt),
force_prepare=True,
invalid_coexist_options=self.invalid_coexist_options,
)
ctx = MagicMock()
ctx.default_map = {}
opts = {
"hook_name": self.terraform,
"help": True,
}
args = []
hook_name_option.handle_parse_result(ctx, opts, args)
self.iac_hook_wrapper_instance_mock.prepare.assert_not_called()
record_hook_telemetry_mock.assert_not_called()

@patch("samcli.commands._utils.custom_options.hook_name_option.record_hook_telemetry")
@patch("samcli.commands._utils.custom_options.hook_name_option.update_experimental_context")
@patch("samcli.commands._utils.custom_options.hook_name_option.prompt_experimental")
Expand Down

0 comments on commit c1b1664

Please sign in to comment.