fix test runner to operate fully within no-site-packages virtualenv; thanks Dave Abrahams for initial patch

This commit is contained in:
Carl Meyer 2010-04-12 20:48:37 -04:00
parent 7b3ea5a9f9
commit 4ee7604e4a
4 changed files with 66 additions and 28 deletions

View File

@ -8,9 +8,24 @@ def test_correct_pip_version():
"""
reset_env()
# where this source distribution lives
base = abspath(join(dirname(__file__), pardir))
# output will contain the directory of the invoked pip
result = run_pip('--version')
assert base in result.stdout, result.stdout
# compare the directory tree of the invoked pip with that of this source distribution
import re,filecmp
dir = re.match(r'\s*pip\s\S+\sfrom\s+(.*)\s\([^(]+\)$', result.stdout).group(1)
diffs = filecmp.dircmp(join(base,'pip'), join(dir,'pip'))
# If any non-matching .py files exist, we have a problem: run_pip
# is picking up some other version! N.B. if this project acquires
# primary resources other than .py files, this code will need
# maintenance
mismatch_py = [x for x in diffs.left_only + diffs.right_only + diffs.diff_files if x.endswith('.py')]
assert not mismatch_py, 'mismatched source files in %r and %r'% (join(base,'pip'), join(dir,'pip'))
def test_distutils_configuration_setting():
"""

View File

@ -26,7 +26,7 @@ def test_freeze():
result = run_pip('install', '-r', 'initools-req.txt')
result = run_pip('freeze', expect_stderr=True)
expected = textwrap.dedent("""\
Script result: ...ython... pip.main() .../test-scratch freeze
Script result: ...pip freeze
-- stdout: --------------------
INITools==0.2
simplejson==1.7.4...
@ -40,7 +40,7 @@ def test_freeze():
cwd=os.path.join(env.base_path, 'initools-trunk'))
result = run_pip('freeze', expect_stderr=True)
expected = textwrap.dedent("""\
Script result: ...ython... pip.main() -E .../test-scratch freeze
Script result: ...pip freeze
-- stdout: --------------------
-e svn+http://svn.colorstudy.com/INITools/trunk@3472#egg=INITools-0.2.1dev_r3472-py2...-dev_r3472
simplejson==1.7.4...
@ -51,7 +51,7 @@ def test_freeze():
result = env.run(os.path.join(env.base_path, 'bin/easy_install'), 'http://svn.colorstudy.com/INITools/trunk')
result = run_pip('freeze', expect_stderr=True)
expected = textwrap.dedent("""\
Script result: ...ython... pip.main() -E .../test-scratch freeze
Script result: ...pip freeze
-- stderr: --------------------
Warning: cannot find svn location for INITools==...dev-r...
<BLANKLINE>
@ -65,7 +65,7 @@ def test_freeze():
# Bah, that's no good! Let's give it a hint::
result = run_pip('freeze', '-f', 'http://svn.colorstudy.com/INITools/trunk#egg=INITools-dev', expect_stderr=True)
expected = textwrap.dedent("""\
Script result: ...ython... pip.main() -E .../test-scratch freeze -f http://svn.colorstudy.com/INITools/trunk#egg=INITools-dev
Script result: ...pip freeze -f http://svn.colorstudy.com/INITools/trunk#egg=INITools-dev
-- stdout: --------------------
-f http://svn.colorstudy.com/INITools/trunk#egg=INITools-dev
# Installing as editable to satisfy requirement INITools==...dev-r...:
@ -89,7 +89,7 @@ def test_freeze_git_clone():
cwd=os.path.join(env.base_path, 'django-pagination'))
result = run_pip('freeze', expect_stderr=True)
expected = textwrap.dedent("""\
Script result: ...ython... pip.main() -E .../test-scratch freeze
Script result: ...pip freeze
-- stdout: --------------------
-e git://github.com/jezdez/django-pagination.git@...#egg=django_pagination-...
...""")
@ -97,7 +97,7 @@ def test_freeze_git_clone():
result = run_pip('freeze', '-f', 'git://github.com/jezdez/django-pagination.git#egg=django_pagination', expect_stderr=True)
expected = textwrap.dedent("""\
Script result: ...ython... pip.main() -E .../test-scratch freeze -f git://github.com/jezdez/django-pagination.git#egg=django_pagination
Script result: ...pip freeze -f git://github.com/jezdez/django-pagination.git#egg=django_pagination
-- stdout: --------------------
-f git://github.com/jezdez/django-pagination.git#egg=django_pagination
-e git://github.com/jezdez/django-pagination.git@...#egg=django_pagination-...-dev
@ -117,7 +117,7 @@ def test_freeze_mercurial_clone():
cwd=os.path.join(env.base_path, 'django-dbtemplates'))
result = run_pip('freeze', expect_stderr=True)
expected = textwrap.dedent("""\
Script result: ...ython... pip.main() -E .../test-scratch freeze
Script result: ...pip freeze
-- stdout: --------------------
-e hg+http://bitbucket.org/jezdez/django-dbtemplates/@...#egg=django_dbtemplates-...
...""")
@ -125,7 +125,7 @@ def test_freeze_mercurial_clone():
result = run_pip('freeze', '-f', 'hg+http://bitbucket.org/jezdez/django-dbtemplates#egg=django_dbtemplates', expect_stderr=True)
expected = textwrap.dedent("""\
Script result: ...ython... pip.main() -E .../test-scratch freeze -f hg+http://bitbucket.org/jezdez/django-dbtemplates#egg=django_dbtemplates
Script result: ...pip freeze -f hg+http://bitbucket.org/jezdez/django-dbtemplates#egg=django_dbtemplates
-- stdout: --------------------
-f hg+http://bitbucket.org/jezdez/django-dbtemplates#egg=django_dbtemplates
-e hg+http://bitbucket.org/jezdez/django-dbtemplates/@...#egg=django_dbtemplates-...
@ -145,7 +145,7 @@ def test_freeze_bazaar_clone():
cwd=os.path.join(env.base_path, 'django-wikiapp'))
result = run_pip('freeze', expect_stderr=True)
expected = textwrap.dedent("""\
Script result: ...ython... pip.main() -E .../test-scratch freeze
Script result: ...pip freeze
-- stdout: --------------------
-e bzr+http://bazaar.launchpad.net/...django-wikiapp/django-wikiapp/release-0.1/@...#egg=django_wikiapp-...
...""")
@ -153,7 +153,7 @@ def test_freeze_bazaar_clone():
result = run_pip('freeze', '-f', 'bzr+http://bazaar.launchpad.net/%7Edjango-wikiapp/django-wikiapp/release-0.1/#egg=django-wikiapp', expect_stderr=True)
expected = textwrap.dedent("""\
Script result: ...ython... pip.main() -E .../test-scratch freeze -f bzr+http://bazaar.launchpad.net/%7Edjango-wikiapp/django-wikiapp/release-0.1/#egg=django-wikiapp
Script result: ...pip freeze -f bzr+http://bazaar.launchpad.net/%7Edjango-wikiapp/django-wikiapp/release-0.1/#egg=django-wikiapp
-- stdout: --------------------
-f bzr+http://bazaar.launchpad.net/...django-wikiapp/django-wikiapp/release-0.1/#egg=django-wikiapp
-e bzr+http://bazaar.launchpad.net/...django-wikiapp/django-wikiapp/release-0.1/@...#egg=django_wikiapp-...
@ -170,7 +170,7 @@ def test_freeze_with_local_option():
result = run_pip('install', 'initools==0.2')
result = run_pip('freeze', expect_stderr=True)
expected = textwrap.dedent("""\
Script result: ...ython... pip.main() .../test-scratch freeze
Script result: ...pip freeze
-- stdout: --------------------
INITools==0.2
wsgiref==...
@ -179,7 +179,7 @@ def test_freeze_with_local_option():
result = run_pip('freeze', '--local', expect_stderr=True)
expected = textwrap.dedent("""\
Script result: ...ython... pip.main() .../test-scratch freeze --local
Script result: ...pip freeze --local
-- stdout: --------------------
INITools==0.2
<BLANKLINE>""")

View File

@ -1,7 +1,5 @@
#!/usr/bin/env python
import os, sys
sys.path.insert(0, os.path.dirname(os.path.dirname(__file__)))
import doctest
pyversion = sys.version[:3]
lib_py = 'lib/python%s/' % pyversion
@ -29,6 +27,12 @@ def clear_environ(environ):
return dict(((k, v) for k, v in environ.iteritems()
if not k.lower().startswith('pip_')))
def virtualenv_bin_dir(path):
if sys.platform == 'win32':
return os.path.join(path, 'Scripts')
else:
return os.path.join(path, 'bin')
env = None
def reset_env(environ=None):
global env
@ -41,14 +45,21 @@ def reset_env(environ=None):
environ['PYTHONPATH'] = os.path.abspath(os.path.join(__file__, os.path.pardir, os.path.pardir))
env = TestFileEnvironment(base_path, ignore_hidden=False, environ=environ)
env.run(sys.executable, '-m', 'virtualenv', '--no-site-packages', env.base_path)
# put the test-scratch virtualenv's bin dir first on the script path
env.script_path.insert(0, virtualenv_bin_dir(env.base_path))
# make sure we have current setuptools to avoid svn incompatibilities
env.run('%s/bin/easy_install' % env.base_path, 'setuptools==0.6c11')
# Uninstall (kind of) pip, so PYTHONPATH can take effect:
env.run('%s/bin/easy_install' % env.base_path, '-m', 'pip')
env.run('mkdir', 'src')
env.run('easy_install', 'setuptools==0.6c11')
# Uninstall whatever version of pip came with the virtualenv
env.run('pip', 'uninstall', '-y', 'pip')
# Install this version instead
env.run('python', 'setup.py', 'install', cwd=os.path.dirname(here))
def run_pip(*args, **kw):
args = (sys.executable, '-c', 'import pip; pip.main()', '-E', env.base_path) + args
args = ('pip',) + args
#print >> sys.__stdout__, 'running', ' '.join(args)
result = env.run(*args, **kw)
return result
@ -102,6 +113,6 @@ def diff_states(start, end, ignore=None):
return dict(deleted=deleted, created=created, updated=updated)
if __name__ == '__main__':
sys.stderr.write("Run pip's tests using nosetests.\n")
sys.stderr.write("Run pip's tests using nosetests. Requires virtualenv, ScriptTest, and nose.\n")
sys.exit(1)

View File

@ -16,7 +16,8 @@ def test_simple_uninstall():
result = run_pip('install', 'INITools==0.2', expect_error=True)
assert join(site_pkg, 'initools') in result.files_created, sorted(result.files_created.keys())
result2 = run_pip('uninstall', 'INITools', '-y', expect_error=True)
assert diff_states(result.files_before, result2.files_after, ignore=['build']).values() == [{}, {}, {}]
diff = diff_states(result.files_before, result2.files_after, ignore=['build']).values()
assert diff == [{}, {}, {}], diff
def test_uninstall_with_scripts():
"""
@ -28,7 +29,9 @@ def test_uninstall_with_scripts():
result = env.run(join(env.base_path, 'bin', 'easy_install'), 'PyLogo')
assert('PyLogo' in result.files_updated[easy_install_pth].bytes), result.files_after[easy_install_pth].bytes
result2 = run_pip('uninstall', 'pylogo', '-y', expect_error=True)
assert diff_states(result.files_before, result2.files_after, ignore=['build']).values() == [{}, {}, {}]
diff = diff_states(result.files_before, result2.files_after, ignore=['build']).values()
assert diff == [{}, {}, {}], diff
def test_uninstall_namespace_package():
"""
@ -52,7 +55,9 @@ def test_uninstall_console_scripts():
result = run_pip('install', 'virtualenv', expect_error=True)
assert ('bin/virtualenv') in result.files_created, sorted(result.files_created.keys())
result2 = run_pip('uninstall', 'virtualenv', '-y', expect_error=True)
assert diff_states(result.files_before, result2.files_after, ignore=['build']).values() == [{}, {}, {}]
diff = diff_states(result.files_before, result2.files_after, ignore=['build']).values()
assert diff == [{}, {}, {}], diff
def test_uninstall_easy_installed_console_scripts():
"""
@ -64,7 +69,9 @@ def test_uninstall_easy_installed_console_scripts():
result = env.run(join(env.base_path, 'bin', 'easy_install'), 'virtualenv')
assert ('bin/virtualenv') in result.files_created, sorted(result.files_created.keys())
result2 = run_pip('uninstall', 'virtualenv', '-y', expect_error=True)
assert diff_states(result.files_before, result2.files_after, ignore=['build']).values() == [{}, {}, {}]
diff = diff_states(result.files_before, result2.files_after, ignore=['build']).values()
assert diff == [{}, {}, {}], diff
def test_uninstall_editable_from_svn():
"""
@ -76,8 +83,10 @@ def test_uninstall_editable_from_svn():
egg_link = result.files_created[join(site_pkg, 'INITools.egg-link')]
result2 = run_pip('uninstall', '-y', 'initools', expect_error=True)
assert ('src/initools' in result2.files_after), 'oh noes, pip deleted my sources!'
assert diff_states(result.files_before, result2.files_after, ignore=['src/initools', 'build']).values() == [{}, {}, {}]
diff = diff_states(result.files_before, result2.files_after, ignore=['build', 'src']).values()
assert diff == [{}, {}, {}], diff
def test_uninstall_editable_with_source_outside_venv():
"""
Test uninstalling editable install from existing source outside the venv.
@ -90,8 +99,10 @@ def test_uninstall_editable_with_source_outside_venv():
result2 = run_pip('install', '-e', tmpdir)
assert (join(site_pkg, 'virtualenv.egg-link') in result2.files_created), result2.files_created.keys()
result3 = run_pip('uninstall', '-y', 'virtualenv', expect_error=True)
assert diff_states(result.files_before, result3.files_after, ignore=['build']).values() == [{}, {}, {}]
diff = diff_states(result.files_before, result3.files_after, ignore=['build']).values()
assert diff == [{}, {}, {}], diff
def test_uninstall_from_reqs_file():
"""
Test uninstall from a requirements file.
@ -105,5 +116,6 @@ def test_uninstall_from_reqs_file():
"""))
result = run_pip('install', '-r', 'test-req.txt')
result2 = run_pip('uninstall', '-r', 'test-req.txt', '-y')
assert diff_states(result.files_before, result2.files_after, ignore=['build', 'src/initools']).values() == [{}, {}, {}]
diff = diff_states(result.files_before, result2.files_after, ignore=['build', 'src']).values()
assert diff == [{}, {}, {}], diff