Skip to content
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
4 changes: 3 additions & 1 deletion samcli/commands/_utils/options.py
Original file line number Diff line number Diff line change
Expand Up @@ -551,8 +551,10 @@ def manifest_option(f):

def cached_click_option():
return click.option(
"--cached",
"--cached/--no-cached",
Copy link
Contributor

@hawflau hawflau May 18, 2022

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Can we add some tests to make sure --no-cached is working fine?
We can safely assume --no-cached would set the option to False, as it's a feature from click.
From the issue, the customer mentioned cached=true is set in samconfig.toml but wanted to override the value set in samconfig.toml. Do we already have tests that validate this behavior?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Working on adding integration tests

"-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"
Expand Down
31 changes: 31 additions & 0 deletions tests/integration/buildcmd/test_build_cmd.py
Original file line number Diff line number Diff line change
Expand Up @@ -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):
"""
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
version = 0.1
[default]
[default.build]
[default.build.parameters]
cached = true
54 changes: 54 additions & 0 deletions tests/unit/commands/samconfig/test_samconfig.py
Original file line number Diff line number Diff line change
Expand Up @@ -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 = {
Expand Down