Skip to content

Commit

Permalink
Better support for rule Equals when static
Browse files Browse the repository at this point in the history
  • Loading branch information
kddejong committed Sep 4, 2024
1 parent 31d4036 commit c530d13
Show file tree
Hide file tree
Showing 3 changed files with 28 additions and 4 deletions.
2 changes: 1 addition & 1 deletion src/cfnlint/conditions/_condition.py
Original file line number Diff line number Diff line change
Expand Up @@ -80,7 +80,7 @@ def build_cnf(self, params: dict[str, Symbol]) -> BooleanFunction | Symbol | Non
if self._condition:
return self._condition.build_cnf(params)
if self._fn_equals:
return params.get(self._fn_equals.hash)
return self._fn_equals.build_cnf(params)
return None

def _test(self, scenarios: Mapping[str, str]) -> bool:
Expand Down
18 changes: 18 additions & 0 deletions src/cfnlint/conditions/_equals.py
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,9 @@
import logging
from typing import Any, Mapping, Tuple

from sympy import Symbol
from sympy.logic.boolalg import BooleanFalse, BooleanFunction, BooleanTrue

from cfnlint.conditions._utils import get_hash
from cfnlint.helpers import is_function

Expand Down Expand Up @@ -142,6 +145,21 @@ def left(self):
def right(self):
return self._right

def build_cnf(self, params: dict[str, Symbol]) -> BooleanFunction:
"""Build a SymPy CNF solver based on the provided params
Args:
params dict[str, Symbol]: params is a dict that represents
the hash of an Equal and the SymPy Symbol
Returns:
BooleanFunction: A Not SymPy BooleanFunction
"""
if self._is_static is not None:
if self._is_static:
return BooleanTrue()
return BooleanFalse()

Check warning on line 159 in src/cfnlint/conditions/_equals.py

View check run for this annotation

Codecov / codecov/patch

src/cfnlint/conditions/_equals.py#L159

Added line #L159 was not covered by tests

return params.get(self.hash)

def test(self, scenarios: Mapping[str, str]) -> bool:
"""Do an equals based on the provided scenario"""
if self._is_static in [True, False]:
Expand Down
12 changes: 9 additions & 3 deletions src/cfnlint/conditions/_rule.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,10 +5,11 @@

from __future__ import annotations

import logging
from typing import Any, Dict

from sympy import And, Implies, Symbol
from sympy.logic.boolalg import BooleanFunction
from sympy.logic.boolalg import BooleanFunction, BooleanTrue

from cfnlint.conditions._condition import (
ConditionAnd,
Expand All @@ -20,6 +21,8 @@
from cfnlint.conditions._equals import Equal
from cfnlint.helpers import FUNCTION_CONDITIONS

LOGGER = logging.getLogger(__name__)

# we leave the type hinting here
_RULE = Dict[str, Any]

Expand Down Expand Up @@ -56,9 +59,12 @@ def build_cnf(self, params: dict[str, Symbol]) -> BooleanFunction | Symbol | Non
return self._fn_equals.hash

if self._condition:
return self._condition.build_cnf(params)
try:
return self._condition.build_cnf(params)
except Exception as e:
LOGGER.debug(f"Error building condition: {e}")

Check warning on line 65 in src/cfnlint/conditions/_rule.py

View check run for this annotation

Codecov / codecov/patch

src/cfnlint/conditions/_rule.py#L64-L65

Added lines #L64 - L65 were not covered by tests

return None
return BooleanTrue()

Check warning on line 67 in src/cfnlint/conditions/_rule.py

View check run for this annotation

Codecov / codecov/patch

src/cfnlint/conditions/_rule.py#L67

Added line #L67 was not covered by tests

@property
def equals(self) -> list[Equal]:
Expand Down

0 comments on commit c530d13

Please sign in to comment.