1616from poetry .puzzle .exceptions import SolverProblemError
1717from poetry .repositories .repository import Repository
1818from poetry .repositories .repository_pool import RepositoryPool
19+ from poetry .utils .env import MockEnv
1920
2021from poetry_plugin_bundle .bundlers .venv_bundler import VenvBundler
2122
@@ -53,6 +54,12 @@ def poetry(config: Config) -> Poetry:
5354 return poetry
5455
5556
57+ def _create_venv_marker_file (tempdir : str | Path ) -> Path :
58+ marker_file = Path (tempdir ) / "existing-venv-marker.txt"
59+ marker_file .write_text ("This file should get deleted as part of venv recreation." )
60+ return marker_file
61+
62+
5663def test_bundler_should_build_a_new_venv_with_existing_python (
5764 io : BufferedIO , tmpdir : str , poetry : Poetry , mocker : MockerFixture
5865) -> None :
@@ -64,10 +71,9 @@ def test_bundler_should_build_a_new_venv_with_existing_python(
6471
6572 assert bundler .bundle (poetry , io )
6673
67- python_version = "." .join (str (v ) for v in sys .version_info [:3 ])
6874 expected = f"""\
6975 • Bundling simple-project (1.2.3) into { tmpdir }
70- • Bundling simple-project (1.2.3) into { tmpdir } : Creating a virtual environment using Python { python_version }
76+ • Bundling simple-project (1.2.3) into { tmpdir } : Creating a virtual environment using Poetry-determined Python
7177 • Bundling simple-project (1.2.3) into { tmpdir } : Installing dependencies
7278 • Bundling simple-project (1.2.3) into { tmpdir } : Installing simple-project (1.2.3)
7379 • Bundled simple-project (1.2.3) into { tmpdir }
@@ -87,10 +93,9 @@ def test_bundler_should_build_a_new_venv_with_given_executable(
8793
8894 assert bundler .bundle (poetry , io )
8995
90- python_version = "." .join (str (v ) for v in sys .version_info [:3 ])
9196 expected = f"""\
9297 • Bundling simple-project (1.2.3) into { tmpdir }
93- • Bundling simple-project (1.2.3) into { tmpdir } : Creating a virtual environment using Python { python_version }
98+ • Bundling simple-project (1.2.3) into { tmpdir } : Creating a virtual environment using Python { sys . executable }
9499 • Bundling simple-project (1.2.3) into { tmpdir } : Installing dependencies
95100 • Bundling simple-project (1.2.3) into { tmpdir } : Installing simple-project (1.2.3)
96101 • Bundled simple-project (1.2.3) into { tmpdir }
@@ -103,16 +108,22 @@ def test_bundler_should_build_a_new_venv_if_existing_venv_is_incompatible(
103108) -> None :
104109 mocker .patch ("poetry.installation.executor.Executor._execute_operation" )
105110
111+ mock_env = MockEnv (path = Path (tmpdir ), is_venv = True , version_info = (1 , 2 , 3 ))
112+ mocker .patch ("poetry.utils.env.EnvManager.get" , return_value = mock_env )
113+
106114 bundler = VenvBundler ()
107115 bundler .set_path (Path (tmpdir ))
108116
117+ marker_file = _create_venv_marker_file (tmpdir )
118+
119+ assert marker_file .exists ()
109120 assert bundler .bundle (poetry , io )
121+ assert not marker_file .exists ()
110122
111- python_version = "." .join (str (v ) for v in sys .version_info [:3 ])
112123 expected = f"""\
113124 • Bundling simple-project (1.2.3) into { tmpdir }
114- • Bundling simple-project (1.2.3) into { tmpdir } : Removing existing virtual environment
115- • Bundling simple-project (1.2.3) into { tmpdir } : Creating a virtual environment using Python { python_version }
125+ • Bundling simple-project (1.2.3) into { tmpdir } : Creating a virtual environment using Poetry-determined Python
126+ • Bundling simple-project (1.2.3) into { tmpdir } : Replacing existing virtual environment due to incompatible Python version
116127 • Bundling simple-project (1.2.3) into { tmpdir } : Installing dependencies
117128 • Bundling simple-project (1.2.3) into { tmpdir } : Installing simple-project (1.2.3)
118129 • Bundled simple-project (1.2.3) into { tmpdir }
@@ -128,12 +139,16 @@ def test_bundler_should_use_an_existing_venv_if_compatible(
128139 bundler = VenvBundler ()
129140 bundler .set_path (tmp_venv .path )
130141
142+ marker_file = _create_venv_marker_file (tmp_venv .path )
143+
144+ assert marker_file .exists ()
131145 assert bundler .bundle (poetry , io )
146+ assert marker_file .exists ()
132147
133148 path = str (tmp_venv .path )
134149 expected = f"""\
135150 • Bundling simple-project (1.2.3) into { path }
136- • Bundling simple-project (1.2.3) into { path } : Using existing virtual environment
151+ • Bundling simple-project (1.2.3) into { path } : Creating a virtual environment using Poetry-determined Python
137152 • Bundling simple-project (1.2.3) into { path } : Installing dependencies
138153 • Bundling simple-project (1.2.3) into { path } : Installing simple-project (1.2.3)
139154 • Bundled simple-project (1.2.3) into { path }
@@ -150,14 +165,16 @@ def test_bundler_should_remove_an_existing_venv_if_forced(
150165 bundler .set_path (tmp_venv .path )
151166 bundler .set_remove (True )
152167
168+ marker_file = _create_venv_marker_file (tmp_venv .path )
169+
170+ assert marker_file .exists ()
153171 assert bundler .bundle (poetry , io )
172+ assert not marker_file .exists ()
154173
155174 path = str (tmp_venv .path )
156- python_version = "." .join (str (v ) for v in sys .version_info [:3 ])
157175 expected = f"""\
158176 • Bundling simple-project (1.2.3) into { path }
159- • Bundling simple-project (1.2.3) into { path } : Removing existing virtual environment
160- • Bundling simple-project (1.2.3) into { path } : Creating a virtual environment using Python { python_version }
177+ • Bundling simple-project (1.2.3) into { path } : Creating a virtual environment using Poetry-determined Python
161178 • Bundling simple-project (1.2.3) into { path } : Installing dependencies
162179 • Bundling simple-project (1.2.3) into { path } : Installing simple-project (1.2.3)
163180 • Bundled simple-project (1.2.3) into { path }
@@ -178,11 +195,9 @@ def test_bundler_should_fail_when_installation_fails(
178195
179196 assert not bundler .bundle (poetry , io )
180197
181- python_version = "." .join (str (v ) for v in sys .version_info [:3 ])
182198 expected = f"""\
183199 • Bundling simple-project (1.2.3) into { tmpdir }
184- • Bundling simple-project (1.2.3) into { tmpdir } : Removing existing virtual environment
185- • Bundling simple-project (1.2.3) into { tmpdir } : Creating a virtual environment using Python { python_version }
200+ • Bundling simple-project (1.2.3) into { tmpdir } : Creating a virtual environment using Poetry-determined Python
186201 • Bundling simple-project (1.2.3) into { tmpdir } : Installing dependencies
187202 • Bundling simple-project (1.2.3) into { tmpdir } : Failed at step Installing dependencies
188203"""
@@ -212,11 +227,9 @@ def test_bundler_should_display_a_warning_for_projects_with_no_module(
212227 assert bundler .bundle (poetry , io )
213228
214229 path = str (tmp_venv .path )
215- python_version = "." .join (str (v ) for v in sys .version_info [:3 ])
216230 expected = f"""\
217231 • Bundling simple-project (1.2.3) into { path }
218- • Bundling simple-project (1.2.3) into { path } : Removing existing virtual environment
219- • Bundling simple-project (1.2.3) into { path } : Creating a virtual environment using Python { python_version }
232+ • Bundling simple-project (1.2.3) into { path } : Creating a virtual environment using Poetry-determined Python
220233 • Bundling simple-project (1.2.3) into { path } : Installing dependencies
221234 • Bundling simple-project (1.2.3) into { path } : Installing simple-project (1.2.3)
222235 • Bundled simple-project (1.2.3) into { path }
@@ -259,11 +272,9 @@ def test_bundler_can_filter_dependency_groups(
259272 assert bundler .bundle (poetry , io )
260273
261274 path = tmpdir
262- python_version = "." .join (str (v ) for v in sys .version_info [:3 ])
263275 expected = f"""\
264276 • Bundling simple-project (1.2.3) into { path }
265- • Bundling simple-project (1.2.3) into { path } : Removing existing virtual environment
266- • Bundling simple-project (1.2.3) into { path } : Creating a virtual environment using Python { python_version }
277+ • Bundling simple-project (1.2.3) into { path } : Creating a virtual environment using Poetry-determined Python
267278 • Bundling simple-project (1.2.3) into { path } : Installing dependencies
268279 • Bundling simple-project (1.2.3) into { path } : Installing simple-project (1.2.3)
269280 • Bundled simple-project (1.2.3) into { path }
@@ -296,11 +307,9 @@ def test_bundler_passes_compile_flag(
296307 mocker .assert_called_once_with (compile )
297308
298309 path = str (tmp_venv .path )
299- python_version = "." .join (str (v ) for v in sys .version_info [:3 ])
300310 expected = f"""\
301311 • Bundling simple-project (1.2.3) into { path }
302- • Bundling simple-project (1.2.3) into { path } : Removing existing virtual environment
303- • Bundling simple-project (1.2.3) into { path } : Creating a virtual environment using Python { python_version }
312+ • Bundling simple-project (1.2.3) into { path } : Creating a virtual environment using Poetry-determined Python
304313 • Bundling simple-project (1.2.3) into { path } : Installing dependencies
305314 • Bundling simple-project (1.2.3) into { path } : Installing simple-project (1.2.3)
306315 • Bundled simple-project (1.2.3) into { path }
@@ -327,11 +336,9 @@ def test_bundler_editable_deps(
327336 bundler .bundle (poetry , io )
328337
329338 path = tmpdir
330- python_version = "." .join (str (v ) for v in sys .version_info [:3 ])
331339 expected = f"""\
332340 • Bundling simple-project (1.2.3) into { path }
333- • Bundling simple-project (1.2.3) into { path } : Removing existing virtual environment
334- • Bundling simple-project (1.2.3) into { path } : Creating a virtual environment using Python { python_version }
341+ • Bundling simple-project (1.2.3) into { path } : Creating a virtual environment using Poetry-determined Python
335342 • Bundling simple-project (1.2.3) into { path } : Installing dependencies
336343 • Bundling simple-project (1.2.3) into { path } : Installing simple-project (1.2.3)
337344 • Bundled simple-project (1.2.3) into { path }
0 commit comments