summaryrefslogtreecommitdiff
path: root/test/lib/ansible_test/_internal/util.py
diff options
context:
space:
mode:
Diffstat (limited to 'test/lib/ansible_test/_internal/util.py')
-rw-r--r--test/lib/ansible_test/_internal/util.py17
1 files changed, 17 insertions, 0 deletions
diff --git a/test/lib/ansible_test/_internal/util.py b/test/lib/ansible_test/_internal/util.py
index 53959d41a2..029f73be22 100644
--- a/test/lib/ansible_test/_internal/util.py
+++ b/test/lib/ansible_test/_internal/util.py
@@ -23,10 +23,14 @@ import time
import functools
import shlex
import typing as t
+import warnings
from struct import unpack, pack
from termios import TIOCGWINSZ
+# CAUTION: Avoid third-party imports in this module whenever possible.
+# Any third-party imports occurring here will result in an error if they are vendored by ansible-core.
+
try:
from typing_extensions import TypeGuard # TypeGuard was added in Python 3.10
except ImportError:
@@ -339,6 +343,17 @@ def get_ansible_version() -> str:
return ansible_version
+def _enable_vendoring() -> None:
+ """Enable support for loading Python packages vendored by ansible-core."""
+ # Load the vendoring code by file path, since ansible may not be in our sys.path.
+ # Convert warnings into errors, to avoid problems from surfacing later.
+
+ with warnings.catch_warnings():
+ warnings.filterwarnings('error')
+
+ load_module(os.path.join(ANSIBLE_LIB_ROOT, '_vendor', '__init__.py'), 'ansible_vendor')
+
+
@cache
def get_available_python_versions() -> dict[str, str]:
"""Return a dictionary indicating which supported Python versions are available."""
@@ -1154,3 +1169,5 @@ def type_guard(sequence: c.Sequence[t.Any], guard_type: t.Type[C]) -> TypeGuard[
display = Display() # pylint: disable=locally-disabled, invalid-name
+
+_enable_vendoring()