From c1b166472f09d8c48d67b8ee3f061e48a94a9200 Mon Sep 17 00:00:00 2001 From: Daniel Mil <84205762+mildaniel@users.noreply.github.com> Date: Tue, 29 Aug 2023 21:11:18 -0700 Subject: [PATCH] fix: Don't run hook if help text option is provided (#5863) * fix: Don't run hook if help text option is provided * Format --- .../_utils/custom_options/hook_name_option.py | 8 ++++- .../test_hook_package_id_option.py | 32 +++++++++++++++++++ 2 files changed, 39 insertions(+), 1 deletion(-) diff --git a/samcli/commands/_utils/custom_options/hook_name_option.py b/samcli/commands/_utils/custom_options/hook_name_option.py index eb333b37bd..87888d63a1 100644 --- a/samcli/commands/_utils/custom_options/hook_name_option.py +++ b/samcli/commands/_utils/custom_options/hook_name_option.py @@ -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"]: diff --git a/tests/unit/commands/_utils/custom_options/test_hook_package_id_option.py b/tests/unit/commands/_utils/custom_options/test_hook_package_id_option.py index 5675d5e772..488ef55f23 100644 --- a/tests/unit/commands/_utils/custom_options/test_hook_package_id_option.py +++ b/tests/unit/commands/_utils/custom_options/test_hook_package_id_option.py @@ -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")