Skip to content

--user fixes part5: reset_env option for adding patches to sitecustomize #576

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 3 commits into from
Jun 19, 2012
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
53 changes: 44 additions & 9 deletions tests/test_pip.py
Original file line number Diff line number Diff line change
Expand Up @@ -102,13 +102,22 @@ def install_setuptools(env):
env = None


def reset_env(environ=None, use_distribute=None, system_site_packages=False):
def reset_env(environ=None, use_distribute=None, system_site_packages=False, sitecustomize=None):
"""Return a test environment.

Keyword arguments:
environ: an environ object to use.
use_distribute: use distribute, not setuptools.
system_site_packages: create a virtualenv that simulates --system-site-packages.
sitecustomize: a string containing python code to add to sitecustomize.py.
"""

global env
# FastTestPipEnv reuses env, not safe if use_distribute specified
if use_distribute is None and not system_site_packages:
env = FastTestPipEnvironment(environ)
env = FastTestPipEnvironment(environ, sitecustomize=sitecustomize)
else:
env = TestPipEnvironment(environ, use_distribute=use_distribute)
env = TestPipEnvironment(environ, use_distribute=use_distribute, sitecustomize=sitecustomize)

if system_site_packages:
#testing often occurs starting from a private virtualenv (e.g. with tox)
Expand Down Expand Up @@ -270,7 +279,7 @@ class TestPipEnvironment(TestFileEnvironment):

verbose = False

def __init__(self, environ=None, use_distribute=None):
def __init__(self, environ=None, use_distribute=None, sitecustomize=None):

self.root_path = Path(tempfile.mkdtemp('-piptest'))

Expand Down Expand Up @@ -346,7 +355,12 @@ def __init__(self, environ=None, use_distribute=None):

# Install this version instead
self.run('python', 'setup.py', 'install', cwd=src_folder, expect_stderr=True)

#create sitecustomize.py and add patches
self._create_empty_sitecustomize()
self._use_cached_pypi_server()
if sitecustomize:
self._add_to_sitecustomize(sitecustomize)

def _ignore_file(self, fn):
if fn.endswith('__pycache__') or fn.endswith(".pyc"):
Expand Down Expand Up @@ -374,21 +388,36 @@ def _use_cached_pypi_server(self):
# '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.

patch = """
import sys
sys.path.insert(0, %r)
import pypi_server
pypi_server.PyPIProxy.setup()
sys.path.remove(%r)""" % (str(here), str(here))
self._add_to_sitecustomize(patch)

def _create_empty_sitecustomize(self):
"Create empty sitecustomize.py."
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()

def _add_to_sitecustomize(self, snippet):
"Adds a python code snippet to sitecustomize.py."
sitecustomize_path = self.lib_path / 'sitecustomize.py'
sitecustomize = open(sitecustomize_path, 'a')
sitecustomize.write(textwrap.dedent('''
%s
''' %snippet))
sitecustomize.close()

fast_test_env_root = here / 'tests_cache' / 'test_ws'
fast_test_env_backup = here / 'tests_cache' / 'test_ws_backup'


class FastTestPipEnvironment(TestPipEnvironment):
def __init__(self, environ=None):
def __init__(self, environ=None, sitecustomize=None):
import virtualenv

self.root_path = fast_test_env_root
Expand Down Expand Up @@ -472,7 +501,13 @@ def __init__(self, environ=None):
# Install this version instead
self.run('python', 'setup.py', 'install', cwd=src_folder, expect_stderr=True)
shutil.copytree(self.root_path, self.backup_path, True)

#create sitecustomize.py and add patches
self._create_empty_sitecustomize()
self._use_cached_pypi_server()
if sitecustomize:
self._add_to_sitecustomize(sitecustomize)

assert self.root_path.exists

def __del__(self):
Expand Down
68 changes: 68 additions & 0 deletions tests/test_test.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,68 @@
"""Test the test support."""

import sys
import os
from os.path import abspath, join, curdir, isdir, isfile
from nose import SkipTest
from tests.local_repos import local_checkout
from tests.test_pip import here, reset_env, run_pip, pyversion


patch_urlopen = """
def mock_urlopen():
pass
import pip
pip.backwardcompat.urllib2.urlopen = mock_urlopen
"""

def test_pypiproxy_patch_applied():
"""
Test the PyPIProxy.setup() patch was applied, and sys.path returned to normal
"""

env = reset_env()
result = env.run('python', '-c', "import pip; print(pip.backwardcompat.urllib2.urlopen.__module__)")
#if it were not patched, the result would be 'urllib2'
assert "pypi_server"== result.stdout.strip(), result.stdout

#confirm the temporary sys.path adjustment is gone
result = env.run('python', '-c', "import sys; print(sys.path)")
paths = eval(result.stdout.strip())
assert here not in paths, paths


def test_add_patch_to_sitecustomize():
"""
Test adding monkey patch snippet to sitecustomize.py (using TestPipEnvironment)
"""

env = reset_env(sitecustomize=patch_urlopen, use_distribute=True)
result = env.run('python', '-c', "import pip; print(pip.backwardcompat.urllib2.urlopen.__module__)")
assert "sitecustomize"== result.stdout.strip(), result.stdout


def test_add_patch_to_sitecustomize_fast():
"""
Test adding monkey patch snippet to sitecustomize.py (using FastTestPipEnvironment)
"""

env = reset_env(sitecustomize=patch_urlopen)
result = env.run('python', '-c', "import pip; print(pip.backwardcompat.urllib2.urlopen.__module__)")
assert "sitecustomize"== result.stdout.strip(), result.stdout


def test_sitecustomize_not_growing_in_fast_environment():
"""
Test that the sitecustomize is not growing with redundant patches in the cached fast environment
"""

patch = "fu = 'bar'"

env1 = reset_env(sitecustomize=patch)
sc1 = env1.lib_path / 'sitecustomize.py'
size1 = os.stat(sc1).st_size
env2 = reset_env(sitecustomize=patch)
sc2 = env2.lib_path / 'sitecustomize.py'
size2 = os.stat(sc2).st_size
assert size1==size2, "size before, %d != size after, %d" %(size1, size2)

4 changes: 2 additions & 2 deletions tests/test_uninstall.py
Original file line number Diff line number Diff line change
Expand Up @@ -14,9 +14,9 @@ def test_simple_uninstall():

"""
env = reset_env()
result = run_pip('install', 'INITools==0.2', expect_error=True)
result = run_pip('install', 'INITools==0.2')
assert join(env.site_packages, 'initools') in result.files_created, sorted(result.files_created.keys())
result2 = run_pip('uninstall', 'INITools', '-y', expect_error=True)
result2 = run_pip('uninstall', 'INITools', '-y')
assert_all_changes(result, result2, [env.venv/'build', 'cache'])


Expand Down