Skip to content

Commit 469fe11

Browse files
authored
Return Symbol instead of None on Fn::Equals logic (#3663)
* Return Symbol instead of None on Fn::Equals logic * Try and except on building all assertions
1 parent df3f505 commit 469fe11

File tree

3 files changed

+73
-2
lines changed

3 files changed

+73
-2
lines changed

src/cfnlint/conditions/_equals.py

+4-1
Original file line numberDiff line numberDiff line change
@@ -158,7 +158,10 @@ def build_cnf(self, params: dict[str, Symbol]) -> BooleanFunction:
158158
return BooleanTrue()
159159
return BooleanFalse()
160160

161-
return params.get(self.hash)
161+
if self.hash in params:
162+
return params.get(self.hash)
163+
164+
return Symbol(self.hash)
162165

163166
def test(self, scenarios: Mapping[str, str]) -> bool:
164167
"""Do an equals based on the provided scenario"""

src/cfnlint/conditions/_rule.py

+5-1
Original file line numberDiff line numberDiff line change
@@ -91,7 +91,11 @@ def build_cnf(self, params: dict[str, Symbol]) -> BooleanFunction | Symbol | Non
9191
for assertion in self._assertions:
9292
assertions.append(assertion.build_cnf(params))
9393

94-
return And(*assertions)
94+
try:
95+
return And(*assertions)
96+
except Exception as e:
97+
LOGGER.debug(f"Error building conditions: {e}")
98+
return BooleanTrue()
9599

96100
@property
97101
def equals(self) -> list[Equal]:

test/unit/module/conditions/test_rules.py

+64
Original file line numberDiff line numberDiff line change
@@ -302,6 +302,70 @@ def test_fn_equals_assertions_one(self):
302302
)
303303
)
304304

305+
def test_fn_equals_assertions_ref_no_data(self):
306+
template = decode_str(
307+
"""
308+
Parameters:
309+
AccountId:
310+
Type: String
311+
Rules:
312+
Rule1:
313+
Assertions:
314+
- Assert: !Equals [!Ref AccountId, !Ref AWS::AccountId]
315+
"""
316+
)[0]
317+
318+
cfn = Template("", template)
319+
self.assertEqual(len(cfn.conditions._conditions), 0)
320+
self.assertEqual(len(cfn.conditions._rules), 1)
321+
322+
self.assertListEqual(
323+
[equal.hash for equal in cfn.conditions._rules[0].equals],
324+
[
325+
"f36e61f3d5bf6cdc6ea2e7f01487af728094a439",
326+
],
327+
)
328+
329+
self.assertTrue(
330+
cfn.conditions.satisfiable(
331+
{},
332+
{},
333+
)
334+
)
335+
336+
def test_fn_equals_assertions_ref_never_satisfiable(self):
337+
template = decode_str(
338+
"""
339+
Parameters:
340+
AccountId:
341+
Type: String
342+
Rules:
343+
Rule1:
344+
Assertions:
345+
- Assert: !Equals [!Ref AccountId, !Ref AWS::AccountId]
346+
- Assert: !Not [!Equals [!Ref AccountId, !Ref AWS::AccountId]]
347+
"""
348+
)[0]
349+
350+
cfn = Template("", template)
351+
self.assertEqual(len(cfn.conditions._conditions), 0)
352+
self.assertEqual(len(cfn.conditions._rules), 1)
353+
354+
self.assertListEqual(
355+
[equal.hash for equal in cfn.conditions._rules[0].equals],
356+
[
357+
"f36e61f3d5bf6cdc6ea2e7f01487af728094a439",
358+
"f36e61f3d5bf6cdc6ea2e7f01487af728094a439",
359+
],
360+
)
361+
362+
self.assertFalse(
363+
cfn.conditions.satisfiable(
364+
{},
365+
{},
366+
)
367+
)
368+
305369

306370
class TestAssertion(TestCase):
307371
def test_assertion_errors(self):

0 commit comments

Comments
 (0)