|
9 | 9 | from pathlib import Path
|
10 | 10 | from typing import TYPE_CHECKING, Any
|
11 | 11 | from unittest import mock
|
| 12 | +from unittest.mock import Mock |
12 | 13 |
|
13 | 14 | import pytest
|
14 | 15 |
|
15 | 16 | import ansiblelint.__main__ as main
|
16 | 17 | from ansiblelint.app import App
|
| 18 | +from ansiblelint.config import Options |
| 19 | +from ansiblelint.errors import MatchError |
17 | 20 | from ansiblelint.file_utils import Lintable
|
18 |
| -from ansiblelint.rules import TransformMixin |
| 21 | +from ansiblelint.rules import AnsibleLintRule, TransformMixin |
19 | 22 |
|
20 | 23 | # noinspection PyProtectedMember
|
21 | 24 | from ansiblelint.runner import LintResult, get_matches
|
22 | 25 | from ansiblelint.transformer import Transformer
|
23 | 26 |
|
24 | 27 | if TYPE_CHECKING:
|
25 |
| - from ansiblelint.config import Options |
26 |
| - from ansiblelint.errors import MatchError |
27 | 28 | from ansiblelint.rules import RulesCollection
|
28 | 29 |
|
29 | 30 |
|
@@ -297,6 +298,74 @@ def test_effective_write_set(write_list: list[str], expected: set[str]) -> None:
|
297 | 298 | assert actual == expected
|
298 | 299 |
|
299 | 300 |
|
| 301 | +@pytest.mark.parametrize( |
| 302 | + ("write_list", "write_exclude_list", "rules"), |
| 303 | + ( |
| 304 | + ( |
| 305 | + ["all"], |
| 306 | + ["none"], |
| 307 | + [("rule-id", True), ("rule1", True), ("rule-03", True)], |
| 308 | + ), |
| 309 | + ( |
| 310 | + ["all"], |
| 311 | + ["all"], |
| 312 | + [("rule-id", False), ("rule1", False), ("rule-03", False)], |
| 313 | + ), |
| 314 | + ( |
| 315 | + ["none"], |
| 316 | + ["none"], |
| 317 | + [("rule-id", False), ("rule1", False), ("rule-03", False)], |
| 318 | + ), |
| 319 | + ( |
| 320 | + ["none"], |
| 321 | + ["all"], |
| 322 | + [("rule-id", False), ("rule1", False), ("rule-03", False)], |
| 323 | + ), |
| 324 | + ( |
| 325 | + ["rule-id"], |
| 326 | + ["none"], |
| 327 | + [("rule-id", True), ("rule1", False), ("rule-03", False)], |
| 328 | + ), |
| 329 | + ( |
| 330 | + ["rule-id"], |
| 331 | + ["all"], |
| 332 | + [("rule-id", False), ("rule1", False), ("rule-03", False)], |
| 333 | + ), |
| 334 | + ), |
| 335 | +) |
| 336 | +def test_write_exclude_list( |
| 337 | + write_list: list[str], |
| 338 | + write_exclude_list: list[str], |
| 339 | + rules: list[tuple[str, bool]], |
| 340 | +) -> None: |
| 341 | + """Test item matching write_exclude_list are excluded correctly.""" |
| 342 | + matches: list[MatchError] = [] |
| 343 | + |
| 344 | + class TestRule(AnsibleLintRule, TransformMixin): |
| 345 | + """Dummy class for transformable rules.""" |
| 346 | + |
| 347 | + for rule_id, transform_expected in rules: |
| 348 | + rule = Mock(spec=TestRule) |
| 349 | + rule.id = rule_id |
| 350 | + rule.tags = [] |
| 351 | + rule.transform_expected = transform_expected |
| 352 | + match = MatchError(rule=rule) |
| 353 | + matches.append(match) |
| 354 | + |
| 355 | + transformer = Transformer( |
| 356 | + LintResult(matches, set()), |
| 357 | + Options(write_list=write_list, write_exclude_list=write_exclude_list), |
| 358 | + ) |
| 359 | + # noinspection PyTypeChecker |
| 360 | + Transformer._do_transforms(transformer, Mock(), "", False, matches) # noqa: SLF001 |
| 361 | + |
| 362 | + for match in matches: |
| 363 | + if match.rule.transform_expected: # type: ignore[attr-defined] |
| 364 | + match.rule.transform.assert_called() # type: ignore[attr-defined] |
| 365 | + else: |
| 366 | + match.rule.transform.assert_not_called() # type: ignore[attr-defined] |
| 367 | + |
| 368 | + |
300 | 369 | def test_pruned_err_after_fix(monkeypatch: pytest.MonkeyPatch, tmpdir: Path) -> None:
|
301 | 370 | """Test that pruned errors are not reported after fixing.
|
302 | 371 |
|
|
0 commit comments