@@ -102,13 +102,22 @@ def install_setuptools(env):
102
102
env = None
103
103
104
104
105
- def reset_env (environ = None , use_distribute = None , system_site_packages = False ):
105
+ def reset_env (environ = None , use_distribute = None , system_site_packages = False , sitecustomize = None ):
106
+ """Return a test environment.
107
+
108
+ Keyword arguments:
109
+ environ: an environ object to use.
110
+ use_distribute: use distribute, not setuptools.
111
+ system_site_packages: create a virtualenv that simulates --system-site-packages.
112
+ sitecustomize: a string containing python code to add to sitecustomize.py.
113
+ """
114
+
106
115
global env
107
116
# FastTestPipEnv reuses env, not safe if use_distribute specified
108
117
if use_distribute is None and not system_site_packages :
109
- env = FastTestPipEnvironment (environ )
118
+ env = FastTestPipEnvironment (environ , sitecustomize = sitecustomize )
110
119
else :
111
- env = TestPipEnvironment (environ , use_distribute = use_distribute )
120
+ env = TestPipEnvironment (environ , use_distribute = use_distribute , sitecustomize = sitecustomize )
112
121
113
122
if system_site_packages :
114
123
#testing often occurs starting from a private virtualenv (e.g. with tox)
@@ -270,7 +279,7 @@ class TestPipEnvironment(TestFileEnvironment):
270
279
271
280
verbose = False
272
281
273
- def __init__ (self , environ = None , use_distribute = None ):
282
+ def __init__ (self , environ = None , use_distribute = None , sitecustomize = None ):
274
283
275
284
self .root_path = Path (tempfile .mkdtemp ('-piptest' ))
276
285
@@ -347,6 +356,8 @@ def __init__(self, environ=None, use_distribute=None):
347
356
# Install this version instead
348
357
self .run ('python' , 'setup.py' , 'install' , cwd = src_folder , expect_stderr = True )
349
358
self ._use_cached_pypi_server ()
359
+ if sitecustomize :
360
+ self ._add_to_sitecustomize (sitecustomize )
350
361
351
362
def _ignore_file (self , fn ):
352
363
if fn .endswith ('__pycache__' ) or fn .endswith (".pyc" ):
@@ -374,21 +385,30 @@ def _use_cached_pypi_server(self):
374
385
# 'import pypi_server' ultimately imports pkg_resources (which intializes pkg_resources.working_set based on the current state of sys.path)
375
386
# pkg_resources.get_distribution (used in pip.req) requires an accurate pkg_resources.working_set
376
387
# therefore, 'import pypi_server' shouldn't occur in a pth file.
388
+
389
+ patch = """
390
+ import sys
391
+ sys.path.insert(0, %r)
392
+ import pypi_server
393
+ pypi_server.PyPIProxy.setup()
394
+ sys.path.remove(%r)""" % (str (here ), str (here ))
395
+ self ._add_to_sitecustomize (patch )
396
+
397
+ def _add_to_sitecustomize (self , snippet ):
398
+ "Adds a python code snippet to sitecustomize.py."
377
399
sitecustomize_path = self .lib_path / 'sitecustomize.py'
378
- sitecustomize = open (sitecustomize_path , 'w' )
379
- sitecustomize .write ('import sys; ' )
380
- sitecustomize .write ('sys.path.insert(0, %r); ' % str (here ))
381
- sitecustomize .write ('import pypi_server; pypi_server.PyPIProxy.setup(); ' )
382
- sitecustomize .write ('sys.path.remove(%r); ' % str (here ))
400
+ sitecustomize = open (sitecustomize_path , 'a' )
401
+ sitecustomize .write (textwrap .dedent ('''
402
+ %s
403
+ ''' % snippet ))
383
404
sitecustomize .close ()
384
405
385
-
386
406
fast_test_env_root = here / 'tests_cache' / 'test_ws'
387
407
fast_test_env_backup = here / 'tests_cache' / 'test_ws_backup'
388
408
389
409
390
410
class FastTestPipEnvironment (TestPipEnvironment ):
391
- def __init__ (self , environ = None ):
411
+ def __init__ (self , environ = None , sitecustomize = None ):
392
412
import virtualenv
393
413
394
414
self .root_path = fast_test_env_root
@@ -473,6 +493,8 @@ def __init__(self, environ=None):
473
493
self .run ('python' , 'setup.py' , 'install' , cwd = src_folder , expect_stderr = True )
474
494
shutil .copytree (self .root_path , self .backup_path , True )
475
495
self ._use_cached_pypi_server ()
496
+ if sitecustomize :
497
+ self ._add_to_sitecustomize (sitecustomize )
476
498
assert self .root_path .exists
477
499
478
500
def __del__ (self ):
0 commit comments