Skip to content

Commit

Permalink
test(write_exclude_list): add test for write_exclude_list config option
Browse files Browse the repository at this point in the history
  • Loading branch information
frq-asgard-josi committed Nov 12, 2024
1 parent 63cc052 commit 781edd5
Show file tree
Hide file tree
Showing 2 changed files with 73 additions and 4 deletions.
75 changes: 72 additions & 3 deletions test/test_transformer.py
Original file line number Diff line number Diff line change
Expand Up @@ -9,21 +9,22 @@
from pathlib import Path
from typing import TYPE_CHECKING, Any
from unittest import mock
from unittest.mock import Mock

import pytest

import ansiblelint.__main__ as main
from ansiblelint.app import App
from ansiblelint.config import Options
from ansiblelint.errors import MatchError
from ansiblelint.file_utils import Lintable
from ansiblelint.rules import TransformMixin
from ansiblelint.rules import AnsibleLintRule, TransformMixin

# noinspection PyProtectedMember
from ansiblelint.runner import LintResult, get_matches
from ansiblelint.transformer import Transformer

if TYPE_CHECKING:
from ansiblelint.config import Options
from ansiblelint.errors import MatchError
from ansiblelint.rules import RulesCollection


Expand Down Expand Up @@ -296,6 +297,74 @@ def test_effective_write_set(write_list: list[str], expected: set[str]) -> None:
actual = Transformer.effective_write_set(write_list)
assert actual == expected

@pytest.mark.parametrize(
("write_list", "write_exclude_list", "rules"),
(
(
["all"],
["none"],
[("rule-id", True), ("rule1", True), ("rule-03", True)],
),
(
["all"],
["all"],
[("rule-id", False), ("rule1", False), ("rule-03", False)],
),
(
["none"],
["none"],
[("rule-id", False), ("rule1", False), ("rule-03", False)],
),
(
["none"],
["all"],
[("rule-id", False), ("rule1", False), ("rule-03", False)],
),
(
["rule-id"],
["none"],
[("rule-id", True), ("rule1", False), ("rule-03", False)],
),
(
["rule-id"],
["all"],
[("rule-id", False), ("rule1", False), ("rule-03", False)],
),
),
)
def test_write_exclude_list(
write_list: list[str],
write_exclude_list: list[str],
rules: list[tuple[str, bool]],
) -> None:
"""Test item matching write_exclude_list are excluded correctly."""
matches: list[MatchError] = []

class TestRule(AnsibleLintRule, TransformMixin):
"""Dummy class for transformable rules."""
pass

for rule_id, transform_expected in rules:
rule = Mock(spec=TestRule)
rule.id = rule_id
rule.tags = []
rule.transform_expected = transform_expected
match = MatchError(rule=rule)
matches.append(match)

transformer = Transformer(
LintResult(matches, set()),
Options(write_list=write_list, write_exclude_list=write_exclude_list),
)
# noinspection PyTypeChecker
Transformer._do_transforms(transformer, Mock(), "", False, matches) # noqa: SLF001

for match in matches:
if match.rule.transform_expected: # type: ignore[attr-defined]
match.rule.transform.assert_called() # type: ignore[attr-defined]
else:
match.rule.transform.assert_not_called() # type: ignore[attr-defined]


def test_pruned_err_after_fix(monkeypatch: pytest.MonkeyPatch, tmpdir: Path) -> None:
"""Test that pruned errors are not reported after fixing.
Expand Down
2 changes: 1 addition & 1 deletion tox.ini
Original file line number Diff line number Diff line change
Expand Up @@ -76,7 +76,7 @@ setenv =
PRE_COMMIT_COLOR = always
# Number of expected test passes, safety measure for accidental skip of
# tests. Update value if you add/remove tests. (tox-extra)
PYTEST_REQPASS = 895
PYTEST_REQPASS = 901
FORCE_COLOR = 1
pre: PIP_PRE = 1
allowlist_externals =
Expand Down

0 comments on commit 781edd5

Please sign in to comment.