Skip to content

Commit 9217370

Browse files
authored
Fix context handling None for filename (#4091)
1 parent 1e32e50 commit 9217370

File tree

2 files changed

+22
-5
lines changed

2 files changed

+22
-5
lines changed

src/cfnlint/context/context.py

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -390,7 +390,7 @@ def is_ssm_parameter(self) -> bool:
390390
return self.type.startswith("AWS::SSM::Parameter::")
391391

392392

393-
def _nested_stack_get_atts(filename, template_url):
393+
def _nested_stack_get_atts(filename: str, template_url: str) -> None | AttributeDict:
394394
if (
395395
template_url.startswith("http://")
396396
or template_url.startswith("https://")
@@ -407,7 +407,7 @@ def _nested_stack_get_atts(filename, template_url):
407407
(tmp, matches) = decode(template_path)
408408
except Exception: # noqa: E722
409409
return None
410-
if matches:
410+
if matches or tmp is None:
411411
return None
412412

413413
outputs = AttributeDict()
@@ -448,6 +448,9 @@ def __post_init__(self, resource: Any, filename: str | None) -> None:
448448
raise ValueError("Condition must be a string")
449449
self.condition = c
450450

451+
if filename is None:
452+
return
453+
451454
if self.type == "AWS::CloudFormation::Stack":
452455
properties = resource.get("Properties")
453456
if isinstance(properties, dict):

test/unit/module/context/test_resource.py

Lines changed: 17 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -49,11 +49,12 @@ def test_resources():
4949

5050

5151
@pytest.mark.parametrize(
52-
"name,instance,decode_results,expected_getatts",
52+
"name,instance,filename,decode_results,expected_getatts",
5353
[
5454
(
5555
"Nested stack with no Properties",
5656
{"Type": "AWS::CloudFormation::Stack"},
57+
"foo/bar.yaml",
5758
None,
5859
AttributeDict({"Outputs\\..*": "/properties/CfnLintStringType"}),
5960
),
@@ -63,6 +64,17 @@ def test_resources():
6364
"Type": "AWS::CloudFormation::Stack",
6465
"Properties": {"TemplateURL": "https://bucket/path.yaml"},
6566
},
67+
"foo/bar.yaml",
68+
None,
69+
AttributeDict({"Outputs\\..*": "/properties/CfnLintStringType"}),
70+
),
71+
(
72+
"Nested stack with a None filename",
73+
{
74+
"Type": "AWS::CloudFormation::Stack",
75+
"Properties": {"TemplateURL": "application.yaml"},
76+
},
77+
None,
6678
None,
6779
AttributeDict({"Outputs\\..*": "/properties/CfnLintStringType"}),
6880
),
@@ -72,6 +84,7 @@ def test_resources():
7284
"Type": "AWS::CloudFormation::Stack",
7385
"Properties": {"TemplateURL": "./bar.yaml"},
7486
},
87+
"foo/bar.yaml",
7588
({"Outputs": {"MyValue": {"Type": "String"}}}, None),
7689
AttributeDict({"Outputs.MyValue": "/properties/CfnLintStringType"}),
7790
),
@@ -81,6 +94,7 @@ def test_resources():
8194
"Type": "AWS::CloudFormation::Stack",
8295
"Properties": {"TemplateURL": "./bar.yaml"},
8396
},
97+
"foo/bar.yaml",
8498
({}, None),
8599
AttributeDict({}),
86100
),
@@ -90,14 +104,14 @@ def test_resources():
90104
"Type": "AWS::CloudFormation::Stack",
91105
"Properties": {"TemplateURL": "./bar.yaml"},
92106
},
107+
"foo/bar.yaml",
93108
(None, Match("test", rule=ParseError())),
94109
AttributeDict({"Outputs\\..*": "/properties/CfnLintStringType"}),
95110
),
96111
],
97112
)
98-
def test_nested_stacks(name, instance, decode_results, expected_getatts):
113+
def test_nested_stacks(name, instance, filename, decode_results, expected_getatts):
99114
region = "us-east-1"
100-
filename = "foo/bar.yaml"
101115

102116
with patch("cfnlint.decode.decode") as mock_decode:
103117
if decode_results is not None:

0 commit comments

Comments
 (0)