@@ -127,6 +127,122 @@ async def test_run_with_env_variables(self, tmp_path: Path):
127
127
class TestPip :
128
128
"""Test the `Pip` class."""
129
129
130
+ @pytest .mark .asyncio
131
+ @pytest .mark .asyncio
132
+ async def test_creation_with_venv (self , tmp_path : Path ):
133
+ """Test the `create_venv` method with a `VenvWrapper`."""
134
+ location = tmp_path / "venv"
135
+ env = Pip (
136
+ tmp_path ,
137
+ "main" ,
138
+ location ,
139
+ args = ["tomli" ],
140
+ creator = VenvWrapper (),
141
+ temporary = False ,
142
+ )
143
+
144
+ await env .create_venv ()
145
+ assert (location / "bin" / "python" ).exists ()
146
+
147
+ @pytest .mark .asyncio
148
+ async def test_creation_without_creator (self , tmp_path : Path ):
149
+ """Test the `create_venv` method without any creator."""
150
+ location = tmp_path / "venv"
151
+ env = Pip (tmp_path , "main" , location , args = ["tomli" ], temporary = False )
152
+
153
+ await env .create_venv ()
154
+ assert not (location / "bin" / "python" ).exists ()
155
+
156
+ @pytest .mark .asyncio
157
+ async def test_run_without_creator (self , tmp_path : Path ):
158
+ """Test running a command in an existing venv."""
159
+ location = tmp_path / "venv"
160
+
161
+ # create env
162
+ await VenvWrapper ([])(location )
163
+
164
+ async with Pip (
165
+ tmp_path , "main" , location , args = ["tomli" ], temporary = False
166
+ ) as env :
167
+ out , err , rc = await env .run (
168
+ "python" ,
169
+ "-c" ,
170
+ "import sys; print(sys.prefix)" ,
171
+ stdout = asyncio .subprocess .PIPE ,
172
+ )
173
+ assert rc == 0
174
+ assert str (location ) == out .strip ()
175
+
176
+ @pytest .mark .asyncio
177
+ async def test_run_with_creator (self , tmp_path : Path ):
178
+ """Test running a command in a new venv."""
179
+ location = tmp_path / "venv"
180
+
181
+ async with Pip (
182
+ tmp_path ,
183
+ "main" ,
184
+ location ,
185
+ args = ["tomli" ],
186
+ creator = VenvWrapper (),
187
+ temporary = False ,
188
+ ) as env :
189
+ out , err , rc = await env .run (
190
+ "python" ,
191
+ "-c" ,
192
+ "import sys; print(sys.prefix)" ,
193
+ stdout = asyncio .subprocess .PIPE ,
194
+ )
195
+ assert rc == 0
196
+ assert str (location ) == out .strip ()
197
+
198
+ @pytest .mark .asyncio
199
+ async def test_creation_with_venv_temporary (self , tmp_path : Path ):
200
+ """Test the `create_venv` method with a `VenvWrapper`."""
201
+ location = "tmpvenv"
202
+ env = Pip (
203
+ tmp_path ,
204
+ "main" ,
205
+ location ,
206
+ args = ["tomli" ],
207
+ creator = VenvWrapper (),
208
+ temporary = True ,
209
+ )
210
+
211
+ await env .create_venv ()
212
+ assert (tmp_path / location / "bin" / "python" ).exists ()
213
+
214
+ @pytest .mark .asyncio
215
+ async def test_creation_without_creator_temporary (self , tmp_path : Path ):
216
+ """Test the `create_venv` method without any creator."""
217
+ location = "tmpvenv"
218
+ with pytest .raises (
219
+ ValueError ,
220
+ match = "Cannot create temporary virtual environment when creator is None" ,
221
+ ):
222
+ Pip (tmp_path , "main" , location , args = ["tomli" ], temporary = True )
223
+
224
+ @pytest .mark .asyncio
225
+ async def test_run_with_creator_temporary (self , tmp_path : Path ):
226
+ """Test running a command in a new venv."""
227
+ location = "tmpvenv"
228
+
229
+ async with Pip (
230
+ tmp_path ,
231
+ "main" ,
232
+ location ,
233
+ args = ["tomli" ],
234
+ creator = VenvWrapper (),
235
+ temporary = True ,
236
+ ) as env :
237
+ out , err , rc = await env .run (
238
+ "python" ,
239
+ "-c" ,
240
+ "import sys; print(sys.prefix)" ,
241
+ stdout = asyncio .subprocess .PIPE ,
242
+ )
243
+ assert rc == 0
244
+ assert str (tmp_path / location ) == out .strip ()
245
+
130
246
@pytest .mark .asyncio
131
247
async def test_install_into_existing_venv (self , tmp_path : Path ):
132
248
"""Test installing a package into an existing venv."""
0 commit comments