Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Refactor tests #3211

Merged
merged 1 commit into from
Mar 24, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
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