diff --git a/samcli/commands/local/invoke/cli.py b/samcli/commands/local/invoke/cli.py index 18b1a36d2b..311420b9a7 100644 --- a/samcli/commands/local/invoke/cli.py +++ b/samcli/commands/local/invoke/cli.py @@ -14,14 +14,17 @@ HELP_TEXT = """ You can use this command to execute your function in a Lambda-like environment locally. -You can pass in the event body via stdin or by using the -e (--event) parameter. -Logs from the Lambda function will be output via stdout.\n +You can pass in an event body using the -e (--event) parameter. +Logs from the Lambda function will be written to stdout.\n +\b +Invoking a Lambda function without an input event +$ sam local invoke "HelloWorldFunction"\n \b Invoking a Lambda function using an event file $ sam local invoke "HelloWorldFunction" -e event.json\n \b Invoking a Lambda function using input from stdin -$ echo '{"message": "Hey, are you there?" }' | sam local invoke "HelloWorldFunction" \n +$ echo '{"message": "Hey, are you there?" }' | sam local invoke "HelloWorldFunction" --event - \n """ STDIN_FILE_NAME = "-" @@ -31,11 +34,10 @@ "--event", "-e", type=click.Path(), - default=STDIN_FILE_NAME, # Defaults to stdin help="JSON file containing event data passed to the Lambda function during invoke. If this option " - "is not specified, we will default to reading JSON from stdin", + "is not specified, no event is assumed. Pass in the value '-' to input JSON via stdin", ) -@click.option("--no-event", is_flag=True, default=False, help="Invoke Function with an empty event") +@click.option("--no-event", is_flag=True, default=True, help="DEPRECATED: By default no event is assumed.", hidden=True) @invoke_common_options @cli_framework_options @aws_creds_options @@ -116,14 +118,10 @@ def do_cli( # pylint: disable=R0914 LOG.debug("local invoke command is called") - if no_event and event != STDIN_FILE_NAME: - # Do not know what the user wants. no_event and event both passed in. - raise UserException("no_event and event cannot be used together. Please provide only one.") - - if no_event: - event_data = "{}" - else: + if event: event_data = _get_event(event) + else: + event_data = "{}" # Pass all inputs to setup necessary context to invoke function locally. # Handler exception raised by the processor for invalid args and print errors diff --git a/tests/integration/local/invoke/test_integrations_cli.py b/tests/integration/local/invoke/test_integrations_cli.py index 9814c71b39..f9c6d66587 100644 --- a/tests/integration/local/invoke/test_integrations_cli.py +++ b/tests/integration/local/invoke/test_integrations_cli.py @@ -151,7 +151,6 @@ def test_invoke_when_function_writes_stderr(self): @pytest.mark.timeout(timeout=300, method="thread") def test_invoke_returns_expected_result_when_no_event_given(self): command_list = self.get_command_list("EchoEventFunction", template_path=self.template_path) - command_list.append("--no-event") process = Popen(command_list, stdout=PIPE) return_code = process.wait() process_stdout = b"".join(process.stdout.readlines()).strip() @@ -159,20 +158,6 @@ def test_invoke_returns_expected_result_when_no_event_given(self): self.assertEqual(return_code, 0) self.assertEqual("{}", process_stdout.decode("utf-8")) - @pytest.mark.flaky(reruns=3) - @pytest.mark.timeout(timeout=300, method="thread") - def test_invoke_raises_exception_with_noargs_and_event(self): - command_list = self.get_command_list( - "HelloWorldLambdaFunction", template_path=self.template_path, event_path=self.event_path - ) - command_list.append("--no-event") - process = Popen(command_list, stderr=PIPE) - process.wait() - - process_stderr = b"".join(process.stderr.readlines()).strip() - error_output = process_stderr.decode("utf-8") - self.assertIn("no_event and event cannot be used together. Please provide only one.", error_output) - @pytest.mark.flaky(reruns=3) @pytest.mark.timeout(timeout=300, method="thread") def test_invoke_with_env_using_parameters(self): diff --git a/tests/unit/commands/local/invoke/test_cli.py b/tests/unit/commands/local/invoke/test_cli.py index 643f223294..935532771f 100644 --- a/tests/unit/commands/local/invoke/test_cli.py +++ b/tests/unit/commands/local/invoke/test_cli.py @@ -32,7 +32,7 @@ def setUp(self): self.docker_network = "network" self.log_file = "logfile" self.skip_pull_image = True - self.no_event = False + self.no_event = True self.parameter_overrides = {} self.layer_cache_basedir = "/some/layers/path" self.force_image_build = True @@ -98,7 +98,7 @@ def test_cli_must_setup_context_and_invoke(self, get_event_mock, InvokeContextMo @patch("samcli.commands.local.cli_common.invoke_context.InvokeContext") @patch("samcli.commands.local.invoke.cli._get_event") def test_cli_must_invoke_with_no_event(self, get_event_mock, InvokeContextMock): - self.no_event = True + self.event = None ctx_mock = Mock() ctx_mock.region = self.region_name @@ -111,7 +111,7 @@ def test_cli_must_invoke_with_no_event(self, get_event_mock, InvokeContextMock): ctx=ctx_mock, function_identifier=self.function_id, template=self.template, - event=STDIN_FILE_NAME, + event=self.event, no_event=self.no_event, env_vars=self.env_vars, debug_port=self.debug_ports, @@ -144,43 +144,10 @@ def test_cli_must_invoke_with_no_event(self, get_event_mock, InvokeContextMock): aws_profile=self.profile, ) + get_event_mock.assert_not_called() context_mock.local_lambda_runner.invoke.assert_called_with( context_mock.function_name, event="{}", stdout=context_mock.stdout, stderr=context_mock.stderr ) - get_event_mock.assert_not_called() - - @patch("samcli.commands.local.cli_common.invoke_context.InvokeContext") - @patch("samcli.commands.local.invoke.cli._get_event") - def test_must_raise_user_exception_on_no_event_and_event(self, get_event_mock, InvokeContextMock): - self.no_event = True - - ctx_mock = Mock() - ctx_mock.region = self.region_name - ctx_mock.profile = self.profile - - with self.assertRaises(UserException) as ex_ctx: - - invoke_cli( - ctx=ctx_mock, - function_identifier=self.function_id, - template=self.template, - event=self.eventfile, - no_event=self.no_event, - env_vars=self.env_vars, - debug_port=self.debug_ports, - debug_args=self.debug_args, - debugger_path=self.debugger_path, - docker_volume_basedir=self.docker_volume_basedir, - docker_network=self.docker_network, - log_file=self.log_file, - skip_pull_image=self.skip_pull_image, - parameter_overrides=self.parameter_overrides, - layer_cache_basedir=self.layer_cache_basedir, - force_image_build=self.force_image_build, - ) - - msg = str(ex_ctx.exception) - self.assertEqual(msg, "no_event and event cannot be used together. Please provide only one.") @parameterized.expand( [