From 5ff1bb7be4e12ccf8a66919166d8df9d5ece2937 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=D0=98=D1=80=D0=B8=D0=BD=D0=B0=20=D0=A0=D0=BE=D0=B7=D0=B5?= =?UTF-8?q?=D1=82?= Date: Mon, 14 Oct 2024 13:45:59 +0300 Subject: [PATCH] Add plugin loader test (#411) --- .github/workflows/tox.yml | 2 +- test/test_runtime.py | 38 ++++++++++++++++++++++++++++++++++++++ 2 files changed, 39 insertions(+), 1 deletion(-) diff --git a/.github/workflows/tox.yml b/.github/workflows/tox.yml index cb41b3a4..4150c602 100644 --- a/.github/workflows/tox.yml +++ b/.github/workflows/tox.yml @@ -50,7 +50,7 @@ jobs: matrix: ${{ fromJson(needs.prepare.outputs.matrix) }} env: FORCE_COLOR: 1 - PYTEST_REQPASS: 108 + PYTEST_REQPASS: 109 steps: - uses: actions/checkout@v4 with: diff --git a/test/test_runtime.py b/test/test_runtime.py index 605dc028..94232038 100644 --- a/test/test_runtime.py +++ b/test/test_runtime.py @@ -15,6 +15,8 @@ import pytest from packaging.version import Version +from ansible.plugins.loader import module_loader + from ansible_compat.constants import INVALID_PREREQUISITES_RC from ansible_compat.errors import ( AnsibleCommandError, @@ -742,6 +744,42 @@ def test_install_collection_from_disk( runtime.clean() +@pytest.mark.parametrize( + ("path", "expected_plugins"), + ( + pytest.param( + "test/collections/acme.goodies", + [ + "ansible.posix.patch", # from tests/requirements.yml + "community.crypto.acme_account", # from galaxy.yml as a git dependency + ], + id="modules", + ), + ), +) +def test_load_plugins( + path: str, + expected_plugins: list[str], +) -> None: + """Tests ability to load plugin from a collection installed by requirement.""" + with cwd(Path(path)): + from ansible_compat.prerun import get_cache_dir + rmtree(get_cache_dir(Path.cwd()), ignore_errors=True) + runtime = Runtime(isolated=True, require_module=True) + runtime.prepare_environment(install_local=True) + for plugin_name in expected_plugins: + loaded_module = module_loader.find_plugin_with_context( + plugin_name, + ignore_deprecated=True, + check_aliases=True, + ) + assert ( + loaded_module.resolved_fqcn is not None + ), f"Unable to load module {plugin_name}" + + runtime.clean() + + def test_install_collection_from_disk_fail() -> None: """Tests that we fail to install a broken collection.""" with cwd(Path("test/collections/acme.broken")):