diff --git a/samcli/commands/_utils/options.py b/samcli/commands/_utils/options.py index 627851792c..cc849f351b 100644 --- a/samcli/commands/_utils/options.py +++ b/samcli/commands/_utils/options.py @@ -551,8 +551,10 @@ def manifest_option(f): def cached_click_option(): return click.option( - "--cached", + "--cached/--no-cached", "-c", + default=False, + required=False, is_flag=True, help="Enable cached builds. Use this flag to reuse build artifacts that have not changed from previous builds. " "AWS SAM evaluates whether you have made any changes to files in your project directory. \n\n" diff --git a/tests/integration/buildcmd/test_build_cmd.py b/tests/integration/buildcmd/test_build_cmd.py index e872266060..2b858f2354 100644 --- a/tests/integration/buildcmd/test_build_cmd.py +++ b/tests/integration/buildcmd/test_build_cmd.py @@ -1560,6 +1560,37 @@ def test_cache_build(self, use_container, code_uri, function1_handler, function2 expected_messages, command_result, self._make_parameter_override_arg(overrides) ) + def test_no_cached_override_build(self): + overrides = { + "FunctionCodeUri": "Python", + "Function1Handler": "main.first_function_handler", + "Function2Handler": "main.second_function_handler", + "FunctionRuntime": "python3.8", + } + config_file = str(Path(self.test_data_path).joinpath("samconfig_no_cached.toml")) + cmdlist = self.get_command_list(parameter_overrides=overrides, cached=True) + command_result = run_command(cmdlist, cwd=self.working_dir) + self.assertTrue( + "Running PythonPipBuilder:ResolveDependencies" in str(command_result.stderr) + and "Running PythonPipBuilder:CopySource" in str(command_result.stderr), + "Non-cached build should have been run", + ) + cmdlist = self.get_command_list(parameter_overrides=overrides) + cmdlist.extend(["--config-file", config_file]) + command_result = run_command(cmdlist, cwd=self.working_dir) + self.assertTrue( + "Valid cache found, copying previously built resources from function build definition of" + in str(command_result.stderr), + "Should have built using cache", + ) + cmdlist.extend(["--no-cached"]) + command_result = run_command(cmdlist, cwd=self.working_dir) + self.assertTrue( + "Running PythonPipBuilder:ResolveDependencies" in str(command_result.stderr) + and "Running PythonPipBuilder:CopySource" in str(command_result.stderr), + "Non-cached build should have been run", + ) + @skipIf(SKIP_DOCKER_TESTS, SKIP_DOCKER_MESSAGE) def test_cached_build_with_env_vars(self): """ diff --git a/tests/integration/testdata/buildcmd/samconfig_no_cached.toml b/tests/integration/testdata/buildcmd/samconfig_no_cached.toml new file mode 100644 index 0000000000..29354d5254 --- /dev/null +++ b/tests/integration/testdata/buildcmd/samconfig_no_cached.toml @@ -0,0 +1,5 @@ +version = 0.1 +[default] +[default.build] +[default.build.parameters] +cached = true \ No newline at end of file diff --git a/tests/unit/commands/samconfig/test_samconfig.py b/tests/unit/commands/samconfig/test_samconfig.py index 825709593e..8caee6b3b3 100644 --- a/tests/unit/commands/samconfig/test_samconfig.py +++ b/tests/unit/commands/samconfig/test_samconfig.py @@ -157,6 +157,60 @@ def test_build(self, do_cli_mock): (), ) + @patch("samcli.commands.build.command.do_cli") + def test_build_with_no_cached_override(self, do_cli_mock): + config_values = { + "resource_logical_id": "foo", + "template_file": "mytemplate.yaml", + "base_dir": "basedir", + "build_dir": "builddir", + "cache_dir": "cachedir", + "cache": False, + "cached": True, + "use_container": True, + "manifest": "requirements.txt", + "docker_network": "mynetwork", + "skip_pull_image": True, + "parameter_overrides": "ParameterKey=Key,ParameterValue=Value ParameterKey=Key2,ParameterValue=Value2", + "container_env_var": (""), + "container_env_var_file": "file", + "build_image": (""), + } + + with samconfig_parameters(["build"], self.scratch_dir, **config_values) as config_path: + from samcli.commands.build.command import cli + + LOG.debug(Path(config_path).read_text()) + runner = CliRunner() + result = runner.invoke(cli, ["--no-cached"]) + + LOG.info(result.output) + LOG.info(result.exception) + if result.exception: + LOG.exception("Command failed", exc_info=result.exc_info) + self.assertIsNone(result.exception) + + do_cli_mock.assert_called_with( + ANY, + "foo", + str(Path(os.getcwd(), "mytemplate.yaml")), + "basedir", + "builddir", + "cachedir", + True, + True, + False, + False, + "requirements.txt", + "mynetwork", + True, + {"Key": "Value", "Key2": "Value2"}, + None, + (), + "file", + (), + ) + @patch("samcli.commands.build.command.do_cli") def test_build_with_container_env_vars(self, do_cli_mock): config_values = {