From 1e467084acc6db9d2a857dfbc649f0a3bbfd5d0d Mon Sep 17 00:00:00 2001 From: Sorin Sbarnea Date: Fri, 24 Mar 2023 13:14:39 +0000 Subject: [PATCH] Refactor tests (#3211) --- examples/playbooks/rule-key-order-pass.yml | 16 ++++++++ src/ansiblelint/rules/key_order.py | 43 ++++++++-------------- 2 files changed, 31 insertions(+), 28 deletions(-) create mode 100644 examples/playbooks/rule-key-order-pass.yml diff --git a/examples/playbooks/rule-key-order-pass.yml b/examples/playbooks/rule-key-order-pass.yml new file mode 100644 index 0000000000..eb141e541b --- /dev/null +++ b/examples/playbooks/rule-key-order-pass.yml @@ -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 diff --git a/src/ansiblelint/rules/key_order.py b/src/ansiblelint/rules/key_order.py index 5de637c09b..383b8a2c19 100644 --- a/src/ansiblelint/rules/key_order.py +++ b/src/ansiblelint/rules/key_order.py @@ -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 @@ -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"),