|
| 1 | +"""Test compatibility functions.""" |
| 2 | + |
| 3 | +from __future__ import annotations |
| 4 | + |
| 5 | +import pytest |
| 6 | +from packaging.version import Version |
| 7 | + |
| 8 | +from ansible_compat.compatibility import should_auto_enable_plugin_loader |
| 9 | + |
| 10 | + |
| 11 | +@pytest.mark.parametrize( |
| 12 | + ("ansible_lint_version", "expected"), |
| 13 | + ( |
| 14 | + # ansible-lint < 25.8.1 should auto-enable |
| 15 | + ("25.8.0", True), |
| 16 | + ("25.7.9", True), |
| 17 | + ("25.0.0", True), |
| 18 | + ("24.9.9", True), |
| 19 | + ("6.22.2", True), |
| 20 | + # ansible-lint >= 25.8.1 should not auto-enable |
| 21 | + ("25.8.1", False), |
| 22 | + ("25.8.2", False), |
| 23 | + ("25.9.0", False), |
| 24 | + ("26.0.0", False), |
| 25 | + ("30.0.0", False), |
| 26 | + # No ansible-lint should not auto-enable |
| 27 | + (None, False), |
| 28 | + ), |
| 29 | +) |
| 30 | +def test_should_auto_enable_plugin_loader( |
| 31 | + ansible_lint_version: str | None, |
| 32 | + expected: bool, |
| 33 | + monkeypatch: pytest.MonkeyPatch, |
| 34 | +) -> None: |
| 35 | + """Test should_auto_enable_plugin_loader with different ansible-lint versions. |
| 36 | +
|
| 37 | + Args: |
| 38 | + ansible_lint_version: The ansible-lint version to mock, or None for no ansible-lint. |
| 39 | + expected: The expected result from should_auto_enable_plugin_loader(). |
| 40 | + monkeypatch: Pytest fixture for mocking. |
| 41 | + """ |
| 42 | + |
| 43 | + def mock_ansible_lint_version() -> Version | None: |
| 44 | + if ansible_lint_version is None: |
| 45 | + return None |
| 46 | + return Version(ansible_lint_version) |
| 47 | + |
| 48 | + monkeypatch.setattr( |
| 49 | + "ansible_compat.compatibility.ansible_lint_version", |
| 50 | + mock_ansible_lint_version, |
| 51 | + ) |
| 52 | + |
| 53 | + result = should_auto_enable_plugin_loader() |
| 54 | + assert ( |
| 55 | + result is expected |
| 56 | + ), f"Expected {expected} for ansible-lint {ansible_lint_version}, got {result}" |
0 commit comments