Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Expand parameter overrides #7876

Open
wants to merge 17 commits into
base: develop
Choose a base branch
from
Open
Changes from 1 commit
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
Prev Previous commit
Next Next commit
Remove merging samconfig and cli
  • Loading branch information
sigJoe committed Feb 10, 2025
commit c2aaa39ce489aca9b711f7602881a8546d3b5f9f
20 changes: 3 additions & 17 deletions samcli/cli/types.py
Original file line number Diff line number Diff line change
@@ -113,10 +113,9 @@ class CfnParameterOverridesType(click.ParamType):

ordered_pattern_match = [_pattern_1, _pattern_2]

def _normalize_parameters(self, values, param, ctx):
def convert(self, values, param, ctx):
"""
Normalizes parameter overrides into key-value pairs of strings
Later keys overwrite previous ones in case of key conflict
Normalizes different parameter overrides formats into key-value pairs of strings
"""
if values in (("",), "", None) or values == {}:
LOG.debug("Empty parameter set (%s)", values)
@@ -136,7 +135,7 @@ def _normalize_parameters(self, values, param, ctx):
file_manager = FILE_MANAGER_MAPPER.get(filepath.suffix, None)
if not file_manager:
self.fail(f"{value} uses an unsupported extension", param, ctx)
parameters.update(self._normalize_parameters(file_manager.read(filepath), param, ctx))
parameters.update(self.convert(file_manager.read(filepath), param, ctx))
else:
# Legacy parameter matching
normalized_value = " " + value.strip()
@@ -177,19 +176,6 @@ def _normalize_parameters(self, values, param, ctx):
LOG.debug("Output parameters: %s", result)
return result

def convert(self, values, param, ctx):
# Merge samconfig with CLI parameter-overrides
if isinstance(values, tuple) and ctx: # ctx isn't set in all unit tests
# default_map was populated from samconfig
default_map = ctx.default_map.get("parameter_overrides", {})
LOG.debug("Default map: %s", default_map)
LOG.debug("Current values: %s", values)
# Easy merge - will flatten later
values = [default_map] + [values]
result = self._normalize_parameters(values, param, ctx)
return result


class CfnMetadataType(click.ParamType):
"""
Custom Click options type to accept values for metadata parameters.
108 changes: 6 additions & 102 deletions tests/unit/cli/test_types.py
Original file line number Diff line number Diff line change
@@ -152,126 +152,33 @@ def test_successful_parsing(self, input, expected):

@parameterized.expand(
[
("", "", {}, ),
(("A=1 A=2"), ("A=3 A=4",), {"A": "4"}),
(("Path=C:\\Windows\\System32"),("Path=/usr/bin",), {"Path": "/usr/bin"}),
(
("A=1 B=2"),
("ParameterKey=C,ParameterValue=3", "ParameterKey=D,ParameterValue=4",),
{"A": "1", "B": "2", "C": "3", "D": "4"},
),
(
('Quoted="Hello, World!"'),
('ParameterKey=Quoted,ParameterValue="\'Hi\'"',),
{"Quoted": "'Hi'"}
),
(
("A=1 B=2"),
("ParameterKey=A,ParameterValue=3", "ParameterKey=D,ParameterValue=4",),
{"A": "3", "B": "2", "D": "4"},
),
(
["A=1", "B=2"],
("ParameterKey=A,ParameterValue=3", "ParameterKey=D,ParameterValue=4",),
{"A": "3", "B": "2", "D": "4"},
),
(
("A=1,2,3 B=1,2,3"),
("ParameterKey=A,ParameterValue=2,2", "ParameterKey=D,ParameterValue=4,5,6",),
{"A": "2,2", "B": "1,2,3", "D": "4,5,6"},
),
(
("A=1 B=2 C=3"),
("C=4",),
{"A": "1", "B": "2", "C": "4"},
),
(
{"Flag": False},
("Flag=True",),
{"Flag": "True"}
),
(
("A=1 B=2"),
"A=3 D=4", # String not tuple
{"A": "3", "D": "4"}
),
(
("A=1 B=2 C=3"),
["C=4"], # List not tuple
{"C": "4"},
),
(
[[[[[[[[[[[[[[[[[[]]]]]]]]]]]], {"A": "1"}, [[[{"B": "2"}]]]]]]]]],
("A=3",),
{"A": "3", "B": "2"},
),
(
{"A": "1"},
("B=2",),
{"A": "1", "B": "2"},
),
(
{"Spaces": "String with spaces"},
("Pam='Param Pam Pam Param'",),
{"Spaces": "String with spaces", "Pam": "Param Pam Pam Param"},
),
(
[None, {"Empty": None}],
("None=None",),
{"Empty": "", "None": "None"}
),
(
{"List": [" A ", 1, False, "", None]},
("OtherList='A,1,False'",),
{"List": "A,1,False", "OtherList": 'A,1,False'},
),
(
{"List": [" A ", "' B '", 1, False, "", None]},
("List2=' A ,1,False'",),
{"List": "A,' B ',1,False", "List2": ' A ,1,False'},
),
]
)
def test_merge_default_map_parsing(self, file_overrides, cli_overrides, expected):
ctx = MagicMock()
ctx.default_map = {"parameter_overrides": file_overrides}
result = self.param_type.convert(cli_overrides, None, ctx)
self.assertEqual(result, expected, msg="Failed with Input = " + str(cli_overrides))

@parameterized.expand(
[
(
("A=1 B=2"),
("file://params.toml",),
{"A": "toml", "B": "toml", "Toml": "toml"},
),
(
("A=1 B=2"),
("file://params.toml", "D=4"),
{"A": "toml", "B": "toml", "Toml": "toml", "D": "4"},
),
(
("A=1 B=2"),
("ParameterKey=Y,ParameterValue=y", "file://params.toml", "D=4", "file://params.yaml", "A=6"),
{"A": "6", "B": "yaml", "Y": "y", "Toml": "toml", "D": "4", "Yaml": "yaml"},
),
(
("A=1 B=2"),
("file://list.yaml",),
{"A": "a", "B": "2", "List": "1,2,3", "Yaml": "yaml"},
("file://params.yaml", "file://list.yaml",),
{"A": "a", "B": "yaml", "List": "1,2,3", "Yaml": "yaml"},
),
(
("A=1 B=2"),
("file://nested.yaml",),
{"A": "yaml", "B": "yaml", "Toml": "toml", "Yaml": "yaml"},
),
]
)
def test_merge_file_parsing(self, file_overrides, cli_overrides, expected):
def test_merge_file_parsing(self, inputs, expected):
mock_files = {
"params.toml": 'A = "toml"\nB = "toml"\nToml = "toml"',
"params.yaml": "A: yaml\nB: yaml\nYaml: yaml",
"list.yaml": "- - - - - - A: a\n- List:\n - 1\n - 2\n - 3\n- Yaml: yaml",
"list.yaml": "- - - - - - A: a\n- List:\n - 1\n - 2\n - 3\n",
"nested.yaml": "- file://params.toml\n- file://params.yaml",
}

@@ -283,12 +190,9 @@ def mock_is_file(file_path):
return file_path.name in mock_files

with patch("pathlib.Path.is_file", new=mock_is_file), patch("pathlib.Path.read_text", new=mock_read_text):
ctx = MagicMock()
ctx.default_map = {"parameter_overrides": file_overrides}

result = self.param_type.convert(cli_overrides, None, ctx)
result = self.param_type.convert(inputs, None, MagicMock())
print(result)
self.assertEqual(result, expected, msg="Failed with Input = " + str(cli_overrides))
self.assertEqual(result, expected, msg="Failed with Input = " + str(inputs))



Loading
Oops, something went wrong.