|
30 | 30 | import pytest |
31 | 31 | from ansible.utils.sentinel import Sentinel |
32 | 32 | from ansible_compat.runtime import Runtime |
| 33 | +from packaging.version import Version |
33 | 34 |
|
34 | 35 | from ansiblelint import cli, constants, utils |
35 | 36 | from ansiblelint.__main__ import initialize_logger |
36 | 37 | from ansiblelint.cli import get_rules_dirs |
| 38 | +from ansiblelint.config import get_deps_versions |
37 | 39 | from ansiblelint.constants import RC |
38 | 40 | from ansiblelint.file_utils import Lintable, cwd |
39 | 41 | from ansiblelint.runner import Runner |
@@ -279,6 +281,47 @@ def test_template(template: str, output: str) -> None: |
279 | 281 | assert result == output |
280 | 282 |
|
281 | 283 |
|
| 284 | +@pytest.mark.parametrize( |
| 285 | + ("template", "has_lookup"), |
| 286 | + ( |
| 287 | + pytest.param( |
| 288 | + "{{ lookup('file', '/etc/hostname') }}", |
| 289 | + True, |
| 290 | + id="file_lookup", |
| 291 | + ), |
| 292 | + pytest.param( |
| 293 | + "Welcome {{ lookup('env', 'USER', default='user') }}!", |
| 294 | + True, |
| 295 | + id="lookup_with_text", |
| 296 | + ), |
| 297 | + ), |
| 298 | +) |
| 299 | +def test_template_lookup_behavior(template: str, has_lookup: bool) -> None: |
| 300 | + """Test template behavior for both ansible-core >= 2.19 and < 2.19.""" |
| 301 | + result = utils.template( |
| 302 | + basedir=Path("/base/dir"), |
| 303 | + value=template, |
| 304 | + variables={"some_var": "test_value"}, |
| 305 | + fail_on_error=False, |
| 306 | + ) |
| 307 | + |
| 308 | + # Get ansible-core version to determine expected behavior |
| 309 | + deps = get_deps_versions() |
| 310 | + ansible_version = deps.get("ansible-core") |
| 311 | + is_new_ansible = ansible_version and ansible_version >= Version("2.19") |
| 312 | + |
| 313 | + if has_lookup and is_new_ansible: |
| 314 | + # For ansible-core >= 2.19: lookups should be skipped (returned unchanged) |
| 315 | + assert result == template, ( |
| 316 | + f"Expected lookup to be skipped for ansible-core >= 2.19, but got: {result}" |
| 317 | + ) |
| 318 | + elif not has_lookup: |
| 319 | + # Normal templates should always be processed |
| 320 | + assert result != template, ( |
| 321 | + f"Expected normal template to be processed, but got unchanged: {result}" |
| 322 | + ) |
| 323 | + |
| 324 | + |
282 | 325 | def test_task_to_str_unicode() -> None: |
283 | 326 | """Ensure that extracting messages from tasks preserves Unicode.""" |
284 | 327 | task = utils.Task({"fail": {"msg": "unicode é ô à"}}, filename="filename.yml") |
|
0 commit comments