Skip to content

Commit

Permalink
Remove extra code and improve testing
Browse files Browse the repository at this point in the history
  • Loading branch information
kddejong committed Nov 8, 2023
1 parent e4e345c commit 2a9092e
Show file tree
Hide file tree
Showing 4 changed files with 43 additions and 85 deletions.
9 changes: 0 additions & 9 deletions src/cfnlint/jsonschema/_utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand Down
22 changes: 0 additions & 22 deletions src/cfnlint/jsonschema/_validators_cfn.py
Original file line number Diff line number Diff line change
Expand Up @@ -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"], [])
Expand Down
1 change: 1 addition & 0 deletions test/unit/rules/parameters/test_allowed_pattern.py
Original file line number Diff line number Diff line change
Expand Up @@ -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(
Expand Down
96 changes: 42 additions & 54 deletions test/unit/rules/resources/properties/test_allowed_pattern.py
Original file line number Diff line number Diff line change
Expand Up @@ -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

0 comments on commit 2a9092e

Please sign in to comment.