Skip to content
Browse files

system_site_packages option for reset_env

  • Loading branch information...
1 parent 2f84e14 commit db2628dd28999bb39fa703a03bd4d5238d05d073 @qwcode committed May 30, 2012
Showing with 36 additions and 9 deletions.
  1. +14 −0 tests/test_basic.py
  2. +22 −9 tests/test_pip.py
View
14 tests/test_basic.py
@@ -615,3 +615,17 @@ def test_find_command_trys_supplied_pathext(mock_isfile, getpath_mock):
assert_raises(BadCommand, find_command, 'foo', 'path_one', pathext)
assert mock_isfile.call_args_list == expected, "Actual: %s\nExpected %s" % (mock_isfile.call_args_list, expected)
assert not getpath_mock.called, "Should not call get_pathext"
+
+
+def test_reset_env_system_site_packages_user_site():
+ """
+ reset_env(system_site_packages=True) produces env where a --user install can be found using pkg_resources
+ """
+ if sys.version_info < (2, 6):
+ raise SkipTest() #no PYTHONUSERBASE
+ env = reset_env(system_site_packages=True)
+ run_pip('install', '--user', 'INITools==0.2')
+ result = env.run('python', '-c', "import pkg_resources; print(pkg_resources.get_distribution('initools').project_name)")
+ project_name = result.stdout.strip()
+ assert 'INITools'== project_name, "'%s' should be 'INITools'" %project_name
+
View
31 tests/test_pip.py
@@ -102,13 +102,21 @@ def install_setuptools(env):
env = None
-def reset_env(environ=None, use_distribute=None):
+def reset_env(environ=None, use_distribute=None, system_site_packages=False):
global env
# FastTestPipEnv reuses env, not safe if use_distribute specified
- if use_distribute is None:
+ if use_distribute is None and not system_site_packages:
env = FastTestPipEnvironment(environ)
else:
env = TestPipEnvironment(environ, use_distribute=use_distribute)
+
+ if system_site_packages:
+ #testing often occurs starting from a private virtualenv (e.g. with tox)
+ #from that context, you can't successfully use virtualenv.create_environment
+ #to create a 'system-site-packages' virtualenv
+ #hence, this workaround
+ (env.lib_path/'no-global-site-packages.txt').rm()
+
return env
@@ -361,13 +369,18 @@ def __del__(self):
rmtree(str(self.root_path), ignore_errors=True)
def _use_cached_pypi_server(self):
- site_packages = self.root_path / self.site_packages
- pth = open(os.path.join(site_packages, 'pypi_intercept.pth'), 'w')
- pth.write('import sys; ')
- pth.write('sys.path.insert(0, %r); ' % str(here))
- pth.write('import pypi_server; pypi_server.PyPIProxy.setup(); ')
- pth.write('sys.path.remove(%r); ' % str(here))
- pth.close()
+ # previously, this was handled in a pth file, and not in sitecustomize.py
+ # pth processing happens during the construction of sys.path.
+ # 'import pypi_server' ultimately imports pkg_resources (which intializes pkg_resources.working_set based on the current state of sys.path)
+ # pkg_resources.get_distribution (used in pip.req) requires an accurate pkg_resources.working_set
+ # therefore, 'import pypi_server' shouldn't occur in a pth file.
+ sitecustomize_path = self.lib_path / 'sitecustomize.py'
+ sitecustomize = open(sitecustomize_path, 'w')
+ sitecustomize.write('import sys; ')
+ sitecustomize.write('sys.path.insert(0, %r); ' % str(here))
+ sitecustomize.write('import pypi_server; pypi_server.PyPIProxy.setup(); ')
+ sitecustomize.write('sys.path.remove(%r); ' % str(here))
+ sitecustomize.close()
fast_test_env_root = here / 'tests_cache' / 'test_ws'

0 comments on commit db2628d

Please sign in to comment.
Something went wrong with that request. Please try again.