diff --git a/src/cfnlint/jsonschema/_utils.py b/src/cfnlint/jsonschema/_utils.py index 5f383f2893..7c0d49412f 100644 --- a/src/cfnlint/jsonschema/_utils.py +++ b/src/cfnlint/jsonschema/_utils.py @@ -70,15 +70,6 @@ def custom_msg(validator, schema): return None -def extras_msg(extras): - """ - Create an error message for extra items or properties. - """ - - verb = "was" if len(extras) == 1 else "were" - return ", ".join(repr(extra) for extra in sorted(extras)), verb - - def ensure_list(thing): """ Wrap ``thing`` in a list if it's a single str. diff --git a/src/cfnlint/jsonschema/_validators_cfn.py b/src/cfnlint/jsonschema/_validators_cfn.py index 3b39f1eb22..bbcbffed8f 100644 --- a/src/cfnlint/jsonschema/_validators_cfn.py +++ b/src/cfnlint/jsonschema/_validators_cfn.py @@ -547,28 +547,6 @@ def resolve( yield err -class FnSub1(_Fn): - def __init__(self) -> None: - super().__init__("Fn::Sub", ["string"], []) - - def schema(self, validator, instance) -> Dict[str, Any]: - return { - "type": ["array", "string"], - "minItems": 2, - "maxItems": 2, - "fn_items": [ - { - "schema": {"type": "string"}, - }, - { - "schema": { - "type": ["object"], - } - }, - ], - } - - class FnSub(_Fn): def __init__(self) -> None: super().__init__("Fn::Sub", ["string"], []) diff --git a/test/unit/rules/parameters/test_allowed_pattern.py b/test/unit/rules/parameters/test_allowed_pattern.py index be1f537812..a0e0b1c19f 100644 --- a/test/unit/rules/parameters/test_allowed_pattern.py +++ b/test/unit/rules/parameters/test_allowed_pattern.py @@ -60,6 +60,7 @@ def test_validate(self): errs = list(self.rule.pattern(validator, "^[A-Z]$", "1", {})) self.assertEqual(len(errs), 1) for err in errs: + self.assertEqual(err.rule.id, "W2031") self.assertEqual(err.message, "'1' does not match '^[A-Z]$'") self.assertEqual(err.rule, self.rule) self.assertEqual( diff --git a/test/unit/rules/resources/properties/test_allowed_pattern.py b/test/unit/rules/resources/properties/test_allowed_pattern.py index c2ee65793d..4c52b250a8 100644 --- a/test/unit/rules/resources/properties/test_allowed_pattern.py +++ b/test/unit/rules/resources/properties/test_allowed_pattern.py @@ -2,70 +2,58 @@ Copyright Amazon.com, Inc. or its affiliates. All Rights Reserved. SPDX-License-Identifier: MIT-0 """ -from test.unit.rules import BaseRuleTestCase +from collections import deque + +import pytest from cfnlint.jsonschema import CfnTemplateValidator +from cfnlint.rules.parameters.AllowedPattern import ( + AllowedPattern as ParameterAllowedPattern, +) from cfnlint.rules.resources.properties.AllowedPattern import AllowedPattern -class TestAllowedPattern(BaseRuleTestCase): - """Test allowed pattern Property Configuration""" +@pytest.fixture(scope="module") +def rule(): + rule = AllowedPattern() + rule.child_rules["W2031"] = ParameterAllowedPattern() + yield rule - def setUp(self): - """Setup""" - self.rule = AllowedPattern() - def test_allowed_pattern(self): - validator = CfnTemplateValidator(schema={"type": "string"}) +@pytest.fixture(scope="module") +def validator(): + yield CfnTemplateValidator(schema={}) - self.assertEqual( - len(list(self.rule.pattern(validator, ".*", "foo", {}))), - 0, - ) - self.assertEqual( - len(list(self.rule.pattern(validator, "foo", "bar", {}))), - 1, - ) - self.assertEqual( - len( - list( - self.rule.pattern( - validator, - "foo", - { - "Ref": "foo", - "Fn::GetAtt": "bar", - }, - {}, - ) - ) - ), - 0, + +def test_allowed_pattern(rule, validator): + assert len(list(rule.pattern(validator, ".*", "foo", {}))) == 0 + assert len(list(rule.pattern(validator, "foo", "bar", {}))) == 1 + + evolved = validator.evolve( + context=validator.context.evolve( + path=deque(["Fn::Sub"]), ) - self.assertEqual( - len( - list( - self.rule.pattern( - validator, - "foo", - "{{resolve:ssm:S3AccessControl:2}}", - {}, - ) - ) - ), - 0, + ) + errs = list(rule.pattern(evolved, "bar", "bar", {})) + assert len(errs) == 0 + + evolved = validator.evolve( + context=validator.context.evolve( + path=deque(["Ref"]), + value_path=deque(["Parameters", "MyParameter", "Default"]), ) + ) + errs = list(rule.pattern(evolved, "foo", "bar", {})) + assert len(errs) == 1 + assert errs[0].rule.id == ParameterAllowedPattern.id - def test_allowed_pattern_exceptions(self): - validator = CfnTemplateValidator(schema={"type": "string"}) + rule.child_rules["W2031"] = None + errs = list(rule.pattern(evolved, "foo", "bar", {})) + assert len(errs) == 0 - self.rule.configure({"exceptions": ["AWS::"]}) - self.assertEqual( - len(list(self.rule.pattern(validator, "foo", "Another AWS::Instance", {}))), - 1, - ) - self.assertEqual( - len(list(self.rule.pattern(validator, "foo", "AWS::Dummy::Resource", {}))), - 0, - ) +def test_allowed_pattern_exceptions(rule, validator): + rule.configure({"exceptions": ["AWS::"]}) + + assert len(list(rule.pattern(validator, "foo", "Another AWS::Instance", {}))) == 1 + assert len(list(rule.pattern(validator, "foo", "AWS::Dummy::Resource", {}))) == 0