hbutils.testing.isolated.entry_point

Entry point isolation utilities for testing plugin-based systems.

This module provides tools for isolating and mocking entry points exposed by Python packages. It supports multiple entry point systems including pkg_resources, importlib.metadata (Python 3.8+), and importlib_metadata (backport for Python 3.7). The primary interface is isolated_entry_points(), which allows test suites to inject fake entry points or clear existing ones in a controlled context.

The module contains the following main components:

Note

The implementation includes compatibility layers for legacy entry point systems. Although pkg_resources is supported here, it is deprecated upstream and may be removed in a future major release.

Example:

>>> from hbutils.testing.isolated.entry_point import isolated_entry_points
>>> with isolated_entry_points('my_plugin', {'tool': 'math.sqrt'}):
...     # Entry points in group "my_plugin" are patched within the context
...     pass

__all__

hbutils.testing.isolated.entry_point.__all__ = ['isolated_entry_points']

Built-in mutable sequence.

If no argument is given, the constructor creates a new empty list. The argument must be an iterable if specified.

isolated_entry_points

hbutils.testing.isolated.entry_point.isolated_entry_points(group: str, fakes: List[Any] | Dict[str, Any] | None = None, auto_import: bool = True, clear: bool = False) Iterator[None][source]

Isolation context manager for entry points functions.

This context manager allows for mocking or clearing entry points during testing. It supports pkg_resources.iter_entry_points, importlib.metadata.entry_points (Python 3.8+), and importlib_metadata.entry_points (Python 3.7 backport).

Parameters:
  • group (str) – The entry point group name to isolate.

  • fakes (Union[List, Dict[str, Any], None]) – Fake entry points to inject. Can be a list, tuple, or dict. List/tuple format: [(name, dist), object, 'import.path', ...] Dict format: {name: dist, name: 'import.path', ...}

  • auto_import (bool) – Auto import objects from string paths. Default is True.

  • clear (bool) – Clear original entry points if True. Default is False.

Raises:

TypeError – If fakes is not of type list, tuple, dict, or None.

Example:

>>> from hbutils.testing import isolated_entry_points
>>> 
>>> # Mock plugins with a list
>>> with isolated_entry_points('my_plugin', [
...     ('quick_import_object', 'hbutils.reflection.quick_import_object'),
...     ('func_filter', filter),
...     map,
...     'hbutils.system.is_binary_file',
... ]):
...     # Entry points are mocked within this context
...     pass
>>> 
>>> # Mock plugins with a dict
>>> with isolated_entry_points('my_plugin', {
...     'func_map': map,
...     'func_binary': 'hbutils.system.is_binary_file'
... }):
...     # Entry points are mocked within this context
...     pass
>>> 
>>> # Clear all entry points for a group
>>> with isolated_entry_points('my_plugin', clear=True):
...     # No entry points available in this context
...     pass

Warning

The pkg_resources package is no longer officially supported. However, certain libraries that rely on hbutils are still in use, hence temporary support will be provided. The official guidance must be followed to migrate to importlib.metadata at the earliest opportunity. In addition, support for the pkg_resources package in this function will be discontinued in the next major version.