From 8dabbd8f94289764a0e4f4b454eed2e140e14f4a Mon Sep 17 00:00:00 2001 From: Felix Fontein Date: Wed, 12 Mar 2025 22:12:12 +0100 Subject: [PATCH] Use shared unit test utils from community.internal_test_tools (#854) * Use shared unit test utils from community.internal_test_tools. * Make sure community.internal_test_tools is installed in CI. --- .github/workflows/ansible-test.yml | 4 +++ tests/unit/compat/__init__.py | 0 tests/unit/compat/builtins.py | 20 ------------- tests/unit/compat/mock.py | 30 ------------------- tests/unit/compat/unittest.py | 25 ---------------- .../acme/test_backend_cryptography.py | 2 +- .../acme/test_backend_openssl_cli.py | 2 +- .../module_utils/acme/test_challenges.py | 2 +- .../plugins/module_utils/acme/test_errors.py | 2 +- .../unit/plugins/module_utils/acme/test_io.py | 2 +- .../plugins/module_utils/acme/test_orders.py | 2 +- tests/utils/shippable/shippable.sh | 12 ++++---- 12 files changed, 15 insertions(+), 88 deletions(-) delete mode 100644 tests/unit/compat/__init__.py delete mode 100644 tests/unit/compat/builtins.py delete mode 100644 tests/unit/compat/mock.py delete mode 100644 tests/unit/compat/unittest.py diff --git a/.github/workflows/ansible-test.yml b/.github/workflows/ansible-test.yml index c8f3ba2c..6fcf441a 100644 --- a/.github/workflows/ansible-test.yml +++ b/.github/workflows/ansible-test.yml @@ -45,6 +45,8 @@ jobs: ansible-core-version: stable-${{ matrix.ansible }} codecov-token: ${{ secrets.CODECOV_TOKEN }} coverage: ${{ github.event_name == 'schedule' && 'always' || 'never' }} + pre-test-cmd: >- + git clone --depth=1 --single-branch https://github.com/ansible-collections/community.internal_test_tools.git ../../community/internal_test_tools pull-request-change-detection: 'true' testing-type: sanity @@ -74,6 +76,8 @@ jobs: ansible-core-version: stable-${{ matrix.ansible }} codecov-token: ${{ secrets.CODECOV_TOKEN }} coverage: ${{ github.event_name == 'schedule' && 'always' || 'never' }} + pre-test-cmd: >- + git clone --depth=1 --single-branch https://github.com/ansible-collections/community.internal_test_tools.git ../../community/internal_test_tools pull-request-change-detection: 'true' testing-type: units diff --git a/tests/unit/compat/__init__.py b/tests/unit/compat/__init__.py deleted file mode 100644 index e69de29b..00000000 diff --git a/tests/unit/compat/builtins.py b/tests/unit/compat/builtins.py deleted file mode 100644 index d548601d..00000000 --- a/tests/unit/compat/builtins.py +++ /dev/null @@ -1,20 +0,0 @@ -# Copyright (c) 2014, Toshio Kuratomi -# GNU General Public License v3.0+ (see LICENSES/GPL-3.0-or-later.txt or https://www.gnu.org/licenses/gpl-3.0.txt) -# SPDX-License-Identifier: GPL-3.0-or-later - -# Make coding more python3-ish -from __future__ import (absolute_import, division, print_function) -__metaclass__ = type - -# -# Compat for python2.7 -# - -# One unittest needs to import builtins via __import__() so we need to have -# the string that represents it -try: - import __builtin__ # noqa: F401, pylint: disable=unused-import -except ImportError: - BUILTINS = 'builtins' -else: - BUILTINS = '__builtin__' diff --git a/tests/unit/compat/mock.py b/tests/unit/compat/mock.py deleted file mode 100644 index 6ef80a7c..00000000 --- a/tests/unit/compat/mock.py +++ /dev/null @@ -1,30 +0,0 @@ -# Copyright (c) 2014, Toshio Kuratomi -# GNU General Public License v3.0+ (see LICENSES/GPL-3.0-or-later.txt or https://www.gnu.org/licenses/gpl-3.0.txt) -# SPDX-License-Identifier: GPL-3.0-or-later - -# Make coding more python3-ish -from __future__ import (absolute_import, division, print_function) -__metaclass__ = type - -''' -Compat module for Python3.x's unittest.mock module -''' -import sys # noqa: F401, pylint: disable=unused-import - -# Python 2.7 - -# Note: Could use the pypi mock library on python3.x as well as python2.x. It -# is the same as the python3 stdlib mock library - -try: - # Allow wildcard import because we really do want to import all of mock's - # symbols into this compat shim - # pylint: disable=wildcard-import,unused-wildcard-import - from unittest.mock import * # noqa: F401, pylint: disable=unused-import -except ImportError: - # Python 2 - # pylint: disable=wildcard-import,unused-wildcard-import - try: - from mock import * # noqa: F401, pylint: disable=unused-import - except ImportError: - print('You need the mock library installed on python2.x to run tests') diff --git a/tests/unit/compat/unittest.py b/tests/unit/compat/unittest.py deleted file mode 100644 index d50bab86..00000000 --- a/tests/unit/compat/unittest.py +++ /dev/null @@ -1,25 +0,0 @@ -# Copyright (c) 2014, Toshio Kuratomi -# GNU General Public License v3.0+ (see LICENSES/GPL-3.0-or-later.txt or https://www.gnu.org/licenses/gpl-3.0.txt) -# SPDX-License-Identifier: GPL-3.0-or-later - -# Make coding more python3-ish -from __future__ import (absolute_import, division, print_function) -__metaclass__ = type - -''' -Compat module for Python2.7's unittest module -''' - -import sys - -# Allow wildcard import because we really do want to import all of -# unittests's symbols into this compat shim -# pylint: disable=wildcard-import,unused-wildcard-import -if sys.version_info < (2, 7): - try: - # Need unittest2 on python2.6 - from unittest2 import * # noqa: F401, pylint: disable=unused-import - except ImportError: - print('You need unittest2 installed on python2.6.x to run tests') -else: - from unittest import * # noqa: F401, pylint: disable=unused-import diff --git a/tests/unit/plugins/module_utils/acme/test_backend_cryptography.py b/tests/unit/plugins/module_utils/acme/test_backend_cryptography.py index 816acf25..732a0f0a 100644 --- a/tests/unit/plugins/module_utils/acme/test_backend_cryptography.py +++ b/tests/unit/plugins/module_utils/acme/test_backend_cryptography.py @@ -10,7 +10,7 @@ import datetime import pytest from freezegun import freeze_time -from ansible_collections.community.crypto.tests.unit.compat.mock import MagicMock +from ansible_collections.community.internal_test_tools.tests.unit.compat.mock import MagicMock from ansible_collections.community.crypto.plugins.module_utils.acme.backend_cryptography import ( HAS_CURRENT_CRYPTOGRAPHY, diff --git a/tests/unit/plugins/module_utils/acme/test_backend_openssl_cli.py b/tests/unit/plugins/module_utils/acme/test_backend_openssl_cli.py index 10020583..0560d1b6 100644 --- a/tests/unit/plugins/module_utils/acme/test_backend_openssl_cli.py +++ b/tests/unit/plugins/module_utils/acme/test_backend_openssl_cli.py @@ -10,7 +10,7 @@ import datetime import pytest from freezegun import freeze_time -from ansible_collections.community.crypto.tests.unit.compat.mock import MagicMock +from ansible_collections.community.internal_test_tools.tests.unit.compat.mock import MagicMock from ansible_collections.community.crypto.plugins.module_utils.acme.backend_openssl_cli import ( diff --git a/tests/unit/plugins/module_utils/acme/test_challenges.py b/tests/unit/plugins/module_utils/acme/test_challenges.py index 18e4118f..aa61fffd 100644 --- a/tests/unit/plugins/module_utils/acme/test_challenges.py +++ b/tests/unit/plugins/module_utils/acme/test_challenges.py @@ -8,7 +8,7 @@ __metaclass__ = type import pytest -from ansible_collections.community.crypto.tests.unit.compat.mock import MagicMock +from ansible_collections.community.internal_test_tools.tests.unit.compat.mock import MagicMock from ansible_collections.community.crypto.plugins.module_utils.acme.challenges import ( diff --git a/tests/unit/plugins/module_utils/acme/test_errors.py b/tests/unit/plugins/module_utils/acme/test_errors.py index c84015d3..6eb0464d 100644 --- a/tests/unit/plugins/module_utils/acme/test_errors.py +++ b/tests/unit/plugins/module_utils/acme/test_errors.py @@ -8,7 +8,7 @@ __metaclass__ = type import pytest -from ansible_collections.community.crypto.tests.unit.compat.mock import MagicMock +from ansible_collections.community.internal_test_tools.tests.unit.compat.mock import MagicMock from ansible_collections.community.crypto.plugins.module_utils.acme.errors import ( diff --git a/tests/unit/plugins/module_utils/acme/test_io.py b/tests/unit/plugins/module_utils/acme/test_io.py index 818a5fe8..02a928ab 100644 --- a/tests/unit/plugins/module_utils/acme/test_io.py +++ b/tests/unit/plugins/module_utils/acme/test_io.py @@ -6,7 +6,7 @@ from __future__ import absolute_import, division, print_function __metaclass__ = type -from ansible_collections.community.crypto.tests.unit.compat.mock import MagicMock +from ansible_collections.community.internal_test_tools.tests.unit.compat.mock import MagicMock from ansible_collections.community.crypto.plugins.module_utils.acme.io import ( diff --git a/tests/unit/plugins/module_utils/acme/test_orders.py b/tests/unit/plugins/module_utils/acme/test_orders.py index c2139083..68a450b3 100644 --- a/tests/unit/plugins/module_utils/acme/test_orders.py +++ b/tests/unit/plugins/module_utils/acme/test_orders.py @@ -8,7 +8,7 @@ __metaclass__ = type import pytest -from ansible_collections.community.crypto.tests.unit.compat.mock import MagicMock +from ansible_collections.community.internal_test_tools.tests.unit.compat.mock import MagicMock from ansible_collections.community.crypto.plugins.module_utils.acme.orders import ( diff --git a/tests/utils/shippable/shippable.sh b/tests/utils/shippable/shippable.sh index 2ef1d2f3..656fc968 100755 --- a/tests/utils/shippable/shippable.sh +++ b/tests/utils/shippable/shippable.sh @@ -83,13 +83,11 @@ if [ "${test}" == "sanity/extra" ]; then fi # START: HACK install integration test dependencies -if [ "${script}" != "units" ] && [ "${script}" != "sanity" ] || [ "${test}" == "sanity/extra" ]; then - # Nothing further should be added to this list. - # This is to prevent modules or plugins in this collection having a runtime dependency on other collections. - retry git clone --depth=1 --single-branch https://github.com/ansible-collections/community.internal_test_tools.git "${ANSIBLE_COLLECTIONS_PATHS}/ansible_collections/community/internal_test_tools" - # NOTE: we're installing with git to work around Galaxy being a huge PITA (https://github.com/ansible/galaxy/issues/2429) - # retry ansible-galaxy -vvv collection install community.internal_test_tools -fi +# Nothing further should be added to this list. +# This is to prevent modules or plugins in this collection having a runtime dependency on other collections. +retry git clone --depth=1 --single-branch https://github.com/ansible-collections/community.internal_test_tools.git "${ANSIBLE_COLLECTIONS_PATHS}/ansible_collections/community/internal_test_tools" +# NOTE: we're installing with git to work around Galaxy being a huge PITA (https://github.com/ansible/galaxy/issues/2429) +# retry ansible-galaxy -vvv collection install community.internal_test_tools if [ "${script}" != "units" ] && [ "${script}" != "sanity" ] && [ "${test}" != "sanity/extra" ] && [ "${ansible_version}" != "2.9" ]; then retry git clone --depth=1 --single-branch https://github.com/ansible-collections/community.general.git "${ANSIBLE_COLLECTIONS_PATHS}/ansible_collections/community/general"