Skip to content

Commit

Permalink
Allow over ride of location in rule matching (#2410)
Browse files Browse the repository at this point in the history
  • Loading branch information
kddejong committed Oct 10, 2022
1 parent 3492649 commit 9226dc5
Show file tree
Hide file tree
Showing 2 changed files with 36 additions and 2 deletions.
8 changes: 6 additions & 2 deletions src/cfnlint/rules/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@
from datetime import datetime
import importlib
import traceback
from typing import Any, Dict, List, Optional
from typing import Any, Dict, List, Optional, Tuple, Union
from cfnlint.exceptions import DuplicateRuleError
import cfnlint.helpers
import cfnlint.rules.custom
Expand Down Expand Up @@ -55,7 +55,11 @@ def wrapper(self, filename, cfn, *args, **kwargs):
error_rule = self
if hasattr(result, 'rule'):
error_rule = result.rule
linenumbers = cfn.get_location_yaml(cfn.template, result.path)
linenumbers: Union[Tuple[int, int, int, int], None] = None
if hasattr(result, 'location'):
linenumbers = result.location
else:
linenumbers = cfn.get_location_yaml(cfn.template, result.path)
if linenumbers:
matches.append(
Match(
Expand Down
30 changes: 30 additions & 0 deletions test/unit/module/rule/test_matching.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
"""
Copyright Amazon.com, Inc. or its affiliates. All Rights Reserved.
SPDX-License-Identifier: MIT-0
"""
from typing import Any
from test.testlib.testcase import BaseTestCase
from cfnlint.rules import CloudFormationLintRule, Match, RuleMatch, matching


class rule(CloudFormationLintRule):
id: str = 'ETest'
pass

rule_match = RuleMatch([], 'Message', rule=rule(), location=(0, 0, 0, 0))

class TestMatching(BaseTestCase):
"""Test Matching Wrapper"""

def setUp(self) -> None:
return super().setUp()

def test(self: Any, *args: Any, **kwargs: Any):
return [rule_match]

def test_matching_location(self):

f = matching('test')
r = f(self.test)
t = r(self, '', None)
self.assertEqual(t, [Match(1, 1, 1, 1, '', rule(), 'Message', rule_match)])

0 comments on commit 9226dc5

Please sign in to comment.