Skip to content

Commit fea7b4c

Browse files
authored
Switch to using path and resource names for directives (#3035)
1 parent d06ad57 commit fea7b4c

File tree

4 files changed

+36
-38
lines changed

4 files changed

+36
-38
lines changed

src/cfnlint/runner.py

+10-17
Original file line numberDiff line numberDiff line change
@@ -56,22 +56,15 @@ def check_metadata_directives(self, matches: Sequence[Match]) -> List[Match]:
5656
return_matches.append(match)
5757
break
5858
else:
59-
for directive in directives.get(match.rule.id):
60-
start = directive.get("start")
61-
end = directive.get("end")
62-
if start[0] < match.linenumber < end[0]:
63-
break
64-
if (
65-
start[0] == match.linenumber
66-
and start[1] <= match.columnnumber
67-
):
68-
break
69-
if (
70-
end[0] == match.linenumber
71-
and end[1] >= match.columnnumberend
72-
):
73-
break
74-
else:
75-
return_matches.append(match)
59+
path = getattr(match, "path", None)
60+
if path:
61+
if len(path) >= 2:
62+
if path[0] != "Resources":
63+
return_matches.append(match)
64+
continue
65+
if path[1] not in directives[match.rule.id]:
66+
return_matches.append(match)
67+
else:
68+
return_matches.append(match)
7669

7770
return return_matches

src/cfnlint/template/template.py

+1-8
Original file line numberDiff line numberDiff line change
@@ -323,14 +323,7 @@ def get_directives(self):
323323
for ignore_rule_id in ignore_rule_ids:
324324
if ignore_rule_id not in results:
325325
results[ignore_rule_id] = []
326-
value_location = self._loc(resource_values)
327-
name_location = self._loc(resource_name)
328-
results[ignore_rule_id].append(
329-
{
330-
"start": (name_location[0] + 1, name_location[1] + 1),
331-
"end": (value_location[2] + 1, value_location[3] + 1),
332-
}
333-
)
326+
results[ignore_rule_id].append(resource_name)
334327
return results
335328

336329
# pylint: disable=too-many-locals

test/unit/module/runner/test_runner.py

+22-3
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,9 @@ def setUp(self):
2727
AWSTemplateFormatVersion: "2010-09-09"
2828
Description: >
2929
Template with all error levels: Warning, Error and Informational
30+
# Adding in an issue outside of the Resources for validating
31+
Conditions:
32+
IsUsEast1: !Equals ["a", "b", "c"]
3033
Resources:
3134
myTable:
3235
Metadata:
@@ -35,6 +38,7 @@ def setUp(self):
3538
ignore_checks:
3639
- I3011
3740
- W1020
41+
- E8003 # condition error #
3842
Type: "AWS::DynamoDB::Table"
3943
Properties:
4044
TableName: !Sub "TableName"
@@ -47,6 +51,21 @@ def setUp(self):
4751
ProvisionedThroughput:
4852
ReadCapacityUnits: 5
4953
WriteCapacityUnits: "5"
54+
myTable2:
55+
# With no ignore_checks so we
56+
# should still get issues from this
57+
Type: "AWS::DynamoDB::Table"
58+
Properties:
59+
TableName: !Sub "TableName"
60+
AttributeDefinitions:
61+
- AttributeName: "Id"
62+
AttributeType: "S" # Valid AllowedValue
63+
KeySchema:
64+
- AttributeName: "Id"
65+
KeyType: "HASH"
66+
ProvisionedThroughput:
67+
ReadCapacityUnits: !If [IsUsEast1, 5, 5]
68+
WriteCapacityUnits: "5"
5069
"""
5170
)
5271

@@ -55,7 +74,7 @@ def test_runner(self):
5574
runner = Runner(self.collection, "", self.template, ["us-east-1"], [])
5675
runner.transform()
5776
failures = runner.run()
58-
assert [] == failures, "Got failures {}".format(failures)
77+
self.assertEqual(len(failures), 3, "Got failures {}".format(failures))
5978

6079
def test_runner_mandatory_rules(self):
6180
"""Success test"""
@@ -64,11 +83,11 @@ def test_runner_mandatory_rules(self):
6483
)
6584
runner.transform()
6685
failures = runner.run()
67-
self.assertEqual(len(failures), 1)
86+
self.assertEqual(len(failures), 4, "Got failures {}".format(failures))
6887

6988
runner = Runner(
7089
self.collection, "", self.template, ["us-east-1"], mandatory_rules=["W9000"]
7190
)
7291
runner.transform()
7392
failures = runner.run()
74-
self.assertEqual(len(failures), 0)
93+
self.assertEqual(len(failures), 3, "Got failures {}".format(failures))

test/unit/module/test_template.py

+3-10
Original file line numberDiff line numberDiff line change
@@ -1228,14 +1228,7 @@ def test_get_directives(self):
12281228
)
12291229
directives = template.get_directives()
12301230
expected_result = {
1231-
"E3012": [
1232-
{"end": (23, 10), "start": (10, 9)},
1233-
{"end": (36, 10), "start": (24, 9)},
1234-
],
1235-
"I1001": [{"end": (23, 10), "start": (10, 9)}],
1231+
"E3012": ["myBucket1", "myBucket2"],
1232+
"I1001": ["myBucket1"],
12361233
}
1237-
self.assertEqual(len(expected_result), len(directives))
1238-
for key, items in directives.items():
1239-
self.assertIn(key, expected_result)
1240-
if key in expected_result:
1241-
self.assertEqualListOfDicts(items, expected_result.get(key))
1234+
self.assertDictEqual(directives, expected_result)

0 commit comments

Comments
 (0)