Skip to content

Commit 6934084

Browse files
committed
reset_env option to add a patch to sitecustomize.py
1 parent ae867db commit 6934084

File tree

2 files changed

+85
-11
lines changed

2 files changed

+85
-11
lines changed

tests/test_pip.py

+33-11
Original file line numberDiff line numberDiff line change
@@ -102,13 +102,22 @@ def install_setuptools(env):
102102
env = None
103103

104104

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+
106115
global env
107116
# FastTestPipEnv reuses env, not safe if use_distribute specified
108117
if use_distribute is None and not system_site_packages:
109-
env = FastTestPipEnvironment(environ)
118+
env = FastTestPipEnvironment(environ, sitecustomize=sitecustomize)
110119
else:
111-
env = TestPipEnvironment(environ, use_distribute=use_distribute)
120+
env = TestPipEnvironment(environ, use_distribute=use_distribute, sitecustomize=sitecustomize)
112121

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

271280
verbose = False
272281

273-
def __init__(self, environ=None, use_distribute=None):
282+
def __init__(self, environ=None, use_distribute=None, sitecustomize=None):
274283

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

@@ -347,6 +356,8 @@ def __init__(self, environ=None, use_distribute=None):
347356
# Install this version instead
348357
self.run('python', 'setup.py', 'install', cwd=src_folder, expect_stderr=True)
349358
self._use_cached_pypi_server()
359+
if sitecustomize:
360+
self._add_to_sitecustomize(sitecustomize)
350361

351362
def _ignore_file(self, fn):
352363
if fn.endswith('__pycache__') or fn.endswith(".pyc"):
@@ -374,21 +385,30 @@ def _use_cached_pypi_server(self):
374385
# 'import pypi_server' ultimately imports pkg_resources (which intializes pkg_resources.working_set based on the current state of sys.path)
375386
# pkg_resources.get_distribution (used in pip.req) requires an accurate pkg_resources.working_set
376387
# therefore, 'import pypi_server' shouldn't occur in a pth file.
388+
389+
patch = """
390+
import sys
391+
sys.path.insert(0, %r)
392+
import pypi_server
393+
pypi_server.PyPIProxy.setup()
394+
sys.path.remove(%r)""" % (str(here), str(here))
395+
self._add_to_sitecustomize(patch)
396+
397+
def _add_to_sitecustomize(self, snippet):
398+
"Adds a python code snippet to sitecustomize.py."
377399
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))
400+
sitecustomize = open(sitecustomize_path, 'a')
401+
sitecustomize.write(textwrap.dedent('''
402+
%s
403+
''' %snippet))
383404
sitecustomize.close()
384405

385-
386406
fast_test_env_root = here / 'tests_cache' / 'test_ws'
387407
fast_test_env_backup = here / 'tests_cache' / 'test_ws_backup'
388408

389409

390410
class FastTestPipEnvironment(TestPipEnvironment):
391-
def __init__(self, environ=None):
411+
def __init__(self, environ=None, sitecustomize=None):
392412
import virtualenv
393413

394414
self.root_path = fast_test_env_root
@@ -473,6 +493,8 @@ def __init__(self, environ=None):
473493
self.run('python', 'setup.py', 'install', cwd=src_folder, expect_stderr=True)
474494
shutil.copytree(self.root_path, self.backup_path, True)
475495
self._use_cached_pypi_server()
496+
if sitecustomize:
497+
self._add_to_sitecustomize(sitecustomize)
476498
assert self.root_path.exists
477499

478500
def __del__(self):

tests/test_test.py

+52
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,52 @@
1+
"""Test the test support."""
2+
3+
import sys
4+
from os.path import abspath, join, curdir, isdir, isfile
5+
from nose import SkipTest
6+
from tests.local_repos import local_checkout
7+
from tests.test_pip import here, reset_env, run_pip, pyversion
8+
9+
10+
patch_urlopen = """
11+
def mock_urlopen():
12+
pass
13+
import pip
14+
pip.backwardcompat.urllib2.urlopen = mock_urlopen
15+
"""
16+
17+
def test_pypiproxy_patch_applied():
18+
"""
19+
Test the PyPIProxy.setup() patch was applied, and sys.path returned to normal
20+
"""
21+
22+
env = reset_env()
23+
result = env.run('python', '-c', "import pip; print(pip.backwardcompat.urllib2.urlopen.__module__)")
24+
#if it were not patched, the result would be 'urllib2'
25+
assert "pypi_server"== result.stdout.strip(), result.stdout
26+
27+
#confirm the temporary sys.path adjustment is gone
28+
result = env.run('python', '-c', "import sys; print(sys.path)")
29+
paths = eval(result.stdout.strip())
30+
assert here not in paths, paths
31+
32+
33+
def test_add_patch_to_sitecustomize():
34+
"""
35+
Test adding monkey patch snippet to sitecustomize.py (using TestPipEnvironment)
36+
"""
37+
38+
env = reset_env(sitecustomize=patch_urlopen, use_distribute=True)
39+
result = env.run('python', '-c', "import pip; print(pip.backwardcompat.urllib2.urlopen.__module__)")
40+
assert "sitecustomize"== result.stdout.strip(), result.stdout
41+
42+
43+
def test_add_patch_to_sitecustomize_fast():
44+
"""
45+
Test adding monkey patch snippet to sitecustomize.py (using FastTestPipEnvironment)
46+
"""
47+
48+
env = reset_env(sitecustomize=patch_urlopen)
49+
result = env.run('python', '-c', "import pip; print(pip.backwardcompat.urllib2.urlopen.__module__)")
50+
assert "sitecustomize"== result.stdout.strip(), result.stdout
51+
52+

0 commit comments

Comments
 (0)