Skip to content

Commit

Permalink
More additions to testing
Browse files Browse the repository at this point in the history
  • Loading branch information
kddejong committed Dec 31, 2024
1 parent f58f5a9 commit 1fb4877
Show file tree
Hide file tree
Showing 10 changed files with 273 additions and 94 deletions.
34 changes: 11 additions & 23 deletions src/cfnlint/config.py
Original file line number Diff line number Diff line change
Expand Up @@ -250,17 +250,6 @@ def comma_separated_arg(string):
return string.split(",")


class key_value(argparse.Action):
def __call__(self, parser, namespace, values, option_string=None):
setattr(namespace, self.dest, dict())

for value in values:
# split it into key and value
key, value = value.split("=", 1)
# assign into dictionary
getattr(namespace, self.dest)[key.strip()] = value.strip()


def _ensure_value(namespace, name, value):
if getattr(namespace, name, None) is None:
setattr(namespace, name, value)
Expand Down Expand Up @@ -324,7 +313,7 @@ def __call__(self, parser, namespace, values, option_string=None):
setattr(namespace, self.dest, items)
except Exception: # pylint: disable=W0703
parser.print_help()
parser.exit()
parser.exit(1)

Check warning on line 316 in src/cfnlint/config.py

View check run for this annotation

Codecov / codecov/patch

src/cfnlint/config.py#L316

Added line #L316 was not covered by tests


class ExtendKeyValuePairs(argparse.Action):
Expand Down Expand Up @@ -366,7 +355,7 @@ def __call__(self, parser, namespace, values, option_string=None):
setattr(namespace, self.dest, result)
except Exception: # pylint: disable=W0703
parser.print_help()
parser.exit()
parser.exit(1)


class ExtendAction(argparse.Action):
Expand Down Expand Up @@ -460,9 +449,8 @@ def error(self, message):
action="extend",
)
parameter_group.add_argument(
"-tp",
"--template-parameters",
dest="template_parameters",
"--parameters",
dest="parameters",
nargs="+",
default=[],
action="extend_key_value",
Expand Down Expand Up @@ -693,7 +681,7 @@ class ManualArgs(TypedDict, total=False):
non_zero_exit_code: str
output_file: str
regions: list
template_parameters: list[dict[str, Any]]
parameters: list[dict[str, Any]]


# pylint: disable=too-many-public-methods
Expand Down Expand Up @@ -730,7 +718,7 @@ def __repr__(self):
"non_zero_exit_code": self.non_zero_exit_code,
"override_spec": self.override_spec,
"regions": self.regions,
"template_parameters": self.template_parameters,
"parameters": self.parameters,
"templates": self.templates,
}
)
Expand Down Expand Up @@ -890,12 +878,12 @@ def append_rules(self):
)

@property
def template_parameters(self):
return self._get_argument_value("template_parameters", True, True)
def parameters(self):
return self._get_argument_value("parameters", True, True)

@template_parameters.setter
def template_parameters(self, template_parameters: list[dict[str, Any]]):
self._manual_args["template_parameters"] = template_parameters
@parameters.setter
def parameters(self, parameters: list[dict[str, Any]]):
self._manual_args["parameters"] = parameters

@property
def override_spec(self):
Expand Down
27 changes: 9 additions & 18 deletions src/cfnlint/rules/deployment_files/Parameters.py
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@ def __init__(self):
)

def _is_type_a_list(self, parameter_type: str) -> bool:
return "List" in parameter_type and "CommaDelimitedList" not in parameter_type
return "List" in parameter_type

def _build_schema(self, instance: Any) -> dict[str, Any]:
if not isinstance(instance, dict):
Expand All @@ -58,32 +58,23 @@ def _build_schema(self, instance: Any) -> dict[str, Any]:
if not isinstance(parameter_type, str):
continue

schema_constraints = {}
if "AllowedValues" in parameter_object:
schema_constraints["enum"] = parameter_object["AllowedValues"]
if "Pattern" in parameter_object:
schema_constraints["pattern"] = parameter_object["Pattern"]

if self._is_type_a_list(parameter_type):
schema["properties"][parameter_name] = {
"type": "array",
"items": {
"type": singular_types,
},
}
if "AllowedValues" in parameter_object:
schema["properties"][parameter_name]["items"]["enum"] = (
parameter_object["AllowedValues"]
)
if "Pattern" in parameter_object:
if self._is_type_a_list(parameter_type):
schema["properties"][parameter_name]["items"]["pattern"] = (
parameter_object["Pattern"]
)
schema["properties"][parameter_name]["items"].update(schema_constraints)
else:
schema["properties"][parameter_name]["type"] = singular_types
if "AllowedValues" in parameter_object:
schema["properties"][parameter_name]["enum"] = parameter_object[
"AllowedValues"
]
if "Pattern" in parameter_object:
schema["properties"][parameter_name]["pattern"] = parameter_object[
"Pattern"
]
schema["properties"][parameter_name].update(schema_constraints)

return schema

Expand Down
2 changes: 1 addition & 1 deletion src/cfnlint/runner/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@
]

from cfnlint.runner.cli import Runner, main
from cfnlint.runner.deployment_file import run_deployment_file
from cfnlint.runner.deployment_file import run_deployment_files
from cfnlint.runner.exceptions import (
CfnLintExitException,
InvalidRegionException,
Expand Down
4 changes: 2 additions & 2 deletions src/cfnlint/runner/deployment_file/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,6 @@
SPDX-License-Identifier: MIT-0
"""

__all__ = ["run_deployment_file"]
__all__ = ["run_deployment_files"]

from cfnlint.runner.deployment_file.runner import run_deployment_file
from cfnlint.runner.deployment_file.runner import run_deployment_files
2 changes: 1 addition & 1 deletion src/cfnlint/runner/deployment_file/runner.py
Original file line number Diff line number Diff line change
Expand Up @@ -68,7 +68,7 @@ def run_deployment_file(
)
template_path = Path(filename).parent / deployment_data.template_file_path
template_config = deepcopy(config)
template_config.template_parameters = [deployment_data.parameters]
template_config.parameters = [deployment_data.parameters]

yield from run_template_by_file_path(
filename=template_path,
Expand Down
6 changes: 3 additions & 3 deletions src/cfnlint/runner/template/runner.py
Original file line number Diff line number Diff line change
Expand Up @@ -107,10 +107,10 @@ def _run_template(
) -> Iterator[Match]:

config.set_template_args(template)
if config.template_parameters:
if config.parameters:
matches: list[Match] = []
for template_parameters in config.template_parameters:
cfn = Template(filename, template, config.regions, template_parameters)
for parameters in config.parameters:
cfn = Template(filename, template, config.regions, parameters)
matches.extend(list(_run_template_per_config(cfn, config, rules)))
yield from _dedup(iter(matches))
else:
Expand Down
37 changes: 37 additions & 0 deletions test/unit/module/config/test_config_mixin.py
Original file line number Diff line number Diff line change
Expand Up @@ -277,3 +277,40 @@ def test_config_merge(self, yaml_mock):
)
# template file wins over config file
self.assertEqual(config.ignore_checks, ["W3001", "E3001"])

@patch("cfnlint.config.ConfigFileArgs._read_config", create=True)
def test_parameters(self, yaml_mock):
yaml_mock.side_effect = [{}, {}]
config = cfnlint.config.ConfigMixIn(["--parameters", "Foo=Bar"])

# test defaults
self.assertEqual(config.parameters, [{"Foo": "Bar"}])

@patch("cfnlint.config.ConfigFileArgs._read_config", create=True)
def test_parameters_lists(self, yaml_mock):
yaml_mock.side_effect = [{}, {}]
config = cfnlint.config.ConfigMixIn(["--parameters", "A=1", "B=2"])

# test defaults
self.assertEqual(config.parameters, [{"A": "1", "B": "2"}])

@patch("cfnlint.config.ConfigFileArgs._read_config", create=True)
def test_parameters_lists_bad_value(self, yaml_mock):
yaml_mock.side_effect = [{}, {}]

with patch("sys.exit") as exit:
cfnlint.config.ConfigMixIn(
[
"--parameters",
"A",
]
)
exit.assert_called_once_with(1)

@patch("cfnlint.config.ConfigFileArgs._read_config", create=True)
def test_template_files(self, yaml_mock):
yaml_mock.side_effect = [{}, {}]
config = cfnlint.config.ConfigMixIn(["--deployment-files", "file1.json"])

# test defaults
self.assertEqual(config.deployment_files, ["file1.json"])
Loading

0 comments on commit 1fb4877

Please sign in to comment.