Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Switch to using path and resource names for directives #3035

Merged
merged 1 commit into from
Jan 24, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
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 @@
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":
return_matches.append(match)
continue
if path[1] not in directives[match.rule.id]:
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
25 changes: 22 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 @@ -35,6 +38,7 @@ def setUp(self):
ignore_checks:
- I3011
- W1020
- E8003 # condition error #
Type: "AWS::DynamoDB::Table"
Properties:
TableName: !Sub "TableName"
Expand All @@ -47,6 +51,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 +74,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 +83,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)