Skip to content

Commit e0ee1f1

Browse files
anusshuklaanushkshpre-commit-ci[bot]
authored
fix: ignore internal types false positives with jinja[invalid] (#4823)
Co-authored-by: anushka-shukla-03 <[email protected]> Co-authored-by: pre-commit-ci[bot] <66853113+pre-commit-ci[bot]@users.noreply.github.com>
1 parent 5a74f30 commit e0ee1f1

File tree

3 files changed

+37
-4
lines changed

3 files changed

+37
-4
lines changed

.config/dictionary.txt

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -53,6 +53,7 @@ indentless
5353
inlinehilite
5454
iscsi
5555
junitxml
56+
kdump
5657
kubevirt
5758
libyaml
5859
licensedb

src/ansiblelint/rules/jinja.py

Lines changed: 11 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -71,6 +71,14 @@ class Token(NamedTuple):
7171
r"An unhandled exception occurred while templating (.*). Error was a <class 'ansible.errors.AnsibleFilterError'>, original message: The (.*) test expects a dictionary$",
7272
r"can only concatenate list \(not \"_AnsibleTaggedStr\"\) to list",
7373
r"can only concatenate str \(not \"_AnsibleTaggedStr\"\) to str",
74+
r"can only concatenate list \(not \"UndefinedMarker\"\) to list",
75+
r"can only concatenate str \(not \"UndefinedMarker\"\) to str",
76+
r"can only concatenate list \(not \"AnsibleUndefined\"\) to list",
77+
r"can only concatenate str \(not \"AnsibleUndefined\"\) to str",
78+
r"can only concatenate list \(not \"StrictUndefined\"\) to list",
79+
r"can only concatenate str \(not \"StrictUndefined\"\) to str",
80+
r"can only concatenate list \(not \"ChainableUndefined\"\) to list",
81+
r"can only concatenate str \(not \"ChainableUndefined\"\) to str",
7482
],
7583
),
7684
flags=re.MULTILINE | re.DOTALL,
@@ -977,7 +985,7 @@ def test_jinja_template_generates_ansible_tagged_str_error() -> None:
977985
f"Normal error should not be ignored: {normal_error_msg}"
978986
)
979987

980-
# Test that the ignore pattern works for the specific error we're testing
988+
# Test UndefinedMarker errors are ignored
981989
assert ignored_re.search(
982-
'can only concatenate str (not "_AnsibleTaggedStr") to str'
983-
), "String concatenation with _AnsibleTaggedStr should also be ignored"
990+
'can only concatenate list (not "UndefinedMarker") to list'
991+
), "UndefinedMarker concatenation should be ignored"

test/test_jinja_ansible_tagged_str.py

Lines changed: 25 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -38,18 +38,42 @@ def test_jinja_template_generates_ansible_tagged_str_error_comprehensive(
3838
@pytest.mark.parametrize(
3939
("error_message", "should_be_ignored"),
4040
(
41+
# _AnsibleTaggedStr errors (cockpit issue)
4142
('can only concatenate list (not "_AnsibleTaggedStr") to list', True),
4243
('can only concatenate str (not "_AnsibleTaggedStr") to str', True),
44+
# UndefinedMarker errors (kdump issue)
45+
('can only concatenate list (not "UndefinedMarker") to list', True),
46+
('can only concatenate str (not "UndefinedMarker") to str', True),
47+
# AnsibleUndefined errors (kernel_settings issue)
48+
('can only concatenate list (not "AnsibleUndefined") to list', True),
49+
('can only concatenate str (not "AnsibleUndefined") to str', True),
50+
# StrictUndefined errors (kernel_settings issue)
51+
('can only concatenate list (not "StrictUndefined") to list', True),
52+
('can only concatenate str (not "StrictUndefined") to str', True),
53+
# ChainableUndefined errors (vpn issue)
54+
('can only concatenate list (not "ChainableUndefined") to list', True),
55+
('can only concatenate str (not "ChainableUndefined") to str', True),
56+
# Other known patterns
4357
("Unexpected templating type error occurred on", True),
4458
("Object of type method is not JSON serializable", True),
59+
# Legitimate errors that should NOT be ignored
4560
('can only concatenate list (not "int") to list', False),
4661
("TemplateSyntaxError: unexpected char '!' at 5", False),
4762
),
4863
)
4964
def test_jinja_ignore_patterns_comprehensive(
5065
self, error_message: str, should_be_ignored: bool
5166
) -> None:
52-
"""Test comprehensive ignore patterns for _AnsibleTaggedStr errors."""
67+
"""Test comprehensive ignore patterns for ansible-core 2.19+ type errors.
68+
69+
This covers:
70+
- _AnsibleTaggedStr errors (cockpit role)
71+
- UndefinedMarker errors (kdump role)
72+
- AnsibleUndefined/StrictUndefined errors (kernel_settings role)
73+
- ChainableUndefined errors (vpn role)
74+
75+
See: linux-system-roles/vpn#207
76+
"""
5377
matches = bool(ignored_re.search(error_message))
5478
assert matches == should_be_ignored, (
5579
f"Error message '{error_message}' should {'be ignored' if should_be_ignored else 'not be ignored'} "

0 commit comments

Comments
 (0)