Skip to content

Commit

Permalink
Simplify transformer test
Browse files Browse the repository at this point in the history
Related: #3818
  • Loading branch information
ssbarnea committed Oct 6, 2023
1 parent e710099 commit f7f8b1e
Show file tree
Hide file tree
Showing 3 changed files with 19 additions and 16 deletions.
5 changes: 4 additions & 1 deletion src/ansiblelint/file_utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -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",
)
Expand Down
30 changes: 15 additions & 15 deletions src/ansiblelint/rules/deprecated_local_action.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,16 +5,16 @@

import copy
import logging
import os
import sys
from pathlib import Path
from typing import TYPE_CHECKING

from ansiblelint.rules import AnsibleLintRule, TransformMixin
from ansiblelint.runner import get_matches
from ansiblelint.transformer import Transformer

if TYPE_CHECKING:
from pathlib import Path

from ruamel.yaml.comments import CommentedMap, CommentedSeq

from ansiblelint.config import Options
Expand Down Expand Up @@ -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

Expand All @@ -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,
Expand All @@ -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()

0 comments on commit f7f8b1e

Please sign in to comment.