Skip to content

Commit 42ae617

Browse files
authored
Ensure that presence of pytest-ansible does not break ansible-test (#478)
1 parent fe150e9 commit 42ae617

File tree

8 files changed

+74
-26
lines changed

8 files changed

+74
-26
lines changed

.config/pydoclint-baseline.txt

Lines changed: 0 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -142,14 +142,6 @@ src/pytest_ansible/molecule.py
142142
DOC103: Method `MoleculeScenario.__init__`: Docstring arguments are different from function arguments. (Or could be other formatting issues: https://jsh9.github.io/pydoclint/violation_codes.html#notes-on-doc103 ). Arguments in the function signature but not in the docstring: [name: str, parent_directory: Path, test_id: str].
143143
--------------------
144144
src/pytest_ansible/plugin.py
145-
DOC101: Function `pytest_addoption`: Docstring contains fewer arguments than in function signature.
146-
DOC106: Function `pytest_addoption`: The option `--arg-type-hints-in-signature` is `True` but there are no argument type hints in the signature
147-
DOC107: Function `pytest_addoption`: The option `--arg-type-hints-in-signature` is `True` but not all args in the signature have type hints
148-
DOC103: Function `pytest_addoption`: Docstring arguments are different from function arguments. (Or could be other formatting issues: https://jsh9.github.io/pydoclint/violation_codes.html#notes-on-doc103 ). Arguments in the function signature but not in the docstring: [parser: ].
149-
DOC101: Function `pytest_configure`: Docstring contains fewer arguments than in function signature.
150-
DOC106: Function `pytest_configure`: The option `--arg-type-hints-in-signature` is `True` but there are no argument type hints in the signature
151-
DOC107: Function `pytest_configure`: The option `--arg-type-hints-in-signature` is `True` but not all args in the signature have type hints
152-
DOC103: Function `pytest_configure`: Docstring arguments are different from function arguments. (Or could be other formatting issues: https://jsh9.github.io/pydoclint/violation_codes.html#notes-on-doc103 ). Arguments in the function signature but not in the docstring: [config: ].
153145
DOC101: Function `pytest_collect_file`: Docstring contains fewer arguments than in function signature.
154146
DOC103: Function `pytest_collect_file`: Docstring arguments are different from function arguments. (Or could be other formatting issues: https://jsh9.github.io/pydoclint/violation_codes.html#notes-on-doc103 ). Arguments in the function signature but not in the docstring: [file_path: Path | None, parent: pytest.Collector].
155147
DOC201: Function `pytest_collect_file` does not have a return section in docstring

.gitignore

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -174,3 +174,4 @@ _readthedocs
174174

175175
# JetBrains
176176
.idea
177+
tests/fixtures/ansible_collections/test/test/tests/output

pyproject.toml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -371,6 +371,7 @@ required-imports = ["from __future__ import annotations"]
371371
[tool.ruff.lint.per-file-ignores]
372372
"_version.py" = ["SIM108"]
373373
"tests/**" = ["SLF001", "S101", "S602", "T201"]
374+
"tests/fixtures/ansible_collections/test/test/tests/unit/plugins/modules/test_pool.py" = ["INP001"]
374375

375376
[tool.ruff.lint.pydocstyle]
376377
convention = "google"

src/pytest_ansible/has_version.py

Lines changed: 21 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -2,15 +2,27 @@
22

33
from __future__ import annotations
44

5-
import ansible
65

7-
from packaging.version import parse as parse_version
6+
has_ansible_v2 = False
7+
has_ansible_v24 = False
8+
has_ansible_v28 = False
9+
has_ansible_v29 = False
10+
has_ansible_v212 = False
11+
has_ansible_v213 = False
12+
has_ansible_v219 = False
813

14+
try:
15+
import ansible
916

10-
has_ansible_v2 = parse_version(ansible.__version__) >= parse_version("2.0.0")
11-
has_ansible_v24 = parse_version(ansible.__version__) >= parse_version("2.4.0")
12-
has_ansible_v28 = parse_version(ansible.__version__) >= parse_version("2.8.0.dev0")
13-
has_ansible_v29 = parse_version(ansible.__version__) >= parse_version("2.9.0")
14-
has_ansible_v212 = parse_version(ansible.__version__) >= parse_version("2.12.0")
15-
has_ansible_v213 = parse_version(ansible.__version__) >= parse_version("2.13.0")
16-
has_ansible_v219 = parse_version(ansible.__version__) >= parse_version("2.19.0.dev0")
17+
from packaging.version import parse as parse_version
18+
19+
has_ansible_v2 = parse_version(ansible.__version__) >= parse_version("2.0.0")
20+
has_ansible_v24 = parse_version(ansible.__version__) >= parse_version("2.4.0")
21+
has_ansible_v28 = parse_version(ansible.__version__) >= parse_version("2.8.0.dev0")
22+
has_ansible_v29 = parse_version(ansible.__version__) >= parse_version("2.9.0")
23+
has_ansible_v212 = parse_version(ansible.__version__) >= parse_version("2.12.0")
24+
has_ansible_v213 = parse_version(ansible.__version__) >= parse_version("2.13.0")
25+
has_ansible_v219 = parse_version(ansible.__version__) >= parse_version("2.19.0.dev0")
26+
27+
except (ImportError, AttributeError):
28+
pass

src/pytest_ansible/plugin.py

Lines changed: 30 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -10,13 +10,20 @@
1010
from pathlib import Path
1111
from typing import TYPE_CHECKING, Any
1212

13-
import ansible
14-
import ansible.constants
15-
import ansible.errors
16-
import ansible.utils
17-
import ansible.utils.display
1813
import pytest
1914

15+
16+
try:
17+
import ansible
18+
import ansible.constants
19+
import ansible.errors
20+
import ansible.utils
21+
import ansible.utils.display
22+
23+
HAS_ANSIBLE = True
24+
except ImportError:
25+
HAS_ANSIBLE = False
26+
2027
from typing_extensions import deprecated
2128

2229
from pytest_ansible.fixtures import (
@@ -86,8 +93,14 @@ def _load_scenarios(config: pytest.Config) -> None:
8693
logger.warning(msg)
8794

8895

89-
def pytest_addoption(parser): # type: ignore[no-untyped-def] # noqa: ANN001, ANN201
90-
"""Add options to control ansible."""
96+
def pytest_addoption(parser: pytest.Parser) -> None:
97+
"""Add options to control ansible.
98+
99+
Args:
100+
parser: pytest.Parser
101+
"""
102+
if not HAS_ANSIBLE:
103+
return
91104
group = parser.getgroup("pytest-ansible")
92105
group.addoption(
93106
"--inventory",
@@ -226,8 +239,14 @@ def pytest_addoption(parser): # type: ignore[no-untyped-def] # noqa: ANN001, A
226239
parser.addini("ansible", "Ansible integration", "args")
227240

228241

229-
def pytest_configure(config): # type: ignore[no-untyped-def] # noqa: ANN001, ANN201
230-
"""Validate --ansible-* parameters."""
242+
def pytest_configure(config: pytest.Config) -> None:
243+
"""Validate --ansible-* parameters.
244+
245+
Args:
246+
config: pytest.Config
247+
"""
248+
if not HAS_ANSIBLE:
249+
return
231250
config.addinivalue_line("markers", "ansible(**kwargs): Ansible integration")
232251

233252
# Enable connection debugging
@@ -265,6 +284,8 @@ def pytest_collect_file(
265284
parent: pytest.Collector,
266285
) -> Node | None:
267286
"""Transform each found molecule.yml into a pytest test.""" # noqa: DOC201
287+
if not hasattr(parent.config.option, "molecule"):
288+
return None
268289
if not parent.config.option.molecule:
269290
return None
270291
if not HAS_MOLECULE: # pragma: no cover
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
"""Sample test file."""

tests/test_ansible_test.py

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
"""Checks compatibility with ansible-test."""
2+
3+
from __future__ import annotations
4+
5+
import subprocess # noqa: S404
6+
7+
8+
def test_ansible_test() -> None:
9+
"""Test for params."""
10+
proc = subprocess.run(
11+
"ansible-test units --target-python default", # noqa: S607
12+
shell=True,
13+
capture_output=True,
14+
cwd="tests/fixtures/ansible_collections/test/test",
15+
check=False,
16+
)
17+
assert proc.returncode == 0

tests/test_host_manager.py

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -96,6 +96,7 @@ def test_host_manager_getitem(host_pattern, num_hosts, hosts, include_extra_inve
9696
assert _hosts[host_pattern]
9797

9898

99+
@skip_ansible_219
99100
@pytest.mark.parametrize(
100101
("host_pattern", "num_hosts"),
101102
NEGATIVE_HOST_PATTERNS,
@@ -149,6 +150,7 @@ def test_host_manager_slice(host_slice, num_hosts, hosts, include_extra_inventor
149150

150151

151152
# pylint: disable=pointless-statement
153+
@skip_ansible_219
152154
@pytest.mark.parametrize(
153155
"host_slice",
154156
NEGATIVE_HOST_SLICES,
@@ -184,6 +186,7 @@ def test_host_manager_not_getattr( # type: ignore[no-untyped-def] # noqa: ANN2
184186
getattr(_hosts, host_pattern)
185187

186188

189+
@skip_ansible_219
187190
def test_defaults(request): # type: ignore[no-untyped-def] # noqa: ANN001, ANN201, D103
188191
from ansible.constants import DEFAULT_TRANSPORT # pylint: disable=no-name-in-module
189192

0 commit comments

Comments
 (0)