ansible-core/77961.patch

97 lines
4.6 KiB
Diff

From 4cb84c83adee345ad1d5dc9b9b79e8a9b9e6b2be Mon Sep 17 00:00:00 2001
From: Matt Clay <matt@mystile.com>
Date: Thu, 2 Jun 2022 15:34:46 -0700
Subject: [PATCH] ansible-test - Adjust unit test mock usage.
---
.../ansible-test-ansible-core-mock.yml | 3 +--
.../_data/pytest/config/ansible-core.ini | 4 ----
.../_data/pytest/config/default.ini | 2 +-
.../_data/pytest/config/legacy.ini | 4 ++++
.../_internal/commands/units/__init__.py | 20 ++++++++++++++-----
5 files changed, 21 insertions(+), 12 deletions(-)
delete mode 100644 test/lib/ansible_test/_data/pytest/config/ansible-core.ini
create mode 100644 test/lib/ansible_test/_data/pytest/config/legacy.ini
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',
--
2.35.3