8
8
import sys
9
9
import time
10
10
from contextlib import ExitStack , contextmanager
11
- from typing import TYPE_CHECKING , Callable , Dict , Iterable , Iterator , List , Optional
11
+ from pathlib import Path
12
+ from typing import (
13
+ TYPE_CHECKING ,
14
+ Callable ,
15
+ Dict ,
16
+ Iterable ,
17
+ Iterator ,
18
+ List ,
19
+ Optional ,
20
+ Union ,
21
+ )
12
22
from unittest .mock import patch
13
23
14
- import py .path
15
24
import pytest
16
25
17
26
# Config will be available from the public API in pytest >= 7.0.0:
27
36
from pip ._internal .locations import _USE_SYSCONFIG
28
37
from pip ._internal .utils .temp_dir import global_tempdir_manager
29
38
from tests .lib import DATA_DIR , SRC_DIR , PipTestEnvironment , TestData
30
- from tests .lib .path import Path
31
39
from tests .lib .server import MockServer as _MockServer
32
40
from tests .lib .server import make_mock_server , server_running
33
41
from tests .lib .venv import VirtualEnvironment , VirtualEnvironmentType
@@ -146,38 +154,55 @@ def resolver_variant(request: pytest.FixtureRequest) -> Iterator[str]:
146
154
147
155
148
156
@pytest .fixture (scope = "session" )
149
- def tmpdir_factory (
150
- request : pytest .FixtureRequest , tmpdir_factory : pytest .TempdirFactory
151
- ) -> Iterator [pytest .TempdirFactory ]:
157
+ def tmp_path_factory (
158
+ request : pytest .FixtureRequest , tmp_path_factory : pytest .TempPathFactory
159
+ ) -> Iterator [pytest .TempPathFactory ]:
152
160
"""Modified `tmpdir_factory` session fixture
153
161
that will automatically cleanup after itself.
154
162
"""
155
- yield tmpdir_factory
163
+ yield tmp_path_factory
156
164
if not request .config .getoption ("--keep-tmpdir" ):
157
165
shutil .rmtree (
158
- tmpdir_factory .getbasetemp (),
166
+ tmp_path_factory .getbasetemp (),
159
167
ignore_errors = True ,
160
168
)
161
169
162
170
171
+ @pytest .fixture (scope = "session" )
172
+ def tmpdir_factory (tmp_path_factory : pytest .TempPathFactory ) -> pytest .TempPathFactory :
173
+ """Override Pytest's ``tmpdir_factory`` with our pathlib implementation.
174
+
175
+ This prevents mis-use of this fixture.
176
+ """
177
+ return tmp_path_factory
178
+
179
+
163
180
@pytest .fixture
164
- def tmpdir (request : pytest .FixtureRequest , tmpdir : py . path . local ) -> Iterator [Path ]:
181
+ def tmp_path (request : pytest .FixtureRequest , tmp_path : Path ) -> Iterator [Path ]:
165
182
"""
166
183
Return a temporary directory path object which is unique to each test
167
184
function invocation, created as a sub directory of the base temporary
168
- directory. The returned object is a ``tests.lib.path. Path`` object.
185
+ directory. The returned object is a ``Path`` object.
169
186
170
- This uses the built-in tmpdir fixture from pytest itself but modified
171
- to return our typical path object instead of py.path.local as well as
172
- deleting the temporary directories at the end of each test case.
187
+ This uses the built-in tmp_path fixture from pytest itself, but deletes the
188
+ temporary directories at the end of each test case.
173
189
"""
174
- assert tmpdir . isdir ()
175
- yield Path ( str ( tmpdir ))
190
+ assert tmp_path . is_dir ()
191
+ yield tmp_path
176
192
# Clear out the temporary directory after the test has finished using it.
177
193
# This should prevent us from needing a multiple gigabyte temporary
178
194
# directory while running the tests.
179
195
if not request .config .getoption ("--keep-tmpdir" ):
180
- tmpdir .remove (ignore_errors = True )
196
+ shutil .rmtree (tmp_path , ignore_errors = True )
197
+
198
+
199
+ @pytest .fixture ()
200
+ def tmpdir (tmp_path : Path ) -> Path :
201
+ """Override Pytest's ``tmpdir`` with our pathlib implementation.
202
+
203
+ This prevents mis-use of this fixture.
204
+ """
205
+ return tmp_path
181
206
182
207
183
208
@pytest .fixture (autouse = True )
@@ -296,7 +321,7 @@ def scoped_global_tempdir_manager(request: pytest.FixtureRequest) -> Iterator[No
296
321
297
322
298
323
@pytest .fixture (scope = "session" )
299
- def pip_src (tmpdir_factory : pytest .TempdirFactory ) -> Path :
324
+ def pip_src (tmpdir_factory : pytest .TempPathFactory ) -> Path :
300
325
def not_code_files_and_folders (path : str , names : List [str ]) -> Iterable [str ]:
301
326
# In the root directory...
302
327
if path == SRC_DIR :
@@ -317,7 +342,7 @@ def not_code_files_and_folders(path: str, names: List[str]) -> Iterable[str]:
317
342
ignored .update (fnmatch .filter (names , pattern ))
318
343
return ignored
319
344
320
- pip_src = Path ( str ( tmpdir_factory .mktemp ("pip_src" )) ).joinpath ("pip_src" )
345
+ pip_src = tmpdir_factory .mktemp ("pip_src" ).joinpath ("pip_src" )
321
346
# Copy over our source tree so that each use is self contained
322
347
shutil .copytree (
323
348
SRC_DIR ,
@@ -328,11 +353,11 @@ def not_code_files_and_folders(path: str, names: List[str]) -> Iterable[str]:
328
353
329
354
330
355
def _common_wheel_editable_install (
331
- tmpdir_factory : pytest .TempdirFactory , common_wheels : Path , package : str
356
+ tmpdir_factory : pytest .TempPathFactory , common_wheels : Path , package : str
332
357
) -> Path :
333
358
wheel_candidates = list (common_wheels .glob (f"{ package } -*.whl" ))
334
359
assert len (wheel_candidates ) == 1 , wheel_candidates
335
- install_dir = Path ( str ( tmpdir_factory .mktemp (package )) ) / "install"
360
+ install_dir = tmpdir_factory .mktemp (package ) / "install"
336
361
Wheel (wheel_candidates [0 ]).install_as_egg (install_dir )
337
362
(install_dir / "EGG-INFO" ).rename (install_dir / f"{ package } .egg-info" )
338
363
assert compileall .compile_dir (str (install_dir ), quiet = 1 )
@@ -341,19 +366,19 @@ def _common_wheel_editable_install(
341
366
342
367
@pytest .fixture (scope = "session" )
343
368
def setuptools_install (
344
- tmpdir_factory : pytest .TempdirFactory , common_wheels : Path
369
+ tmpdir_factory : pytest .TempPathFactory , common_wheels : Path
345
370
) -> Path :
346
371
return _common_wheel_editable_install (tmpdir_factory , common_wheels , "setuptools" )
347
372
348
373
349
374
@pytest .fixture (scope = "session" )
350
- def wheel_install (tmpdir_factory : pytest .TempdirFactory , common_wheels : Path ) -> Path :
375
+ def wheel_install (tmpdir_factory : pytest .TempPathFactory , common_wheels : Path ) -> Path :
351
376
return _common_wheel_editable_install (tmpdir_factory , common_wheels , "wheel" )
352
377
353
378
354
379
@pytest .fixture (scope = "session" )
355
380
def coverage_install (
356
- tmpdir_factory : pytest .TempdirFactory , common_wheels : Path
381
+ tmpdir_factory : pytest .TempPathFactory , common_wheels : Path
357
382
) -> Path :
358
383
return _common_wheel_editable_install (tmpdir_factory , common_wheels , "coverage" )
359
384
@@ -370,7 +395,7 @@ def install_egg_link(
370
395
@pytest .fixture (scope = "session" )
371
396
def virtualenv_template (
372
397
request : pytest .FixtureRequest ,
373
- tmpdir_factory : pytest .TempdirFactory ,
398
+ tmpdir_factory : pytest .TempPathFactory ,
374
399
pip_src : Path ,
375
400
setuptools_install : Path ,
376
401
coverage_install : Path ,
@@ -383,12 +408,12 @@ def virtualenv_template(
383
408
venv_type = "virtualenv"
384
409
385
410
# Create the virtual environment
386
- tmpdir = Path ( str ( tmpdir_factory .mktemp ("virtualenv" )) )
411
+ tmpdir = tmpdir_factory .mktemp ("virtualenv" )
387
412
venv = VirtualEnvironment (tmpdir .joinpath ("venv_orig" ), venv_type = venv_type )
388
413
389
414
# Install setuptools and pip.
390
415
install_egg_link (venv , "setuptools" , setuptools_install )
391
- pip_editable = Path ( str ( tmpdir_factory .mktemp ("pip" )) ) / "pip"
416
+ pip_editable = tmpdir_factory .mktemp ("pip" ) / "pip"
392
417
shutil .copytree (pip_src , pip_editable , symlinks = True )
393
418
# noxfile.py is Python 3 only
394
419
assert compileall .compile_dir (
@@ -397,7 +422,7 @@ def virtualenv_template(
397
422
rx = re .compile ("noxfile.py$" ),
398
423
)
399
424
subprocess .check_call (
400
- [venv .bin / "python" , "setup.py" , "-q" , "develop" ], cwd = pip_editable
425
+ [os . fspath ( venv .bin / "python" ) , "setup.py" , "-q" , "develop" ], cwd = pip_editable
401
426
)
402
427
403
428
# Install coverage and pth file for executing it in any spawned processes
@@ -493,7 +518,7 @@ def factory(
493
518
def script (
494
519
tmpdir : Path ,
495
520
virtualenv : VirtualEnvironment ,
496
- script_factory : Callable [[ Path , Optional [ VirtualEnvironment ]], PipTestEnvironment ] ,
521
+ script_factory : ScriptFactory ,
497
522
) -> PipTestEnvironment :
498
523
"""
499
524
Return a PipTestEnvironment which is unique to each test function and
@@ -511,8 +536,8 @@ def common_wheels() -> Path:
511
536
512
537
513
538
@pytest .fixture (scope = "session" )
514
- def shared_data (tmpdir_factory : pytest .TempdirFactory ) -> TestData :
515
- return TestData .copy (Path ( str ( tmpdir_factory .mktemp ("data" )) ))
539
+ def shared_data (tmpdir_factory : pytest .TempPathFactory ) -> TestData :
540
+ return TestData .copy (tmpdir_factory .mktemp ("data" ))
516
541
517
542
518
543
@pytest .fixture
@@ -527,12 +552,12 @@ def __init__(self, returncode: int, stdout: str) -> None:
527
552
528
553
529
554
class InMemoryPip :
530
- def pip (self , * args : str ) -> InMemoryPipResult :
555
+ def pip (self , * args : Union [ str , Path ] ) -> InMemoryPipResult :
531
556
orig_stdout = sys .stdout
532
557
stdout = io .StringIO ()
533
558
sys .stdout = stdout
534
559
try :
535
- returncode = pip_entry_point (list ( args ) )
560
+ returncode = pip_entry_point ([ os . fspath ( a ) for a in args ] )
536
561
except SystemExit as e :
537
562
returncode = e .code or 0
538
563
finally :
@@ -555,15 +580,15 @@ def deprecated_python() -> bool:
555
580
556
581
557
582
@pytest .fixture (scope = "session" )
558
- def cert_factory (tmpdir_factory : pytest .TempdirFactory ) -> CertFactory :
583
+ def cert_factory (tmpdir_factory : pytest .TempPathFactory ) -> CertFactory :
559
584
# Delay the import requiring cryptography in order to make it possible
560
585
# to deselect relevant tests on systems where cryptography cannot
561
586
# be installed.
562
587
from tests .lib .certs import make_tls_cert , serialize_cert , serialize_key
563
588
564
589
def factory () -> str :
565
590
"""Returns path to cert/key file."""
566
- output_path = Path ( str ( tmpdir_factory .mktemp ("certs" )) ) / "cert.pem"
591
+ output_path = tmpdir_factory .mktemp ("certs" ) / "cert.pem"
567
592
# Must be Text on PY2.
568
593
cert , key = make_tls_cert ("localhost" )
569
594
with open (str (output_path ), "wb" ) as f :
0 commit comments