Skip to content

Commit 6c68db6

Browse files
committed
Merge pull request #548 from qwcode/userscheme_pt1
--user fixes part 1
2 parents 2f84e14 + db2628d commit 6c68db6

File tree

2 files changed

+36
-9
lines changed

2 files changed

+36
-9
lines changed

tests/test_basic.py

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -615,3 +615,17 @@ def test_find_command_trys_supplied_pathext(mock_isfile, getpath_mock):
615615
assert_raises(BadCommand, find_command, 'foo', 'path_one', pathext)
616616
assert mock_isfile.call_args_list == expected, "Actual: %s\nExpected %s" % (mock_isfile.call_args_list, expected)
617617
assert not getpath_mock.called, "Should not call get_pathext"
618+
619+
620+
def test_reset_env_system_site_packages_user_site():
621+
"""
622+
reset_env(system_site_packages=True) produces env where a --user install can be found using pkg_resources
623+
"""
624+
if sys.version_info < (2, 6):
625+
raise SkipTest() #no PYTHONUSERBASE
626+
env = reset_env(system_site_packages=True)
627+
run_pip('install', '--user', 'INITools==0.2')
628+
result = env.run('python', '-c', "import pkg_resources; print(pkg_resources.get_distribution('initools').project_name)")
629+
project_name = result.stdout.strip()
630+
assert 'INITools'== project_name, "'%s' should be 'INITools'" %project_name
631+

tests/test_pip.py

Lines changed: 22 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -102,13 +102,21 @@ def install_setuptools(env):
102102
env = None
103103

104104

105-
def reset_env(environ=None, use_distribute=None):
105+
def reset_env(environ=None, use_distribute=None, system_site_packages=False):
106106
global env
107107
# 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:
109109
env = FastTestPipEnvironment(environ)
110110
else:
111111
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+
112120
return env
113121

114122

@@ -361,13 +369,18 @@ def __del__(self):
361369
rmtree(str(self.root_path), ignore_errors=True)
362370

363371
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()
371384

372385

373386
fast_test_env_root = here / 'tests_cache' / 'test_ws'

0 commit comments

Comments
 (0)