|
10 | 10 | from pathlib import Path |
11 | 11 | from typing import TYPE_CHECKING, Any |
12 | 12 |
|
13 | | -import ansible |
14 | | -import ansible.constants |
15 | | -import ansible.errors |
16 | | -import ansible.utils |
17 | | -import ansible.utils.display |
18 | 13 | import pytest |
19 | 14 |
|
| 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 | + |
20 | 27 | from typing_extensions import deprecated |
21 | 28 |
|
22 | 29 | from pytest_ansible.fixtures import ( |
@@ -86,8 +93,14 @@ def _load_scenarios(config: pytest.Config) -> None: |
86 | 93 | logger.warning(msg) |
87 | 94 |
|
88 | 95 |
|
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 |
91 | 104 | group = parser.getgroup("pytest-ansible") |
92 | 105 | group.addoption( |
93 | 106 | "--inventory", |
@@ -226,8 +239,14 @@ def pytest_addoption(parser): # type: ignore[no-untyped-def] # noqa: ANN001, A |
226 | 239 | parser.addini("ansible", "Ansible integration", "args") |
227 | 240 |
|
228 | 241 |
|
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 |
231 | 250 | config.addinivalue_line("markers", "ansible(**kwargs): Ansible integration") |
232 | 251 |
|
233 | 252 | # Enable connection debugging |
@@ -265,6 +284,8 @@ def pytest_collect_file( |
265 | 284 | parent: pytest.Collector, |
266 | 285 | ) -> Node | None: |
267 | 286 | """Transform each found molecule.yml into a pytest test.""" # noqa: DOC201 |
| 287 | + if not hasattr(parent.config.option, "molecule"): |
| 288 | + return None |
268 | 289 | if not parent.config.option.molecule: |
269 | 290 | return None |
270 | 291 | if not HAS_MOLECULE: # pragma: no cover |
|
0 commit comments