Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: Don't run hook if help text option is provided #5863

Merged
merged 3 commits into from
Aug 30, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
Loading