@@ -102,13 +102,21 @@ def install_setuptools(env):
102
102
env = None
103
103
104
104
105
- def reset_env (environ = None , use_distribute = None ):
105
+ def reset_env (environ = None , use_distribute = None , system_site_packages = False ):
106
106
global env
107
107
# FastTestPipEnv reuses env, not safe if use_distribute specified
108
- if use_distribute is None :
108
+ if use_distribute is None and not system_site_packages :
109
109
env = FastTestPipEnvironment (environ )
110
110
else :
111
111
env = TestPipEnvironment (environ , use_distribute = use_distribute )
112
+
113
+ if system_site_packages :
114
+ #testing often occurs starting from a private virtualenv (e.g. with tox)
115
+ #from that context, you can't successfully use virtualenv.create_environment
116
+ #to create a 'system-site-packages' virtualenv
117
+ #hence, this workaround
118
+ (env .lib_path / 'no-global-site-packages.txt' ).rm ()
119
+
112
120
return env
113
121
114
122
@@ -361,13 +369,18 @@ def __del__(self):
361
369
rmtree (str (self .root_path ), ignore_errors = True )
362
370
363
371
def _use_cached_pypi_server (self ):
364
- site_packages = self .root_path / self .site_packages
365
- pth = open (os .path .join (site_packages , 'pypi_intercept.pth' ), 'w' )
366
- pth .write ('import sys; ' )
367
- pth .write ('sys.path.insert(0, %r); ' % str (here ))
368
- pth .write ('import pypi_server; pypi_server.PyPIProxy.setup(); ' )
369
- pth .write ('sys.path.remove(%r); ' % str (here ))
370
- pth .close ()
372
+ # previously, this was handled in a pth file, and not in sitecustomize.py
373
+ # pth processing happens during the construction of sys.path.
374
+ # 'import pypi_server' ultimately imports pkg_resources (which intializes pkg_resources.working_set based on the current state of sys.path)
375
+ # pkg_resources.get_distribution (used in pip.req) requires an accurate pkg_resources.working_set
376
+ # therefore, 'import pypi_server' shouldn't occur in a pth file.
377
+ 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 ))
383
+ sitecustomize .close ()
371
384
372
385
373
386
fast_test_env_root = here / 'tests_cache' / 'test_ws'
0 commit comments