@@ -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
@@ -346,7 +355,12 @@ def __init__(self, environ=None, use_distribute=None):
346
355
347
356
# Install this version instead
348
357
self .run ('python' , 'setup.py' , 'install' , cwd = src_folder , expect_stderr = True )
358
+
359
+ #create sitecustomize.py and add patches
360
+ self ._create_empty_sitecustomize ()
349
361
self ._use_cached_pypi_server ()
362
+ if sitecustomize :
363
+ self ._add_to_sitecustomize (sitecustomize )
350
364
351
365
def _ignore_file (self , fn ):
352
366
if fn .endswith ('__pycache__' ) or fn .endswith (".pyc" ):
@@ -374,21 +388,36 @@ def _use_cached_pypi_server(self):
374
388
# 'import pypi_server' ultimately imports pkg_resources (which intializes pkg_resources.working_set based on the current state of sys.path)
375
389
# pkg_resources.get_distribution (used in pip.req) requires an accurate pkg_resources.working_set
376
390
# therefore, 'import pypi_server' shouldn't occur in a pth file.
391
+
392
+ patch = """
393
+ import sys
394
+ sys.path.insert(0, %r)
395
+ import pypi_server
396
+ pypi_server.PyPIProxy.setup()
397
+ sys.path.remove(%r)""" % (str (here ), str (here ))
398
+ self ._add_to_sitecustomize (patch )
399
+
400
+ def _create_empty_sitecustomize (self ):
401
+ "Create empty sitecustomize.py."
377
402
sitecustomize_path = self .lib_path / 'sitecustomize.py'
378
403
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 ))
383
404
sitecustomize .close ()
384
405
406
+ def _add_to_sitecustomize (self , snippet ):
407
+ "Adds a python code snippet to sitecustomize.py."
408
+ sitecustomize_path = self .lib_path / 'sitecustomize.py'
409
+ sitecustomize = open (sitecustomize_path , 'a' )
410
+ sitecustomize .write (textwrap .dedent ('''
411
+ %s
412
+ ''' % snippet ))
413
+ sitecustomize .close ()
385
414
386
415
fast_test_env_root = here / 'tests_cache' / 'test_ws'
387
416
fast_test_env_backup = here / 'tests_cache' / 'test_ws_backup'
388
417
389
418
390
419
class FastTestPipEnvironment (TestPipEnvironment ):
391
- def __init__ (self , environ = None ):
420
+ def __init__ (self , environ = None , sitecustomize = None ):
392
421
import virtualenv
393
422
394
423
self .root_path = fast_test_env_root
@@ -472,7 +501,13 @@ def __init__(self, environ=None):
472
501
# Install this version instead
473
502
self .run ('python' , 'setup.py' , 'install' , cwd = src_folder , expect_stderr = True )
474
503
shutil .copytree (self .root_path , self .backup_path , True )
504
+
505
+ #create sitecustomize.py and add patches
506
+ self ._create_empty_sitecustomize ()
475
507
self ._use_cached_pypi_server ()
508
+ if sitecustomize :
509
+ self ._add_to_sitecustomize (sitecustomize )
510
+
476
511
assert self .root_path .exists
477
512
478
513
def __del__ (self ):
0 commit comments