Skip to content

Commit

Permalink
Refactor tests (#3211)
Browse files Browse the repository at this point in the history
  • Loading branch information
ssbarnea authored Mar 24, 2023
1 parent 33de6bc commit 1e46708
Show file tree
Hide file tree
Showing 2 changed files with 31 additions and 28 deletions.
16 changes: 16 additions & 0 deletions examples/playbooks/rule-key-order-pass.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
---
- name: Fixture for key-order rule
hosts: localhost
tasks:
- name: Test
ansible.builtin.command: echo "test"
changed_when: false
- name: Test2
ansible.builtin.debug:
msg: "Debug without a name"
- name: Flush handlers
ansible.builtin.meta: flush_handlers
- no_log: true # noqa: key-order[task] command-instead-of-shell
ansible.builtin.shell: echo hello
name: Task with no_log on top
changed_when: false
43 changes: 15 additions & 28 deletions src/ansiblelint/rules/key_order.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,6 @@

from ansiblelint.file_utils import Lintable
from ansiblelint.rules import AnsibleLintRule
from ansiblelint.testing import RunFromText

if TYPE_CHECKING:
from ansiblelint.errors import MatchError
Expand Down Expand Up @@ -79,36 +78,24 @@ def matchtask(
if "pytest" in sys.modules:
import pytest

PLAY_SUCCESS = """---
- hosts: localhost
tasks:
- name: Test
command: echo "test"
- name: Test2
debug:
msg: "Debug without a name"
- name: Flush handlers
meta: flush_handlers
- no_log: true # noqa: key-order
shell: echo hello
name: Task with no_log on top
"""

@pytest.mark.parametrize("rule_runner", (KeyOrderRule,), indirect=["rule_runner"])
def test_key_order_task_name_has_name_first_rule_pass(
rule_runner: RunFromText,
) -> None:
"""Test rule matches."""
results = rule_runner.run_playbook(PLAY_SUCCESS)
assert len(results) == 0
from ansiblelint.rules import RulesCollection # pylint: disable=ungrouped-imports
from ansiblelint.runner import Runner # pylint: disable=ungrouped-imports

@pytest.mark.parametrize("rule_runner", (KeyOrderRule,), indirect=["rule_runner"])
def test_key_order_task_name_has_name_first_rule_fail(
rule_runner: RunFromText,
@pytest.mark.parametrize(
("test_file", "failures"),
(
pytest.param("examples/playbooks/rule-key-order-pass.yml", 0, id="pass"),
pytest.param("examples/playbooks/rule-key-order-fail.yml", 6, id="fail"),
),
)
def test_key_order_rule(
default_rules_collection: RulesCollection, test_file: str, failures: int
) -> None:
"""Test rule matches."""
results = rule_runner.run("examples/playbooks/rule-key-order-fail.yml")
assert len(results) == 6
results = Runner(test_file, rules=default_rules_collection).run()
assert len(results) == failures
for result in results:
assert result.rule.id == "key-order"

@pytest.mark.parametrize(
("properties", "expected"),
Expand Down

0 comments on commit 1e46708

Please sign in to comment.