|
| 1 | +""" |
| 2 | +Tests for ProjectSpec functionality. |
| 3 | +""" |
| 4 | + |
| 5 | +from __future__ import annotations |
| 6 | + |
| 7 | +from pluggy import HookimplMarker |
| 8 | +from pluggy import HookspecMarker |
| 9 | +from pluggy import PluginManager |
| 10 | +from pluggy import ProjectSpec |
| 11 | + |
| 12 | + |
| 13 | +def test_project_spec_basic_creation() -> None: |
| 14 | + project = ProjectSpec("testproject") |
| 15 | + |
| 16 | + assert project.project_name == "testproject" |
| 17 | + assert isinstance(project.hookspec, HookspecMarker) |
| 18 | + assert isinstance(project.hookimpl, HookimplMarker) |
| 19 | + assert project.hookspec.project_name == "testproject" |
| 20 | + assert project.hookimpl.project_name == "testproject" |
| 21 | + assert repr(project) == "ProjectSpec(project_name='testproject')" |
| 22 | + |
| 23 | + |
| 24 | +def test_project_spec_plugin_manager_creation() -> None: |
| 25 | + project = ProjectSpec("testproject") |
| 26 | + |
| 27 | + pm1 = project.create_plugin_manager() |
| 28 | + pm2 = project.create_plugin_manager() |
| 29 | + |
| 30 | + assert pm1 is not pm2 |
| 31 | + assert pm1.project_name == "testproject" |
| 32 | + assert pm2.project_name == "testproject" |
| 33 | + assert isinstance(pm1, PluginManager) |
| 34 | + assert isinstance(pm2, PluginManager) |
| 35 | + |
| 36 | + |
| 37 | +def test_project_spec_custom_plugin_manager_class() -> None: |
| 38 | + class CustomPluginManager(PluginManager): |
| 39 | + def __init__(self, project_name: str | ProjectSpec) -> None: |
| 40 | + super().__init__(project_name) |
| 41 | + self.custom_attr = "custom_value" |
| 42 | + |
| 43 | + project = ProjectSpec("testproject", plugin_manager_cls=CustomPluginManager) |
| 44 | + pm = project.create_plugin_manager() |
| 45 | + |
| 46 | + assert isinstance(pm, CustomPluginManager) |
| 47 | + assert pm.project_name == "testproject" |
| 48 | + assert pm.custom_attr == "custom_value" |
| 49 | + |
| 50 | + |
| 51 | +def test_project_spec_functional_integration() -> None: |
| 52 | + project = ProjectSpec("testproject") |
| 53 | + |
| 54 | + hookspec = project.hookspec |
| 55 | + hookimpl = project.hookimpl |
| 56 | + |
| 57 | + class HookSpecs: |
| 58 | + @hookspec |
| 59 | + def my_hook(self, arg: int) -> int: # type: ignore[empty-body] |
| 60 | + ... |
| 61 | + |
| 62 | + class Plugin: |
| 63 | + @hookimpl |
| 64 | + def my_hook(self, arg: int) -> int: |
| 65 | + return arg * 2 |
| 66 | + |
| 67 | + pm = project.create_plugin_manager() |
| 68 | + pm.add_hookspecs(HookSpecs) |
| 69 | + pm.register(Plugin()) |
| 70 | + |
| 71 | + result = pm.hook.my_hook(arg=5) |
| 72 | + assert result == [10] |
| 73 | + |
| 74 | + |
| 75 | +def test_project_spec_multiple_plugin_managers_independent() -> None: |
| 76 | + project = ProjectSpec("testproject") |
| 77 | + |
| 78 | + pm1 = project.create_plugin_manager() |
| 79 | + pm2 = project.create_plugin_manager() |
| 80 | + |
| 81 | + class Plugin1: |
| 82 | + pass |
| 83 | + |
| 84 | + class Plugin2: |
| 85 | + pass |
| 86 | + |
| 87 | + pm1.register(Plugin1(), name="plugin1") |
| 88 | + pm2.register(Plugin2(), name="plugin2") |
| 89 | + |
| 90 | + assert pm1.has_plugin("plugin1") |
| 91 | + assert not pm1.has_plugin("plugin2") |
| 92 | + assert pm2.has_plugin("plugin2") |
| 93 | + assert not pm2.has_plugin("plugin1") |
| 94 | + |
| 95 | + |
| 96 | +def test_project_spec_hook_attribute_naming() -> None: |
| 97 | + project = ProjectSpec("myproject") |
| 98 | + |
| 99 | + @project.hookspec |
| 100 | + def test_hook() -> None: |
| 101 | + pass |
| 102 | + |
| 103 | + @project.hookimpl |
| 104 | + def test_hook_impl() -> None: |
| 105 | + pass |
| 106 | + |
| 107 | + assert hasattr(test_hook, "myproject_spec") |
| 108 | + assert hasattr(test_hook_impl, "myproject_impl") |
| 109 | + |
| 110 | + |
| 111 | +def test_project_spec_get_hook_configs() -> None: |
| 112 | + project = ProjectSpec("testproject") |
| 113 | + |
| 114 | + @project.hookspec(firstresult=True) |
| 115 | + def my_hook() -> None: |
| 116 | + pass |
| 117 | + |
| 118 | + @project.hookimpl(tryfirst=True, optionalhook=True) |
| 119 | + def my_hook_impl() -> None: |
| 120 | + pass |
| 121 | + |
| 122 | + spec_config = project.get_hookspec_config(my_hook) |
| 123 | + assert spec_config is not None |
| 124 | + assert spec_config.firstresult is True |
| 125 | + |
| 126 | + impl_config = project.get_hookimpl_config(my_hook_impl) |
| 127 | + assert impl_config is not None |
| 128 | + assert impl_config.tryfirst is True |
| 129 | + assert impl_config.optionalhook is True |
| 130 | + assert impl_config.wrapper is False |
| 131 | + |
| 132 | + def undecorated() -> None: |
| 133 | + pass |
| 134 | + |
| 135 | + assert project.get_hookspec_config(undecorated) is None |
| 136 | + assert project.get_hookimpl_config(undecorated) is None |
| 137 | + |
| 138 | + |
| 139 | +def test_marker_classes_accept_project_spec() -> None: |
| 140 | + project = ProjectSpec("testproject") |
| 141 | + |
| 142 | + hookspec_from_project = HookspecMarker(project) |
| 143 | + hookimpl_from_project = HookimplMarker(project) |
| 144 | + |
| 145 | + assert hookspec_from_project.project_name == "testproject" |
| 146 | + assert hookimpl_from_project.project_name == "testproject" |
| 147 | + assert hookspec_from_project._project_spec is project |
| 148 | + assert hookimpl_from_project._project_spec is project |
| 149 | + |
| 150 | + |
| 151 | +def test_marker_classes_accept_string() -> None: |
| 152 | + hookspec_from_string = HookspecMarker("testproject") |
| 153 | + hookimpl_from_string = HookimplMarker("testproject") |
| 154 | + |
| 155 | + assert hookspec_from_string.project_name == "testproject" |
| 156 | + assert hookimpl_from_string.project_name == "testproject" |
| 157 | + assert hookspec_from_string._project_spec.project_name == "testproject" |
| 158 | + assert hookimpl_from_string._project_spec.project_name == "testproject" |
| 159 | + |
| 160 | + |
| 161 | +def test_plugin_manager_accepts_project_spec() -> None: |
| 162 | + project = ProjectSpec("testproject") |
| 163 | + pm = PluginManager(project) |
| 164 | + |
| 165 | + assert pm.project_name == "testproject" |
| 166 | + assert pm._project_spec is project |
| 167 | + |
| 168 | + |
| 169 | +def test_plugin_manager_accepts_string() -> None: |
| 170 | + pm = PluginManager("testproject") |
| 171 | + |
| 172 | + assert pm.project_name == "testproject" |
| 173 | + assert pm._project_spec.project_name == "testproject" |
0 commit comments