From 9894c8baacaea1d7142e08b14a4fdead37c38e01 Mon Sep 17 00:00:00 2001 From: Donald Stufft Date: Wed, 21 Aug 2013 05:16:07 -0400 Subject: [PATCH] Refactor tests.lib * Move virtualenv creation out of TestPipEnvironment * Remove global state and force explicit use of TestPipEnvironment instances * Remove "backup" virtualenv copying and instead create new virtual environments each time. * Remove the monkeypatched "PyPICache" functionality * Remove things that were not being used anymore and were dead weight * Remove sitecustomize support which was primarily used to monkeypatch the "PyPICache" but was used in one or two other tests. --- tests/functional/test_bundle.py | 28 +- tests/functional/test_completion.py | 25 +- tests/functional/test_freeze.py | 80 ++-- tests/functional/test_help.py | 26 +- tests/functional/test_install.py | 227 +++++----- tests/functional/test_install_bundle.py | 8 +- tests/functional/test_install_cleanup.py | 102 +++-- tests/functional/test_install_compat.py | 23 +- tests/functional/test_install_config.py | 52 +-- tests/functional/test_install_download.py | 44 +- tests/functional/test_install_extras.py | 20 +- tests/functional/test_install_index.py | 38 +- tests/functional/test_install_reqs.py | 68 ++- tests/functional/test_install_upgrade.py | 196 ++++----- tests/functional/test_install_user.py | 157 +++---- tests/functional/test_install_vcs.py | 88 ++-- tests/functional/test_install_vcs_git.py | 46 +-- tests/functional/test_install_wheel.py | 43 +- tests/functional/test_list.py | 40 +- tests/functional/test_search.py | 27 +- tests/functional/test_show.py | 20 +- tests/functional/test_uninstall.py | 137 +++--- tests/functional/test_wheel.py | 50 ++- tests/lib/__init__.py | 480 +++++++--------------- tests/lib/git_submodule_helpers.py | 22 +- tests/lib/local_repos.py | 11 +- tests/lib/pypi_server.py | 138 ------- tests/lib/test_lib.py | 106 +---- tests/unit/test_index.py | 2 +- tests/unit/test_util.py | 20 +- 30 files changed, 958 insertions(+), 1366 deletions(-) delete mode 100644 tests/lib/pypi_server.py diff --git a/tests/functional/test_bundle.py b/tests/functional/test_bundle.py index 4c22de7e8..8d0c69b7f 100644 --- a/tests/functional/test_bundle.py +++ b/tests/functional/test_bundle.py @@ -2,7 +2,7 @@ import zipfile import textwrap from os.path import abspath, exists, join from pip.download import path_to_url2 -from tests.lib import tests_data, reset_env, run_pip, write_file +from tests.lib import tests_data, reset_env from tests.lib.path import Path from tests.lib.local_repos import local_checkout @@ -14,16 +14,16 @@ def test_create_bundle(): index (pip itself). """ - env = reset_env() + script = reset_env() fspkg = path_to_url2(Path(tests_data)/'packages'/'FSPkg') - run_pip('install', '-e', fspkg) + script.pip('install', '-e', fspkg) pkg_lines = textwrap.dedent('''\ -e %s -e %s#egg=initools-dev pip''' % (fspkg, local_checkout('svn+http://svn.colorstudy.com/INITools/trunk'))) - write_file('bundle-req.txt', pkg_lines) + script.scratch_path.join("bundle-req.txt").write(pkg_lines) # Create a bundle in env.scratch_path/ test.pybundle - result = run_pip('bundle', '-r', env.scratch_path/ 'bundle-req.txt', env.scratch_path/ 'test.pybundle') + result = script.pip('bundle', '-r', script.scratch_path/ 'bundle-req.txt', script.scratch_path/ 'test.pybundle') bundle = result.files_after.get(join('scratch', 'test.pybundle'), None) assert bundle is not None @@ -38,15 +38,15 @@ def test_cleanup_after_create_bundle(): Test clean up after making a bundle. Make sure (build|src)-bundle/ dirs are removed but not src/. """ - env = reset_env() + script = reset_env() # Install an editable to create a src/ dir. args = ['install'] args.extend(['-e', '%s#egg=pip-test-package' % local_checkout('git+http://github.com/pypa/pip-test-package.git')]) - run_pip(*args) - build = env.venv_path/"build" - src = env.venv_path/"src" + script.pip(*args) + build = script.venv_path/"build" + src = script.venv_path/"src" assert not exists(build), "build/ dir still exists: %s" % build assert exists(src), "expected src/ dir doesn't exist: %s" % src @@ -56,13 +56,13 @@ def test_cleanup_after_create_bundle(): -e %s -e %s#egg=initools-dev pip''' % (fspkg, local_checkout('svn+http://svn.colorstudy.com/INITools/trunk'))) - write_file('bundle-req.txt', pkg_lines) - run_pip('bundle', '-r', 'bundle-req.txt', 'test.pybundle') - build_bundle = env.scratch_path/"build-bundle" - src_bundle = env.scratch_path/"src-bundle" + script.scratch_path.join("bundle-req.txt").write(pkg_lines) + script.pip('bundle', '-r', 'bundle-req.txt', 'test.pybundle') + build_bundle = script.scratch_path/"build-bundle" + src_bundle = script.scratch_path/"src-bundle" assert not exists(build_bundle), "build-bundle/ dir still exists: %s" % build_bundle assert not exists(src_bundle), "src-bundle/ dir still exists: %s" % src_bundle - env.assert_no_temp() + script.assert_no_temp() # Make sure previously created src/ from editable still exists assert exists(src), "expected src dir doesn't exist: %s" % src diff --git a/tests/functional/test_completion.py b/tests/functional/test_completion.py index 77afb9196..6deccc44a 100644 --- a/tests/functional/test_completion.py +++ b/tests/functional/test_completion.py @@ -1,12 +1,12 @@ import os -from tests.lib import reset_env, run_pip, get_env +from tests.lib import reset_env def test_completion_for_bash(): """ Test getting completion for bash shell """ - reset_env() + script = reset_env() bash_completion = """\ _pip_completion() { @@ -16,7 +16,7 @@ _pip_completion() } complete -o default -F _pip_completion pip""" - result = run_pip('completion', '--bash') + result = script.pip('completion', '--bash') assert bash_completion in result.stdout, 'bash completion is wrong' @@ -24,7 +24,7 @@ def test_completion_for_zsh(): """ Test getting completion for zsh shell """ - reset_env() + script = reset_env() zsh_completion = """\ function _pip_completion { local words cword @@ -36,7 +36,7 @@ function _pip_completion { } compctl -K _pip_completion pip""" - result = run_pip('completion', '--zsh') + result = script.pip('completion', '--zsh') assert zsh_completion in result.stdout, 'zsh completion is wrong' @@ -44,9 +44,9 @@ def test_completion_for_unknown_shell(): """ Test getting completion for an unknown shell """ - reset_env() + script = reset_env() error_msg = 'no such option: --myfooshell' - result = run_pip('completion', '--myfooshell', expect_error=True) + result = script.pip('completion', '--myfooshell', expect_error=True) assert error_msg in result.stderr, 'tests for an unknown shell failed' @@ -54,25 +54,24 @@ def test_completion_alone(): """ Test getting completion for none shell, just pip completion """ - reset_env() - result = run_pip('completion', expect_error=True) + script = reset_env() + result = script.pip('completion', expect_error=True) assert 'ERROR: You must pass --bash or --zsh' in result.stderr, \ 'completion alone failed -- ' + result.stderr def setup_completion(words, cword): environ = os.environ.copy() - reset_env(environ) + script = reset_env(environ) environ['PIP_AUTO_COMPLETE'] = '1' environ['COMP_WORDS'] = words environ['COMP_CWORD'] = cword - env = get_env() # expect_error is True because autocomplete exists with 1 status code - result = env.run('python', '-c', 'import pip;pip.autocomplete()', + result = script.run('python', '-c', 'import pip;pip.autocomplete()', expect_error=True) - return result, env + return result, script def test_completion_for_un_snippet(): diff --git a/tests/functional/test_freeze.py b/tests/functional/test_freeze.py index 604825b71..3db0aaa60 100644 --- a/tests/functional/test_freeze.py +++ b/tests/functional/test_freeze.py @@ -2,7 +2,7 @@ import sys import re import textwrap from doctest import OutputChecker, ELLIPSIS -from tests.lib import reset_env, run_pip, write_file, get_env, pyversion, pip_install_local +from tests.lib import reset_env from tests.lib.local_repos import local_checkout, local_repo @@ -42,14 +42,14 @@ def test_freeze_basic(): currently it is not). """ - env = reset_env() - write_file('initools-req.txt', textwrap.dedent("""\ + script = reset_env() + script.scratch_path.join("initools-req.txt").write(textwrap.dedent("""\ simple==2.0 # and something else to test out: simple2<=3.0 """)) - result = pip_install_local('-r', env.scratch_path/'initools-req.txt') - result = run_pip('freeze', expect_stderr=True) + result = script.pip_install_local('-r', script.scratch_path/'initools-req.txt') + result = script.pip('freeze', expect_stderr=True) expected = textwrap.dedent("""\ Script result: pip freeze -- stdout: -------------------- @@ -66,13 +66,13 @@ def test_freeze_svn(): #svn internally stores windows drives as uppercase; we'll match that. checkout_path = checkout_path.replace('c:', 'C:') - env = reset_env() - result = env.run('svn', 'co', '-r10', + script = reset_env() + result = script.run('svn', 'co', '-r10', local_repo('svn+http://svn.colorstudy.com/INITools/trunk'), 'initools-trunk') - result = env.run('python', 'setup.py', 'develop', - cwd=env.scratch_path/ 'initools-trunk', expect_stderr=True) - result = run_pip('freeze', expect_stderr=True) + result = script.run('python', 'setup.py', 'develop', + cwd=script.scratch_path/ 'initools-trunk', expect_stderr=True) + result = script.pip('freeze', expect_stderr=True) expected = textwrap.dedent("""\ Script result: ...pip freeze -- stdout: -------------------- @@ -86,13 +86,13 @@ def test_freeze_git_clone(): Test freezing a Git clone. """ - env = reset_env() - result = env.run('git', 'clone', local_repo('git+http://github.com/pypa/pip-test-package.git'), 'pip-test-package') - result = env.run('git', 'checkout', '7d654e66c8fa7149c165ddeffa5b56bc06619458', - cwd=env.scratch_path / 'pip-test-package', expect_stderr=True) - result = env.run('python', 'setup.py', 'develop', - cwd=env.scratch_path / 'pip-test-package') - result = run_pip('freeze', expect_stderr=True) + script = reset_env() + result = script.run('git', 'clone', local_repo('git+http://github.com/pypa/pip-test-package.git'), 'pip-test-package') + result = script.run('git', 'checkout', '7d654e66c8fa7149c165ddeffa5b56bc06619458', + cwd=script.scratch_path / 'pip-test-package', expect_stderr=True) + result = script.run('python', 'setup.py', 'develop', + cwd=script.scratch_path / 'pip-test-package') + result = script.pip('freeze', expect_stderr=True) expected = textwrap.dedent("""\ Script result: ...pip freeze -- stdout: -------------------- @@ -100,7 +100,7 @@ def test_freeze_git_clone(): ...""" % local_checkout('git+http://github.com/pypa/pip-test-package.git')) _check_output(result, expected) - result = run_pip('freeze', '-f', + result = script.pip('freeze', '-f', '%s#egg=pip_test_package' % local_checkout('git+http://github.com/pypa/pip-test-package.git'), expect_stderr=True) expected = textwrap.dedent("""\ @@ -117,15 +117,14 @@ def test_freeze_mercurial_clone(): Test freezing a Mercurial clone. """ - reset_env() - env = get_env() - result = env.run('hg', 'clone', + script = reset_env() + result = script.run('hg', 'clone', '-r', 'c9963c111e7c', local_repo('hg+http://bitbucket.org/pypa/pip-test-package'), 'pip-test-package') - result = env.run('python', 'setup.py', 'develop', - cwd=env.scratch_path/'pip-test-package', expect_stderr=True) - result = run_pip('freeze', expect_stderr=True) + result = script.run('python', 'setup.py', 'develop', + cwd=script.scratch_path/'pip-test-package', expect_stderr=True) + result = script.pip('freeze', expect_stderr=True) expected = textwrap.dedent("""\ Script result: ...pip freeze -- stdout: -------------------- @@ -133,7 +132,7 @@ def test_freeze_mercurial_clone(): ...""" % local_checkout('hg+http://bitbucket.org/pypa/pip-test-package')) _check_output(result, expected) - result = run_pip('freeze', '-f', + result = script.pip('freeze', '-f', '%s#egg=pip_test_package' % local_checkout('hg+http://bitbucket.org/pypa/pip-test-package'), expect_stderr=True) expected = textwrap.dedent("""\ @@ -155,14 +154,13 @@ def test_freeze_bazaar_clone(): #bzr internally stores windows drives as uppercase; we'll match that. checkout_pathC = checkout_path.replace('c:', 'C:') - reset_env() - env = get_env() - result = env.run('bzr', 'checkout', '-r', '174', + script = reset_env() + result = script.run('bzr', 'checkout', '-r', '174', local_repo('bzr+http://bazaar.launchpad.net/%7Edjango-wikiapp/django-wikiapp/release-0.1'), 'django-wikiapp') - result = env.run('python', 'setup.py', 'develop', - cwd=env.scratch_path/'django-wikiapp') - result = run_pip('freeze', expect_stderr=True) + result = script.run('python', 'setup.py', 'develop', + cwd=script.scratch_path/'django-wikiapp') + result = script.pip('freeze', expect_stderr=True) expected = textwrap.dedent("""\ Script result: ...pip freeze -- stdout: -------------------- @@ -170,7 +168,7 @@ def test_freeze_bazaar_clone(): ...""" % checkout_pathC) _check_output(result, expected) - result = run_pip('freeze', '-f', + result = script.pip('freeze', '-f', '%s/#egg=django-wikiapp' % checkout_path, expect_stderr=True) expected = textwrap.dedent("""\ @@ -187,9 +185,9 @@ def test_freeze_with_local_option(): Test that wsgiref (from global site-packages) is reported normally, but not with --local. """ - reset_env() - result = run_pip('install', 'initools==0.2') - result = run_pip('freeze', expect_stderr=True) + script = reset_env() + result = script.pip('install', 'initools==0.2') + result = script.pip('freeze', expect_stderr=True) expected = textwrap.dedent("""\ Script result: ...pip freeze -- stdout: -------------------- @@ -204,7 +202,7 @@ def test_freeze_with_local_option(): # _check_output(result, expected) - result = run_pip('freeze', '--local', expect_stderr=True) + result = script.pip('freeze', '--local', expect_stderr=True) expected = textwrap.dedent("""\ Script result: ...pip freeze --local -- stdout: -------------------- @@ -218,7 +216,7 @@ def test_freeze_with_requirement_option(): Test that new requirements are created correctly with --requirement hints """ - reset_env() + script = reset_env() ignores = textwrap.dedent("""\ # Unchanged requirements below this line -r ignore.txt @@ -231,13 +229,13 @@ def test_freeze_with_requirement_option(): --find-links http://ignore --index-url http://ignore """) - write_file('hint.txt', textwrap.dedent("""\ + script.scratch_path.join("hint.txt").write(textwrap.dedent("""\ INITools==0.1 NoExist==4.2 """) + ignores) - result = run_pip('install', 'initools==0.2') - result = pip_install_local('simple') - result = run_pip('freeze', '--requirement', 'hint.txt', expect_stderr=True) + result = script.pip('install', 'initools==0.2') + result = script.pip_install_local('simple') + result = script.pip('freeze', '--requirement', 'hint.txt', expect_stderr=True) expected = textwrap.dedent("""\ Script result: pip freeze --requirement hint.txt -- stderr: -------------------- diff --git a/tests/functional/test_help.py b/tests/functional/test_help.py index a28fb7c14..8ca3d7329 100644 --- a/tests/functional/test_help.py +++ b/tests/functional/test_help.py @@ -6,7 +6,7 @@ from pip.basecommand import ERROR, SUCCESS from pip.commands.help import HelpCommand from pip.commands import commands from mock import Mock -from tests.lib import run_pip, reset_env +from tests.lib import reset_env def test_run_method_should_return_sucess_when_finds_command_name(): @@ -47,8 +47,8 @@ def test_help_command_should_exit_status_ok_when_command_exists(): """ Test `help` command for existing command """ - reset_env() - result = run_pip('help', 'freeze') + script = reset_env() + result = script.pip('help', 'freeze') assert result.returncode == SUCCESS @@ -56,8 +56,8 @@ def test_help_command_should_exit_status_ok_when_no_command_is_specified(): """ Test `help` command for no command """ - reset_env() - result = run_pip('help') + script = reset_env() + result = script.pip('help') assert result.returncode == SUCCESS @@ -65,18 +65,18 @@ def test_help_command_should_exit_status_error_when_command_does_not_exist(): """ Test `help` command for non-existing command """ - reset_env() - result = run_pip('help', 'mycommand', expect_error=True) + script = reset_env() + result = script.pip('help', 'mycommand', expect_error=True) assert result.returncode == ERROR def test_help_commands_equally_functional(): """ Test if `pip help` and 'pip --help' behave the same way. """ - reset_env() + script = reset_env() - results = list(map(run_pip, ('help', '--help'))) - results.append(run_pip()) + results = list(map(script.pip, ('help', '--help'))) + results.append(script.pip()) out = map(lambda x: x.stdout, results) ret = map(lambda x: x.returncode, results) @@ -87,7 +87,5 @@ def test_help_commands_equally_functional(): for name, cls in commands.items(): if cls.hidden: continue - assert run_pip('help', name).stdout == \ - run_pip(name, '--help').stdout - - + assert script.pip('help', name).stdout == \ + script.pip(name, '--help').stdout diff --git a/tests/functional/test_install.py b/tests/functional/test_install.py index e5ba35c7d..26e2572c0 100644 --- a/tests/functional/test_install.py +++ b/tests/functional/test_install.py @@ -7,7 +7,7 @@ from os.path import abspath, join, curdir, pardir import pytest from pip.util import rmtree -from tests.lib import tests_data, reset_env, run_pip, pyversion, mkdir, pip_install_local, write_file, find_links +from tests.lib import tests_data, reset_env, pyversion, find_links from tests.lib.local_repos import local_checkout from tests.lib.path import Path @@ -16,13 +16,13 @@ def test_pip_second_command_line_interface_works(): """ Check if ``pip`` commands behaves equally """ - e = reset_env() + script = reset_env() args = ['pip%s' % pyversion] args.extend(['install', 'INITools==0.2']) - result = e.run(*args) - egg_info_folder = e.site_packages / 'INITools-0.2-py%s.egg-info' % pyversion - initools_folder = e.site_packages / 'initools' + result = script.run(*args) + egg_info_folder = script.site_packages / 'INITools-0.2-py%s.egg-info' % pyversion + initools_folder = script.site_packages / 'initools' assert egg_info_folder in result.files_created, str(result) assert initools_folder in result.files_created, str(result) @@ -31,10 +31,10 @@ def test_install_from_pypi(): """ Test installing a package from PyPI. """ - e = reset_env() - result = run_pip('install', '-vvv', 'INITools==0.2') - egg_info_folder = e.site_packages / 'INITools-0.2-py%s.egg-info' % pyversion - initools_folder = e.site_packages / 'initools' + script = reset_env() + result = script.pip('install', '-vvv', 'INITools==0.2') + egg_info_folder = script.site_packages / 'INITools-0.2-py%s.egg-info' % pyversion + initools_folder = script.site_packages / 'initools' assert egg_info_folder in result.files_created, str(result) assert initools_folder in result.files_created, str(result) @@ -43,8 +43,8 @@ def test_editable_install(): """ Test editable installation. """ - reset_env() - result = run_pip('install', '-e', 'INITools==0.2', expect_error=True) + script = reset_env() + result = script.pip('install', '-e', 'INITools==0.2', expect_error=True) assert "INITools==0.2 should either by a path to a local project or a VCS url" in result.stdout assert len(result.files_created) == 1, result.files_created assert not result.files_updated, result.files_updated @@ -54,8 +54,8 @@ def test_install_editable_from_svn(): """ Test checking out from svn. """ - reset_env() - result = run_pip('install', + script = reset_env() + result = script.pip('install', '-e', '%s#egg=initools-dev' % local_checkout('svn+http://svn.colorstudy.com/INITools/trunk')) @@ -66,9 +66,9 @@ def test_download_editable_to_custom_path(): """ Test downloading an editable using a relative custom src folder. """ - reset_env() - mkdir('customdl') - result = run_pip('install', + script = reset_env() + script.scratch_path.join("customdl").mkdir() + result = script.pip('install', '-e', '%s#egg=initools-dev' % local_checkout('svn+http://svn.colorstudy.com/INITools/trunk'), @@ -90,16 +90,16 @@ def test_editable_no_install_followed_by_no_download(): """ Test installing an editable in two steps (first with --no-install, then with --no-download). """ - reset_env() + script = reset_env() - result = run_pip('install', + result = script.pip('install', '-e', '%s#egg=initools-dev' % local_checkout('svn+http://svn.colorstudy.com/INITools/trunk'), '--no-install', expect_error=True) result.assert_installed('INITools', without_egg_link=True, with_files=['.svn']) - result = run_pip('install', + result = script.pip('install', '-e', '%s#egg=initools-dev' % local_checkout('svn+http://svn.colorstudy.com/INITools/trunk'), @@ -111,19 +111,19 @@ def test_no_install_followed_by_no_download(): """ Test installing in two steps (first with --no-install, then with --no-download). """ - env = reset_env() + script = reset_env() - egg_info_folder = env.site_packages/'INITools-0.2-py%s.egg-info' % pyversion - initools_folder = env.site_packages/'initools' - build_dir = env.venv/'build'/'INITools' + egg_info_folder = script.site_packages/'INITools-0.2-py%s.egg-info' % pyversion + initools_folder = script.site_packages/'initools' + build_dir = script.venv/'build'/'INITools' - result1 = run_pip('install', 'INITools==0.2', '--no-install', expect_error=True) + result1 = script.pip('install', 'INITools==0.2', '--no-install', expect_error=True) assert egg_info_folder not in result1.files_created, str(result1) assert initools_folder not in result1.files_created, sorted(result1.files_created) assert build_dir in result1.files_created, result1.files_created assert build_dir/'INITools.egg-info' in result1.files_created - result2 = run_pip('install', 'INITools==0.2', '--no-download', expect_error=True) + result2 = script.pip('install', 'INITools==0.2', '--no-download', expect_error=True) assert egg_info_folder in result2.files_created, str(result2) assert initools_folder in result2.files_created, sorted(result2.files_created) assert build_dir not in result2.files_created @@ -134,8 +134,8 @@ def test_bad_install_with_no_download(): """ Test that --no-download behaves sensibly if the package source can't be found. """ - reset_env() - result = run_pip('install', 'INITools==0.2', '--no-download', expect_error=True) + script = reset_env() + result = script.pip('install', 'INITools==0.2', '--no-download', expect_error=True) assert "perhaps --no-download was used without first running "\ "an equivalent install with --no-install?" in result.stdout @@ -144,24 +144,24 @@ def test_install_dev_version_from_pypi(): """ Test using package==dev. """ - e = reset_env() - result = run_pip('install', 'INITools==dev', + script = reset_env() + result = script.pip('install', 'INITools==dev', '--allow-external', 'INITools', '--allow-insecure', 'INITools', expect_error=True) - assert (e.site_packages / 'initools') in result.files_created, str(result.stdout) + assert (script.site_packages / 'initools') in result.files_created, str(result.stdout) def test_install_editable_from_git(): """ Test cloning from Git. """ - reset_env() + script = reset_env() args = ['install'] args.extend(['-e', '%s#egg=pip-test-package' % local_checkout('git+http://github.com/pypa/pip-test-package.git')]) - result = run_pip(*args, **{"expect_error": True}) + result = script.pip(*args, **{"expect_error": True}) result.assert_installed('pip-test-package', with_files=['.git']) @@ -169,8 +169,8 @@ def test_install_editable_from_hg(): """ Test cloning from Mercurial. """ - reset_env() - result = run_pip('install', '-e', + script = reset_env() + result = script.pip('install', '-e', '%s#egg=ScriptTest' % local_checkout('hg+https://bitbucket.org/ianb/scripttest'), expect_error=True) @@ -181,8 +181,8 @@ def test_vcs_url_final_slash_normalization(): """ Test that presence or absence of final slash in VCS URL is normalized. """ - reset_env() - result = run_pip('install', '-e', + script = reset_env() + result = script.pip('install', '-e', '%s/#egg=ScriptTest' % local_checkout('hg+https://bitbucket.org/ianb/scripttest'), expect_error=True) @@ -193,8 +193,8 @@ def test_install_editable_from_bazaar(): """ Test checking out from Bazaar. """ - reset_env() - result = run_pip('install', '-e', + script = reset_env() + result = script.pip('install', '-e', '%s/@174#egg=django-wikiapp' % local_checkout('bzr+http://bazaar.launchpad.net/%7Edjango-wikiapp/django-wikiapp/release-0.1'), expect_error=True) @@ -205,8 +205,8 @@ def test_vcs_url_urlquote_normalization(): """ Test that urlquoted characters are normalized for repo URL comparison. """ - reset_env() - result = run_pip('install', '-e', + script = reset_env() + result = script.pip('install', '-e', '%s/#egg=django-wikiapp' % local_checkout('bzr+http://bazaar.launchpad.net/%7Edjango-wikiapp/django-wikiapp/release-0.1'), expect_error=True) @@ -217,11 +217,11 @@ def test_install_from_local_directory(): """ Test installing from a local directory. """ - env = reset_env() + script = reset_env() to_install = abspath(join(tests_data, 'packages', 'FSPkg')) - result = run_pip('install', to_install, expect_error=False) - fspkg_folder = env.site_packages/'fspkg' - egg_info_folder = env.site_packages/'FSPkg-0.1dev-py%s.egg-info' % pyversion + result = script.pip('install', to_install, expect_error=False) + fspkg_folder = script.site_packages/'fspkg' + egg_info_folder = script.site_packages/'FSPkg-0.1dev-py%s.egg-info' % pyversion assert fspkg_folder in result.files_created, str(result.stdout) assert egg_info_folder in result.files_created, str(result) @@ -230,8 +230,8 @@ def test_install_from_local_directory_with_no_setup_py(): """ Test installing from a local directory with no 'setup.py'. """ - reset_env() - result = run_pip('install', tests_data, expect_error=True) + script = reset_env() + result = script.pip('install', tests_data, expect_error=True) assert len(result.files_created) == 1, result.files_created assert 'pip-log.txt' in result.files_created, result.files_created assert "is not installable. File 'setup.py' not found." in result.stdout @@ -241,8 +241,8 @@ def test_editable_install_from_local_directory_with_no_setup_py(): """ Test installing from a local directory with no 'setup.py'. """ - reset_env() - result = run_pip('install', '-e', tests_data, expect_error=True) + script = reset_env() + result = script.pip('install', '-e', tests_data, expect_error=True) assert len(result.files_created) == 1, result.files_created assert 'pip-log.txt' in result.files_created, result.files_created assert "is not installable. File 'setup.py' not found." in result.stdout @@ -252,11 +252,11 @@ def test_install_as_egg(): """ Test installing as egg, instead of flat install. """ - env = reset_env() + script = reset_env() to_install = abspath(join(tests_data, 'packages', 'FSPkg')) - result = run_pip('install', to_install, '--egg', expect_error=False) - fspkg_folder = env.site_packages/'fspkg' - egg_folder = env.site_packages/'FSPkg-0.1dev-py%s.egg' % pyversion + result = script.pip('install', to_install, '--egg', expect_error=False) + fspkg_folder = script.site_packages/'fspkg' + egg_folder = script.site_packages/'FSPkg-0.1dev-py%s.egg' % pyversion assert fspkg_folder not in result.files_created, str(result.stdout) assert egg_folder in result.files_created, str(result) assert join(egg_folder, 'fspkg') in result.files_created, str(result) @@ -266,15 +266,15 @@ def test_install_curdir(): """ Test installing current directory ('.'). """ - env = reset_env() + script = reset_env() run_from = abspath(join(tests_data, 'packages', 'FSPkg')) # Python 2.4 Windows balks if this exists already egg_info = join(run_from, "FSPkg.egg-info") if os.path.isdir(egg_info): rmtree(egg_info) - result = run_pip('install', curdir, cwd=run_from, expect_error=False) - fspkg_folder = env.site_packages/'fspkg' - egg_info_folder = env.site_packages/'FSPkg-0.1dev-py%s.egg-info' % pyversion + result = script.pip('install', curdir, cwd=run_from, expect_error=False) + fspkg_folder = script.site_packages/'fspkg' + egg_info_folder = script.site_packages/'FSPkg-0.1dev-py%s.egg-info' % pyversion assert fspkg_folder in result.files_created, str(result.stdout) assert egg_info_folder in result.files_created, str(result) @@ -283,11 +283,11 @@ def test_install_pardir(): """ Test installing parent directory ('..'). """ - env = reset_env() + script = reset_env() run_from = abspath(join(tests_data, 'packages', 'FSPkg', 'fspkg')) - result = run_pip('install', pardir, cwd=run_from, expect_error=False) - fspkg_folder = env.site_packages/'fspkg' - egg_info_folder = env.site_packages/'FSPkg-0.1dev-py%s.egg-info' % pyversion + result = script.pip('install', pardir, cwd=run_from, expect_error=False) + fspkg_folder = script.site_packages/'fspkg' + egg_info_folder = script.site_packages/'FSPkg-0.1dev-py%s.egg-info' % pyversion assert fspkg_folder in result.files_created, str(result.stdout) assert egg_info_folder in result.files_created, str(result) @@ -297,8 +297,8 @@ def test_install_global_option(): Test using global distutils options. (In particular those that disable the actual install action) """ - reset_env() - result = run_pip('install', '--global-option=--version', "INITools==0.1") + script = reset_env() + result = script.pip('install', '--global-option=--version', "INITools==0.1") assert '0.1\n' in result.stdout @@ -306,18 +306,18 @@ def test_install_with_pax_header(): """ test installing from a tarball with pax header for python<2.6 """ - reset_env() + script = reset_env() run_from = abspath(join(tests_data, 'packages')) - run_pip('install', 'paxpkg.tar.bz2', cwd=run_from) + script.pip('install', 'paxpkg.tar.bz2', cwd=run_from) def test_install_with_hacked_egg_info(): """ test installing a package which defines its own egg_info class """ - reset_env() + script = reset_env() run_from = abspath(join(tests_data, 'packages', 'HackedEggInfo')) - result = run_pip('install', '.', cwd=run_from) + result = script.pip('install', '.', cwd=run_from) assert 'Successfully installed hackedegginfo\n' in result.stdout @@ -325,14 +325,14 @@ def test_install_using_install_option_and_editable(): """ Test installing a tool using -e and --install-option """ - env = reset_env() + script = reset_env() folder = 'script_folder' - mkdir(folder) + script.scratch_path.join(folder).mkdir() url = 'git+git://github.com/pypa/virtualenv' - result = run_pip('install', '-e', '%s#egg=virtualenv' % + result = script.pip('install', '-e', '%s#egg=virtualenv' % local_checkout(url), '--install-option=--script-dir=%s' % folder) - virtualenv_bin = env.venv/'src'/'virtualenv'/folder/'virtualenv'+env.exe + virtualenv_bin = script.venv/'src'/'virtualenv'/folder/'virtualenv'+script.exe assert virtualenv_bin in result.files_created @@ -340,9 +340,9 @@ def test_install_global_option_using_editable(): """ Test using global distutils options, but in an editable installation """ - reset_env() + script = reset_env() url = 'hg+http://bitbucket.org/runeh/anyjson' - result = run_pip('install', '--global-option=--version', + result = script.pip('install', '--global-option=--version', '-e', '%s@0.2.5#egg=anyjson' % local_checkout(url)) assert '0.2.5\n' in result.stdout @@ -352,10 +352,10 @@ def test_install_package_with_same_name_in_curdir(): """ Test installing a package with the same name of a local folder """ - env = reset_env() - mkdir('mock==0.6') - result = run_pip('install', 'mock==0.6') - egg_folder = env.site_packages / 'mock-0.6.0-py%s.egg-info' % pyversion + script = reset_env() + script.scratch_path.join("mock==0.6").mkdir() + result = script.pip('install', 'mock==0.6') + egg_folder = script.site_packages / 'mock-0.6.0-py%s.egg-info' % pyversion assert egg_folder in result.files_created, str(result) @@ -369,12 +369,12 @@ def test_install_folder_using_dot_slash(): """ Test installing a folder using pip install ./foldername """ - env = reset_env() - mkdir('mock') - pkg_path = env.scratch_path/'mock' - write_file('setup.py', mock100_setup_py, pkg_path) - result = run_pip('install', './mock') - egg_folder = env.site_packages / 'mock-100.1-py%s.egg-info' % pyversion + script = reset_env() + script.scratch_path.join("mock").mkdir() + pkg_path = script.scratch_path/'mock' + pkg_path.join("setup.py").write(mock100_setup_py) + result = script.pip('install', './mock') + egg_folder = script.site_packages / 'mock-100.1-py%s.egg-info' % pyversion assert egg_folder in result.files_created, str(result) @@ -382,12 +382,12 @@ def test_install_folder_using_slash_in_the_end(): r""" Test installing a folder using pip install foldername/ or foldername\ """ - env = reset_env() - mkdir('mock') - pkg_path = env.scratch_path/'mock' - write_file('setup.py', mock100_setup_py, pkg_path) - result = run_pip('install', 'mock' + os.path.sep) - egg_folder = env.site_packages / 'mock-100.1-py%s.egg-info' % pyversion + script = reset_env() + script.scratch_path.join("mock").mkdir() + pkg_path = script.scratch_path/'mock' + pkg_path.join("setup.py").write(mock100_setup_py) + result = script.pip('install', 'mock' + os.path.sep) + egg_folder = script.site_packages / 'mock-100.1-py%s.egg-info' % pyversion assert egg_folder in result.files_created, str(result) @@ -395,13 +395,13 @@ def test_install_folder_using_relative_path(): """ Test installing a folder using pip install folder1/folder2 """ - env = reset_env() - mkdir('initools') - mkdir(Path('initools')/'mock') - pkg_path = env.scratch_path/'initools'/'mock' - write_file('setup.py', mock100_setup_py, pkg_path) - result = run_pip('install', Path('initools')/'mock') - egg_folder = env.site_packages / 'mock-100.1-py%s.egg-info' % pyversion + script = reset_env() + script.scratch_path.join("initools").mkdir() + script.scratch_path.join("initools", "mock").mkdir() + pkg_path = script.scratch_path/'initools'/'mock' + pkg_path.join("setup.py").write(mock100_setup_py) + result = script.pip('install', Path('initools')/'mock') + egg_folder = script.site_packages / 'mock-100.1-py%s.egg-info' % pyversion assert egg_folder in result.files_created, str(result) @@ -409,10 +409,10 @@ def test_install_package_which_contains_dev_in_name(): """ Test installing package from pypi which contains 'dev' in name """ - env = reset_env() - result = run_pip('install', 'django-devserver==0.0.4') - devserver_folder = env.site_packages/'devserver' - egg_info_folder = env.site_packages/'django_devserver-0.0.4-py%s.egg-info' % pyversion + script = reset_env() + result = script.pip('install', 'django-devserver==0.0.4') + devserver_folder = script.site_packages/'devserver' + egg_info_folder = script.site_packages/'django_devserver-0.0.4-py%s.egg-info' % pyversion assert devserver_folder in result.files_created, str(result.stdout) assert egg_info_folder in result.files_created, str(result) @@ -421,9 +421,9 @@ def test_install_package_with_target(): """ Test installing a package using pip install --target """ - env = reset_env() - target_dir = env.scratch_path/'target' - result = run_pip('install', '-t', target_dir, "initools==0.1") + script = reset_env() + target_dir = script.scratch_path/'target' + result = script.pip('install', '-t', target_dir, "initools==0.1") assert Path('scratch')/'target'/'initools' in result.files_created, str(result) @@ -431,13 +431,13 @@ def test_install_package_with_root(): """ Test installing a package using pip install --root """ - env = reset_env() - root_dir = env.scratch_path/'root' - result = run_pip('install', '--root', root_dir, '-f', find_links, '--no-index', 'simple==1.0') - normal_install_path = env.root_path / env.site_packages / 'simple-1.0-py%s.egg-info' % pyversion + script = reset_env() + root_dir = script.scratch_path/'root' + result = script.pip('install', '--root', root_dir, '-f', find_links, '--no-index', 'simple==1.0') + normal_install_path = script.base_path / script.site_packages / 'simple-1.0-py%s.egg-info' % pyversion #use distutils to change the root exactly how the --root option does it from distutils.util import change_root - root_path = change_root(os.path.join(env.scratch, 'root'), normal_install_path) + root_path = change_root(os.path.join(script.scratch, 'root'), normal_install_path) assert root_path in result.files_created, str(result) @@ -458,9 +458,9 @@ def test_install_package_that_emits_unicode(): Refs https://github.com/pypa/pip/issues/326 """ - env = reset_env() + script = reset_env() to_install = os.path.abspath(os.path.join(tests_data, 'packages', 'BrokenEmitsUTF8')) - result = run_pip('install', to_install, expect_error=True, expect_temp=True, quiet=True) + result = script.pip('install', to_install, expect_error=True, expect_temp=True, quiet=True) assert 'FakeError: this package designed to fail on install' in result.stdout assert 'UnicodeDecodeError' not in result.stdout @@ -473,13 +473,12 @@ def test_url_req_case_mismatch(): tests/packages contains Upper-1.0.tar.gz and Upper-2.0.tar.gz 'requiresupper' has install_requires = ['upper'] """ - env = reset_env() + script = reset_env() Upper = os.path.join(find_links, 'Upper-1.0.tar.gz') - result = run_pip('install', '--no-index', '-f', find_links, Upper, 'requiresupper') + result = script.pip('install', '--no-index', '-f', find_links, Upper, 'requiresupper') #only Upper-1.0.tar.gz should get installed. - egg_folder = env.site_packages / 'Upper-1.0-py%s.egg-info' % pyversion + egg_folder = script.site_packages / 'Upper-1.0-py%s.egg-info' % pyversion assert egg_folder in result.files_created, str(result) - egg_folder = env.site_packages / 'Upper-2.0-py%s.egg-info' % pyversion + egg_folder = script.site_packages / 'Upper-2.0-py%s.egg-info' % pyversion assert egg_folder not in result.files_created, str(result) - diff --git a/tests/functional/test_install_bundle.py b/tests/functional/test_install_bundle.py index ea45f30ef..401a6e0f7 100644 --- a/tests/functional/test_install_bundle.py +++ b/tests/functional/test_install_bundle.py @@ -1,11 +1,13 @@ import os -from tests.lib import reset_env, pip_install_local, packages + +from tests.lib import reset_env, packages + def test_install_pybundle(): """ Test intalling a *.pybundle file """ - env = reset_env() - result = pip_install_local(os.path.join(packages, 'simplebundle.pybundle'), expect_temp=True) + script = reset_env() + result = script.pip_install_local(os.path.join(packages, 'simplebundle.pybundle'), expect_temp=True) result.assert_installed('simple', editable=False) result.assert_installed('simple2', editable=False) diff --git a/tests/functional/test_install_cleanup.py b/tests/functional/test_install_cleanup.py index 92ccf0547..9fe7f9c89 100644 --- a/tests/functional/test_install_cleanup.py +++ b/tests/functional/test_install_cleanup.py @@ -1,8 +1,7 @@ import os import textwrap from os.path import abspath, exists, join -from tests.lib import (tests_data, reset_env, run_pip, pip_install_local, - write_file, mkdir, path_to_url, find_links) +from tests.lib import tests_data, reset_env, find_links from tests.lib.local_repos import local_checkout from tests.lib.path import Path @@ -11,21 +10,21 @@ def test_cleanup_after_install(): """ Test clean up after installing a package. """ - env = reset_env() - run_pip('install', '--no-index', '--find-links=%s' % find_links, 'simple') - build = env.venv_path/"build" - src = env.venv_path/"src" + script = reset_env() + script.pip('install', '--no-index', '--find-links=%s' % find_links, 'simple') + build = script.venv_path/"build" + src = script.venv_path/"src" assert not exists(build), "build/ dir still exists: %s" % build assert not exists(src), "unexpected src/ dir exists: %s" % src - env.assert_no_temp() + script.assert_no_temp() def test_no_clean_option_blocks_cleaning_after_install(): """ Test --no-clean option blocks cleaning after install """ - env = reset_env() - result = run_pip('install', '--no-clean', '--no-index', '--find-links=%s' % find_links, 'simple') - build = env.venv_path/'build'/'simple' + script = reset_env() + result = script.pip('install', '--no-clean', '--no-index', '--find-links=%s' % find_links, 'simple') + build = script.venv_path/'build'/'simple' assert exists(build), "build/simple should still exist %s" % str(result) @@ -34,17 +33,17 @@ def test_cleanup_after_install_editable_from_hg(): Test clean up after cloning from Mercurial. """ - env = reset_env() - run_pip('install', + script = reset_env() + script.pip('install', '-e', '%s#egg=ScriptTest' % local_checkout('hg+https://bitbucket.org/ianb/scripttest'), expect_error=True) - build = env.venv_path/'build' - src = env.venv_path/'src' + build = script.venv_path/'build' + src = script.venv_path/'src' assert not exists(build), "build/ dir still exists: %s" % build assert exists(src), "expected src/ dir doesn't exist: %s" % src - env.assert_no_temp() + script.assert_no_temp() def test_cleanup_after_install_from_local_directory(): @@ -52,26 +51,26 @@ def test_cleanup_after_install_from_local_directory(): Test clean up after installing from a local directory. """ - env = reset_env() + script = reset_env() to_install = abspath(join(tests_data, 'packages', 'FSPkg')) - run_pip('install', to_install, expect_error=False) - build = env.venv_path/'build' - src = env.venv_path/'src' + script.pip('install', to_install, expect_error=False) + build = script.venv_path/'build' + src = script.venv_path/'src' assert not exists(build), "unexpected build/ dir exists: %s" % build assert not exists(src), "unexpected src/ dir exist: %s" % src - env.assert_no_temp() + script.assert_no_temp() def test_no_install_and_download_should_not_leave_build_dir(): """ It should remove build/ dir if it was pip that created """ - env = reset_env() - mkdir('downloaded_packages') - assert not os.path.exists(env.venv_path/'/build') - result = run_pip('install', '--no-install', 'INITools==0.2', '-d', 'downloaded_packages') + script = reset_env() + script.scratch_path.join("downloaded_packages").mkdir() + assert not os.path.exists(script.venv_path/'/build') + result = script.pip('install', '--no-install', 'INITools==0.2', '-d', 'downloaded_packages') assert Path('scratch')/'downloaded_packages/build' not in result.files_created, 'pip should not leave build/ dir' - assert not os.path.exists(env.venv_path/'/build'), "build/ dir should be deleted" + assert not os.path.exists(script.venv_path/'/build'), "build/ dir should be deleted" def test_cleanup_req_satisifed_no_name(): @@ -85,61 +84,58 @@ def test_cleanup_req_satisifed_no_name(): # 2) parent-0.1.tar.gz dist = abspath(join(tests_data, 'packages', 'parent-0.1.tar.gz')) - env = reset_env() - result = run_pip('install', dist) - result = run_pip('install', dist) - build = env.venv_path/'build' + script = reset_env() + result = script.pip('install', dist) + result = script.pip('install', dist) + build = script.venv_path/'build' assert not exists(build), "unexpected build/ dir exists: %s" % build - env.assert_no_temp() + script.assert_no_temp() def test_download_should_not_delete_existing_build_dir(): """ It should not delete build/ if existing before run the command """ - env = reset_env() - mkdir(env.venv_path/'build') - f = open(env.venv_path/'build'/'somefile.txt', 'w') - f.write('I am not empty!') - f.close() - run_pip('install', '--no-install', 'INITools==0.2', '-d', '.') - f = open(env.venv_path/'build'/'somefile.txt') - content = f.read() - f.close() - assert os.path.exists(env.venv_path/'build'), "build/ should be left if it exists before pip run" + script = reset_env() + script.venv_path.join("build").mkdir() + script.venv_path.join("build", "somefile.txt").write("I am not empty!") + script.pip('install', '--no-install', 'INITools==0.2', '-d', '.') + with open(script.venv_path/'build'/'somefile.txt') as fp: + content = fp.read() + assert os.path.exists(script.venv_path/'build'), "build/ should be left if it exists before pip run" assert content == 'I am not empty!', "it should not affect build/ and its content" - assert ['somefile.txt'] == os.listdir(env.venv_path/'build') + assert ['somefile.txt'] == os.listdir(script.venv_path/'build') def test_cleanup_after_install_exception(): """ Test clean up after a 'setup.py install' exception. """ - env = reset_env() + script = reset_env() #broken==0.2broken fails during install; see packages readme file - result = run_pip('install', '-f', find_links, '--no-index', 'broken==0.2broken', expect_error=True) - build = env.venv_path/'build' + result = script.pip('install', '-f', find_links, '--no-index', 'broken==0.2broken', expect_error=True) + build = script.venv_path/'build' assert not exists(build), "build/ dir still exists: %s" % result.stdout - env.assert_no_temp() + script.assert_no_temp() def test_cleanup_after_egg_info_exception(): """ Test clean up after a 'setup.py egg_info' exception. """ - env = reset_env() + script = reset_env() #brokenegginfo fails during egg_info; see packages readme file - result = run_pip('install', '-f', find_links, '--no-index', 'brokenegginfo==0.1', expect_error=True) - build = env.venv_path/'build' + result = script.pip('install', '-f', find_links, '--no-index', 'brokenegginfo==0.1', expect_error=True) + build = script.venv_path/'build' assert not exists(build), "build/ dir still exists: %s" % result.stdout - env.assert_no_temp() + script.assert_no_temp() def test_cleanup_prevented_upon_build_dir_exception(): """ Test no cleanup occurs after a PreviousBuildDirError """ - env = reset_env() - build = env.venv_path/'build'/'simple' + script = reset_env() + build = script.venv_path/'build'/'simple' os.makedirs(build) - write_file("setup.py", "#", dest=build) - result = run_pip('install', '-f', find_links, '--no-index', 'simple', expect_error=True) + build.join("setup.py").write("#") + result = script.pip('install', '-f', find_links, '--no-index', 'simple', expect_error=True) assert "pip can't proceed" in result.stdout, result.stdout assert exists(build) diff --git a/tests/functional/test_install_compat.py b/tests/functional/test_install_compat.py index 8ab413ac2..a3da8e3ce 100644 --- a/tests/functional/test_install_compat.py +++ b/tests/functional/test_install_compat.py @@ -3,8 +3,7 @@ Tests for compatibility workarounds. """ import os -from tests.lib import (tests_data, reset_env, run_pip, pyversion, - assert_all_changes) +from tests.lib import tests_data, reset_env, pyversion, assert_all_changes def test_debian_egg_name_workaround(): @@ -18,11 +17,11 @@ def test_debian_egg_name_workaround(): https://bitbucket.org/ianb/pip/issue/104/pip-uninstall-on-ubuntu-linux """ - env = reset_env() - result = run_pip('install', 'INITools==0.2', expect_error=True) + script = reset_env() + result = script.pip('install', 'INITools==0.2', expect_error=True) egg_info = os.path.join( - env.site_packages, "INITools-0.2-py%s.egg-info" % pyversion) + script.site_packages, "INITools-0.2-py%s.egg-info" % pyversion) # Debian only removes pyversion for global installs, not inside a venv # so even if this test runs on a Debian/Ubuntu system with broken setuptools, @@ -30,19 +29,19 @@ def test_debian_egg_name_workaround(): assert egg_info in result.files_created, "Couldn't find %s" % egg_info # The Debian no-pyversion version of the .egg-info - mangled = os.path.join(env.site_packages, "INITools-0.2.egg-info") + mangled = os.path.join(script.site_packages, "INITools-0.2.egg-info") assert mangled not in result.files_created, "Found unexpected %s" % mangled # Simulate a Debian install by copying the .egg-info to their name for it - full_egg_info = os.path.join(env.root_path, egg_info) + full_egg_info = os.path.join(script.base_path, egg_info) assert os.path.isdir(full_egg_info) - full_mangled = os.path.join(env.root_path, mangled) + full_mangled = os.path.join(script.base_path, mangled) os.renames(full_egg_info, full_mangled) assert os.path.isdir(full_mangled) # Try the uninstall and verify that everything is removed. - result2 = run_pip("uninstall", "INITools", "-y") - assert_all_changes(result, result2, [env.venv/'build', 'cache']) + result2 = script.pip("uninstall", "INITools", "-y") + assert_all_changes(result, result2, [script.venv/'build', 'cache']) def test_setup_py_with_dos_line_endings(): @@ -51,6 +50,6 @@ def test_setup_py_with_dos_line_endings(): Refs https://github.com/pypa/pip/issues/237 """ - reset_env() + script = reset_env() to_install = os.path.abspath(os.path.join(tests_data, 'packages', 'LineEndings')) - run_pip('install', to_install, expect_error=False) + script.pip('install', to_install, expect_error=False) diff --git a/tests/functional/test_install_config.py b/tests/functional/test_install_config.py index 769151319..791226467 100644 --- a/tests/functional/test_install_config.py +++ b/tests/functional/test_install_config.py @@ -1,8 +1,8 @@ import os import tempfile import textwrap -from tests.lib import (reset_env, run_pip, clear_environ, write_file, path_to_url, - find_links) + +from tests.lib import reset_env, clear_environ, path_to_url, find_links def test_options_from_env_vars(): @@ -12,8 +12,8 @@ def test_options_from_env_vars(): """ environ = clear_environ(os.environ.copy()) environ['PIP_NO_INDEX'] = '1' - reset_env(environ) - result = run_pip('install', '-vvv', 'INITools', expect_error=True) + script = reset_env(environ) + result = script.pip('install', '-vvv', 'INITools', expect_error=True) assert "Ignoring indexes:" in result.stdout, str(result) assert "DistributionNotFound: No distributions at all found for INITools" in result.stdout @@ -25,11 +25,11 @@ def test_command_line_options_override_env_vars(): """ environ = clear_environ(os.environ.copy()) environ['PIP_INDEX_URL'] = 'http://b.pypi.python.org/simple/' - reset_env(environ) - result = run_pip('install', '-vvv', 'INITools', expect_error=True) + script = reset_env(environ) + result = script.pip('install', '-vvv', 'INITools', expect_error=True) assert "Getting page http://b.pypi.python.org/simple/INITools" in result.stdout - reset_env(environ) - result = run_pip('install', '-vvv', '--index-url', 'http://download.zope.org/ppix', 'INITools', expect_error=True) + script = reset_env(environ) + result = script.pip('install', '-vvv', '--index-url', 'http://download.zope.org/ppix', 'INITools', expect_error=True) assert "b.pypi.python.org" not in result.stdout assert "Getting page http://download.zope.org/ppix" in result.stdout @@ -52,20 +52,20 @@ def test_env_vars_override_config_file(): def _test_env_vars_override_config_file(config_file): environ = clear_environ(os.environ.copy()) environ['PIP_CONFIG_FILE'] = config_file # set this to make pip load it - reset_env(environ) + script = reset_env(environ) # It's important that we test this particular config value ('no-index') # because their is/was a bug which only shows up in cases in which # 'config-item' and 'config_item' hash to the same value modulo the size # of the config dictionary. - write_file(config_file, textwrap.dedent("""\ + (script.scratch_path/config_file).write(textwrap.dedent("""\ [global] no-index = 1 """)) - result = run_pip('install', '-vvv', 'INITools', expect_error=True) + result = script.pip('install', '-vvv', 'INITools', expect_error=True) assert "DistributionNotFound: No distributions at all found for INITools" in result.stdout environ['PIP_NO_INDEX'] = '0' - reset_env(environ) - result = run_pip('install', '-vvv', 'INITools', expect_error=True) + script = reset_env(environ) + result = script.pip('install', '-vvv', 'INITools', expect_error=True) assert "Successfully installed INITools" in result.stdout @@ -76,11 +76,11 @@ def test_command_line_append_flags(): """ environ = clear_environ(os.environ.copy()) environ['PIP_FIND_LINKS'] = 'http://pypi.pinaxproject.com' - reset_env(environ) - result = run_pip('install', '-vvv', 'INITools', expect_error=True) + script = reset_env(environ) + result = script.pip('install', '-vvv', 'INITools', expect_error=True) assert "Analyzing links from page http://pypi.pinaxproject.com" in result.stdout - reset_env(environ) - result = run_pip('install', '-vvv', '--find-links', find_links, 'INITools', expect_error=True) + script = reset_env(environ) + result = script.pip('install', '-vvv', '--find-links', find_links, 'INITools', expect_error=True) assert "Analyzing links from page http://pypi.pinaxproject.com" in result.stdout assert "Skipping link %s" % find_links in result.stdout @@ -92,8 +92,8 @@ def test_command_line_appends_correctly(): """ environ = clear_environ(os.environ.copy()) environ['PIP_FIND_LINKS'] = 'http://pypi.pinaxproject.com %s' % find_links - reset_env(environ) - result = run_pip('install', '-vvv', 'INITools', expect_error=True) + script = reset_env(environ) + result = script.pip('install', '-vvv', 'INITools', expect_error=True) assert "Analyzing links from page http://pypi.pinaxproject.com" in result.stdout, result.stdout assert "Skipping link %s" % find_links in result.stdout @@ -118,23 +118,23 @@ def test_config_file_override_stack(): def _test_config_file_override_stack(config_file): environ = clear_environ(os.environ.copy()) environ['PIP_CONFIG_FILE'] = config_file # set this to make pip load it - reset_env(environ) - write_file(config_file, textwrap.dedent("""\ + script = reset_env(environ) + (script.scratch_path/config_file).write(textwrap.dedent("""\ [global] index-url = http://download.zope.org/ppix """)) - result = run_pip('install', '-vvv', 'INITools', expect_error=True) + result = script.pip('install', '-vvv', 'INITools', expect_error=True) assert "Getting page http://download.zope.org/ppix/INITools" in result.stdout - reset_env(environ) - write_file(config_file, textwrap.dedent("""\ + script = reset_env(environ) + (script.scratch_path/config_file).write(textwrap.dedent("""\ [global] index-url = http://download.zope.org/ppix [install] index-url = http://pypi.appspot.com/ """)) - result = run_pip('install', '-vvv', 'INITools', expect_error=True) + result = script.pip('install', '-vvv', 'INITools', expect_error=True) assert "Getting page http://pypi.appspot.com/INITools" in result.stdout - result = run_pip('install', '-vvv', '--index-url', 'http://pypi.python.org/simple', 'INITools', expect_error=True) + result = script.pip('install', '-vvv', '--index-url', 'http://pypi.python.org/simple', 'INITools', expect_error=True) assert "Getting page http://download.zope.org/ppix/INITools" not in result.stdout assert "Getting page http://pypi.appspot.com/INITools" not in result.stdout assert "Getting page http://pypi.python.org/simple/INITools" in result.stdout diff --git a/tests/functional/test_install_download.py b/tests/functional/test_install_download.py index 0861a7283..dc0614dfd 100644 --- a/tests/functional/test_install_download.py +++ b/tests/functional/test_install_download.py @@ -1,6 +1,6 @@ import textwrap -from tests.lib import reset_env, run_pip, write_file +from tests.lib import reset_env from tests.lib.path import Path @@ -9,10 +9,10 @@ def test_download_if_requested(): It should download (in the scratch path) and not install if requested. """ - env = reset_env() - result = run_pip('install', 'INITools==0.1', '-d', '.', expect_error=True) + script = reset_env() + result = script.pip('install', 'INITools==0.1', '-d', '.', expect_error=True) assert Path('scratch')/ 'INITools-0.1.tar.gz' in result.files_created - assert env.site_packages/ 'initools' not in result.files_created + assert script.site_packages/ 'initools' not in result.files_created def test_download_wheel(): @@ -22,12 +22,12 @@ def test_download_wheel(): --find-links has a bug https://github.com/pypa/pip/issues/1111 """ - env = reset_env() - result = run_pip('install', '--use-wheel', + script = reset_env() + result = script.pip('install', '--use-wheel', '-f', 'https://bitbucket.org/pypa/pip-test-package/downloads', '-d', '.', 'pip-test-package') assert Path('scratch')/ 'pip_test_package-0.1.1-py2.py3-none-any.whl' in result.files_created - assert env.site_packages/ 'piptestpackage' not in result.files_created + assert script.site_packages/ 'piptestpackage' not in result.files_created def test_single_download_from_requirements_file(): @@ -35,13 +35,13 @@ def test_single_download_from_requirements_file(): It should support download (in the scratch path) from PyPi from a requirements file """ - env = reset_env() - write_file('test-req.txt', textwrap.dedent(""" + script = reset_env() + script.scratch_path.join("test-req.txt").write(textwrap.dedent(""" INITools==0.1 """)) - result = run_pip('install', '-r', env.scratch_path/ 'test-req.txt', '-d', '.', expect_error=True) + result = script.pip('install', '-r', script.scratch_path/ 'test-req.txt', '-d', '.', expect_error=True) assert Path('scratch')/ 'INITools-0.1.tar.gz' in result.files_created - assert env.site_packages/ 'initools' not in result.files_created + assert script.site_packages/ 'initools' not in result.files_created def test_download_should_download_dependencies(): @@ -49,38 +49,38 @@ def test_download_should_download_dependencies(): It should download dependencies (in the scratch path) """ - env = reset_env() - result = run_pip('install', 'Paste[openid]==1.7.5.1', '-d', '.', expect_error=True) + script = reset_env() + result = script.pip('install', 'Paste[openid]==1.7.5.1', '-d', '.', expect_error=True) assert Path('scratch')/ 'Paste-1.7.5.1.tar.gz' in result.files_created openid_tarball_prefix = str(Path('scratch')/ 'python-openid-') assert any(path.startswith(openid_tarball_prefix) for path in result.files_created) - assert env.site_packages/ 'openid' not in result.files_created + assert script.site_packages/ 'openid' not in result.files_created def test_download_should_skip_existing_files(): """ It should not download files already existing in the scratch dir """ - env = reset_env() + script = reset_env() - write_file('test-req.txt', textwrap.dedent(""" + script.scratch_path.join("test-req.txt").write(textwrap.dedent(""" INITools==0.1 """)) - result = run_pip('install', '-r', env.scratch_path/ 'test-req.txt', '-d', '.', expect_error=True) + result = script.pip('install', '-r', script.scratch_path/ 'test-req.txt', '-d', '.', expect_error=True) assert Path('scratch')/ 'INITools-0.1.tar.gz' in result.files_created - assert env.site_packages/ 'initools' not in result.files_created + assert script.site_packages/ 'initools' not in result.files_created # adding second package to test-req.txt - write_file('test-req.txt', textwrap.dedent(""" + script.scratch_path.join("test-req.txt").write(textwrap.dedent(""" INITools==0.1 python-openid==2.2.5 """)) # only the second package should be downloaded - result = run_pip('install', '-r', env.scratch_path/ 'test-req.txt', '-d', '.', expect_error=True) + result = script.pip('install', '-r', script.scratch_path/ 'test-req.txt', '-d', '.', expect_error=True) openid_tarball_prefix = str(Path('scratch')/ 'python-openid-') assert any(path.startswith(openid_tarball_prefix) for path in result.files_created) assert Path('scratch')/ 'INITools-0.1.tar.gz' not in result.files_created - assert env.site_packages/ 'initools' not in result.files_created - assert env.site_packages/ 'openid' not in result.files_created + assert script.site_packages/ 'initools' not in result.files_created + assert script.site_packages/ 'openid' not in result.files_created diff --git a/tests/functional/test_install_extras.py b/tests/functional/test_install_extras.py index 60221567c..0af06e77c 100644 --- a/tests/functional/test_install_extras.py +++ b/tests/functional/test_install_extras.py @@ -1,15 +1,15 @@ from os.path import join -from tests.lib import reset_env, run_pip +from tests.lib import reset_env def test_simple_extras_install_from_pypi(): """ Test installing a package from PyPI using extras dependency Paste[openid]. """ - e = reset_env() - result = run_pip('install', 'Paste[openid]==1.7.5.1', expect_stderr=True) - initools_folder = e.site_packages / 'openid' + script = reset_env() + result = script.pip('install', 'Paste[openid]==1.7.5.1', expect_stderr=True) + initools_folder = script.site_packages / 'openid' assert initools_folder in result.files_created, result.files_created @@ -17,11 +17,11 @@ def test_no_extras_uninstall(): """ No extras dependency gets uninstalled when the root package is uninstalled """ - env = reset_env() - result = run_pip('install', 'Paste[openid]==1.7.5.1', expect_stderr=True) - assert join(env.site_packages, 'paste') in result.files_created, sorted(result.files_created.keys()) - assert join(env.site_packages, 'openid') in result.files_created, sorted(result.files_created.keys()) - result2 = run_pip('uninstall', 'Paste', '-y') + script = reset_env() + result = script.pip('install', 'Paste[openid]==1.7.5.1', expect_stderr=True) + assert join(script.site_packages, 'paste') in result.files_created, sorted(result.files_created.keys()) + assert join(script.site_packages, 'openid') in result.files_created, sorted(result.files_created.keys()) + result2 = script.pip('uninstall', 'Paste', '-y') # openid should not be uninstalled - initools_folder = env.site_packages / 'openid' + initools_folder = script.site_packages / 'openid' assert not initools_folder in result2.files_deleted, result.files_deleted diff --git a/tests/functional/test_install_index.py b/tests/functional/test_install_index.py index bde28a75d..7690f645b 100644 --- a/tests/functional/test_install_index.py +++ b/tests/functional/test_install_index.py @@ -2,41 +2,41 @@ import os import textwrap from pip.backwardcompat import urllib -from tests.lib import (reset_env, run_pip, pyversion, tests_data, write_file, - path_to_url) + +from tests.lib import reset_env, pyversion, tests_data, path_to_url def test_find_links_relative_path(): """Test find-links as a relative path.""" - e = reset_env() - result = run_pip( + script = reset_env() + result = script.pip( 'install', 'parent==0.1', '--no-index', '--find-links', 'packages/', cwd=tests_data) - egg_info_folder = e.site_packages / 'parent-0.1-py%s.egg-info' % pyversion - initools_folder = e.site_packages / 'parent' + egg_info_folder = script.site_packages / 'parent-0.1-py%s.egg-info' % pyversion + initools_folder = script.site_packages / 'parent' assert egg_info_folder in result.files_created, str(result) assert initools_folder in result.files_created, str(result) def test_find_links_requirements_file_relative_path(): """Test find-links as a relative path to a reqs file.""" - e = reset_env() - write_file('test-req.txt', textwrap.dedent(""" + script = reset_env() + script.scratch_path.join("test-req.txt").write(textwrap.dedent(""" --no-index --find-links=../../../data/packages/ parent==0.1 """)) - result = run_pip( + result = script.pip( 'install', '-r', - e.scratch_path / "test-req.txt", + script.scratch_path / "test-req.txt", cwd=tests_data) - egg_info_folder = e.site_packages / 'parent-0.1-py%s.egg-info' % pyversion - initools_folder = e.site_packages / 'parent' + egg_info_folder = script.site_packages / 'parent-0.1-py%s.egg-info' % pyversion + initools_folder = script.site_packages / 'parent' assert egg_info_folder in result.files_created, str(result) assert initools_folder in result.files_created, str(result) @@ -45,10 +45,10 @@ def test_install_from_file_index_hash_link(): """ Test that a pkg can be installed from a file:// index using a link with a hash """ - env = reset_env() + script = reset_env() index_url = path_to_url(os.path.join(tests_data, 'indexes', 'simple')) - result = run_pip('install', '-i', index_url, 'simple==1.0') - egg_info_folder = env.site_packages / 'simple-1.0-py%s.egg-info' % pyversion + result = script.pip('install', '-i', index_url, 'simple==1.0') + egg_info_folder = script.site_packages / 'simple-1.0-py%s.egg-info' % pyversion assert egg_info_folder in result.files_created, str(result) @@ -57,7 +57,7 @@ def test_file_index_url_quoting(): Test url quoting of file index url with a space """ index_url = path_to_url(os.path.join(tests_data, 'indexes', urllib.quote('in dex'))) - env = reset_env() - result = run_pip('install', '-vvv', '--index-url', index_url, 'simple', expect_error=False) - assert (env.site_packages/'simple') in result.files_created, str(result.stdout) - assert (env.site_packages/'simple-1.0-py%s.egg-info' % pyversion) in result.files_created, str(result) + script = reset_env() + result = script.pip('install', '-vvv', '--index-url', index_url, 'simple', expect_error=False) + assert (script.site_packages/'simple') in result.files_created, str(result.stdout) + assert (script.site_packages/'simple-1.0-py%s.egg-info' % pyversion) in result.files_created, str(result) diff --git a/tests/functional/test_install_reqs.py b/tests/functional/test_install_reqs.py index 5b3071da6..b3fca0a07 100644 --- a/tests/functional/test_install_reqs.py +++ b/tests/functional/test_install_reqs.py @@ -7,8 +7,8 @@ from mock import patch from pip.backwardcompat import urllib from pip.req import Requirements, parse_editable, parse_requirements -from tests.lib import (reset_env, run_pip, write_file, pyversion, tests_data, - path_to_url, find_links) + +from tests.lib import reset_env, pyversion, tests_data, path_to_url, find_links from tests.lib.local_repos import local_checkout from tests.lib.path import Path @@ -19,18 +19,18 @@ def test_requirements_file(): """ other_lib_name, other_lib_version = 'anyjson', '0.3' - env = reset_env() - write_file('initools-req.txt', textwrap.dedent("""\ + script = reset_env() + script.scratch_path.join("initools-req.txt").write(textwrap.dedent("""\ INITools==0.2 # and something else to test out: %s<=%s """ % (other_lib_name, other_lib_version))) - result = run_pip('install', '-r', env.scratch_path / 'initools-req.txt') - assert env.site_packages/'INITools-0.2-py%s.egg-info' % pyversion in result.files_created - assert env.site_packages/'initools' in result.files_created - assert result.files_created[env.site_packages/other_lib_name].dir + result = script.pip('install', '-r', script.scratch_path / 'initools-req.txt') + assert script.site_packages/'INITools-0.2-py%s.egg-info' % pyversion in result.files_created + assert script.site_packages/'initools' in result.files_created + assert result.files_created[script.site_packages/other_lib_name].dir fn = '%s-%s-py%s.egg-info' % (other_lib_name, other_lib_version, pyversion) - assert result.files_created[env.site_packages/fn].dir + assert result.files_created[script.site_packages/fn].dir def test_schema_check_in_requirements_file(): @@ -38,13 +38,13 @@ def test_schema_check_in_requirements_file(): Test installing from a requirements file with an invalid vcs schema.. """ - env = reset_env() - write_file('file-egg-req.txt', textwrap.dedent("""\ + script = reset_env() + script.scratch_path.join("file-egg-req.txt").write(textwrap.dedent("""\ git://github.com/alex/django-fixture-generator.git#egg=fixture_generator """)) with pytest.raises(AssertionError): - run_pip("install", "-vvv", "-r", env.scratch_path / "file-egg-req.txt") + script.pip("install", "-vvv", "-r", script.scratch_path / "file-egg-req.txt") def test_relative_requirements_file(): @@ -53,13 +53,13 @@ def test_relative_requirements_file(): """ url = path_to_url(os.path.join(tests_data, 'packages', '..', 'packages', 'FSPkg')) + '#egg=FSPkg' - env = reset_env() - write_file('file-egg-req.txt', textwrap.dedent("""\ + script = reset_env() + script.scratch_path.join("file-egg-req.txt").write(textwrap.dedent("""\ %s """ % url)) - result = run_pip('install', '-vvv', '-r', env.scratch_path / 'file-egg-req.txt') - assert (env.site_packages/'FSPkg-0.1dev-py%s.egg-info' % pyversion) in result.files_created, str(result) - assert (env.site_packages/'fspkg') in result.files_created, str(result.stdout) + result = script.pip('install', '-vvv', '-r', script.scratch_path / 'file-egg-req.txt') + assert (script.site_packages/'FSPkg-0.1dev-py%s.egg-info' % pyversion) in result.files_created, str(result) + assert (script.site_packages/'fspkg') in result.files_created, str(result.stdout) def test_multiple_requirements_files(): @@ -68,30 +68,30 @@ def test_multiple_requirements_files(): """ other_lib_name, other_lib_version = 'anyjson', '0.3' - env = reset_env() - write_file('initools-req.txt', textwrap.dedent("""\ + script = reset_env() + script.scratch_path.join("initools-req.txt").write(textwrap.dedent("""\ -e %s@10#egg=INITools-dev -r %s-req.txt""" % (local_checkout('svn+http://svn.colorstudy.com/INITools/trunk'), other_lib_name))) - write_file('%s-req.txt' % other_lib_name, textwrap.dedent("""\ + script.scratch_path.join("%s-req.txt" % other_lib_name).write(textwrap.dedent("""\ %s<=%s """ % (other_lib_name, other_lib_version))) - result = run_pip('install', '-r', env.scratch_path / 'initools-req.txt') - assert result.files_created[env.site_packages/other_lib_name].dir + result = script.pip('install', '-r', script.scratch_path / 'initools-req.txt') + assert result.files_created[script.site_packages/other_lib_name].dir fn = '%s-%s-py%s.egg-info' % (other_lib_name, other_lib_version, pyversion) - assert result.files_created[env.site_packages/fn].dir - assert env.venv/'src'/'initools' in result.files_created + assert result.files_created[script.site_packages/fn].dir + assert script.venv/'src'/'initools' in result.files_created def test_respect_order_in_requirements_file(): - env = reset_env() - write_file('frameworks-req.txt', textwrap.dedent("""\ + script = reset_env() + script.scratch_path.join("frameworks-req.txt").write(textwrap.dedent("""\ parent child simple """)) - result = run_pip('install', '--no-index', '-f', find_links, '-r', env.scratch_path / 'frameworks-req.txt') + result = script.pip('install', '--no-index', '-f', find_links, '-r', script.scratch_path / 'frameworks-req.txt') downloaded = [line for line in result.stdout.split('\n') if 'Downloading/unpacking' in line] @@ -106,13 +106,9 @@ def test_respect_order_in_requirements_file(): def test_install_local_editable_with_extras(): - env = reset_env() + script = reset_env() to_install = os.path.abspath(os.path.join(tests_data, 'packages', 'LocalExtras')) - res = run_pip('install', '-e', to_install + '[bar]', expect_error=False) - assert env.site_packages/'easy-install.pth' in res.files_updated, str(result) - assert env.site_packages/'LocalExtras.egg-link' in res.files_created, str(result) - assert env.site_packages/'simple' in res.files_created, str(result) - - - - + res = script.pip('install', '-e', to_install + '[bar]', expect_error=False) + assert script.site_packages/'easy-install.pth' in res.files_updated, str(result) + assert script.site_packages/'LocalExtras.egg-link' in res.files_created, str(result) + assert script.site_packages/'simple' in res.files_created, str(result) diff --git a/tests/functional/test_install_upgrade.py b/tests/functional/test_install_upgrade.py index 00a04d4ad..bf7c92eaf 100644 --- a/tests/functional/test_install_upgrade.py +++ b/tests/functional/test_install_upgrade.py @@ -6,9 +6,9 @@ from os.path import join import pytest -from tests.lib import (reset_env, run_pip, assert_all_changes, src_folder, - write_file, pyversion, _create_test_package, pip_install_local, - _change_test_package_version, path_to_url, find_links) +from tests.lib import (reset_env, assert_all_changes, src_folder, pyversion, + _create_test_package, _change_test_package_version, + find_links) from tests.lib.local_repos import local_checkout @@ -17,9 +17,9 @@ def test_no_upgrade_unless_requested(): No upgrade if not specifically requested. """ - reset_env() - run_pip('install', 'INITools==0.1', expect_error=True) - result = run_pip('install', 'INITools', expect_error=True) + script = reset_env() + script.pip('install', 'INITools==0.1', expect_error=True) + result = script.pip('install', 'INITools', expect_error=True) assert not result.files_created, 'pip install INITools upgraded when it should not have' @@ -28,12 +28,12 @@ def test_upgrade_to_specific_version(): It does upgrade to specific version requested. """ - env = reset_env() - run_pip('install', 'INITools==0.1', expect_error=True) - result = run_pip('install', 'INITools==0.2', expect_error=True) + script = reset_env() + script.pip('install', 'INITools==0.1', expect_error=True) + result = script.pip('install', 'INITools==0.2', expect_error=True) assert result.files_created, 'pip install with specific version did not upgrade' - assert env.site_packages/'INITools-0.1-py%s.egg-info' % pyversion in result.files_deleted - assert env.site_packages/'INITools-0.2-py%s.egg-info' % pyversion in result.files_created + assert script.site_packages/'INITools-0.1-py%s.egg-info' % pyversion in result.files_deleted + assert script.site_packages/'INITools-0.2-py%s.egg-info' % pyversion in result.files_created def test_upgrade_if_requested(): @@ -41,11 +41,11 @@ def test_upgrade_if_requested(): And it does upgrade if requested. """ - env = reset_env() - run_pip('install', 'INITools==0.1', expect_error=True) - result = run_pip('install', '--upgrade', 'INITools', expect_error=True) + script = reset_env() + script.pip('install', 'INITools==0.1', expect_error=True) + result = script.pip('install', '--upgrade', 'INITools', expect_error=True) assert result.files_created, 'pip install --upgrade did not upgrade' - assert env.site_packages/'INITools-0.1-py%s.egg-info' % pyversion not in result.files_created + assert script.site_packages/'INITools-0.1-py%s.egg-info' % pyversion not in result.files_created def test_upgrade_with_newest_already_installed(): @@ -54,9 +54,9 @@ def test_upgrade_with_newest_already_installed(): not be reinstalled and the user should be informed. """ - env = reset_env() - run_pip('install', '-f', find_links, '--no-index', 'simple') - result = run_pip('install', '--upgrade', '-f', find_links, '--no-index', 'simple') + script = reset_env() + script.pip('install', '-f', find_links, '--no-index', 'simple') + result = script.pip('install', '--upgrade', '-f', find_links, '--no-index', 'simple') assert not result.files_created, 'simple upgraded when it should not have' assert 'already up-to-date' in result.stdout, result.stdout @@ -67,13 +67,13 @@ def test_upgrade_force_reinstall_newest(): version if --force-reinstall is supplied. """ - env = reset_env() - result = run_pip('install', 'INITools') - assert env.site_packages/ 'initools' in result.files_created, sorted(result.files_created.keys()) - result2 = run_pip('install', '--upgrade', '--force-reinstall', 'INITools') + script = reset_env() + result = script.pip('install', 'INITools') + assert script.site_packages/ 'initools' in result.files_created, sorted(result.files_created.keys()) + result2 = script.pip('install', '--upgrade', '--force-reinstall', 'INITools') assert result2.files_updated, 'upgrade to INITools 0.3 failed' - result3 = run_pip('uninstall', 'initools', '-y', expect_error=True) - assert_all_changes(result, result3, [env.venv/'build', 'cache']) + result3 = script.pip('uninstall', 'initools', '-y', expect_error=True) + assert_all_changes(result, result3, [script.venv/'build', 'cache']) def test_uninstall_before_upgrade(): @@ -81,13 +81,13 @@ def test_uninstall_before_upgrade(): Automatic uninstall-before-upgrade. """ - env = reset_env() - result = run_pip('install', 'INITools==0.2', expect_error=True) - assert env.site_packages/ 'initools' in result.files_created, sorted(result.files_created.keys()) - result2 = run_pip('install', 'INITools==0.3', expect_error=True) + script = reset_env() + result = script.pip('install', 'INITools==0.2', expect_error=True) + assert script.site_packages/ 'initools' in result.files_created, sorted(result.files_created.keys()) + result2 = script.pip('install', 'INITools==0.3', expect_error=True) assert result2.files_created, 'upgrade to INITools 0.3 failed' - result3 = run_pip('uninstall', 'initools', '-y', expect_error=True) - assert_all_changes(result, result3, [env.venv/'build', 'cache']) + result3 = script.pip('uninstall', 'initools', '-y', expect_error=True) + assert_all_changes(result, result3, [script.venv/'build', 'cache']) def test_uninstall_before_upgrade_from_url(): @@ -95,13 +95,13 @@ def test_uninstall_before_upgrade_from_url(): Automatic uninstall-before-upgrade from URL. """ - env = reset_env() - result = run_pip('install', 'INITools==0.2', expect_error=True) - assert env.site_packages/ 'initools' in result.files_created, sorted(result.files_created.keys()) - result2 = run_pip('install', 'http://pypi.python.org/packages/source/I/INITools/INITools-0.3.tar.gz', expect_error=True) + script = reset_env() + result = script.pip('install', 'INITools==0.2', expect_error=True) + assert script.site_packages/ 'initools' in result.files_created, sorted(result.files_created.keys()) + result2 = script.pip('install', 'http://pypi.python.org/packages/source/I/INITools/INITools-0.3.tar.gz', expect_error=True) assert result2.files_created, 'upgrade to INITools 0.3 failed' - result3 = run_pip('uninstall', 'initools', '-y', expect_error=True) - assert_all_changes(result, result3, [env.venv/'build', 'cache']) + result3 = script.pip('uninstall', 'initools', '-y', expect_error=True) + assert_all_changes(result, result3, [script.venv/'build', 'cache']) def test_upgrade_to_same_version_from_url(): @@ -110,13 +110,13 @@ def test_upgrade_to_same_version_from_url(): need to uninstall and reinstall if --upgrade is not specified. """ - env = reset_env() - result = run_pip('install', 'INITools==0.3', expect_error=True) - assert env.site_packages/ 'initools' in result.files_created, sorted(result.files_created.keys()) - result2 = run_pip('install', 'http://pypi.python.org/packages/source/I/INITools/INITools-0.3.tar.gz', expect_error=True) + script = reset_env() + result = script.pip('install', 'INITools==0.3', expect_error=True) + assert script.site_packages/ 'initools' in result.files_created, sorted(result.files_created.keys()) + result2 = script.pip('install', 'http://pypi.python.org/packages/source/I/INITools/INITools-0.3.tar.gz', expect_error=True) assert not result2.files_updated, 'INITools 0.3 reinstalled same version' - result3 = run_pip('uninstall', 'initools', '-y', expect_error=True) - assert_all_changes(result, result3, [env.venv/'build', 'cache']) + result3 = script.pip('uninstall', 'initools', '-y', expect_error=True) + assert_all_changes(result, result3, [script.venv/'build', 'cache']) def test_upgrade_from_reqs_file(): @@ -124,21 +124,21 @@ def test_upgrade_from_reqs_file(): Upgrade from a requirements file. """ - env = reset_env() - write_file('test-req.txt', textwrap.dedent("""\ + script = reset_env() + script.scratch_path.join("test-req.txt").write(textwrap.dedent("""\ PyLogo<0.4 # and something else to test out: INITools==0.3 """)) - install_result = run_pip('install', '-r', env.scratch_path/ 'test-req.txt') - write_file('test-req.txt', textwrap.dedent("""\ + install_result = script.pip('install', '-r', script.scratch_path/ 'test-req.txt') + script.scratch_path.join("test-req.txt").write(textwrap.dedent("""\ PyLogo # and something else to test out: INITools """)) - run_pip('install', '--upgrade', '-r', env.scratch_path/ 'test-req.txt') - uninstall_result = run_pip('uninstall', '-r', env.scratch_path/ 'test-req.txt', '-y') - assert_all_changes(install_result, uninstall_result, [env.venv/'build', 'cache', env.scratch/'test-req.txt']) + script.pip('install', '--upgrade', '-r', script.scratch_path/ 'test-req.txt') + uninstall_result = script.pip('uninstall', '-r', script.scratch_path/ 'test-req.txt', '-y') + assert_all_changes(install_result, uninstall_result, [script.venv/'build', 'cache', script.scratch/'test-req.txt']) def test_uninstall_rollback(): @@ -147,13 +147,13 @@ def test_uninstall_rollback(): crafted to fail on install). """ - env = reset_env() - result = run_pip('install', '-f', find_links, '--no-index', 'broken==0.1') - assert env.site_packages / 'broken.py' in result.files_created, list(result.files_created.keys()) - result2 = run_pip('install', '-f', find_links, '--no-index', 'broken==0.2broken', expect_error=True) + script = reset_env() + result = script.pip('install', '-f', find_links, '--no-index', 'broken==0.1') + assert script.site_packages / 'broken.py' in result.files_created, list(result.files_created.keys()) + result2 = script.pip('install', '-f', find_links, '--no-index', 'broken==0.2broken', expect_error=True) assert result2.returncode == 1, str(result2) - assert env.run('python', '-c', "import broken; print(broken.VERSION)").stdout == '0.1\n' - assert_all_changes(result.files_after, result2, [env.venv/'build', 'pip-log.txt']) + assert script.run('python', '-c', "import broken; print(broken.VERSION)").stdout == '0.1\n' + assert_all_changes(result.files_after, result2, [script.venv/'build', 'pip-log.txt']) # Issue #530 - temporarily disable flaky test @pytest.mark.skipif @@ -162,14 +162,14 @@ def test_editable_git_upgrade(): Test installing an editable git package from a repository, upgrading the repository, installing again, and check it gets the newer version """ - env = reset_env() - version_pkg_path = _create_test_package(env) - run_pip('install', '-e', '%s#egg=version_pkg' % ('git+file://' + version_pkg_path)) - version = env.run('version_pkg') + script = reset_env() + version_pkg_path = _create_test_package(script) + script.pip('install', '-e', '%s#egg=version_pkg' % ('git+file://' + version_pkg_path)) + version = script.run('version_pkg') assert '0.1' in version.stdout - _change_test_package_version(env, version_pkg_path) - run_pip('install', '-e', '%s#egg=version_pkg' % ('git+file://' + version_pkg_path)) - version2 = env.run('version_pkg') + _change_test_package_version(script, version_pkg_path) + script.pip('install', '-e', '%s#egg=version_pkg' % ('git+file://' + version_pkg_path)) + version2 = script.run('version_pkg') assert 'some different version' in version2.stdout, "Output: %s" % (version2.stdout) @@ -178,12 +178,12 @@ def test_should_not_install_always_from_cache(): If there is an old cached package, pip should download the newer version Related to issue #175 """ - env = reset_env() - run_pip('install', 'INITools==0.2', expect_error=True) - run_pip('uninstall', '-y', 'INITools') - result = run_pip('install', 'INITools==0.1', expect_error=True) - assert env.site_packages/'INITools-0.2-py%s.egg-info' % pyversion not in result.files_created - assert env.site_packages/'INITools-0.1-py%s.egg-info' % pyversion in result.files_created + script = reset_env() + script.pip('install', 'INITools==0.2', expect_error=True) + script.pip('uninstall', '-y', 'INITools') + result = script.pip('install', 'INITools==0.1', expect_error=True) + assert script.site_packages/'INITools-0.2-py%s.egg-info' % pyversion not in result.files_created + assert script.site_packages/'INITools-0.1-py%s.egg-info' % pyversion in result.files_created def test_install_with_ignoreinstalled_requested(): @@ -191,30 +191,30 @@ def test_install_with_ignoreinstalled_requested(): It installs package if ignore installed is set. """ - env = reset_env() - run_pip('install', 'INITools==0.1', expect_error=True) - result = run_pip('install', '-I', 'INITools', expect_error=True) + script = reset_env() + script.pip('install', 'INITools==0.1', expect_error=True) + result = script.pip('install', '-I', 'INITools', expect_error=True) assert result.files_created, 'pip install -I did not install' - assert env.site_packages/'INITools-0.1-py%s.egg-info' % pyversion not in result.files_created + assert script.site_packages/'INITools-0.1-py%s.egg-info' % pyversion not in result.files_created def test_upgrade_vcs_req_with_no_dists_found(): """It can upgrade a VCS requirement that has no distributions otherwise.""" - reset_env() + script = reset_env() req = "%s#egg=pip-test-package" % local_checkout( "git+http://github.com/pypa/pip-test-package.git") - run_pip("install", req) - result = run_pip("install", "-U", req) + script.pip("install", req) + result = script.pip("install", "-U", req) assert not result.returncode def test_upgrade_vcs_req_with_dist_found(): """It can upgrade a VCS requirement that has distributions on the index.""" - reset_env() + script = reset_env() # TODO(pnasrat) Using local_checkout fails on windows - oddness with the test path urls/git. req = "%s#egg=virtualenv" % "git+git://github.com/pypa/virtualenv@c21fef2c2d53cf19f49bcc37f9c058a33fb50499" - run_pip("install", req) - result = run_pip("install", "-U", req) + script.pip("install", req) + result = script.pip("install", "-U", req) assert not "pypi.python.org" in result.stdout, result.stdout @@ -227,56 +227,56 @@ class TestUpgradeSetuptools(object): """ def prep_ve(self, version, distribute=False): - self.env = reset_env(pypi_cache=False) - pip_install_local('virtualenv==%s' %version) - args = ['virtualenv', self.env.scratch_path/'VE'] + self.script = reset_env() + self.script.pip_install_local('virtualenv==%s' %version) + args = ['virtualenv', self.script.scratch_path/'VE'] if distribute: args.insert(1, '--distribute') - self.env.run(*args) - self.ve_bin = self.env.scratch_path/'VE'/'bin' - self.env.run(self.ve_bin/'pip', 'uninstall', '-y', 'pip') - self.env.run(self.ve_bin/'python', 'setup.py', 'install', cwd=src_folder, expect_stderr=True) + self.script.run(*args) + self.ve_bin = self.script.scratch_path/'VE'/'bin' + self.script.run(self.ve_bin/'pip', 'uninstall', '-y', 'pip') + self.script.run(self.ve_bin/'python', 'setup.py', 'install', cwd=src_folder, expect_stderr=True) @pytest.mark.skipif("sys.version_info >= (3,0)") def test_py2_from_setuptools_6_to_setuptools_7(self): self.prep_ve('1.9.1') - result = self.env.run(self.ve_bin/'pip', 'install', '--no-index', '--find-links=%s' % find_links, '-U', 'setuptools') + result = self.script.run(self.ve_bin/'pip', 'install', '--no-index', '--find-links=%s' % find_links, '-U', 'setuptools') assert "Found existing installation: setuptools 0.6c11" in result.stdout - result = self.env.run(self.ve_bin/'pip', 'list') + result = self.script.run(self.ve_bin/'pip', 'list') "setuptools (0.9.8)" in result.stdout def test_py2_py3_from_distribute_6_to_setuptools_7(self): self.prep_ve('1.9.1', distribute=True) - result = self.env.run(self.ve_bin/'pip', 'install', '--no-index', '--find-links=%s' % find_links, '-U', 'setuptools') + result = self.script.run(self.ve_bin/'pip', 'install', '--no-index', '--find-links=%s' % find_links, '-U', 'setuptools') assert "Found existing installation: distribute 0.6.34" in result.stdout - result = self.env.run(self.ve_bin/'pip', 'list') + result = self.script.run(self.ve_bin/'pip', 'list') "setuptools (0.9.8)" in result.stdout "distribute (0.7.3)" in result.stdout def test_from_setuptools_7_to_setuptools_7(self): self.prep_ve('1.10') - result = self.env.run(self.ve_bin/'pip', 'install', '--no-index', '--find-links=%s' % find_links, '-U', 'setuptools') + result = self.script.run(self.ve_bin/'pip', 'install', '--no-index', '--find-links=%s' % find_links, '-U', 'setuptools') assert "Found existing installation: setuptools 0.9.7" in result.stdout - result = self.env.run(self.ve_bin/'pip', 'list') + result = self.script.run(self.ve_bin/'pip', 'list') "setuptools (0.9.8)" in result.stdout def test_from_setuptools_7_to_setuptools_7_using_wheel(self): self.prep_ve('1.10') - result = self.env.run(self.ve_bin/'pip', 'install', '--use-wheel', '--no-index', '--find-links=%s' % find_links, '-U', 'setuptools') + result = self.script.run(self.ve_bin/'pip', 'install', '--use-wheel', '--no-index', '--find-links=%s' % find_links, '-U', 'setuptools') assert "Found existing installation: setuptools 0.9.7" in result.stdout assert 'setuptools-0.9.8.dist-info' in str(result.files_created) #only wheels use dist-info - result = self.env.run(self.ve_bin/'pip', 'list') + result = self.script.run(self.ve_bin/'pip', 'list') "setuptools (0.9.8)" in result.stdout def test_from_setuptools_7_to_setuptools_7_with_distribute_7_installed(self): self.prep_ve('1.9.1', distribute=True) - result = self.env.run(self.ve_bin/'pip', 'install', '--no-index', '--find-links=%s' % find_links, '-U', 'setuptools') - result = self.env.run(self.ve_bin/'pip', 'install', '--no-index', '--find-links=%s' % find_links, 'setuptools==0.9.6') - result = self.env.run(self.ve_bin/'pip', 'list') + result = self.script.run(self.ve_bin/'pip', 'install', '--no-index', '--find-links=%s' % find_links, '-U', 'setuptools') + result = self.script.run(self.ve_bin/'pip', 'install', '--no-index', '--find-links=%s' % find_links, 'setuptools==0.9.6') + result = self.script.run(self.ve_bin/'pip', 'list') "setuptools (0.9.6)" in result.stdout "distribute (0.7.3)" in result.stdout - result = self.env.run(self.ve_bin/'pip', 'install', '--no-index', '--find-links=%s' % find_links, '-U', 'setuptools') + result = self.script.run(self.ve_bin/'pip', 'install', '--no-index', '--find-links=%s' % find_links, '-U', 'setuptools') assert "Found existing installation: setuptools 0.9.6" in result.stdout - result = self.env.run(self.ve_bin/'pip', 'list') + result = self.script.run(self.ve_bin/'pip', 'list') "setuptools (0.9.8)" in result.stdout "distribute (0.7.3)" in result.stdout diff --git a/tests/functional/test_install_user.py b/tests/functional/test_install_user.py index df39b7ae5..38f06457a 100644 --- a/tests/functional/test_install_user.py +++ b/tests/functional/test_install_user.py @@ -1,24 +1,41 @@ """ tests specific to "--user" option """ +import imp import sys import os +import textwrap from os.path import abspath, join, curdir, isdir, isfile import pytest +from pip.backwardcompat import uses_pycache + from tests.lib.local_repos import local_checkout -from tests.lib import (tests_data, reset_env, run_pip, pyversion, assert_all_changes, - path_to_url, find_links, pip_install_local) +from tests.lib import (tests_data, reset_env, pyversion, assert_all_changes, + find_links) -patch_dist_in_site_packages = """ - def dist_in_site_packages(dist): - return False - from pip import req - req.dist_in_site_packages=dist_in_site_packages -""" +def _patch_dist_in_site_packages(script): + sitecustomize_path = script.lib_path.join("sitecustomize.py") + sitecustomize_path.write(textwrap.dedent(""" + def dist_in_site_packages(dist): + return False + + from pip import req + req.dist_in_site_packages = dist_in_site_packages + """)) + + # Caught py32 with an outdated __pycache__ file after a sitecustomize + # update (after python should have updated it) so will delete the cache + # file to be sure + # See: https://github.com/pypa/pip/pull/893#issuecomment-16426701 + if uses_pycache: + cache_path = imp.cache_from_source(sitecustomize_path) + if os.path.isfile(cache_path): + os.remove(cache_path) + # --user option is broken in pypy @@ -29,9 +46,9 @@ class Tests_UserSite: """ reset_env(system_site_packages=True) produces env where a --user install can be found using pkg_resources """ - 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)") + script = reset_env(system_site_packages=True) + script.pip('install', '--user', 'INITools==0.2') + result = script.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 @@ -40,8 +57,8 @@ class Tests_UserSite: """ Test installing current directory ('.') into usersite after installing distribute """ - env = reset_env(system_site_packages=True) - result = run_pip('install', '--user', '-e', + script = reset_env(system_site_packages=True) + result = script.pip('install', '--user', '-e', '%s#egg=initools-dev' % local_checkout('svn+http://svn.colorstudy.com/INITools/trunk')) result.assert_installed('INITools', use_user_site=True) @@ -51,11 +68,11 @@ class Tests_UserSite: """ Test installing current directory ('.') into usersite """ - env = reset_env(system_site_packages=True) + script = reset_env(system_site_packages=True) run_from = abspath(join(tests_data, 'packages', 'FSPkg')) - result = run_pip('install', '--user', curdir, cwd=run_from, expect_error=False) - fspkg_folder = env.user_site/'fspkg' - egg_info_folder = env.user_site/'FSPkg-0.1dev-py%s.egg-info' % pyversion + result = script.pip('install', '--user', curdir, cwd=run_from, expect_error=False) + fspkg_folder = script.user_site/'fspkg' + egg_info_folder = script.user_site/'FSPkg-0.1dev-py%s.egg-info' % pyversion assert fspkg_folder in result.files_created, str(result.stdout) assert egg_info_folder in result.files_created, str(result) @@ -65,9 +82,9 @@ class Tests_UserSite: """ user install in virtualenv (with no system packages) fails with message """ - env = reset_env() + script = reset_env() run_from = abspath(join(tests_data, 'packages', 'FSPkg')) - result = run_pip('install', '--user', curdir, cwd=run_from, expect_error=True) + result = script.pip('install', '--user', curdir, cwd=run_from, expect_error=True) assert "Can not perform a '--user' install. User site-packages are not visible in this virtualenv." in result.stdout @@ -76,13 +93,13 @@ class Tests_UserSite: Test user install with conflict in usersite updates usersite. """ - env = reset_env(system_site_packages=True) - result1 = run_pip('install', '--user', 'INITools==0.3') - result2 = run_pip('install', '--user', 'INITools==0.1') + script = reset_env(system_site_packages=True) + result1 = script.pip('install', '--user', 'INITools==0.3') + result2 = script.pip('install', '--user', 'INITools==0.1') #usersite has 0.1 - egg_info_folder = env.user_site / 'INITools-0.1-py%s.egg-info' % pyversion - initools_v3_file = env.root_path / env.user_site / 'initools' / 'configparser.py' #file only in 0.3 + egg_info_folder = script.user_site / 'INITools-0.1-py%s.egg-info' % pyversion + initools_v3_file = script.base_path / script.user_site / 'initools' / 'configparser.py' #file only in 0.3 assert egg_info_folder in result2.files_created, str(result2) assert not isfile(initools_v3_file), initools_v3_file @@ -99,21 +116,22 @@ class Tests_UserSite: # if we don't patch this, pip will return an installation error: "Will not install to the usersite because it will lack sys.path precedence..." # 2) adding usersite to PYTHONPATH, so usersite as sys.path precedence over the virtualenv site - env = reset_env(system_site_packages=True, sitecustomize=patch_dist_in_site_packages) - env.environ["PYTHONPATH"] = env.root_path / env.user_site + script = reset_env(system_site_packages=True) + script.environ["PYTHONPATH"] = script.base_path / script.user_site + _patch_dist_in_site_packages(script) - result1 = run_pip('install', 'INITools==0.2') - result2 = run_pip('install', '--user', 'INITools==0.1') + result1 = script.pip('install', 'INITools==0.2') + result2 = script.pip('install', '--user', 'INITools==0.1') #usersite has 0.1 - egg_info_folder = env.user_site / 'INITools-0.1-py%s.egg-info' % pyversion - initools_folder = env.user_site / 'initools' + egg_info_folder = script.user_site / 'INITools-0.1-py%s.egg-info' % pyversion + initools_folder = script.user_site / 'initools' assert egg_info_folder in result2.files_created, str(result2) assert initools_folder in result2.files_created, str(result2) #site still has 0.2 (can't look in result1; have to check) - egg_info_folder = env.root_path / env.site_packages / 'INITools-0.2-py%s.egg-info' % pyversion - initools_folder = env.root_path / env.site_packages / 'initools' + egg_info_folder = script.base_path / script.site_packages / 'INITools-0.2-py%s.egg-info' % pyversion + initools_folder = script.base_path / script.site_packages / 'initools' assert isdir(egg_info_folder) assert isdir(initools_folder) @@ -130,21 +148,22 @@ class Tests_UserSite: # if we don't patch this, pip will return an installation error: "Will not install to the usersite because it will lack sys.path precedence..." # 2) adding usersite to PYTHONPATH, so usersite as sys.path precedence over the virtualenv site - env = reset_env(system_site_packages=True, sitecustomize=patch_dist_in_site_packages) - env.environ["PYTHONPATH"] = env.root_path / env.user_site + script = reset_env(system_site_packages=True) + script.environ["PYTHONPATH"] = script.base_path / script.user_site + _patch_dist_in_site_packages(script) - result1 = run_pip('install', 'INITools==0.2') - result2 = run_pip('install', '--user', '--upgrade', 'INITools') + result1 = script.pip('install', 'INITools==0.2') + result2 = script.pip('install', '--user', '--upgrade', 'INITools') #usersite has 0.3.1 - egg_info_folder = env.user_site / 'INITools-0.3.1-py%s.egg-info' % pyversion - initools_folder = env.user_site / 'initools' + egg_info_folder = script.user_site / 'INITools-0.3.1-py%s.egg-info' % pyversion + initools_folder = script.user_site / 'initools' assert egg_info_folder in result2.files_created, str(result2) assert initools_folder in result2.files_created, str(result2) #site still has 0.2 (can't look in result1; have to check) - egg_info_folder = env.root_path / env.site_packages / 'INITools-0.2-py%s.egg-info' % pyversion - initools_folder = env.root_path / env.site_packages / 'initools' + egg_info_folder = script.base_path / script.site_packages / 'INITools-0.2-py%s.egg-info' % pyversion + initools_folder = script.base_path / script.site_packages / 'initools' assert isdir(egg_info_folder), result2.stdout assert isdir(initools_folder) @@ -161,22 +180,23 @@ class Tests_UserSite: # if we don't patch this, pip will return an installation error: "Will not install to the usersite because it will lack sys.path precedence..." # 2) adding usersite to PYTHONPATH, so usersite as sys.path precedence over the virtualenv site - env = reset_env(system_site_packages=True, sitecustomize=patch_dist_in_site_packages) - env.environ["PYTHONPATH"] = env.root_path / env.user_site + script = reset_env(system_site_packages=True) + script.environ["PYTHONPATH"] = script.base_path / script.user_site + _patch_dist_in_site_packages(script) - result1 = run_pip('install', 'INITools==0.2') - result2 = run_pip('install', '--user', 'INITools==0.3') - result3 = run_pip('install', '--user', 'INITools==0.1') + result1 = script.pip('install', 'INITools==0.2') + result2 = script.pip('install', '--user', 'INITools==0.3') + result3 = script.pip('install', '--user', 'INITools==0.1') #usersite has 0.1 - egg_info_folder = env.user_site / 'INITools-0.1-py%s.egg-info' % pyversion - initools_v3_file = env.root_path / env.user_site / 'initools' / 'configparser.py' #file only in 0.3 + egg_info_folder = script.user_site / 'INITools-0.1-py%s.egg-info' % pyversion + initools_v3_file = script.base_path / script.user_site / 'initools' / 'configparser.py' #file only in 0.3 assert egg_info_folder in result3.files_created, str(result3) assert not isfile(initools_v3_file), initools_v3_file #site still has 0.2 (can't just look in result1; have to check) - egg_info_folder = env.root_path / env.site_packages / 'INITools-0.2-py%s.egg-info' % pyversion - initools_folder = env.root_path / env.site_packages / 'initools' + egg_info_folder = script.base_path / script.site_packages / 'INITools-0.2-py%s.egg-info' % pyversion + initools_folder = script.base_path / script.site_packages / 'initools' assert isdir(egg_info_folder) assert isdir(initools_folder) @@ -185,10 +205,10 @@ class Tests_UserSite: """ Test user install in --system-site-packages virtualenv with conflict in site fails. """ - env = reset_env(system_site_packages=True) - result1 = run_pip('install', 'INITools==0.2') - result2 = run_pip('install', '--user', 'INITools==0.1', expect_error=True) - resultp = env.run('python', '-c', "import pkg_resources; print(pkg_resources.get_distribution('initools').location)") + script = reset_env(system_site_packages=True) + result1 = script.pip('install', 'INITools==0.2') + result2 = script.pip('install', '--user', 'INITools==0.1', expect_error=True) + resultp = script.run('python', '-c', "import pkg_resources; print(pkg_resources.get_distribution('initools').location)") dist_location = resultp.stdout.strip() assert "Will not install to the user site because it will lack sys.path precedence to %s in %s" \ % ('INITools', dist_location) in result2.stdout, result2.stdout @@ -198,41 +218,40 @@ class Tests_UserSite: """ Test uninstall from usersite """ - env = reset_env(system_site_packages=True) - result1 = run_pip('install', '--user', 'INITools==0.3') - result2 = run_pip('uninstall', '-y', 'INITools') - assert_all_changes(result1, result2, [env.venv/'build', 'cache']) + script = reset_env(system_site_packages=True) + result1 = script.pip('install', '--user', 'INITools==0.3') + result2 = script.pip('uninstall', '-y', 'INITools') + assert_all_changes(result1, result2, [script.venv/'build', 'cache']) def test_uninstall_editable_from_usersite(self): """ Test uninstall editable local user install """ - env = reset_env(system_site_packages=True) + script = reset_env(system_site_packages=True) + script.user_site_path.makedirs() #install to_install = abspath(join(tests_data, 'packages', 'FSPkg')) - result1 = run_pip('install', '--user', '-e', to_install, expect_error=False) - egg_link = env.user_site/'FSPkg.egg-link' + result1 = script.pip('install', '--user', '-e', to_install, expect_error=False) + egg_link = script.user_site/'FSPkg.egg-link' assert egg_link in result1.files_created, str(result1.stdout) #uninstall - result2 = run_pip('uninstall', '-y', 'FSPkg') - assert not isfile(env.root_path / egg_link) + result2 = script.pip('uninstall', '-y', 'FSPkg') + assert not isfile(script.base_path / egg_link) assert_all_changes(result1, result2, - [env.venv/'build', 'cache', env.user_site/'easy-install.pth']) + [script.venv/'build', 'cache', script.user_site/'easy-install.pth']) def test_install_user_wheel(self): """ Test user install from wheel """ - env = reset_env(system_site_packages=True) - pip_install_local('wheel') - result = run_pip('install', 'simple.dist==0.1', '--user', '--use-wheel', + script = reset_env(system_site_packages=True) + script.pip_install_local('wheel') + result = script.pip('install', 'simple.dist==0.1', '--user', '--use-wheel', '--no-index', '--find-links='+find_links) - egg_info_folder = env.user_site / 'simple.dist-0.1.dist-info' + egg_info_folder = script.user_site / 'simple.dist-0.1.dist-info' assert egg_info_folder in result.files_created, str(result) - - diff --git a/tests/functional/test_install_vcs.py b/tests/functional/test_install_vcs.py index 94bf8f68b..147ebd79f 100644 --- a/tests/functional/test_install_vcs.py +++ b/tests/functional/test_install_vcs.py @@ -1,4 +1,4 @@ -from tests.lib import (reset_env, run_pip, +from tests.lib import (reset_env, _create_test_package, _change_test_package_version) from tests.lib.local_repos import local_checkout @@ -7,8 +7,8 @@ def test_install_editable_from_git_with_https(): """ Test cloning from Git with https. """ - reset_env() - result = run_pip('install', '-e', + script = reset_env() + result = script.pip('install', '-e', '%s#egg=pip-test-package' % local_checkout('git+https://github.com/pypa/pip-test-package.git'), expect_error=True) @@ -19,12 +19,12 @@ def test_git_with_sha1_revisions(): """ Git backend should be able to install from SHA1 revisions """ - env = reset_env() - version_pkg_path = _create_test_package(env) - _change_test_package_version(env, version_pkg_path) - sha1 = env.run('git', 'rev-parse', 'HEAD~1', cwd=version_pkg_path).stdout.strip() - run_pip('install', '-e', '%s@%s#egg=version_pkg' % ('git+file://' + version_pkg_path.abspath.replace('\\', '/'), sha1)) - version = env.run('version_pkg') + script = reset_env() + version_pkg_path = _create_test_package(script) + _change_test_package_version(script, version_pkg_path) + sha1 = script.run('git', 'rev-parse', 'HEAD~1', cwd=version_pkg_path).stdout.strip() + script.pip('install', '-e', '%s@%s#egg=version_pkg' % ('git+file://' + version_pkg_path.abspath.replace('\\', '/'), sha1)) + version = script.run('version_pkg') assert '0.1' in version.stdout, version.stdout @@ -32,12 +32,12 @@ def test_git_with_branch_name_as_revision(): """ Git backend should be able to install from branch names """ - env = reset_env() - version_pkg_path = _create_test_package(env) - env.run('git', 'checkout', '-b', 'test_branch', expect_stderr=True, cwd=version_pkg_path) - _change_test_package_version(env, version_pkg_path) - run_pip('install', '-e', '%s@test_branch#egg=version_pkg' % ('git+file://' + version_pkg_path.abspath.replace('\\', '/'))) - version = env.run('version_pkg') + script = reset_env() + version_pkg_path = _create_test_package(script) + script.run('git', 'checkout', '-b', 'test_branch', expect_stderr=True, cwd=version_pkg_path) + _change_test_package_version(script, version_pkg_path) + script.pip('install', '-e', '%s@test_branch#egg=version_pkg' % ('git+file://' + version_pkg_path.abspath.replace('\\', '/'))) + version = script.run('version_pkg') assert 'some different version' in version.stdout @@ -45,12 +45,12 @@ def test_git_with_tag_name_as_revision(): """ Git backend should be able to install from tag names """ - env = reset_env() - version_pkg_path = _create_test_package(env) - env.run('git', 'tag', 'test_tag', expect_stderr=True, cwd=version_pkg_path) - _change_test_package_version(env, version_pkg_path) - run_pip('install', '-e', '%s@test_tag#egg=version_pkg' % ('git+file://' + version_pkg_path.abspath.replace('\\', '/'))) - version = env.run('version_pkg') + script = reset_env() + version_pkg_path = _create_test_package(script) + script.run('git', 'tag', 'test_tag', expect_stderr=True, cwd=version_pkg_path) + _change_test_package_version(script, version_pkg_path) + script.pip('install', '-e', '%s@test_tag#egg=version_pkg' % ('git+file://' + version_pkg_path.abspath.replace('\\', '/'))) + version = script.run('version_pkg') assert '0.1' in version.stdout @@ -58,12 +58,12 @@ def test_git_with_tag_name_and_update(): """ Test cloning a git repository and updating to a different version. """ - reset_env() - result = run_pip('install', '-e', '%s#egg=pip-test-package' % + script = reset_env() + result = script.pip('install', '-e', '%s#egg=pip-test-package' % local_checkout('git+http://github.com/pypa/pip-test-package.git'), expect_error=True) result.assert_installed('pip-test-package', with_files=['.git']) - result = run_pip('install', '--global-option=--version', '-e', + result = script.pip('install', '--global-option=--version', '-e', '%s@0.1.2#egg=pip-test-package' % local_checkout('git+http://github.com/pypa/pip-test-package.git'), expect_error=True) @@ -75,12 +75,12 @@ def test_git_branch_should_not_be_changed(): Editable installations should not change branch related to issue #32 and #161 """ - env = reset_env() - run_pip('install', '-e', '%s#egg=pip-test-package' % + script = reset_env() + script.pip('install', '-e', '%s#egg=pip-test-package' % local_checkout('git+http://github.com/pypa/pip-test-package.git'), expect_error=True) - source_dir = env.venv_path/'src'/'pip-test-package' - result = env.run('git', 'branch', cwd=source_dir) + source_dir = script.venv_path/'src'/'pip-test-package' + result = script.run('git', 'branch', cwd=source_dir) assert '* master' in result.stdout, result.stdout @@ -88,8 +88,8 @@ def test_git_with_non_editable_unpacking(): """ Test cloning a git repository from a non-editable URL with a given tag. """ - reset_env() - result = run_pip('install', '--global-option=--version', local_checkout( + script = reset_env() + result = script.pip('install', '--global-option=--version', local_checkout( 'git+http://github.com/pypa/pip-test-package.git@0.1.2#egg=pip-test-package' ), expect_error=True) assert '0.1.2' in result.stdout @@ -99,8 +99,8 @@ def test_git_with_editable_where_egg_contains_dev_string(): """ Test cloning a git repository from an editable url which contains "dev" string """ - reset_env() - result = run_pip('install', '-e', '%s#egg=django-devserver' % + script = reset_env() + result = script.pip('install', '-e', '%s#egg=django-devserver' % local_checkout('git+git://github.com/dcramer/django-devserver.git')) result.assert_installed('django-devserver', with_files=['.git']) @@ -109,10 +109,10 @@ def test_git_with_non_editable_where_egg_contains_dev_string(): """ Test cloning a git repository from a non-editable url which contains "dev" string """ - env = reset_env() - result = run_pip('install', '%s#egg=django-devserver' % + script = reset_env() + result = script.pip('install', '%s#egg=django-devserver' % local_checkout('git+git://github.com/dcramer/django-devserver.git')) - devserver_folder = env.site_packages/'devserver' + devserver_folder = script.site_packages/'devserver' assert devserver_folder in result.files_created, str(result) @@ -120,11 +120,11 @@ def test_git_with_ambiguous_revs(): """ Test git with two "names" (tag/branch) pointing to the same commit """ - env = reset_env() - version_pkg_path = _create_test_package(env) + script = reset_env() + version_pkg_path = _create_test_package(script) package_url = 'git+file://%s@0.1#egg=version_pkg' % (version_pkg_path.abspath.replace('\\', '/')) - env.run('git', 'tag', '0.1', cwd=version_pkg_path) - result = run_pip('install', '-e', package_url) + script.run('git', 'tag', '0.1', cwd=version_pkg_path) + result = script.pip('install', '-e', package_url) assert 'Could not find a tag or branch' not in result.stdout # it is 'version-pkg' instead of 'version_pkg' because # egg-link name is version-pkg.egg-link because it is a single .py module @@ -133,14 +133,12 @@ def test_git_with_ambiguous_revs(): def test_git_works_with_editable_non_origin_repo(): # set up, create a git repo and install it as editable from a local directory path - env = reset_env() - version_pkg_path = _create_test_package(env) - run_pip('install', '-e', version_pkg_path.abspath) + script = reset_env() + version_pkg_path = _create_test_package(script) + script.pip('install', '-e', version_pkg_path.abspath) # 'freeze'ing this should not fall over, but should result in stderr output warning - result = run_pip('freeze', expect_stderr=True) + result = script.pip('freeze', expect_stderr=True) assert "Error when trying to get requirement" in result.stderr assert "Could not determine repository location" in result.stdout assert "version-pkg==0.1" in result.stdout - - diff --git a/tests/functional/test_install_vcs_git.py b/tests/functional/test_install_vcs_git.py index 4f8941dae..0304d766f 100644 --- a/tests/functional/test_install_vcs_git.py +++ b/tests/functional/test_install_vcs_git.py @@ -5,8 +5,7 @@ import pytest from mock import patch from pip.vcs.git import Git -from tests.lib import (reset_env, run_pip, - _create_test_package,) +from tests.lib import reset_env, _create_test_package from tests.lib.git_submodule_helpers import ( _change_test_package_submodule, _pull_in_submodule_changes_to_module, @@ -15,11 +14,11 @@ from tests.lib.git_submodule_helpers import ( def test_get_refs_should_return_tag_name_and_commit_pair(): - env = reset_env() - version_pkg_path = _create_test_package(env) - env.run('git', 'tag', '0.1', cwd=version_pkg_path) - env.run('git', 'tag', '0.2', cwd=version_pkg_path) - commit = env.run('git', 'rev-parse', 'HEAD', + script = reset_env() + version_pkg_path = _create_test_package(script) + script.run('git', 'tag', '0.1', cwd=version_pkg_path) + script.run('git', 'tag', '0.2', cwd=version_pkg_path) + commit = script.run('git', 'rev-parse', 'HEAD', cwd=version_pkg_path).stdout.strip() git = Git() result = git.get_refs(version_pkg_path) @@ -28,10 +27,10 @@ def test_get_refs_should_return_tag_name_and_commit_pair(): def test_get_refs_should_return_branch_name_and_commit_pair(): - env = reset_env() - version_pkg_path = _create_test_package(env) - env.run('git', 'branch', 'branch0.1', cwd=version_pkg_path) - commit = env.run('git', 'rev-parse', 'HEAD', + script = reset_env() + version_pkg_path = _create_test_package(script) + script.run('git', 'branch', 'branch0.1', cwd=version_pkg_path) + commit = script.run('git', 'rev-parse', 'HEAD', cwd=version_pkg_path).stdout.strip() git = Git() result = git.get_refs(version_pkg_path) @@ -40,13 +39,13 @@ def test_get_refs_should_return_branch_name_and_commit_pair(): def test_get_refs_should_ignore_no_branch(): - env = reset_env() - version_pkg_path = _create_test_package(env) - env.run('git', 'branch', 'branch0.1', cwd=version_pkg_path) - commit = env.run('git', 'rev-parse', 'HEAD', + script = reset_env() + version_pkg_path = _create_test_package(script) + script.run('git', 'branch', 'branch0.1', cwd=version_pkg_path) + commit = script.run('git', 'rev-parse', 'HEAD', cwd=version_pkg_path).stdout.strip() # current branch here is "* (nobranch)" - env.run('git', 'checkout', commit, + script.run('git', 'checkout', commit, cwd=version_pkg_path, expect_stderr=True) git = Git() result = git.get_refs(version_pkg_path) @@ -88,17 +87,16 @@ def test_check_submodule_addition(): Submodules are pulled in on install and updated on upgrade. """ - env = reset_env() - module_path, submodule_path = _create_test_package_with_submodule(env) + script = reset_env() + module_path, submodule_path = _create_test_package_with_submodule(script) - install_result = run_pip('install', '-e', 'git+'+module_path+'#egg=version_pkg') + install_result = script.pip('install', '-e', 'git+'+module_path+'#egg=version_pkg') assert '.virtualenv/src/version-pkg/testpkg/static/testfile' in install_result.files_created - _change_test_package_submodule(env, submodule_path) - _pull_in_submodule_changes_to_module(env, module_path) + _change_test_package_submodule(script, submodule_path) + _pull_in_submodule_changes_to_module(script, module_path) # expect error because git may write to stderr - update_result = run_pip('install', '-e', 'git+'+module_path+'#egg=version_pkg', '--upgrade', expect_error=True) - - assert env.venv/'src/version-pkg/testpkg/static/testfile2' in update_result.files_created + update_result = script.pip('install', '-e', 'git+'+module_path+'#egg=version_pkg', '--upgrade', expect_error=True) + assert script.venv/'src/version-pkg/testpkg/static/testfile2' in update_result.files_created diff --git a/tests/functional/test_install_wheel.py b/tests/functional/test_install_wheel.py index 2f25ffbf8..a1711b42d 100644 --- a/tests/functional/test_install_wheel.py +++ b/tests/functional/test_install_wheel.py @@ -1,6 +1,6 @@ from os.path import abspath, join -from tests.lib import tests_data, reset_env, run_pip, pip_install_local, find_links +from tests.lib import tests_data, reset_env, find_links from tests.lib.path import Path @@ -8,11 +8,11 @@ def test_install_from_wheel(): """ Test installing from a wheel. """ - env = reset_env() - result = run_pip('install', 'simple.dist', '--use-wheel', + script = reset_env() + result = script.pip('install', 'simple.dist', '--use-wheel', '--no-index', '--find-links='+find_links, expect_error=False) - dist_info_folder = env.site_packages/'simple.dist-0.1.dist-info' + dist_info_folder = script.site_packages/'simple.dist-0.1.dist-info' assert dist_info_folder in result.files_created, (dist_info_folder, result.files_created, result.stdout) @@ -22,15 +22,15 @@ def test_install_from_wheel_with_extras(): """ Test installing from a wheel with extras. """ - env = reset_env() - result = run_pip('install', 'complex-dist[simple]', '--use-wheel', + script = reset_env() + result = script.pip('install', 'complex-dist[simple]', '--use-wheel', '--no-index', '--find-links='+find_links, expect_error=False) - dist_info_folder = env.site_packages/'complex_dist-0.1.dist-info' + dist_info_folder = script.site_packages/'complex_dist-0.1.dist-info' assert dist_info_folder in result.files_created, (dist_info_folder, result.files_created, result.stdout) - dist_info_folder = env.site_packages/'simple.dist-0.1.dist-info' + dist_info_folder = script.site_packages/'simple.dist-0.1.dist-info' assert dist_info_folder in result.files_created, (dist_info_folder, result.files_created, result.stdout) @@ -40,12 +40,12 @@ def test_install_from_wheel_file(): """ Test installing directly from a wheel file. """ - env = reset_env() + script = reset_env() package = abspath(join(tests_data, 'packages', 'headers.dist-0.1-py2.py3-none-any.whl')) - result = run_pip('install', package, '--no-index', expect_error=False) - dist_info_folder = env.site_packages/'headers.dist-0.1.dist-info' + result = script.pip('install', package, '--no-index', expect_error=False) + dist_info_folder = script.site_packages/'headers.dist-0.1.dist-info' assert dist_info_folder in result.files_created, (dist_info_folder, result.files_created, result.stdout) @@ -55,10 +55,10 @@ def test_install_wheel_with_target(): """ Test installing a wheel using pip install --target """ - env = reset_env() - pip_install_local('wheel') - target_dir = env.scratch_path/'target' - result = run_pip('install', 'simple.dist==0.1', '-t', target_dir, '--use-wheel', + script = reset_env() + script.pip_install_local('wheel') + target_dir = script.scratch_path/'target' + result = script.pip('install', 'simple.dist==0.1', '-t', target_dir, '--use-wheel', '--no-index', '--find-links='+find_links) assert Path('scratch')/'target'/'simpledist' in result.files_created, str(result) @@ -68,11 +68,11 @@ def test_install_from_wheel_installs_deps(): Test can install dependencies of wheels """ # 'requires_source' depends on the 'source' project - env = reset_env() + script = reset_env() package = abspath(join(tests_data, 'packages', 'requires_source-1.0-py2.py3-none-any.whl')) - result = run_pip('install', '--no-index', '--find-links', find_links, package) + result = script.pip('install', '--no-index', '--find-links', find_links, package) result.assert_installed('source', editable=False) @@ -81,13 +81,10 @@ def test_install_from_wheel_no_deps(): Test --no-deps works with wheel installs """ # 'requires_source' depends on the 'source' project - env = reset_env() + script = reset_env() package = abspath(join(tests_data, 'packages', 'requires_source-1.0-py2.py3-none-any.whl')) - result = run_pip('install', '--no-index', '--find-links', find_links, '--no-deps', package) - pkg_folder = env.site_packages/'source' + result = script.pip('install', '--no-index', '--find-links', find_links, '--no-deps', package) + pkg_folder = script.site_packages/'source' assert pkg_folder not in result.files_created - - - diff --git a/tests/functional/test_list.py b/tests/functional/test_list.py index 4d8822bf8..e5dc054a5 100644 --- a/tests/functional/test_list.py +++ b/tests/functional/test_list.py @@ -1,8 +1,7 @@ import os import re import textwrap -from tests.lib import (pyversion, reset_env, run_pip, write_file, path_to_url, - tests_data, find_links) +from tests.lib import pyversion, reset_env, path_to_url, tests_data, find_links from tests.lib.local_repos import local_checkout @@ -11,9 +10,9 @@ def test_list_command(): Test default behavior of list command. """ - reset_env() - run_pip('install', '-f', find_links, '--no-index', 'simple==1.0', 'simple2==3.0') - result = run_pip('list') + script = reset_env() + script.pip('install', '-f', find_links, '--no-index', 'simple==1.0', 'simple2==3.0') + result = script.pip('list') assert 'simple (1.0)' in result.stdout, str(result) assert 'simple2 (3.0)' in result.stdout, str(result) @@ -23,9 +22,9 @@ def test_local_flag(): Test the behavior of --local flag in the list command """ - reset_env() - run_pip('install', '-f', find_links, '--no-index', 'simple==1.0') - result = run_pip('list', '--local') + script = reset_env() + script.pip('install', '-f', find_links, '--no-index', 'simple==1.0') + result = script.pip('list', '--local') assert 'simple (1.0)' in result.stdout @@ -34,10 +33,10 @@ def test_uptodate_flag(): Test the behavior of --uptodate flag in the list command """ - reset_env() - run_pip('install', '-f', find_links, '--no-index', 'simple==1.0', 'simple2==3.0') - run_pip('install', '-e', 'git+https://github.com/pypa/pip-test-package.git#egg=pip-test-package') - result = run_pip('list', '-f', find_links, '--no-index', '--uptodate') + script = reset_env() + script.pip('install', '-f', find_links, '--no-index', 'simple==1.0', 'simple2==3.0') + script.pip('install', '-e', 'git+https://github.com/pypa/pip-test-package.git#egg=pip-test-package') + result = script.pip('list', '-f', find_links, '--no-index', '--uptodate') assert 'simple (1.0)' not in result.stdout #3.0 is latest assert 'pip-test-package' not in result.stdout #editables excluded assert 'simple2 (3.0)' in result.stdout, str(result) @@ -48,10 +47,10 @@ def test_outdated_flag(): Test the behavior of --outdated flag in the list command """ - reset_env() - run_pip('install', '-f', find_links, '--no-index', 'simple==1.0', 'simple2==3.0') - run_pip('install', '-e', 'git+https://github.com/pypa/pip-test-package.git#egg=pip-test-package') - result = run_pip('list', '-f', find_links, '--no-index', '--outdated') + script = reset_env() + script.pip('install', '-f', find_links, '--no-index', 'simple==1.0', 'simple2==3.0') + script.pip('install', '-e', 'git+https://github.com/pypa/pip-test-package.git#egg=pip-test-package') + result = script.pip('list', '-f', find_links, '--no-index', '--outdated') assert 'simple (Current: 1.0 Latest: 3.0)' in result.stdout assert 'pip-test-package' not in result.stdout #editables excluded assert 'simple2' not in result.stdout, str(result) #3.0 is latest @@ -61,10 +60,9 @@ def test_editables_flag(): """ Test the behavior of --editables flag in the list command """ - reset_env() - run_pip('install', '-f', find_links, '--no-index', 'simple==1.0') - result = run_pip('install', '-e', 'git+https://github.com/pypa/pip-test-package.git#egg=pip-test-package') - result = run_pip('list', '--editable') + script = reset_env() + script.pip('install', '-f', find_links, '--no-index', 'simple==1.0') + result = script.pip('install', '-e', 'git+https://github.com/pypa/pip-test-package.git#egg=pip-test-package') + result = script.pip('list', '--editable') assert 'simple (1.0)' not in result.stdout, str(result) assert os.path.join('src', 'pip-test-package') in result.stdout, str(result) - diff --git a/tests/functional/test_search.py b/tests/functional/test_search.py index 000102740..ac27c5199 100644 --- a/tests/functional/test_search.py +++ b/tests/functional/test_search.py @@ -7,8 +7,7 @@ from pip.status_codes import NO_MATCHES_FOUND, SUCCESS from pip.backwardcompat import xmlrpclib, b from pip.baseparser import create_main_parser from mock import Mock -from tests.lib import run_pip, reset_env, pyversion -from tests.lib.pypi_server import assert_equal +from tests.lib import reset_env, pyversion if pyversion >= '3': @@ -40,7 +39,7 @@ def test_pypi_xml_transformation(): {'_pypi_ordering': 50, 'name': 'bar', 'summary': 'bar summary', 'version': '1.0'}] expected = [{'score': 200, 'versions': ['1.0', '2.0'], 'name': 'foo', 'summary': 'foo summary v2'}, {'score': 50, 'versions': ['1.0'], 'name': 'bar', 'summary': 'bar summary'}] - assert_equal(transform_hits(pypi_hits), expected) + assert transform_hits(pypi_hits) == expected def test_invalid_pypi_transformation(): @@ -52,7 +51,7 @@ def test_invalid_pypi_transformation(): expected = [{'score': 100, 'versions': ['1.0'], 'name': 'foo', 'summary': 'foo summary'}, {'score': 0, 'versions': ['1.0'], 'name': 'bar', 'summary': 'bar summary'}] - assert_equal(transform_hits(pypi_hits), expected) + assert transform_hits(pypi_hits) == expected def test_search(): @@ -60,8 +59,8 @@ def test_search(): End to end test of search command. """ - reset_env() - output = run_pip('search', 'pip') + script = reset_env() + output = script.pip('search', 'pip') assert 'A tool for installing and managing Python packages' in output.stdout @@ -70,8 +69,8 @@ def test_multiple_search(): Test searching for multiple packages at once. """ - reset_env() - output = run_pip('search', 'pip', 'INITools') + script = reset_env() + output = script.pip('search', 'pip', 'INITools') assert 'A tool for installing and managing Python packages' in output.stdout assert 'Tools for parsing and using INI-style files' in output.stdout @@ -80,8 +79,8 @@ def test_search_missing_argument(): """ Test missing required argument for search """ - env = reset_env() - result = run_pip('search', expect_error=True) + script = reset_env() + result = script.pip('search', expect_error=True) assert 'ERROR: Missing required argument (search query).' in result.stdout @@ -111,8 +110,8 @@ def test_search_should_exit_status_code_zero_when_find_packages(): """ Test search exit status code for package found """ - env = reset_env() - result = run_pip('search', 'pip') + script = reset_env() + result = script.pip('search', 'pip') assert result.returncode == SUCCESS @@ -120,6 +119,6 @@ def test_search_exit_status_code_when_finds_no_package(): """ Test search exit status code for no matches """ - env = reset_env() - result = run_pip('search', 'non-existant-package', expect_error=True) + script = reset_env() + result = script.pip('search', 'non-existant-package', expect_error=True) assert result.returncode == NO_MATCHES_FOUND, result.returncode diff --git a/tests/functional/test_show.py b/tests/functional/test_show.py index 24bb57071..3eb759201 100644 --- a/tests/functional/test_show.py +++ b/tests/functional/test_show.py @@ -1,7 +1,7 @@ import re from pip import __version__ from pip.commands.show import search_packages_info -from tests.lib import reset_env, run_pip +from tests.lib import reset_env def test_show(): @@ -9,8 +9,8 @@ def test_show(): Test end to end test for show command. """ - reset_env() - result = run_pip('show', 'pip') + script = reset_env() + result = script.pip('show', 'pip') lines = result.stdout.split('\n') assert len(lines) == 6 assert lines[0] == '---', lines[0] @@ -26,8 +26,8 @@ def test_show_with_files_not_found(): installed-files.txt not found. """ - reset_env() - result = run_pip('show', '-f', 'pip') + script = reset_env() + result = script.pip('show', '-f', 'pip') lines = result.stdout.split('\n') assert len(lines) == 8 assert lines[0] == '---', lines[0] @@ -44,9 +44,9 @@ def test_show_with_all_files(): Test listing all files in the show command. """ - reset_env() - result = run_pip('install', 'initools==0.2') - result = run_pip('show', '--files', 'initools') + script = reset_env() + result = script.pip('install', 'initools==0.2') + result = script.pip('show', '--files', 'initools') assert re.search(r"Files:\n( .+\n)+", result.stdout) @@ -55,8 +55,8 @@ def test_missing_argument(): Test show command with no arguments. """ - reset_env() - result = run_pip('show') + script = reset_env() + result = script.pip('show') assert 'ERROR: Please provide a package name or names.' in result.stdout diff --git a/tests/functional/test_uninstall.py b/tests/functional/test_uninstall.py index 60a55005f..728bb9caa 100644 --- a/tests/functional/test_uninstall.py +++ b/tests/functional/test_uninstall.py @@ -6,7 +6,7 @@ import sys from os.path import join, abspath, normpath from tempfile import mkdtemp from mock import patch -from tests.lib import tests_data, reset_env, run_pip, assert_all_changes, write_file, pyversion +from tests.lib import tests_data, reset_env, assert_all_changes, pyversion from tests.lib.local_repos import local_repo, local_checkout from pip.util import rmtree @@ -17,13 +17,13 @@ def test_simple_uninstall(): Test simple install and uninstall. """ - env = reset_env() - result = run_pip('install', 'INITools==0.2') - assert join(env.site_packages, 'initools') in result.files_created, sorted(result.files_created.keys()) + script = reset_env() + result = script.pip('install', 'INITools==0.2') + assert join(script.site_packages, 'initools') in result.files_created, sorted(result.files_created.keys()) #the import forces the generation of __pycache__ if the version of python supports it - env.run('python', '-c', "import initools") - result2 = run_pip('uninstall', 'INITools', '-y') - assert_all_changes(result, result2, [env.venv/'build', 'cache']) + script.run('python', '-c', "import initools") + result2 = script.pip('uninstall', 'INITools', '-y') + assert_all_changes(result, result2, [script.venv/'build', 'cache']) def test_uninstall_with_scripts(): @@ -31,13 +31,13 @@ def test_uninstall_with_scripts(): Uninstall an easy_installed package with scripts. """ - env = reset_env() - result = env.run('easy_install', 'PyLogo', expect_stderr=True) - easy_install_pth = env.site_packages/ 'easy-install.pth' + script = reset_env() + result = script.run('easy_install', 'PyLogo', expect_stderr=True) + easy_install_pth = script.site_packages/ 'easy-install.pth' pylogo = sys.platform == 'win32' and 'pylogo' or 'PyLogo' assert(pylogo in result.files_updated[easy_install_pth].bytes) - result2 = run_pip('uninstall', 'pylogo', '-y', expect_error=True) - assert_all_changes(result, result2, [env.venv/'build', 'cache']) + result2 = script.pip('uninstall', 'pylogo', '-y', expect_error=True) + assert_all_changes(result, result2, [script.venv/'build', 'cache']) def test_uninstall_easy_install_after_import(): @@ -45,12 +45,12 @@ def test_uninstall_easy_install_after_import(): Uninstall an easy_installed package after it's been imported """ - env = reset_env() - result = env.run('easy_install', 'INITools==0.2', expect_stderr=True) + script = reset_env() + result = script.run('easy_install', 'INITools==0.2', expect_stderr=True) #the import forces the generation of __pycache__ if the version of python supports it - env.run('python', '-c', "import initools") - result2 = run_pip('uninstall', 'INITools', '-y') - assert_all_changes(result, result2, [env.venv/'build', 'cache']) + script.run('python', '-c', "import initools") + result2 = script.pip('uninstall', 'INITools', '-y') + assert_all_changes(result, result2, [script.venv/'build', 'cache']) def test_uninstall_namespace_package(): @@ -59,12 +59,12 @@ def test_uninstall_namespace_package(): the namespace and everything in it. """ - env = reset_env() - result = run_pip('install', 'pd.requires==0.0.3', expect_error=True) - assert join(env.site_packages, 'pd') in result.files_created, sorted(result.files_created.keys()) - result2 = run_pip('uninstall', 'pd.find', '-y', expect_error=True) - assert join(env.site_packages, 'pd') not in result2.files_deleted, sorted(result2.files_deleted.keys()) - assert join(env.site_packages, 'pd', 'find') in result2.files_deleted, sorted(result2.files_deleted.keys()) + script = reset_env() + result = script.pip('install', 'pd.requires==0.0.3', expect_error=True) + assert join(script.site_packages, 'pd') in result.files_created, sorted(result.files_created.keys()) + result2 = script.pip('uninstall', 'pd.find', '-y', expect_error=True) + assert join(script.site_packages, 'pd') not in result2.files_deleted, sorted(result2.files_deleted.keys()) + assert join(script.site_packages, 'pd', 'find') in result2.files_deleted, sorted(result2.files_deleted.keys()) def test_uninstall_overlapping_package(): @@ -77,18 +77,18 @@ def test_uninstall_overlapping_package(): """ parent_pkg = abspath(join(tests_data, 'packages', 'parent-0.1.tar.gz')) child_pkg = abspath(join(tests_data, 'packages', 'child-0.1.tar.gz')) - env = reset_env() - result1 = run_pip('install', parent_pkg, expect_error=False) - assert join(env.site_packages, 'parent') in result1.files_created, sorted(result1.files_created.keys()) - result2 = run_pip('install', child_pkg, expect_error=False) - assert join(env.site_packages, 'child') in result2.files_created, sorted(result2.files_created.keys()) - assert normpath(join(env.site_packages, 'parent/plugins/child_plugin.py')) in result2.files_created, sorted(result2.files_created.keys()) + script = reset_env() + result1 = script.pip('install', parent_pkg, expect_error=False) + assert join(script.site_packages, 'parent') in result1.files_created, sorted(result1.files_created.keys()) + result2 = script.pip('install', child_pkg, expect_error=False) + assert join(script.site_packages, 'child') in result2.files_created, sorted(result2.files_created.keys()) + assert normpath(join(script.site_packages, 'parent/plugins/child_plugin.py')) in result2.files_created, sorted(result2.files_created.keys()) #the import forces the generation of __pycache__ if the version of python supports it - env.run('python', '-c', "import parent.plugins.child_plugin, child") - result3 = run_pip('uninstall', '-y', 'child', expect_error=False) - assert join(env.site_packages, 'child') in result3.files_deleted, sorted(result3.files_created.keys()) - assert normpath(join(env.site_packages, 'parent/plugins/child_plugin.py')) in result3.files_deleted, sorted(result3.files_deleted.keys()) - assert join(env.site_packages, 'parent') not in result3.files_deleted, sorted(result3.files_deleted.keys()) + script.run('python', '-c', "import parent.plugins.child_plugin, child") + result3 = script.pip('uninstall', '-y', 'child', expect_error=False) + assert join(script.site_packages, 'child') in result3.files_deleted, sorted(result3.files_created.keys()) + assert normpath(join(script.site_packages, 'parent/plugins/child_plugin.py')) in result3.files_deleted, sorted(result3.files_deleted.keys()) + assert join(script.site_packages, 'parent') not in result3.files_deleted, sorted(result3.files_deleted.keys()) # Additional check: uninstalling 'child' should return things to the # previous state, without unintended side effects. assert_all_changes(result2, result3, []) @@ -99,13 +99,13 @@ def test_uninstall_console_scripts(): Test uninstalling a package with more files (console_script entry points, extra directories). """ - env = reset_env() + script = reset_env() args = ['install'] args.append('discover') - result = run_pip(*args, **{"expect_error": True}) - assert env.bin/'discover'+env.exe in result.files_created, sorted(result.files_created.keys()) - result2 = run_pip('uninstall', 'discover', '-y', expect_error=True) - assert_all_changes(result, result2, [env.venv/'build', 'cache']) + result = script.pip(*args, **{"expect_error": True}) + assert script.bin/'discover'+script.exe in result.files_created, sorted(result.files_created.keys()) + result2 = script.pip('uninstall', 'discover', '-y', expect_error=True) + assert_all_changes(result, result2, [script.venv/'build', 'cache']) def test_uninstall_easy_installed_console_scripts(): @@ -113,13 +113,13 @@ def test_uninstall_easy_installed_console_scripts(): Test uninstalling package with console_scripts that is easy_installed. """ - env = reset_env() + script = reset_env() args = ['easy_install'] args.append('discover') - result = env.run(*args, **{"expect_stderr": True}) - assert env.bin/'discover'+env.exe in result.files_created, sorted(result.files_created.keys()) - result2 = run_pip('uninstall', 'discover', '-y') - assert_all_changes(result, result2, [env.venv/'build', 'cache']) + result = script.run(*args, **{"expect_stderr": True}) + assert script.bin/'discover'+script.exe in result.files_created, sorted(result.files_created.keys()) + result2 = script.pip('uninstall', 'discover', '-y') + assert_all_changes(result, result2, [script.venv/'build', 'cache']) def test_uninstall_editable_from_svn(): @@ -127,13 +127,13 @@ def test_uninstall_editable_from_svn(): Test uninstalling an editable installation from svn. """ - env = reset_env() - result = run_pip('install', '-e', '%s#egg=initools-dev' % + script = reset_env() + result = script.pip('install', '-e', '%s#egg=initools-dev' % local_checkout('svn+http://svn.colorstudy.com/INITools/trunk')) result.assert_installed('INITools') - result2 = run_pip('uninstall', '-y', 'initools') - assert (env.venv/'src'/'initools' in result2.files_after), 'oh noes, pip deleted my sources!' - assert_all_changes(result, result2, [env.venv/'src', env.venv/'build']) + result2 = script.pip('uninstall', '-y', 'initools') + assert (script.venv/'src'/'initools' in result2.files_after), 'oh noes, pip deleted my sources!' + assert_all_changes(result, result2, [script.venv/'src', script.venv/'build']) def test_uninstall_editable_with_source_outside_venv(): @@ -150,12 +150,12 @@ def test_uninstall_editable_with_source_outside_venv(): def _test_uninstall_editable_with_source_outside_venv(tmpdir): - env = reset_env() - result = env.run('git', 'clone', local_repo('git+git://github.com/pypa/virtualenv'), tmpdir) - result2 = run_pip('install', '-e', tmpdir) - assert (join(env.site_packages, 'virtualenv.egg-link') in result2.files_created), list(result2.files_created.keys()) - result3 = run_pip('uninstall', '-y', 'virtualenv', expect_error=True) - assert_all_changes(result, result3, [env.venv/'build']) + script = reset_env() + result = script.run('git', 'clone', local_repo('git+git://github.com/pypa/virtualenv'), tmpdir) + result2 = script.pip('install', '-e', tmpdir) + assert (join(script.site_packages, 'virtualenv.egg-link') in result2.files_created), list(result2.files_created.keys()) + result3 = script.pip('uninstall', '-y', 'virtualenv', expect_error=True) + assert_all_changes(result, result3, [script.venv/'build']) def test_uninstall_from_reqs_file(): @@ -163,14 +163,14 @@ def test_uninstall_from_reqs_file(): Test uninstall from a requirements file. """ - env = reset_env() - write_file('test-req.txt', textwrap.dedent("""\ + script = reset_env() + script.scratch_path.join("test-req.txt").write(textwrap.dedent("""\ -e %s#egg=initools-dev # and something else to test out: PyLogo<0.4 """ % local_checkout('svn+http://svn.colorstudy.com/INITools/trunk'))) - result = run_pip('install', '-r', 'test-req.txt') - write_file('test-req.txt', textwrap.dedent("""\ + result = script.pip('install', '-r', 'test-req.txt') + script.scratch_path.join("test-req.txt").write(textwrap.dedent("""\ # -f, -i, and --extra-index-url should all be ignored by uninstall -f http://www.example.com -i http://www.example.com @@ -180,9 +180,9 @@ def test_uninstall_from_reqs_file(): # and something else to test out: PyLogo<0.4 """ % local_checkout('svn+http://svn.colorstudy.com/INITools/trunk'))) - result2 = run_pip('uninstall', '-r', 'test-req.txt', '-y') + result2 = script.pip('uninstall', '-r', 'test-req.txt', '-y') assert_all_changes( - result, result2, [env.venv/'build', env.venv/'src', env.scratch/'test-req.txt']) + result, result2, [script.venv/'build', script.venv/'src', script.scratch/'test-req.txt']) def test_uninstall_as_egg(): @@ -190,16 +190,16 @@ def test_uninstall_as_egg(): Test uninstall package installed as egg. """ - env = reset_env() + script = reset_env() to_install = abspath(join(tests_data, 'packages', 'FSPkg')) - result = run_pip('install', to_install, '--egg', expect_error=False) - fspkg_folder = env.site_packages/'fspkg' - egg_folder = env.site_packages/'FSPkg-0.1dev-py%s.egg' % pyversion + result = script.pip('install', to_install, '--egg', expect_error=False) + fspkg_folder = script.site_packages/'fspkg' + egg_folder = script.site_packages/'FSPkg-0.1dev-py%s.egg' % pyversion assert fspkg_folder not in result.files_created, str(result.stdout) assert egg_folder in result.files_created, str(result) - result2 = run_pip('uninstall', 'FSPkg', '-y', expect_error=True) - assert_all_changes(result, result2, [env.venv/'build', 'cache']) + result2 = script.pip('uninstall', 'FSPkg', '-y', expect_error=True) + assert_all_changes(result, result2, [script.venv/'build', 'cache']) @patch('pip.req.logger') @@ -238,6 +238,3 @@ def test_uninstallpathset_non_local(mock_logger): uninstall_set = UninstallPathSet(test_dist) uninstall_set.remove() #with no files added to set; which is the case when trying to remove non-local dists mock_logger.notify.assert_any_call("Not uninstalling pip at %s, outside environment %s" % (nonlocal_path, sys.prefix)), mock_logger.notify.mock_calls - - - diff --git a/tests/functional/test_wheel.py b/tests/functional/test_wheel.py index ec5225ec3..e6d661b3c 100644 --- a/tests/functional/test_wheel.py +++ b/tests/functional/test_wheel.py @@ -7,26 +7,26 @@ from os.path import exists from pip import wheel from pip.download import path_to_url as path_to_url_d -from tests.lib import tests_data, reset_env, run_pip, pyversion_nodot, write_file, path_to_url, find_links, pip_install_local +from tests.lib import tests_data, reset_env, pyversion_nodot, path_to_url, find_links def test_pip_wheel_fails_without_wheel(): """ Test 'pip wheel' fails without wheel """ - env = reset_env() - result = run_pip('wheel', '--no-index', '-f', find_links, 'simple==3.0', expect_error=True) + script = reset_env() + result = script.pip('wheel', '--no-index', '-f', find_links, 'simple==3.0', expect_error=True) assert "'pip wheel' requires bdist_wheel" in result.stdout def test_pip_wheel_success(): """ Test 'pip wheel' success. """ - env = reset_env() - pip_install_local('wheel') - result = run_pip('wheel', '--no-index', '-f', find_links, 'simple==3.0') + script = reset_env() + script.pip_install_local('wheel') + result = script.pip('wheel', '--no-index', '-f', find_links, 'simple==3.0') wheel_file_name = 'simple-3.0-py%s-none-any.whl' % pyversion_nodot - wheel_file_path = env.scratch/'wheelhouse'/wheel_file_name + wheel_file_path = script.scratch/'wheelhouse'/wheel_file_name assert wheel_file_path in result.files_created, result.stdout assert "Successfully built simple" in result.stdout, result.stdout @@ -35,11 +35,11 @@ def test_pip_wheel_fail(): """ Test 'pip wheel' failure. """ - env = reset_env() - pip_install_local('wheel') - result = run_pip('wheel', '--no-index', '-f', find_links, 'wheelbroken==0.1') + script = reset_env() + script.pip_install_local('wheel') + result = script.pip('wheel', '--no-index', '-f', find_links, 'wheelbroken==0.1') wheel_file_name = 'wheelbroken-0.1-py%s-none-any.whl' % pyversion_nodot - wheel_file_path = env.scratch/'wheelhouse'/wheel_file_name + wheel_file_path = script.scratch/'wheelhouse'/wheel_file_name assert wheel_file_path not in result.files_created, (wheel_file_path, result.files_created) assert "FakeError" in result.stdout, result.stdout assert "Failed to build wheelbroken" in result.stdout, result.stdout @@ -49,19 +49,19 @@ def test_pip_wheel_ignore_wheels_editables(): """ Test 'pip wheel' ignores editables and *.whl files in requirements """ - env = reset_env() - pip_install_local('wheel') + script = reset_env() + script.pip_install_local('wheel') local_wheel = '%s/simple.dist-0.1-py2.py3-none-any.whl' % find_links local_editable = os.path.abspath(os.path.join(tests_data, 'packages', 'FSPkg')) - write_file('reqs.txt', textwrap.dedent("""\ + script.scratch_path.join("reqs.txt").write(textwrap.dedent("""\ %s -e %s simple """ % (local_wheel, local_editable))) - result = run_pip('wheel', '--no-index', '-f', find_links, '-r', env.scratch_path / 'reqs.txt') + result = script.pip('wheel', '--no-index', '-f', find_links, '-r', script.scratch_path / 'reqs.txt') wheel_file_name = 'simple-3.0-py%s-none-any.whl' % pyversion_nodot - wheel_file_path = env.scratch/'wheelhouse'/wheel_file_name + wheel_file_path = script.scratch/'wheelhouse'/wheel_file_name assert wheel_file_path in result.files_created, (wheel_file_path, result.files_created) assert "Successfully built simple" in result.stdout, result.stdout assert "Failed to build" not in result.stdout, result.stdout @@ -77,10 +77,10 @@ def test_no_clean_option_blocks_cleaning_after_wheel(): """ Test --no-clean option blocks cleaning after wheel build """ - env = reset_env() - pip_install_local('wheel') - result = run_pip('wheel', '--no-clean', '--no-index', '--find-links=%s' % find_links, 'simple') - build = env.venv_path/'build'/'simple' + script = reset_env() + script.pip_install_local('wheel') + result = script.pip('wheel', '--no-clean', '--no-index', '--find-links=%s' % find_links, 'simple') + build = script.venv_path/'build'/'simple' assert exists(build), "build/simple should still exist %s" % str(result) @@ -89,12 +89,10 @@ def test_pip_wheel_source_deps(): Test 'pip wheel --use-wheel' finds and builds source archive dependencies of wheels """ # 'requires_source' is a wheel that depends on the 'source' project - env = reset_env() - pip_install_local('wheel') - result = run_pip('wheel', '--use-wheel', '--no-index', '-f', find_links, 'requires_source') + script = reset_env() + script.pip_install_local('wheel') + result = script.pip('wheel', '--use-wheel', '--no-index', '-f', find_links, 'requires_source') wheel_file_name = 'source-1.0-py%s-none-any.whl' % pyversion_nodot - wheel_file_path = env.scratch/'wheelhouse'/wheel_file_name + wheel_file_path = script.scratch/'wheelhouse'/wheel_file_name assert wheel_file_path in result.files_created, result.stdout assert "Successfully built source" in result.stdout, result.stdout - - diff --git a/tests/lib/__init__.py b/tests/lib/__init__.py index db2913b52..dbbb25478 100644 --- a/tests/lib/__init__.py +++ b/tests/lib/__init__.py @@ -1,33 +1,32 @@ -#!/usr/bin/env python +# #!/usr/bin/env python import imp import os import sys import re -import tempfile -import shutil -import glob import atexit import textwrap import site -import shutil +import subprocess + +import scripttest +import virtualenv -from scripttest import TestFileEnvironment, FoundDir -from tests.lib.path import Path, curdir, u -from pip.util import rmtree from pip.backwardcompat import uses_pycache +from tests.lib.path import Path, curdir, u + + pyversion = sys.version[:3] pyversion_nodot = "%d%d" % (sys.version_info[0], sys.version_info[1]) -tests_lib = Path(__file__).abspath.folder # pip/tests/lib -tests_root = tests_lib.folder # pip/tests -tests_cache = os.path.join(tests_root, 'tests_cache') # pip/tests/tests_cache +tests_lib = Path(__file__).abspath.folder # pip/tests/lib +tests_root = tests_lib.folder # pip/tests +tests_cache = os.path.join(tests_root, 'tests_cache') # pip/tests/tests_cache src_folder = tests_root.folder # pip/ -tests_data = os.path.join(tests_root, 'data') # pip/tests/data -packages = os.path.join(tests_data, 'packages') # pip/tests/data/packages -tests_unit = os.path.join(tests_root, 'unit') # pip/tests/unit -tests_functional = os.path.join(tests_root, 'functional') # pip/tests/functional -download_cache = tempfile.mkdtemp(prefix='pip-test-cache') -site_packages_suffix = site.USER_SITE[len(site.USER_BASE) + 1:] +tests_data = os.path.join(tests_root, 'data') # pip/tests/data +packages = os.path.join(tests_data, 'packages') # pip/tests/data/packages + +fast_test_env_root = tests_cache / 'test_ws' + def path_to_url(path): """ @@ -41,112 +40,81 @@ def path_to_url(path): url = '/'.join(filepath) if drive: return 'file:///' + drive + url - return 'file://' +url + return 'file://' + url find_links = path_to_url(os.path.join(tests_data, 'packages')) find_links2 = path_to_url(os.path.join(tests_data, 'packages2')) -def demand_dirs(path): - if not os.path.exists(path): - os.makedirs(path) - - -# Tweak the path so we can find up-to-date pip sources -# (http://bitbucket.org/ianb/pip/issue/98) -sys.path = [src_folder] + sys.path - - -def create_virtualenv(where): - import virtualenv - virtualenv.create_environment( - where, unzip_setuptools=True) - - return virtualenv.path_locations(where) - - -def relpath(root, other): - """a poor man's os.path.relpath, since we may not have Python 2.6""" - prefix = root+Path.sep - assert other.startswith(prefix) - return Path(other[len(prefix):]) - -if 'PYTHONPATH' in os.environ: - del os.environ['PYTHONPATH'] - - -try: - any -except NameError: - - def any(seq): - for item in seq: - if item: - return True - return False - def clear_environ(environ): return dict(((k, v) for k, v in environ.items() if not k.lower().startswith('pip_'))) -env = None -def reset_env(environ=None, - system_site_packages=False, - sitecustomize=None, - pypi_cache=True, - insecure=True): - """Return a test environment. +def reset_env(environ=None, system_site_packages=False): + """ + Return a test environment. Keyword arguments: environ: an environ object to use. - system_site_packages: create a virtualenv that simulates --system-site-packages. - pypi_cache: use pip's internal pypi cache - sitecustomize: a string containing python code to add to sitecustomize.py. - insecure: how to set the --insecure option for py25 tests. + system_site_packages: create a virtualenv that simulates + --system-site-packages. """ + # Clear our previous test directory + fast_test_env_root.rmtree() - global env + # Create a virtual environment + venv_root = fast_test_env_root.join(".virtualenv") + virtualenv.create_environment(venv_root, + clear=True, + never_download=True, + no_pip=True, + ) - env = TestPipEnvironment(environ, sitecustomize=sitecustomize, pypi_cache=pypi_cache) - TestPipEnvironment.rebuild_venv = False + # On Python < 3.3 we don't have subprocess.DEVNULL + try: + devnull = subprocess.DEVNULL + except AttributeError: + devnull = open(os.devnull, "wb") + + # Install our development version of pip install the virtual environment + p = subprocess.Popen( + [venv_root.join("bin/python"), "setup.py", "develop"], + stderr=subprocess.STDOUT, + stdout=devnull, + ) + p.communicate() + + if p.returncode != 0: + raise subprocess.CalledProcessError(p.returncode, p.args) + + # Create our pip test environment + env = TestPipEnvironment(fast_test_env_root, + environ=environ, + virtualenv=venv_root, + ignore_hidden=False, + start_clear=False, + capture_temp=True, + assert_no_temp=True, + ) 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() - TestPipEnvironment.rebuild_venv = True + # Testing often occurs starting from a private virtualenv (e.g. 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.join("no-global-site-packages.txt").rm() return env class TestFailure(AssertionError): """ - An "assertion" failed during testing. - """ pass -# -# This cleanup routine prevents the __del__ method that cleans up the tree of -# the last TestPipEnvironment from firing after shutil has already been -# unloaded. It also ensures that FastTestPipEnvironment doesn't leave an -# environment hanging around that might confuse the next test run. -# -def _cleanup(): - global env - del env - rmtree(download_cache, ignore_errors=True) - rmtree(fast_test_env_root, ignore_errors=True) - rmtree(fast_test_env_backup, ignore_errors=True) - -atexit.register(_cleanup) - - class TestPipResult(object): def __init__(self, impl, verbose=False): @@ -180,19 +148,22 @@ class TestPipResult(object): def __str__(self): return str(self._impl) - def assert_installed(self, pkg_name, editable=True, with_files=[], without_files=[], without_egg_link=False, use_user_site=False): + def assert_installed(self, pkg_name, editable=True, with_files=[], + without_files=[], without_egg_link=False, + use_user_site=False): e = self.test_env if editable: - pkg_dir = e.venv/ 'src'/ pkg_name.lower() + pkg_dir = e.venv/'src'/pkg_name.lower() else: without_egg_link = True - pkg_dir = e.site_packages / pkg_name + pkg_dir = e.site_packages/pkg_name if use_user_site: egg_link_path = e.user_site / pkg_name + '.egg-link' else: egg_link_path = e.site_packages / pkg_name + '.egg-link' + if without_egg_link: if egg_link_path in self.files_created: raise TestFailure('unexpected egg link file created: '\ @@ -239,7 +210,7 @@ class TestPipResult(object): for f in with_files: if not (pkg_dir/f).normpath in self.files_created: raise TestFailure('Package directory %r missing '\ - 'expected content %f' % (pkg_dir, f)) + 'expected content %r' % (pkg_dir, f)) for f in without_files: if (pkg_dir/f).normpath in self.files_created: @@ -247,12 +218,10 @@ class TestPipResult(object): 'unexpected content %f' % (pkg_dir, f)) -fast_test_env_root = tests_cache / 'test_ws' -fast_test_env_backup = tests_cache / 'test_ws_backup' - - -class TestPipEnvironment(TestFileEnvironment): - """A specialized TestFileEnvironment for testing pip""" +class TestPipEnvironment(scripttest.TestFileEnvironment): + """ + A specialized TestFileEnvironment for testing pip + """ # # Attribute naming convention @@ -263,137 +232,66 @@ class TestPipEnvironment(TestFileEnvironment): # a name of the form xxxx_path and relative paths have a name that # does not end in '_path'. - # The following paths are relative to the root_path, and should be - # treated by clients as instance attributes. The fact that they - # are defined in the class is an implementation detail - - # where we'll create the virtual Python installation for testing - # - # Named with a leading dot to reduce the chance of spurious - # results due to being mistaken for the virtualenv package. - venv = Path('.virtualenv') - - # The root of a directory tree to be used arbitrarily by tests - scratch = Path('scratch') - exe = sys.platform == 'win32' and '.exe' or '' verbose = False - rebuild_venv = True - def __init__(self, environ=None, sitecustomize=None, pypi_cache=True): - import virtualenv + def __init__(self, base_path, *args, **kwargs): + # Make our base_path a test.lib.path.Path object + base_path = Path(base_path) - self.root_path = fast_test_env_root - self.backup_path = fast_test_env_backup - - self.scratch_path = self.root_path / self.scratch - - # We will set up a virtual environment at root_path. - self.venv_path = self.root_path / self.venv - - if not environ: - environ = os.environ.copy() - environ = clear_environ(environ) - environ['PIP_DOWNLOAD_CACHE'] = str(download_cache) - - environ['PIP_NO_INPUT'] = '1' - environ['PIP_LOG_FILE'] = str(self.root_path/'pip-log.txt') - - TestFileEnvironment.__init__(self, - self.root_path, ignore_hidden=False, - environ=environ, split_cmd=False, start_clear=False, - cwd=self.scratch_path, capture_temp=True, assert_no_temp=True) - - virtualenv_paths = virtualenv.path_locations(self.venv_path) - - for id, path in zip(('venv', 'lib', 'include', 'bin'), virtualenv_paths): - #fix for virtualenv issue #306 - if hasattr(sys, "pypy_version_info") and id == 'lib': - path = os.path.join(self.venv_path, 'lib-python', pyversion) - setattr(self, id+'_path', Path(path)) - setattr(self, id, relpath(self.root_path, path)) - - assert self.venv == TestPipEnvironment.venv # sanity check + # Store paths related to the virtual environment + _virtualenv = kwargs.pop("virtualenv") + venv, lib, include, bin = virtualenv.path_locations(_virtualenv) + self.venv_path = venv + self.lib_path = lib + self.include_path = include + self.bin_path = bin if hasattr(sys, "pypy_version_info"): - self.site_packages = self.venv/'site-packages' + self.site_packages_path = self.venv_path.join("site-packages") else: - self.site_packages = self.lib/'site-packages' - self.user_base_path = self.venv_path/'user' - self.user_site_path = self.venv_path/'user'/site_packages_suffix + self.site_packages_path = self.lib_path.join("site-packages") - self.user_site = relpath(self.root_path, self.user_site_path) + self.user_base_path = self.venv_path.join("user") + self.user_site_path = self.venv_path.join( + "user", + site.USER_SITE[len(site.USER_BASE) + 1:], + ) - self.environ["PYTHONUSERBASE"] = self.user_base_path + # Create a Directory to use as a scratch pad + self.scratch_path = base_path.join("scratch").mkdir() - # put the test-scratch virtualenv's bin dir first on the PATH - self.environ['PATH'] = Path.pathsep.join((self.bin_path, self.environ['PATH'])) + # Set our default working directory + kwargs.setdefault("cwd", self.scratch_path) - if self.root_path.exists: - # There's something strange going on here, Python 3.3 introduced - # a new version of shutil.rmtree which is safe against symlink - # attacks causing arbitrary things from being deleted. However - # this seemingly fails on Travis for unknown reasons. The problem - # seems to stem from os.path.samestat(orig_st, os.fstat(dirfd)) - # returning False even when the directory is *not* a symlink. By - # switching to shutil._rmtree_unsafe we use a version of rmtree - # that is slightly vulnerable to a race condition that shouldn't - # matter for our uses. - def _onerror(*args, **kwargs): - raise - _rmtree = getattr(shutil, "_rmtree_unsafe", shutil.rmtree) - _rmtree(self.root_path, onerror=_onerror) - if self.backup_path.exists and not self.rebuild_venv: - shutil.copytree(self.backup_path, self.root_path, True) - else: - demand_dirs(self.venv_path) - demand_dirs(self.scratch_path) + # Setup our environment + environ = kwargs.get("environ") + if environ is None: + environ = os.environ.copy() - # Create a virtualenv and remember where it's putting things. - create_virtualenv(self.venv_path) + environ["PIP_LOG_FILE"] = base_path.join("pip-log.txt") + environ["PATH"] = Path.pathsep.join( + [self.bin_path] + [environ.get("PATH", [])], + ) + environ["PYTHONUSERBASE"] = self.user_base_path + kwargs["environ"] = environ - demand_dirs(self.user_site_path) + # Call the TestFileEnvironment __init__ + super(TestPipEnvironment, self).__init__(base_path, *args, **kwargs) - # create easy-install.pth in user_site, so we always have it updated instead of created - open(self.user_site_path/'easy-install.pth', 'w').close() + # Expand our absolute path directories into relative + for name in ["base", "venv", "lib", "include", "bin", "site_packages", + "user_base", "user_site", "scratch"]: + real_name = "%s_path" % name + setattr(self, name, getattr(self, real_name) - self.base_path) - # test that test-scratch virtualenv creation produced sensible venv python - result = self.run('python', '-c', 'import sys; print(sys.executable)') - pythonbin = result.stdout.strip() - - if Path(pythonbin).noext != self.bin_path/'python': - raise RuntimeError( - "Oops! 'python' in our test environment runs %r" - " rather than expected %r" % (pythonbin, self.bin_path/'python')) - - # Uninstall whatever version of pip came with the virtualenv. - # Earlier versions of pip were incapable of - # self-uninstallation on Windows, so we use the one we're testing. - self.run('python', '-c', - '"import sys; sys.path.insert(0, %r); import pip; sys.exit(pip.main());"' % src_folder, - 'uninstall', '-vvv', '-y', 'pip') - - # Install this version instead - self.run('python', 'setup.py', 'install', cwd=src_folder, expect_stderr=True) - - # make the backup (remove previous backup if exists) - if self.backup_path.exists: - rmtree(self.backup_path) - shutil.copytree(self.root_path, self.backup_path, True) - - #create sitecustomize.py and add patches - self._create_empty_sitecustomize() - if pypi_cache: - self._use_cached_pypi_server() - if sitecustomize: - self._add_to_sitecustomize(sitecustomize) - - assert self.root_path.exists - - # Ensure that $TMPDIR exists (because we use start_clear=False, it's not created for us) - if self.temp_path and not os.path.exists(self.temp_path): - os.makedirs(self.temp_path) + # Ensure the tmp dir exists + self.temp_path.mkdir() + # create easy-install.pth in user_site, so we always have it updated + # instead of created + self.user_site_path.makedirs() + self.user_site_path.join("easy-install.pth").touch() def _ignore_file(self, fn): if fn.endswith('__pycache__') or fn.endswith(".pyc"): @@ -411,82 +309,13 @@ class TestPipEnvironment(TestFileEnvironment): cwd = cwd or run_from or self.cwd return TestPipResult(super(TestPipEnvironment, self).run(cwd=cwd, *args, **kw), verbose=self.verbose) - def _use_cached_pypi_server(self): - # 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. + def pip(self, *args, **kwargs): + return self.run("pip", *args, **kwargs) - patch = """ - import sys - sys.path.insert(0, %r) - import pypi_server - pypi_server.PyPIProxy.setup() - sys.path.remove(%r)""" % (str(tests_lib), str(tests_lib)) - 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.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() - # caught py32 with an outdated __pycache__ file after a sitecustomize update (after python should have updated it) - # https://github.com/pypa/pip/pull/893#issuecomment-16426701 - # will delete the cache file to be sure - if uses_pycache: - cache_path = imp.cache_from_source(sitecustomize_path) - if os.path.isfile(cache_path): - os.remove(cache_path) - - -def run_pip(*args, **kw): - result = env.run('pip', *args, **kw) - ignore = [] - for path, f in result.files_before.items(): - # ignore updated directories, often due to .pyc or __pycache__ - if (path in result.files_updated and - isinstance(result.files_updated[path], FoundDir)): - ignore.append(path) - for path in ignore: - del result.files_updated[path] - return result - -def pip_install_local(*args, **kw): - """Run 'pip install' using --find-links against our local test packages""" - return run_pip('install', '--no-index', '--find-links=%s' % find_links, *args, **kw) - -def write_file(filename, text, dest=None): - """Write a file in the dest (default=env.scratch_path) - - """ - env = get_env() - if dest: - complete_path = dest/ filename - else: - complete_path = env.scratch_path/ filename - f = open(complete_path, 'w') - f.write(text) - f.close() - - -def mkdir(dirname): - os.mkdir(os.path.join(get_env().scratch_path, dirname)) - - -def get_env(): - if env is None: - reset_env() - return env + def pip_install_local(self, *args, **kwargs): + return self.pip("install", "--no-index", "--find-links", find_links, + *args, **kwargs + ) # FIXME ScriptTest does something similar, but only within a single @@ -566,35 +395,40 @@ def assert_all_changes(start_state, end_state, expected_changes): return diff -def _create_test_package(env): - mkdir('version_pkg') - version_pkg_path = env.scratch_path/'version_pkg' - write_file('version_pkg.py', textwrap.dedent('''\ - def main(): - print('0.1') - '''), version_pkg_path) - write_file('setup.py', textwrap.dedent('''\ - from setuptools import setup, find_packages - setup(name='version_pkg', - version='0.1', - packages=find_packages(), - py_modules=['version_pkg'], - entry_points=dict(console_scripts=['version_pkg=version_pkg:main'])) - '''), version_pkg_path) - env.run('git', 'init', cwd=version_pkg_path) - env.run('git', 'add', '.', cwd=version_pkg_path) - env.run('git', 'commit', '-q', +def _create_test_package(script): + script.scratch_path.join("version_pkg").mkdir() + version_pkg_path = script.scratch_path/'version_pkg' + version_pkg_path.join("version_pkg.py").write(textwrap.dedent(""" + def main(): + print('0.1') + """)) + version_pkg_path.join("setup.py").write(textwrap.dedent(""" + from setuptools import setup, find_packages + setup( + name='version_pkg', + version='0.1', + packages=find_packages(), + py_modules=['version_pkg'], + entry_points=dict(console_scripts=['version_pkg=version_pkg:main']) + ) + """)) + script.run('git', 'init', cwd=version_pkg_path) + script.run('git', 'add', '.', cwd=version_pkg_path) + script.run('git', 'commit', '-q', '--author', 'Pip ', '-am', 'initial version', cwd=version_pkg_path) return version_pkg_path -def _change_test_package_version(env, version_pkg_path): - write_file('version_pkg.py', textwrap.dedent('''\ +def _change_test_package_version(script, version_pkg_path): + version_pkg_path.join("version_pkg.py").write(textwrap.dedent('''\ def main(): - print("some different version")'''), version_pkg_path) - env.run('git', 'clean', '-qfdx', cwd=version_pkg_path, expect_stderr=True) - env.run('git', 'commit', '-q', + print("some different version")''')) + script.run('git', 'clean', '-qfdx', + cwd=version_pkg_path, + expect_stderr=True, + ) + script.run('git', 'commit', '-q', '--author', 'Pip ', '-am', 'messed version', cwd=version_pkg_path, expect_stderr=True) @@ -604,13 +438,17 @@ def assert_raises_regexp(exception, reg, run, *args, **kwargs): """Like assertRaisesRegexp in unittest""" try: run(*args, **kwargs) - assert False, "%s should have been thrown" %exception + assert False, "%s should have been thrown" % exception except Exception: e = sys.exc_info()[1] p = re.compile(reg) assert p.search(str(e)), str(e) -if __name__ == '__main__': - sys.stderr.write("Run pip's tests using py.test. Requires virtualenv, ScriptTest, mock, and pytest.\n") - sys.exit(1) +# +# This cleanup routine prevents the __del__ method that cleans up the tree of +# the last TestPipEnvironment from firing after shutil has already been +# unloaded. It also ensures that FastTestPipEnvironment doesn't leave an +# environment hanging around that might confuse the next test run. +# +atexit.register(fast_test_env_root.rmtree) diff --git a/tests/lib/git_submodule_helpers.py b/tests/lib/git_submodule_helpers.py index 5f3a0745e..1db62a665 100644 --- a/tests/lib/git_submodule_helpers.py +++ b/tests/lib/git_submodule_helpers.py @@ -1,8 +1,8 @@ import textwrap -from tests.lib import (mkdir, write_file,) + def _create_test_package_submodule(env): - mkdir('version_pkg_submodule') + env.scratch_path.join("version_pkg_submodule").mkdir() submodule_path = env.scratch_path/'version_pkg_submodule' env.run('touch', 'testfile', cwd=submodule_path) env.run('git', 'init', cwd=submodule_path) @@ -13,8 +13,8 @@ def _create_test_package_submodule(env): return submodule_path def _change_test_package_submodule(env, submodule_path): - write_file(submodule_path/'testfile', 'this is a changed file') - write_file(submodule_path/'testfile2', 'this is an added file') + submodule_path.join("testfile").write("this is a changed file") + submodule_path.join("testfile2").write("this is an added file") env.run('git', 'add', '.', cwd=submodule_path) env.run('git', 'commit', '-q', '--author', 'Pip ', @@ -27,23 +27,23 @@ def _pull_in_submodule_changes_to_module(env, module_path): '-am', 'submodule change', cwd=module_path) def _create_test_package_with_submodule(env): - mkdir('version_pkg') + env.scratch_path.join("version_pkg").mkdir() version_pkg_path = env.scratch_path/'version_pkg' - mkdir(version_pkg_path/'testpkg') + version_pkg_path.join("testpkg").mkdir() pkg_path = version_pkg_path/'testpkg' - write_file('__init__.py', '# hello there', pkg_path) - write_file('version_pkg.py', textwrap.dedent('''\ + pkg_path.join("__init__.py").write("# hello there") + pkg_path.join("version_pkg.py").write(textwrap.dedent('''\ def main(): print('0.1') - '''), pkg_path) - write_file('setup.py', textwrap.dedent('''\ + ''')) + version_pkg_path.join("setup.py").write(textwrap.dedent('''\ from setuptools import setup, find_packages setup(name='version_pkg', version='0.1', packages=find_packages(), ) - '''), version_pkg_path) + ''')) env.run('git', 'init', cwd=version_pkg_path, expect_error=True) env.run('git', 'add', '.', cwd=version_pkg_path, expect_error=True) env.run('git', 'commit', '-q', diff --git a/tests/lib/local_repos.py b/tests/lib/local_repos.py index d9c029d69..c9e2e6b10 100644 --- a/tests/lib/local_repos.py +++ b/tests/lib/local_repos.py @@ -3,7 +3,6 @@ import subprocess from pip.vcs import subversion, git, bazaar, mercurial from pip.backwardcompat import urlretrieve from tests.lib import path_to_url -from tests.lib.pypi_server import PyPIProxy if hasattr(subprocess, "check_call"): @@ -12,6 +11,9 @@ else: subprocess_call = subprocess.call +CACHE_PATH = os.path.join(os.path.dirname(os.path.dirname(__file__)), 'tests_cache') + + def _create_initools_repository(): subprocess_call('svnadmin create INITools'.split(), cwd=_get_vcs_folder()) @@ -35,10 +37,9 @@ def _create_svn_repository_for_initools(): def _get_vcs_folder(): - folder_name = PyPIProxy.CACHE_PATH - if not os.path.exists(folder_name): - os.mkdir(folder_name) - return folder_name + if not os.path.exists(CACHE_PATH): + os.mkdir(CACHE_PATH) + return CACHE_PATH def _get_vcs_and_checkout_url(remote_repository): diff --git a/tests/lib/pypi_server.py b/tests/lib/pypi_server.py deleted file mode 100644 index 04e5a8e5c..000000000 --- a/tests/lib/pypi_server.py +++ /dev/null @@ -1,138 +0,0 @@ -import os -import sys -import pip.backwardcompat -from pip.backwardcompat import urllib, string_types, b, u, emailmessage - -urlopen_original = pip.backwardcompat.urllib2.urlopen - - -class CachedResponse(object): - """ - CachedResponse always cache url access and returns the cached response. - It returns an object compatible with ``urllib.addinfourl``, - it means the object is like the result of a call like:: - - >>> response = urllib2.urlopen('http://example.com') - """ - - def __init__(self, url, folder): - self.headers = emailmessage.Message() - - # patch due to setuptools>=0.7 header processing - # easy_install fails w/o this on windows/py2 - # https://github.com/pypa/pip/issues/946#issuecomment-20860320 - if sys.version_info < (3,): - def getheaders(key): - return self.headers.get_all(key) - self.headers.getheaders = getheaders - - self.code = 500 - self.msg = 'Internal Server Error' - # url can be a simple string, or a urllib2.Request object - if isinstance(url, string_types): - self.url = url - else: - self.url = url.get_full_url() - for key, value in url.headers.items(): - self.headers[key] = value - self._body = b('') - self._set_all_fields(folder) - - def _set_all_fields(self, folder): - filename = os.path.join(folder, urllib.quote(self.url, '')) - if not os.path.exists(filename): - self._cache_url(filename) - fp = open(filename, 'rb') - try: - line = fp.readline().strip() - self.code, self.msg = line.split(None, 1) - except ValueError: - raise ValueError('Bad field line: %r' % line) - self.code = int(self.code) - self.msg = u(self.msg) - for line in fp: - if line == b('\n'): - break - key, value = line.split(b(': '), 1) - self.headers[u(key)] = u(value.strip()) - for line in fp: - self._body += line - fp.close() - - def getcode(self): - return self.code - - def geturl(self): - return self.url - - def info(self): - return self.headers - - def read(self, bytes=None): - """ - it can read a chunk of bytes or everything - """ - if bytes: - result = self._body[:bytes] - self._body = self._body[bytes:] - return result - return self._body - - def close(self): - pass - - def _cache_url(self, filepath): - response = urlopen_original(self.url) - fp = open(filepath, 'wb') - # when it uses file:// scheme, code is None and there is no msg attr - # but it has been successfully opened - status = b('%s %s' % (getattr(response, 'code', 200) or 200, getattr(response, 'msg', 'OK'))) - headers = [b('%s: %s' % (key, value)) for key, value in list(response.headers.items())] - body = response.read() - fp.write(b('\n').join([status] + headers + [b(''), body])) - fp.close() - - -class PyPIProxy(object): - - CACHE_PATH = os.path.join(os.path.dirname(os.path.dirname(__file__)), 'tests_cache') - - @classmethod - def setup(cls): - instance = cls() - instance._create_cache_folder() - instance._monkey_patch_urllib2_to_cache_everything() - - def _monkey_patch_urllib2_to_cache_everything(self): - def urlopen(url): - return CachedResponse(url, self.CACHE_PATH) - pip.backwardcompat.urllib2.urlopen = urlopen - - def _create_cache_folder(self): - if not os.path.exists(self.CACHE_PATH): - os.mkdir(self.CACHE_PATH) - - -def assert_equal(a, b): - assert a == b, "\nexpected:\n%r\ngot:\n%r" % (b, a) - - -def test_cache_proxy(): - url = 'http://example.com' - here = os.path.dirname(os.path.abspath(__file__)) - filepath = os.path.join(here, urllib.quote(url, '')) - if os.path.exists(filepath): - os.remove(filepath) - response = pip.backwardcompat.urllib2.urlopen(url) - r = CachedResponse(url, here) - try: - assert_equal(r.code, response.code) - assert_equal(r.msg, response.msg) - assert_equal(r.read(), response.read()) - assert_equal(r.url, response.url) - assert_equal(r.geturl(), response.geturl()) - assert_equal(set(r.headers.keys()), set(response.headers.keys())) - assert_equal(set(r.info().keys()), set(response.info().keys())) - assert_equal(r.headers['content-length'], response.headers['content-length']) - finally: - os.remove(filepath) diff --git a/tests/lib/test_lib.py b/tests/lib/test_lib.py index d2dff5703..96ac4a7f3 100644 --- a/tests/lib/test_lib.py +++ b/tests/lib/test_lib.py @@ -1,109 +1,9 @@ """Test the test support.""" import filecmp -import imp -import os import re -import sys from os.path import join, isdir -from pip.backwardcompat import uses_pycache -from tests.lib import tests_lib, reset_env, run_pip, src_folder - - -patch_urlopen = """ - def mock_isdir(d): - pass - import os - os.path.isdir = mock_isdir - """ - -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 tests_lib not in paths, paths - - -class Test_reset_env: - - def setup(self): - - # create a TestPipEnvironment env and add a file to the backup - self.env = reset_env() - self.test_file = self.env.backup_path / self.env.venv / 'test_file' - f = open(self.test_file, 'w') - f.close() - - def teardown(self): - if os.path.isfile(self.test_file): - self.test_file.rm() - - def test_cache_venv(self): - """ - Test reset_env cache's internal virtualenv - """ - env = reset_env() - assert os.path.isfile(self.test_file) - - - def test_reset_env_system_site_packages(self): - """ - Test using system_site_packages with reset_env resets the venv cache - """ - env = reset_env(system_site_packages=True) - env = reset_env() - assert not os.path.isfile(self.env.backup_path / self.env.venv / 'test_file') - - -def test_add_patch_to_sitecustomize(): - """ - Test adding monkey patch snippet to sitecustomize.py (using TestPipEnvironment) - """ - - env = reset_env(sitecustomize=patch_urlopen) - - if uses_pycache: - # caught py32 with an outdated __pycache__ file after a sitecustomize update (after python should have updated it) - # https://github.com/pypa/pip/pull/893#issuecomment-16426701 - # now we delete the cache file to be sure in TestPipEnvironment._add_to_sitecustomize - # it should not exist after creating the env - cache_path = imp.cache_from_source(env.lib_path / 'sitecustomize.py') - assert not os.path.isfile(cache_path) - - debug_content = open(env.lib_path / 'sitecustomize.py').read() - result = env.run('python', '-c', "import os; print(os.path.isdir.__module__)") - if uses_pycache: - # if this next assert fails, let's have the modified time to look at - cache_path = imp.cache_from_source(env.lib_path / 'sitecustomize.py') - src_mtime = os.stat(env.lib_path / 'sitecustomize.py').st_mtime - cache_mtime = os.stat(cache_path).st_mtime - debug_content += "src mtime: %s, cache mtime: %s" % (src_mtime, cache_mtime) - 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) +from tests.lib import reset_env, src_folder def test_tmp_dir_exists_in_env(): @@ -121,11 +21,11 @@ def test_correct_pip_version(): """ Check we are running proper version of pip in run_pip. """ - reset_env() + script = reset_env() # output is like: # pip PIPVERSION from PIPDIRECTORY (python PYVERSION) - result = run_pip('--version') + result = script.pip('--version') # compare the directory tree of the invoked pip with that of this source distribution dir = re.match(r'pip \d(\.[\d])+(\.?(rc|dev|pre|post)\d+)? from (.*) \(python \d(.[\d])+\)$', diff --git a/tests/unit/test_index.py b/tests/unit/test_index.py index 9de26b1fc..47ac05395 100644 --- a/tests/unit/test_index.py +++ b/tests/unit/test_index.py @@ -3,7 +3,7 @@ from pip.backwardcompat import urllib from tests.lib.path import Path from pip.index import package_to_requirement, HTMLPage from pip.index import PackageFinder, Link, InfLink -from tests.lib import reset_env, run_pip, pyversion, tests_data, path_to_url, find_links +from tests.lib import tests_data, path_to_url, find_links from string import ascii_lowercase from mock import patch diff --git a/tests/unit/test_util.py b/tests/unit/test_util.py index 736e06087..b2c73abe9 100644 --- a/tests/unit/test_util.py +++ b/tests/unit/test_util.py @@ -12,9 +12,9 @@ import pytest from mock import Mock, patch from pip.exceptions import BadCommand -from pip.util import (egg_link_path, Inf, get_installed_distributions, find_command, - untar_file, unzip_file) -from tests.lib import reset_env, mkdir, write_file, tests_data +from pip.util import (egg_link_path, Inf, get_installed_distributions, + find_command, untar_file, unzip_file) +from tests.lib import reset_env, tests_data class Tests_EgglinkPath: @@ -209,13 +209,13 @@ def test_find_command_folder_in_path(): the 'git' executable, it should not match the folder, but rather keep looking. """ - env = reset_env() - mkdir('path_one') - path_one = env.scratch_path/'path_one' - mkdir(path_one/'foo') - mkdir('path_two') - path_two = env.scratch_path/'path_two' - write_file(path_two/'foo', '# nothing') + script = reset_env() + script.scratch_path.join("path_one").mkdir() + path_one = script.scratch_path/'path_one' + path_one.join("foo").mkdir() + script.scratch_path.join("path_two").mkdir() + path_two = script.scratch_path/'path_two' + path_two.join("foo").write("# nothing") found_path = find_command('foo', map(str, [path_one, path_two])) assert found_path == path_two/'foo'