From 0db926a7295ca0921033b70beee13b731734bbc9 Mon Sep 17 00:00:00 2001 From: Daniel Mil Date: Tue, 29 Aug 2023 16:33:04 -0700 Subject: [PATCH 1/2] fix: Don't run hook if help text option is provided --- .../_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..c47d71ad4b 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") From ced5fbad79cff09d5e611a4e9f4553a6cc4e78d8 Mon Sep 17 00:00:00 2001 From: Daniel Mil Date: Tue, 29 Aug 2023 16:35:10 -0700 Subject: [PATCH 2/2] Format --- .../custom_options/test_hook_package_id_option.py | 10 +++++----- 1 file changed, 5 insertions(+), 5 deletions(-) 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 c47d71ad4b..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 @@ -164,11 +164,11 @@ def test_valid_hook_package_with_other_options( @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, + 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