summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorMatt Clay <matt@mystile.com>2022-06-08 07:52:26 -0700
committerGitHub <noreply@github.com>2022-06-08 09:52:26 -0500
commit567a295c0026d6e79b8970846ee3550b4c124982 (patch)
tree7cef0d21d227f778e34abc858dad0bbfd6ee72a5
parentae380e3bef989cfce16ce3ab70d7b5fc28fb6c86 (diff)
downloadansible-567a295c0026d6e79b8970846ee3550b4c124982.tar.gz
[stable-2.13] ansible-test - Adjust unit test mock usage. (#77961) (#77999)
(cherry picked from commit 23914d3f0b5d69e7710c3744f373203c8caa6693) Co-authored-by: Matt Clay <matt@mystile.com>
-rw-r--r--changelogs/fragments/ansible-test-ansible-core-mock.yml3
-rw-r--r--test/lib/ansible_test/_data/pytest/config/ansible-core.ini4
-rw-r--r--test/lib/ansible_test/_data/pytest/config/default.ini2
-rw-r--r--test/lib/ansible_test/_data/pytest/config/legacy.ini4
-rw-r--r--test/lib/ansible_test/_internal/commands/units/__init__.py20
5 files changed, 21 insertions, 12 deletions
diff --git a/changelogs/fragments/ansible-test-ansible-core-mock.yml b/changelogs/fragments/ansible-test-ansible-core-mock.yml
index 86e3ea6546..60ea2d25fd 100644
--- a/changelogs/fragments/ansible-test-ansible-core-mock.yml
+++ b/changelogs/fragments/ansible-test-ansible-core-mock.yml
@@ -1,3 +1,2 @@
minor_changes:
- - ansible-test - Avoid using the ``mock_use_standalone_module`` setting for ``pytest`` when running ansible-core unit tests.
- This has no effect on unit tests for collections.
+ - ansible-test - Avoid using the ``mock_use_standalone_module`` setting for unit tests running on Python 3.8 or later.
diff --git a/test/lib/ansible_test/_data/pytest/config/ansible-core.ini b/test/lib/ansible_test/_data/pytest/config/ansible-core.ini
deleted file mode 100644
index 60575bfe32..0000000000
--- a/test/lib/ansible_test/_data/pytest/config/ansible-core.ini
+++ /dev/null
@@ -1,4 +0,0 @@
-[pytest]
-xfail_strict = true
-# avoid using 'mock_use_standalone_module = true' so package maintainers can avoid packaging 'mock'
-junit_family = xunit1
diff --git a/test/lib/ansible_test/_data/pytest/config/default.ini b/test/lib/ansible_test/_data/pytest/config/default.ini
index b2668dc287..60575bfe32 100644
--- a/test/lib/ansible_test/_data/pytest/config/default.ini
+++ b/test/lib/ansible_test/_data/pytest/config/default.ini
@@ -1,4 +1,4 @@
[pytest]
xfail_strict = true
-mock_use_standalone_module = true
+# avoid using 'mock_use_standalone_module = true' so package maintainers can avoid packaging 'mock'
junit_family = xunit1
diff --git a/test/lib/ansible_test/_data/pytest/config/legacy.ini b/test/lib/ansible_test/_data/pytest/config/legacy.ini
new file mode 100644
index 0000000000..b2668dc287
--- /dev/null
+++ b/test/lib/ansible_test/_data/pytest/config/legacy.ini
@@ -0,0 +1,4 @@
+[pytest]
+xfail_strict = true
+mock_use_standalone_module = true
+junit_family = xunit1
diff --git a/test/lib/ansible_test/_internal/commands/units/__init__.py b/test/lib/ansible_test/_internal/commands/units/__init__.py
index ef65df29d4..f20e96fd2f 100644
--- a/test/lib/ansible_test/_internal/commands/units/__init__.py
+++ b/test/lib/ansible_test/_internal/commands/units/__init__.py
@@ -21,6 +21,7 @@ from ...util import (
ANSIBLE_TEST_DATA_ROOT,
display,
is_subdir,
+ str_to_version,
SubprocessError,
ANSIBLE_LIB_ROOT,
ANSIBLE_TEST_TARGET_ROOT,
@@ -234,12 +235,21 @@ def command_units(args): # type: (UnitsConfig) -> None
if args.requirements_mode == 'only':
sys.exit()
- if data_context().content.is_ansible:
- config_name = 'ansible-core.ini'
- else:
- config_name = 'default.ini'
-
for test_context, python, paths, env in test_sets:
+ # When using pytest-mock, make sure that features introduced in Python 3.8 are available to older Python versions.
+ # This is done by enabling the mock_use_standalone_module feature, which forces use of mock even when unittest.mock is available.
+ # Later Python versions have not introduced additional unittest.mock features, so use of mock is not needed as of Python 3.8.
+ # If future Python versions introduce new unittest.mock features, they will not be available to older Python versions.
+ # Having the cutoff at Python 3.8 also eases packaging of ansible-core since no supported controller version requires the use of mock.
+ #
+ # NOTE: This only affects use of pytest-mock.
+ # Collection unit tests may directly import mock, which will be provided by ansible-test when it installs requirements using pip.
+ # Although mock is available for ansible-core unit tests, they should import units.compat.mock instead.
+ if str_to_version(python.version) < (3, 8):
+ config_name = 'legacy.ini'
+ else:
+ config_name = 'default.ini'
+
cmd = [
'pytest',
'--forked',