Debian Codegraph @

ansible_collections/community/windows/tests/unit/conftest.py

raw
1 """Enable unit testing of Ansible collections. PYTEST_DONT_REWRITE"""
2 from __future__ import (absolute_import, division, print_function)
3 __metaclass__ = type
4
5 import os
6 import os.path
7
8 from ansible.utils.collection_loader._collection_finder import _AnsibleCollectionFinder
9
10
11 ANSIBLE_COLLECTIONS_PATH = os.path.abspath(os.path.join(__file__, '..', '..', '..', '..', '..'))
12
13
14 # this monkeypatch to _pytest.pathlib.resolve_package_path fixes PEP420 resolution for collections in pytest >= 6.0.0
15 def collection_resolve_package_path(path):
16 """Configure the Python package path so that pytest can find our collections."""
17 for parent in path.parents:
18 if str(parent) == ANSIBLE_COLLECTIONS_PATH:
19 return parent
20
21 raise Exception('File "%s" not found in collection path "%s".' % (path, ANSIBLE_COLLECTIONS_PATH))
22
23
24 def pytest_configure():
25 """Configure this pytest plugin."""
26
27 try:
28 if pytest_configure.executed:
29 return
30 except AttributeError:
31 pytest_configure.executed = True
32
33 # allow unit tests to import code from collections
34
35 # noinspection PyProtectedMember
36 _AnsibleCollectionFinder(paths=[os.path.dirname(ANSIBLE_COLLECTIONS_PATH)])._install() # pylint: disable=protected-access
37
38 # noinspection PyProtectedMember
39 from _pytest import pathlib as pytest_pathlib
40 pytest_pathlib.resolve_package_path = collection_resolve_package_path
41
42
43 pytest_configure()