Skip to content

Commit

Permalink
Code cleanup and improve testing (#2937)
Browse files Browse the repository at this point in the history
  • Loading branch information
kddejong authored Nov 3, 2023
1 parent 9521000 commit c87981c
Show file tree
Hide file tree
Showing 5 changed files with 21 additions and 65 deletions.
1 change: 0 additions & 1 deletion src/cfnlint/context/context.py
Original file line number Diff line number Diff line change
Expand Up @@ -77,7 +77,6 @@ class Context:
ref_values: Dict[str, Any] = field(init=True, default_factory=dict)

# Resolved value
resolved_value: bool = field(init=True, default=False)
resolved_conditions: Mapping[str, bool] = field(init=True, default_factory=dict)

transforms: Transforms = field(init=True, default_factory=lambda: Transforms([]))
Expand Down
29 changes: 16 additions & 13 deletions src/cfnlint/jsonschema/_validators_cfn.py
Original file line number Diff line number Diff line change
Expand Up @@ -152,6 +152,21 @@ def iter_errors(
validator.descend(value, self.schema(validator, instance), key)
)

def iter_errors_resolved(
self,
validator: Validator,
s: Any,
instance: Any,
schema: Any,
) -> ValidationResult:
key = list(instance.keys())[0]

for value in validator.resolve_value(instance):
for err in self._resolve_errors(validator.descend(value, s, key)):
err.message = err.message.replace(f"{value!r}", f"{instance!r}")
err.message = f"{err.message} when {self.fn.name!r} is resolved"
yield err


class FnArray:
def __init__(self, name: str, min_length: int = 0, max_length: int = 0) -> None:
Expand Down Expand Up @@ -233,7 +248,6 @@ def iter_errors(
for err in self._resolve_errors(
self.items[i].iter_errors(validator, s, value[i], schema)
):
err.validator = self.fn.py
err.path.extendleft([i, key])
yield err
except (IndexError, StopIteration):
Expand Down Expand Up @@ -274,9 +288,6 @@ def ref(
yield err
return

validator = validator.evolve(
context=validator.context.evolve(resolved_value=True)
)
for value in validator.resolve_value(instance):
if value is None:
continue
Expand Down Expand Up @@ -415,15 +426,7 @@ def get_azs(
if errs:
return

validator = validator.evolve(
context=validator.context.evolve(resolved_value=True)
)
for value in validator.resolve_value(instance):
for err in validator.descend(value, s):
err.path.appendleft(self.fn.name)
err.message = err.message.replace(f"{value!r}", f"{instance!r}")
err.message = f"{err.message} when {self.fn.name!r} is resolved"
yield err
yield from self.iter_errors_resolved(validator, s, instance, schema)


class FnImportValue(Fn):
Expand Down
1 change: 1 addition & 0 deletions src/cfnlint/rules/conditions/JsonSchema.py
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,7 @@ def __init__(self):
"fn_and": "E8004",
"fn_or": "E8006",
"condition": "E8007",
"ref": "E1020",
}
self.child_rules = dict.fromkeys(list(self.rule_set.values()))
self.schema = load_resource(schema_conditions, "conditions.json")
Expand Down
39 changes: 0 additions & 39 deletions src/cfnlint/rules/functions/RefInCondition.py

This file was deleted.

16 changes: 4 additions & 12 deletions src/cfnlint/rules/resources/properties/AvailabilityZone.py
Original file line number Diff line number Diff line change
Expand Up @@ -23,18 +23,14 @@ def __init__(self):

# pylint: disable=unused-argument
def availabilityzone(self, validator, aZ, zone, schema):
if validator.context.resolved_value:
return

if not validator.is_type(zone, "string"):
return

if zone in self.exceptions:
return

if len(validator.context.path) > 0:
if validator.context.path[-1] in FUNCTIONS:
return
if any(fn in validator.context.path for fn in FUNCTIONS):
return

yield ValidationError(
f"Avoid hardcoding availability zones {zone!r}",
Expand All @@ -43,15 +39,11 @@ def availabilityzone(self, validator, aZ, zone, schema):

# pylint: disable=unused-argument
def availabilityzones(self, validator, aZ, zones, schema):
if validator.context.resolved_value:
return

if not validator.is_type(zones, "array"):
return

if len(validator.context.path) > 0:
if validator.context.path[-1] in FUNCTIONS:
return
if any(fn in validator.context.path for fn in FUNCTIONS):
return

for zone in zones:
yield from self.availabilityzone(validator, aZ, zone, schema)

0 comments on commit c87981c

Please sign in to comment.