Skip to content

Commit

Permalink
Refactor literal-compare tests (#3265)
Browse files Browse the repository at this point in the history
  • Loading branch information
ssbarnea committed Apr 11, 2023
1 parent c0c2022 commit f0f198a
Show file tree
Hide file tree
Showing 6 changed files with 79 additions and 95 deletions.
2 changes: 1 addition & 1 deletion .github/workflows/tox.yml
Expand Up @@ -59,7 +59,7 @@ jobs:
WSLENV: FORCE_COLOR:PYTEST_REQPASS:TOXENV:GITHUB_STEP_SUMMARY
# Number of expected test passes, safety measure for accidental skip of
# tests. Update value if you add/remove tests.
PYTEST_REQPASS: 801
PYTEST_REQPASS: 798
steps:
- name: Activate WSL1
if: "contains(matrix.shell, 'wsl')"
Expand Down
19 changes: 19 additions & 0 deletions examples/playbooks/rule_literal_compare_fail.yml
@@ -0,0 +1,19 @@
---
- name: Fixture for literal-compare
hosts: localhost
tasks:
- name: Example task # <-- 1st
ansible.builtin.debug:
msg: test
when: my_var == True

- name: Example task # <-- 2nd
ansible.builtin.debug:
msg: test
when: my_var == false

- name: Another example task # <-- 3rd
ansible.builtin.debug:
msg: test
when:
- my_var == false
25 changes: 25 additions & 0 deletions examples/playbooks/rule_literal_compare_pass.yml
@@ -0,0 +1,25 @@
---
- name: Fixture for literal-compare
hosts: localhost
tasks:
- name: Example task
ansible.builtin.debug:
msg: test
when: my_var

- name: Another example task
ansible.builtin.debug:
msg: test
when:
- 1 + 1 == 2
- true

- name: Example task
ansible.builtin.debug:
msg: test
when: not my_var

- name: Example task
ansible.builtin.debug:
msg: test
when: my_var not None
33 changes: 33 additions & 0 deletions src/ansiblelint/rules/literal_compare.py
Expand Up @@ -5,6 +5,7 @@
from __future__ import annotations

import re
import sys
from typing import TYPE_CHECKING, Any

from ansiblelint.rules import AnsibleLintRule
Expand Down Expand Up @@ -46,3 +47,35 @@ def matchtask(
return True

return False


if "pytest" in sys.modules:
import pytest

from ansiblelint.rules import RulesCollection # pylint: disable=ungrouped-imports
from ansiblelint.runner import Runner # pylint: disable=ungrouped-imports

@pytest.mark.parametrize(
("test_file", "failures"),
(
pytest.param(
"examples/playbooks/rule_literal_compare_fail.yml",
3,
id="fail",
),
pytest.param(
"examples/playbooks/rule_literal_compare_pass.yml",
0,
id="pass",
),
),
)
def test_literal_compare(
default_rules_collection: RulesCollection, test_file: str, failures: int
) -> None:
"""Test rule matches."""
# Enable checking of loop variable prefixes in roles
results = Runner(test_file, rules=default_rules_collection).run()
for result in results:
assert result.rule.id == "literal-compare"
assert len(results) == failures
2 changes: 1 addition & 1 deletion src/ansiblelint/schemas/__store__.json
Expand Up @@ -24,7 +24,7 @@
"url": "https://raw.githubusercontent.com/ansible/ansible-lint/main/src/ansiblelint/schemas/inventory.json"
},
"meta": {
"etag": "f7bf684445ab4b7fffe05a4bfb56ecb22421fff04910b236f1961db3ad817999",
"etag": "45184c720bb76a0160b44a0fe1cb024075ae275b9a978fd547cc2c0a14e32ecc",
"url": "https://raw.githubusercontent.com/ansible/ansible-lint/main/src/ansiblelint/schemas/meta.json"
},
"meta-runtime": {
Expand Down
93 changes: 0 additions & 93 deletions test/rules/test_literal_compare.py

This file was deleted.

0 comments on commit f0f198a

Please sign in to comment.