Coverage for src/pytest_samples/plugin/_meta.py: 100%
23 statements
« prev ^ index » next coverage.py v7.4.2, created at 2024-02-20 19:47 +0000
« prev ^ index » next coverage.py v7.4.2, created at 2024-02-20 19:47 +0000
1"""This module contains meta information and functions for the
2plugin.
3"""
5__all__ = [
6 "PLUGIN_NAME", "PLUGIN_FULL_NAME",
7 "swap_plugin", "register", "unregister"
8]
10import logging
12from pytest import PytestPluginManager as _PytestPluginManager
13from typing import Optional as _Optional
16_logger = logging.getLogger(__name__)
17"""The logger for this module."""
20PLUGIN_NAME = "samples"
21"""The name of the plugin."""
24PLUGIN_FULL_NAME = "pytest-samples"
25"""The full name of the plugin with "pytest-" prefix."""
28def swap_plugin(
29 pluginmanager: _PytestPluginManager, old_value: object, new_value: object
30) -> None:
31 """Swap the plugin object with a new object.
33 Args:
34 pluginmanager (PytestPluginManager): The pytest plugin manager.
35 new_value (object): The new lugin object to register.
36 unreg (bool, optional): Whether to unregister the module first.
37 """
38 unregister(pluginmanager, old_value)
39 register(pluginmanager, new_value)
42def register(
43 pluginmanager: _PytestPluginManager, plugin_object: object
44) -> _Optional[str]:
45 """Register a new plugin object under this plugin's name.
47 Args:
48 pluginmanager (PytestPluginManager): The pytest plugin manager.
49 plugin_object (object): The plugin object to register.
51 Returns:
52 Optional[str]: The result of the `register` call on
53 `pluginmanager`.
54 """
55 _logger.debug("Registering new module object %r.", plugin_object)
56 res = pluginmanager.register(plugin_object, PLUGIN_NAME)
57 _logger.debug("New module object registered. Result: %r.", res)
58 return res
61def unregister(
62 pluginmanager: _PytestPluginManager, plugin_object: object
63) -> _Optional[object]:
64 """Unregister a plugin object.
66 Args:
67 pluginmanager (PytestPluginManager): The pytest plugin manager.
68 plugin_object (object): The plugin object to unregister.
70 Returns:
71 Optional[object]: The result of the `unregister` call on
72 `pluginmanager`.
73 """
74 _logger.debug("Unregistering module object %r.", plugin_object)
75 old = pluginmanager.unregister(plugin_object)
76 _logger.debug("Got %r.", old)
77 return old