Skip to content

Commit 66378f6

Browse files
authored
Avoid IndexError exception with jinja transform (#3747)
1 parent 46ee8d7 commit 66378f6

File tree

4 files changed

+61
-2
lines changed

4 files changed

+61
-2
lines changed

.github/workflows/tox.yml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -71,7 +71,7 @@ jobs:
7171
WSLENV: FORCE_COLOR:PYTEST_REQPASS:TOXENV:GITHUB_STEP_SUMMARY
7272
# Number of expected test passes, safety measure for accidental skip of
7373
# tests. Update value if you add/remove tests.
74-
PYTEST_REQPASS: 824
74+
PYTEST_REQPASS: 825
7575
steps:
7676
- name: Activate WSL1
7777
if: "contains(matrix.shell, 'wsl')"
Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
---
2+
# https://github.com/ansible/ansible-lint/issues/3739
3+
- name: Reproducer bug 3739
4+
hosts: all
5+
tasks:
6+
- name: Generate keypair
7+
community.crypto.openssh_keypair:
8+
path: "{{ env.path }}"
9+
when: ( env.path is not none )
Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
---
2+
# https://github.com/ansible/ansible-lint/issues/3739
3+
- name: Reproducer bug 3739
4+
hosts: all
5+
tasks:
6+
- name: Generate keypair
7+
community.crypto.openssh_keypair:
8+
path: "{{env.path}}"
9+
when: ( env.path is not none )

src/ansiblelint/rules/jinja.py

Lines changed: 42 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,7 @@
2626
if TYPE_CHECKING:
2727
from ruamel.yaml.comments import CommentedMap, CommentedSeq
2828

29+
from ansiblelint.config import Options
2930
from ansiblelint.errors import MatchError
3031
from ansiblelint.utils import Task
3132

@@ -450,6 +451,8 @@ def _transform_spacing(
450451
ignored_keys=ignored_keys,
451452
):
452453
if key == match.transform_meta.key and value == match.transform_meta.value:
454+
if not path:
455+
continue
453456
for pth in path[:-1]:
454457
try:
455458
obj = obj[pth]
@@ -479,7 +482,13 @@ def blacken(text: str) -> str:
479482
import pytest
480483

481484
from ansiblelint.rules import RulesCollection # pylint: disable=ungrouped-imports
482-
from ansiblelint.runner import Runner # pylint: disable=ungrouped-imports
485+
from ansiblelint.runner import (
486+
Runner,
487+
_get_matches,
488+
)
489+
490+
# pylint: disable=ungrouped-imports
491+
from ansiblelint.transformer import Transformer # pylint: disable=ungrouped-imports
483492

484493
@pytest.fixture(name="error_expected_lines")
485494
def fixture_error_expected_lines() -> list[int]:
@@ -821,6 +830,38 @@ def test_jinja_valid() -> None:
821830
errs = Runner(success, rules=collection).run()
822831
assert len(errs) == 0
823832

833+
def test_jinja_transform(
834+
config_options: Options,
835+
copy_examples_dir: tuple[Path, Path],
836+
default_rules_collection: RulesCollection,
837+
) -> None:
838+
"""Test transform functionality for jinja rule."""
839+
playbook: str = "examples/playbooks/rule-jinja-before.yml"
840+
config_options.write_list = ["all"]
841+
842+
config_options.lintables = [playbook]
843+
runner_result = _get_matches(
844+
rules=default_rules_collection,
845+
options=config_options,
846+
)
847+
transformer = Transformer(result=runner_result, options=config_options)
848+
transformer.run()
849+
850+
matches = runner_result.matches
851+
assert len(matches) == 2
852+
853+
orig_dir, tmp_dir = copy_examples_dir
854+
orig_playbook = orig_dir / playbook
855+
expected_playbook = orig_dir / playbook.replace(".yml", ".transformed.yml")
856+
transformed_playbook = tmp_dir / playbook
857+
858+
orig_playbook_content = orig_playbook.read_text()
859+
expected_playbook_content = expected_playbook.read_text()
860+
transformed_playbook_content = transformed_playbook.read_text()
861+
862+
assert orig_playbook_content != transformed_playbook_content
863+
assert transformed_playbook_content == expected_playbook_content
864+
824865

825866
def _get_error_line(task: dict[str, Any], path: list[str | int]) -> int:
826867
"""Return error line number."""

0 commit comments

Comments
 (0)