|
1 | 1 | from __future__ import annotations |
2 | 2 |
|
3 | 3 | import shutil |
| 4 | +import sys |
4 | 5 |
|
5 | 6 | from pathlib import Path |
6 | 7 | from typing import TYPE_CHECKING |
@@ -116,6 +117,12 @@ def io() -> BufferedIO: |
116 | 117 | return BufferedIO() |
117 | 118 |
|
118 | 119 |
|
| 120 | +@pytest.fixture(autouse=True) |
| 121 | +def mock_sys_path(mocker: MockerFixture) -> None: |
| 122 | + sys_path_copy = sys.path.copy() |
| 123 | + mocker.patch("poetry.plugins.plugin_manager.sys.path", new=sys_path_copy) |
| 124 | + |
| 125 | + |
119 | 126 | @pytest.fixture() |
120 | 127 | def manager_factory(poetry: Poetry, io: BufferedIO) -> ManagerFactory: |
121 | 128 | def _manager(group: str = Plugin.group) -> PluginManager: |
@@ -187,6 +194,59 @@ def test_add_project_plugin_path( |
187 | 194 | } == {"my-application-plugin 1.0"} |
188 | 195 |
|
189 | 196 |
|
| 197 | +def test_add_project_plugin_path_addsitedir_called( |
| 198 | + poetry_with_plugins: Poetry, |
| 199 | + io: BufferedIO, |
| 200 | + mocker: MockerFixture, |
| 201 | +) -> None: |
| 202 | + """Test that addsitedir is called when plugin path exists.""" |
| 203 | + cache = ProjectPluginCache(poetry_with_plugins, io) |
| 204 | + cache._path.mkdir(parents=True, exist_ok=True) |
| 205 | + |
| 206 | + mock_addsitedir = mocker.patch("poetry.plugins.plugin_manager.addsitedir") |
| 207 | + |
| 208 | + PluginManager.add_project_plugin_path(poetry_with_plugins.pyproject_path.parent) |
| 209 | + |
| 210 | + # sys.path is mocked, so we can check it was modified |
| 211 | + assert str(cache._path) in sys.path |
| 212 | + assert sys.path[0] == str(cache._path) |
| 213 | + mock_addsitedir.assert_called_once_with(str(cache._path)) |
| 214 | + |
| 215 | + |
| 216 | +def test_add_project_plugin_path_no_addsitedir_when_path_missing( |
| 217 | + poetry_with_plugins: Poetry, |
| 218 | + mocker: MockerFixture, |
| 219 | +) -> None: |
| 220 | + """Test that addsitedir is not called when plugin path doesn't exist.""" |
| 221 | + cache = ProjectPluginCache(poetry_with_plugins, BufferedIO()) |
| 222 | + # Ensure the plugin path does not exist |
| 223 | + if cache._path.exists(): |
| 224 | + shutil.rmtree(cache._path) |
| 225 | + |
| 226 | + mock_addsitedir = mocker.patch("poetry.plugins.plugin_manager.addsitedir") |
| 227 | + initial_sys_path = sys.path.copy() |
| 228 | + |
| 229 | + PluginManager.add_project_plugin_path(poetry_with_plugins.pyproject_path.parent) |
| 230 | + |
| 231 | + assert sys.path == initial_sys_path |
| 232 | + mock_addsitedir.assert_not_called() |
| 233 | + |
| 234 | + |
| 235 | +def test_add_project_plugin_path_no_pyproject( |
| 236 | + tmp_path: Path, |
| 237 | + mocker: MockerFixture, |
| 238 | +) -> None: |
| 239 | + """Test that no action is taken when pyproject.toml is missing.""" |
| 240 | + mock_addsitedir = mocker.patch("poetry.plugins.plugin_manager.addsitedir") |
| 241 | + initial_sys_path = sys.path.copy() |
| 242 | + |
| 243 | + # Call with a directory that has no pyproject.toml |
| 244 | + PluginManager.add_project_plugin_path(tmp_path) |
| 245 | + |
| 246 | + assert sys.path == initial_sys_path |
| 247 | + mock_addsitedir.assert_not_called() |
| 248 | + |
| 249 | + |
190 | 250 | def test_ensure_plugins_no_plugins_no_output(poetry: Poetry, io: BufferedIO) -> None: |
191 | 251 | PluginManager.ensure_project_plugins(poetry, io) |
192 | 252 |
|
|
0 commit comments