From f7f8b1e24a2b54bfa9cd6861fd12fb374df82afa Mon Sep 17 00:00:00 2001 From: Sorin Sbarnea Date: Fri, 6 Oct 2023 16:26:05 +0100 Subject: [PATCH] Simplify transformer test Related: https://github.com/ansible/ansible-lint/issues/3818 --- ...nsformed.yml => local_action.expected.yml} | 0 src/ansiblelint/file_utils.py | 5 +++- .../rules/deprecated_local_action.py | 30 +++++++++---------- 3 files changed, 19 insertions(+), 16 deletions(-) rename examples/playbooks/tasks/{local_action.transformed.yml => local_action.expected.yml} (100%) diff --git a/examples/playbooks/tasks/local_action.transformed.yml b/examples/playbooks/tasks/local_action.expected.yml similarity index 100% rename from examples/playbooks/tasks/local_action.transformed.yml rename to examples/playbooks/tasks/local_action.expected.yml diff --git a/src/ansiblelint/file_utils.py b/src/ansiblelint/file_utils.py index a39a7de8057..d0519df622c 100644 --- a/src/ansiblelint/file_utils.py +++ b/src/ansiblelint/file_utils.py @@ -374,7 +374,10 @@ def write(self, *, force: bool = False) -> None: if not force and not self.updated: # No changes to write. return - self.path.expanduser().resolve().write_text( + dump_filename = self.path.expanduser().resolve() + if os.environ.get("ANSIBLE_LINT_WRITE_TMP", "0") == "1": + dump_filename = dump_filename.with_suffix(f".tmp{dump_filename.suffix}") + dump_filename.write_text( self._content or "", encoding="utf-8", ) diff --git a/src/ansiblelint/rules/deprecated_local_action.py b/src/ansiblelint/rules/deprecated_local_action.py index b7557d3bbd7..4200055d6d9 100644 --- a/src/ansiblelint/rules/deprecated_local_action.py +++ b/src/ansiblelint/rules/deprecated_local_action.py @@ -5,7 +5,9 @@ import copy import logging +import os import sys +from pathlib import Path from typing import TYPE_CHECKING from ansiblelint.rules import AnsibleLintRule, TransformMixin @@ -13,8 +15,6 @@ from ansiblelint.transformer import Transformer if TYPE_CHECKING: - from pathlib import Path - from ruamel.yaml.comments import CommentedMap, CommentedSeq from ansiblelint.config import Options @@ -84,6 +84,8 @@ def transform( # testing code to be loaded only with pytest or when executed the rule file if "pytest" in sys.modules: + from unittest import mock # pylint: disable=ungrouped-imports + from ansiblelint.rules import RulesCollection # pylint: disable=ungrouped-imports from ansiblelint.runner import Runner # pylint: disable=ungrouped-imports @@ -97,16 +99,16 @@ def test_local_action(default_rules_collection: RulesCollection) -> None: assert len(results) == 1 assert results[0].tag == "deprecated-local-action" + @mock.patch.dict(os.environ, {"ANSIBLE_LINT_WRITE_TMP": "1"}, clear=True) def test_local_action_transform( config_options: Options, - copy_examples_dir: tuple[Path, Path], default_rules_collection: RulesCollection, ) -> None: """Test transform functionality for no-log-password rule.""" - playbook: str = "examples/playbooks/tasks/local_action.yml" + playbook = Path("examples/playbooks/tasks/local_action.yml") config_options.write_list = ["all"] - config_options.lintables = [playbook] + config_options.lintables = [str(playbook)] runner_result = get_matches( rules=default_rules_collection, options=config_options, @@ -117,14 +119,12 @@ def test_local_action_transform( matches = runner_result.matches assert len(matches) == 3 - orig_dir, tmp_dir = copy_examples_dir - orig_playbook = orig_dir / playbook - expected_playbook = orig_dir / playbook.replace(".yml", ".transformed.yml") - transformed_playbook = tmp_dir / playbook - - orig_playbook_content = orig_playbook.read_text() - expected_playbook_content = expected_playbook.read_text() - transformed_playbook_content = transformed_playbook.read_text() + orig_content = playbook.read_text() + expected_content = playbook.with_suffix( + f".expected{playbook.suffix}", + ).read_text() + transformed_content = playbook.with_suffix(f".tmp{playbook.suffix}").read_text() - assert orig_playbook_content != transformed_playbook_content - assert transformed_playbook_content == expected_playbook_content + assert orig_content != transformed_content + assert expected_content == transformed_content + playbook.with_suffix(f".tmp{playbook.suffix}").unlink()