Skip to content

Commit ed9bf76

Browse files
anusshuklaanushkshpre-commit-ci[bot]
authored
whole test cases where template error is being generated and we are i… (#4742)
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 4b393e7 commit ed9bf76

File tree

2 files changed

+76
-34
lines changed

2 files changed

+76
-34
lines changed

src/ansiblelint/rules/jinja.py

Lines changed: 19 additions & 34 deletions
Original file line numberDiff line numberDiff line change
@@ -628,9 +628,6 @@ def test_jinja_spacing_vars() -> None:
628628
),
629629
pytest.param("{{foo(123)}}", "{{ foo(123) }}", "spacing", id="11"),
630630
pytest.param("{{ foo(a.b.c) }}", "{{ foo(a.b.c) }}", "spacing", id="12"),
631-
# pytest.param(
632-
# "spacing",
633-
# ),
634631
pytest.param(
635632
"{{foo(x =['server_options'])}}",
636633
"{{ foo(x=['server_options']) }}",
@@ -912,12 +909,11 @@ def test_jinja_transform(
912909

913910
orig_content = playbook.read_text(encoding="utf-8")
914911
expected_content = playbook.with_suffix(
915-
f".transformed{playbook.suffix}",
912+
f".transformed{playbook.suffix}"
916913
).read_text(encoding="utf-8")
917914
transformed_content = playbook.with_suffix(f".tmp{playbook.suffix}").read_text(
918-
encoding="utf-8",
915+
encoding="utf-8"
919916
)
920-
921917
assert orig_content != transformed_content
922918
assert expected_content == transformed_content
923919
playbook.with_suffix(f".tmp{playbook.suffix}").unlink()
@@ -964,35 +960,24 @@ def test_filter_import_failure(
964960
results = Runner(lintable, rules=collection).run()
965961
assert len(results) == expected_results
966962

967-
def test_ansible_core_2_19_supported_version() -> None:
968-
"""Test that ansible-core 2.19 is in the supported versions list."""
969-
from ansiblelint.config import Options
970-
971-
options = Options()
972-
supported_versions = options.supported_ansible
963+
def test_jinja_template_generates_ansible_tagged_str_error() -> None:
964+
"""Test that demonstrates ansible-core generating _AnsibleTaggedStr errors and us ignoring them."""
965+
# Test the ignore pattern directly without full RulesCollection setup
966+
from ansiblelint.rules.jinja import ignored_re
973967

974-
# Check that 2.19 is in the supported versions
975-
assert any("2.19" in version for version in supported_versions), (
976-
f"ansible-core 2.19 not found in supported versions: {supported_versions}"
968+
# Test _AnsibleTaggedStr error is ignored
969+
tagged_error_msg = 'can only concatenate list (not "_AnsibleTaggedStr") to list'
970+
assert ignored_re.search(tagged_error_msg), (
971+
f"_AnsibleTaggedStr error should be ignored: {tagged_error_msg}"
977972
)
978973

979-
@pytest.mark.parametrize(
980-
("error_message", "should_be_ignored"),
981-
(
982-
('can only concatenate list (not "_AnsibleTaggedStr") to list', True),
983-
('can only concatenate str (not "_AnsibleTaggedStr") to str', True),
984-
("Unexpected templating type error occurred on (var): details", True),
985-
("Object of type method is not JSON serializable", True),
986-
('can only concatenate list (not "int") to list', False),
987-
("TemplateSyntaxError: unexpected token", False),
988-
("UndefinedError: variable not defined", False),
989-
("can only concatenate list (not AnsibleTaggedStr) to list", False),
990-
),
991-
)
992-
def test_jinja_ignore_patterns(error_message: str, should_be_ignored: bool) -> None:
993-
"""Test that ignore patterns correctly handle ansible-core 2.19 _AnsibleTaggedStr errors."""
994-
matches = bool(ignored_re.search(error_message))
995-
assert matches == should_be_ignored, (
996-
f"Error message '{error_message}' should {'be ignored' if should_be_ignored else 'not be ignored'} "
997-
f"but {'was' if matches else 'was not'} matched by ignore pattern"
974+
# Test similar error without _AnsibleTaggedStr is NOT ignored
975+
normal_error_msg = 'can only concatenate list (not "int") to list'
976+
assert not ignored_re.search(normal_error_msg), (
977+
f"Normal error should not be ignored: {normal_error_msg}"
998978
)
979+
980+
# Test that the ignore pattern works for the specific error we're testing
981+
assert ignored_re.search(
982+
'can only concatenate str (not "_AnsibleTaggedStr") to str'
983+
), "String concatenation with _AnsibleTaggedStr should also be ignored"
Lines changed: 57 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,57 @@
1+
"""Test cases for ansible-core _AnsibleTaggedStr error handling in jinja rule."""
2+
3+
from __future__ import annotations
4+
5+
import pytest
6+
from ansible.errors import AnsibleError
7+
8+
from ansiblelint.rules.jinja import ignored_re
9+
10+
11+
class TestAnsibleTaggedStrErrorHandling:
12+
"""Test cases for _AnsibleTaggedStr error handling."""
13+
14+
def test_jinja_cockpit_style_template_error_handling(self) -> None:
15+
"""Test that cockpit-style templates with _AnsibleTaggedStr errors are properly ignored."""
16+
# Test _AnsibleTaggedStr error is ignored
17+
tagged_error = AnsibleError(
18+
'can only concatenate list (not "_AnsibleTaggedStr") to list'
19+
)
20+
assert ignored_re.search(str(tagged_error)), (
21+
f"_AnsibleTaggedStr error should be ignored: {tagged_error}"
22+
)
23+
24+
# Test similar error without _AnsibleTaggedStr is NOT ignored
25+
normal_error = AnsibleError('can only concatenate list (not "str") to list')
26+
assert not ignored_re.search(str(normal_error)), (
27+
f"Normal error should not be ignored: {normal_error}"
28+
)
29+
30+
@pytest.mark.skip(reason="Requires full environment setup")
31+
def test_jinja_template_generates_ansible_tagged_str_error_comprehensive(
32+
self,
33+
) -> None:
34+
"""Test comprehensive _AnsibleTaggedStr error generation and handling."""
35+
# This test is skipped because it requires full ansible-lint environment
36+
# The actual functionality is tested in the main jinja.py test
37+
38+
@pytest.mark.parametrize(
39+
("error_message", "should_be_ignored"),
40+
(
41+
('can only concatenate list (not "_AnsibleTaggedStr") to list', True),
42+
('can only concatenate str (not "_AnsibleTaggedStr") to str', True),
43+
("Unexpected templating type error occurred on", True),
44+
("Object of type method is not JSON serializable", True),
45+
('can only concatenate list (not "int") to list', False),
46+
("TemplateSyntaxError: unexpected char '!' at 5", False),
47+
),
48+
)
49+
def test_jinja_ignore_patterns_comprehensive(
50+
self, error_message: str, should_be_ignored: bool
51+
) -> None:
52+
"""Test comprehensive ignore patterns for _AnsibleTaggedStr errors."""
53+
matches = bool(ignored_re.search(error_message))
54+
assert matches == should_be_ignored, (
55+
f"Error message '{error_message}' should {'be ignored' if should_be_ignored else 'not be ignored'} "
56+
f"but {'was' if matches else 'was not'} matched by ignore pattern"
57+
)

0 commit comments

Comments
 (0)