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

Allow over ride of location in rule matching #2410

Merged
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.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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)])