|
26 | 26 | if TYPE_CHECKING:
|
27 | 27 | from ruamel.yaml.comments import CommentedMap, CommentedSeq
|
28 | 28 |
|
| 29 | + from ansiblelint.config import Options |
29 | 30 | from ansiblelint.errors import MatchError
|
30 | 31 | from ansiblelint.utils import Task
|
31 | 32 |
|
@@ -450,6 +451,8 @@ def _transform_spacing(
|
450 | 451 | ignored_keys=ignored_keys,
|
451 | 452 | ):
|
452 | 453 | if key == match.transform_meta.key and value == match.transform_meta.value:
|
| 454 | + if not path: |
| 455 | + continue |
453 | 456 | for pth in path[:-1]:
|
454 | 457 | try:
|
455 | 458 | obj = obj[pth]
|
@@ -479,7 +482,13 @@ def blacken(text: str) -> str:
|
479 | 482 | import pytest
|
480 | 483 |
|
481 | 484 | 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 |
483 | 492 |
|
484 | 493 | @pytest.fixture(name="error_expected_lines")
|
485 | 494 | def fixture_error_expected_lines() -> list[int]:
|
@@ -821,6 +830,38 @@ def test_jinja_valid() -> None:
|
821 | 830 | errs = Runner(success, rules=collection).run()
|
822 | 831 | assert len(errs) == 0
|
823 | 832 |
|
| 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 | + |
824 | 865 |
|
825 | 866 | def _get_error_line(task: dict[str, Any], path: list[str | int]) -> int:
|
826 | 867 | """Return error line number."""
|
|
0 commit comments