|
| 1 | +""" |
| 2 | +Integration test: validates that data_loaders and plugin_registry work correctly |
| 3 | +without pkg_resources, using only importlib.resources / importlib.metadata. |
| 4 | +
|
| 5 | +Run with: |
| 6 | + pytest tests/functional_importlib_compat.py -v |
| 7 | +""" |
| 8 | +# pylint: disable=import-outside-toplevel |
| 9 | +import sys |
| 10 | + |
| 11 | + |
| 12 | +def test_no_pkg_resources_imported_by_data_loaders(): |
| 13 | + """data_loaders must not import pkg_resources at all.""" |
| 14 | + from unittest import mock |
| 15 | + |
| 16 | + # Force reimport to catch top-level imports |
| 17 | + if "rpdk.core.data_loaders" in sys.modules: |
| 18 | + del sys.modules["rpdk.core.data_loaders"] |
| 19 | + |
| 20 | + with mock.patch.dict("sys.modules", {"pkg_resources": None}): |
| 21 | + # Should not raise ModuleNotFoundError |
| 22 | + __import__("rpdk.core.data_loaders") |
| 23 | + |
| 24 | + |
| 25 | +def test_no_pkg_resources_imported_by_plugin_registry(): |
| 26 | + """plugin_registry must not import pkg_resources at all.""" |
| 27 | + from unittest import mock |
| 28 | + |
| 29 | + if "rpdk.core.plugin_registry" in sys.modules: |
| 30 | + del sys.modules["rpdk.core.plugin_registry"] |
| 31 | + |
| 32 | + with mock.patch.dict("sys.modules", {"pkg_resources": None}): |
| 33 | + __import__("rpdk.core.plugin_registry") |
| 34 | + |
| 35 | + |
| 36 | +def test_resource_json_loads_real_schema(): |
| 37 | + """resource_json must load an actual bundled schema file end-to-end.""" |
| 38 | + from rpdk.core.data_loaders import resource_json |
| 39 | + |
| 40 | + schema = resource_json( |
| 41 | + "rpdk.core", "data/schema/provider.definition.schema.v1.json" |
| 42 | + ) |
| 43 | + assert "$schema" in schema or "properties" in schema |
| 44 | + |
| 45 | + |
| 46 | +def test_resource_stream_returns_readable_content(): |
| 47 | + """resource_stream must return a readable text stream for a bundled file.""" |
| 48 | + from rpdk.core.data_loaders import resource_stream |
| 49 | + |
| 50 | + with resource_stream( |
| 51 | + "rpdk.core", "data/schema/provider.definition.schema.v1.json" |
| 52 | + ) as f: |
| 53 | + content = f.read() |
| 54 | + assert len(content) > 0 |
| 55 | + assert "$schema" in content or "properties" in content |
| 56 | + |
| 57 | + |
| 58 | +def test_plugin_registry_get_plugin_choices_does_not_raise(): |
| 59 | + """get_plugin_choices must not raise even with no plugins installed.""" |
| 60 | + from rpdk.core.plugin_registry import get_plugin_choices |
| 61 | + |
| 62 | + choices = get_plugin_choices() |
| 63 | + assert isinstance(choices, list) |
| 64 | + |
| 65 | + |
| 66 | +def test_importlib_resources_files_available(): |
| 67 | + """Verify the compat shim resolves correctly on this Python version.""" |
| 68 | + if sys.version_info >= (3, 9): |
| 69 | + from importlib.resources import files |
| 70 | + else: |
| 71 | + from importlib_resources import files |
| 72 | + assert callable(files) |
0 commit comments