Skip to content

Commit

Permalink
Switch to using path and resource names for directives
Browse files Browse the repository at this point in the history
  • Loading branch information
kddejong committed Jan 24, 2024
1 parent d06ad57 commit 151c221
Show file tree
Hide file tree
Showing 4 changed files with 35 additions and 38 deletions.
27 changes: 10 additions & 17 deletions src/cfnlint/runner.py
Original file line number Diff line number Diff line change
Expand Up @@ -56,22 +56,15 @@ def check_metadata_directives(self, matches: Sequence[Match]) -> List[Match]:
return_matches.append(match)
break
else:
for directive in directives.get(match.rule.id):
start = directive.get("start")
end = directive.get("end")
if start[0] < match.linenumber < end[0]:
break
if (
start[0] == match.linenumber
and start[1] <= match.columnnumber
):
break
if (
end[0] == match.linenumber
and end[1] >= match.columnnumberend
):
break
else:
return_matches.append(match)
path = getattr(match, "path", None)
if path:
if len(path) >= 2:
if path[0] != "Resources":
continue

Check warning on line 63 in src/cfnlint/runner.py

View check run for this annotation

Codecov / codecov/patch

src/cfnlint/runner.py#L63

Added line #L63 was not covered by tests
if path[1] in directives[match.rule.id]:
continue
return_matches.append(match)
else:
return_matches.append(match)

Check warning on line 68 in src/cfnlint/runner.py

View check run for this annotation

Codecov / codecov/patch

src/cfnlint/runner.py#L68

Added line #L68 was not covered by tests

return return_matches
9 changes: 1 addition & 8 deletions src/cfnlint/template/template.py
Original file line number Diff line number Diff line change
Expand Up @@ -323,14 +323,7 @@ def get_directives(self):
for ignore_rule_id in ignore_rule_ids:
if ignore_rule_id not in results:
results[ignore_rule_id] = []
value_location = self._loc(resource_values)
name_location = self._loc(resource_name)
results[ignore_rule_id].append(
{
"start": (name_location[0] + 1, name_location[1] + 1),
"end": (value_location[2] + 1, value_location[3] + 1),
}
)
results[ignore_rule_id].append(resource_name)
return results

# pylint: disable=too-many-locals
Expand Down
24 changes: 21 additions & 3 deletions test/unit/module/runner/test_runner.py
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,9 @@ def setUp(self):
AWSTemplateFormatVersion: "2010-09-09"
Description: >
Template with all error levels: Warning, Error and Informational
# Adding in an issue outside of the Resources for validating
Conditions:
IsUsEast1: !Equals ["a", "b", "c"]
Resources:
myTable:
Metadata:
Expand All @@ -47,6 +50,21 @@ def setUp(self):
ProvisionedThroughput:
ReadCapacityUnits: 5
WriteCapacityUnits: "5"
myTable2:
# With no ignore_checks so we
# should still get issues from this
Type: "AWS::DynamoDB::Table"
Properties:
TableName: !Sub "TableName"
AttributeDefinitions:
- AttributeName: "Id"
AttributeType: "S" # Valid AllowedValue
KeySchema:
- AttributeName: "Id"
KeyType: "HASH"
ProvisionedThroughput:
ReadCapacityUnits: !If [IsUsEast1, 5, 5]
WriteCapacityUnits: "5"
"""
)

Expand All @@ -55,7 +73,7 @@ def test_runner(self):
runner = Runner(self.collection, "", self.template, ["us-east-1"], [])
runner.transform()
failures = runner.run()
assert [] == failures, "Got failures {}".format(failures)
self.assertEqual(len(failures), 3, "Got failures {}".format(failures))

def test_runner_mandatory_rules(self):
"""Success test"""
Expand All @@ -64,11 +82,11 @@ def test_runner_mandatory_rules(self):
)
runner.transform()
failures = runner.run()
self.assertEqual(len(failures), 1)
self.assertEqual(len(failures), 4, "Got failures {}".format(failures))

runner = Runner(
self.collection, "", self.template, ["us-east-1"], mandatory_rules=["W9000"]
)
runner.transform()
failures = runner.run()
self.assertEqual(len(failures), 0)
self.assertEqual(len(failures), 3, "Got failures {}".format(failures))
13 changes: 3 additions & 10 deletions test/unit/module/test_template.py
Original file line number Diff line number Diff line change
Expand Up @@ -1228,14 +1228,7 @@ def test_get_directives(self):
)
directives = template.get_directives()
expected_result = {
"E3012": [
{"end": (23, 10), "start": (10, 9)},
{"end": (36, 10), "start": (24, 9)},
],
"I1001": [{"end": (23, 10), "start": (10, 9)}],
"E3012": ["myBucket1", "myBucket2"],
"I1001": ["myBucket1"],
}
self.assertEqual(len(expected_result), len(directives))
for key, items in directives.items():
self.assertIn(key, expected_result)
if key in expected_result:
self.assertEqualListOfDicts(items, expected_result.get(key))
self.assertDictEqual(directives, expected_result)

0 comments on commit 151c221

Please sign in to comment.