def tmpdir(request, tmpdir): """ Return a temporary directory path object which is unique to each test function invocation, created as a sub directory of the base temporary directory. The returned object is a ``tests.lib.path.Path`` object. This uses the built-in tmpdir fixture from pytest itself but modified to return our typical path object instead of py.path.local as well as deleting the temporary directories at the end of each test case. """ assert tmpdir.isdir() yield Path(str(tmpdir)) # Clear out the temporary directory after the test has finished using it. # This should prevent us from needing a multiple gigabyte temporary # directory while running the tests. if not request.config.getoption("--keep-tmpdir"): # py.path.remove() uses str paths on Python 2 and cannot # handle non-ASCII file names. This works around the problem by # passing a unicode object to rmtree(). shutil.rmtree(str(tmpdir), ignore_errors=True)
def tmpdir(request): """ Return a temporary directory path object which is unique to each test function invocation, created as a sub directory of the base temporary directory. The returned object is a ``tests.lib.path.Path`` object. This is taken from pytest itself but modified to return our typical path object instead of py.path.local as well as deleting the temporary directories at the end of each test case. """ name = request.node.name name = py.std.re.sub("[\W]", "_", name) tmp = request.config._tmpdirhandler.mktemp(name, numbered=True) # Clear out the temporary directory after the test has finished using it. # This should prevent us from needing a multiple gigabyte temporary # directory while running the tests. request.addfinalizer(lambda: shutil.rmtree(str(tmp), ignore_errors=True)) return Path(str(tmp))
def test_installed_files_recorded_in_deterministic_order(script, data): """ Ensure that we record the files installed by a package in a deterministic order, to make installs reproducible. """ to_install = data.packages.join("FSPkg") result = script.pip('install', to_install, expect_error=False) fspkg_folder = script.site_packages / 'fspkg' egg_info = 'FSPkg-0.1.dev0-py%s.egg-info' % pyversion installed_files_path = ( script.site_packages / egg_info / 'installed-files.txt' ) assert fspkg_folder in result.files_created, str(result.stdout) assert installed_files_path in result.files_created, str(result) installed_files_path = result.files_created[installed_files_path].full installed_files_lines = [ p for p in Path(installed_files_path).read_text().split('\n') if p ] assert installed_files_lines == sorted(installed_files_lines)
def test_install_wheel_with_prefix(script: PipTestEnvironment, shared_data: TestData, tmpdir: Path) -> None: """ Test installing a wheel using pip install --prefix """ prefix_dir = script.scratch_path / "prefix" shutil.copy(shared_data.packages / "simple.dist-0.1-py2.py3-none-any.whl", tmpdir) result = script.pip( "install", "simple.dist==0.1", "--prefix", prefix_dir, "--no-index", "--find-links", tmpdir, ) lib = distutils.sysconfig.get_python_lib(prefix=Path("scratch") / "prefix") result.did_create(lib)
def test_download_universal(self, platform: str, script: PipTestEnvironment, data: TestData) -> None: """ Universal wheels are returned even for specific platforms. """ fake_wheel(data, "fake-1.0-py2.py3-none-any.whl") result = script.pip( "download", "--no-index", "--find-links", data.find_links, "--only-binary=:all:", "--dest", ".", "--platform", platform, "fake", ) result.did_create(Path("scratch") / "fake-1.0-py2.py3-none-any.whl")
def test_relative_requirements_file(script, data): """ Test installing from a requirements file with a relative path. For path URLs, use an egg= definition. """ egg_info_file = ( script.site_packages / 'FSPkg-0.1.dev0-py%s.egg-info' % pyversion ) egg_link_file = ( script.site_packages / 'FSPkg.egg-link' ) package_folder = script.site_packages / 'fspkg' # Compute relative install path to FSPkg from scratch path. full_rel_path = Path( os.path.relpath(data.packages.joinpath('FSPkg'), script.scratch_path) ) full_rel_url = 'file:' + full_rel_path + '#egg=FSPkg' embedded_rel_path = script.scratch_path.joinpath(full_rel_path) # For each relative path, install as either editable or not using either # URLs with egg links or not. for req_path in (full_rel_path, full_rel_url, embedded_rel_path): req_path = req_path.replace(os.path.sep, '/') # Regular install. with requirements_file(req_path + '\n', script.scratch_path) as reqs_file: result = script.pip('install', '-vvv', '-r', reqs_file.name, cwd=script.scratch_path) assert egg_info_file in result.files_created, str(result) assert package_folder in result.files_created, str(result) script.pip('uninstall', '-y', 'fspkg') # Editable install. with requirements_file('-e ' + req_path + '\n', script.scratch_path) as reqs_file: result = script.pip('install', '-vvv', '-r', reqs_file.name, cwd=script.scratch_path) assert egg_link_file in result.files_created, str(result) script.pip('uninstall', '-y', 'fspkg')
def pip_src(tmpdir_factory): def not_code_files_and_folders(path, names): # In the root directory, ignore all folders except "src" if path == SRC_DIR: folders = {name for name in names if os.path.isdir(path / name)} return folders - {"src"} # Ignore all compiled files and egg-info. ignored = list() for pattern in ["__pycache__", "*.pyc", "pip.egg-info"]: ignored.extend(fnmatch.filter(names, pattern)) return set(ignored) pip_src = Path(str(tmpdir_factory.mktemp('pip_src'))).join('pip_src') # Copy over our source tree so that each use is self contained shutil.copytree( SRC_DIR, pip_src.abspath, ignore=not_code_files_and_folders, ) return pip_src
def pip_src(tmpdir_factory): pip_src = Path(str(tmpdir_factory.mktemp('pip_src'))).join('pip_src') # Copy over our source tree so that each use is self contained shutil.copytree( SRC_DIR, pip_src.abspath, ignore=shutil.ignore_patterns( "*.pyc", "__pycache__", "contrib", "docs", "tasks", "tests", "pip.egg-info", "build", "dist", ".tox", ".git", ), ) return pip_src
def test_no_deps_set_then_download_specific_platform( script: PipTestEnvironment, data: TestData) -> None: """ Confirm that specifying an interpreter/platform constraint is allowed when ``--no-deps`` is set. """ fake_wheel(data, "fake-1.0-py2.py3-none-any.whl") result = script.pip( "download", "--no-index", "--find-links", data.find_links, "--no-deps", "--dest", ".", "--platform", "linux_x86_64", "fake", ) result.did_create(Path("scratch") / "fake-1.0-py2.py3-none-any.whl")
def test_uninstall_console_scripts(script: PipTestEnvironment) -> None: """ Test uninstalling a package with more files (console_script entry points, extra directories). """ pkg_path = create_test_package_with_setup( script, name="discover", version="0.1", entry_points={"console_scripts": ["discover = discover:main"]}, ) result = script.pip("install", pkg_path) result.did_create(script.bin / "discover" + script.exe) result2 = script.pip("uninstall", "discover", "-y") assert_all_changes( result, result2, [ script.venv / "build", "cache", Path("scratch") / "discover" / "discover.egg-info", ], )
def test_download_ignore_requires_python_dont_fail_with_wrong_python( script: PipTestEnvironment, ) -> None: """ Test that --ignore-requires-python ignores Requires-Python check. """ wheel_path = make_wheel_with_python_requires( script, "mypackage", python_requires="==999", ) wheel_dir = os.path.dirname(wheel_path) result = script.pip( "download", "--ignore-requires-python", "--no-index", "--find-links", wheel_dir, "--only-binary=:all:", "--dest", ".", "mypackage==1.0", ) result.did_create(Path("scratch") / "mypackage-1.0-py2.py3-none-any.whl")
def test_new_resolver_handles_compatible_wheel_tags_in_constraint_url( script, make_fake_wheel): initial_path = make_fake_wheel("base", "0.1.0", "fakepy1-fakeabi-fakeplat") constrained = script.scratch_path / "constrained" constrained.mkdir() final_path = constrained / initial_path.name initial_path.rename(final_path) constraints_file = script.scratch_path / "constraints.txt" constraints_file.write_text("base @ " + path_to_url(final_path)) result = script.pip( "install", "--implementation", "fakepy", '--only-binary=:all:', "--python-version", "1", "--abi", "fakeabi", "--platform", "fakeplat", "--target", script.scratch_path / "target", "--no-cache-dir", "--no-index", "-c", constraints_file, "base", ) dist_info = Path("scratch", "target", "base-0.1.0.dist-info") result.did_create(dist_info)
def test_self_check_state_reads_expected_statefile(monkeypatch, tmpdir): cache_dir = tmpdir / "cache_dir" cache_dir.mkdir() key = "helloworld" statefile_path = _get_statefile_path(str(cache_dir), key) last_check = "1970-01-02T11:00:00Z" pypi_version = "1.0" content = { "key": key, "last_check": last_check, "pypi_version": pypi_version, } Path(statefile_path).parent.mkdir() with open(statefile_path, "w") as f: json.dump(content, f) monkeypatch.setattr(sys, "prefix", key) state = self_outdated_check.SelfCheckState(str(cache_dir)) assert state.state["last_check"] == last_check assert state.state["pypi_version"] == pypi_version
def test_install_package_with_target(script): """ Test installing a package using pip install --target """ 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) ) # Test repeated call without --upgrade, no files should have changed result = script.pip('install', '-t', target_dir, "initools==0.1") assert not Path('scratch') / 'target' / 'initools' in result.files_updated # Test upgrade call, check that new version is installed result = script.pip('install', '--upgrade', '-t', target_dir, "initools==0.2") assert Path('scratch') / 'target' / 'initools' in result.files_updated, ( str(result) ) egg_folder = ( Path('scratch') / 'target' / 'INITools-0.2-py%s.egg-info' % pyversion) assert egg_folder in result.files_created, ( str(result) ) # Test install and upgrade of single-module package result = script.pip('install', '-t', target_dir, 'six') assert Path('scratch') / 'target' / 'six.py' in result.files_created, ( str(result) ) result = script.pip('install', '-t', target_dir, '--upgrade', 'six') assert Path('scratch') / 'target' / 'six.py' in result.files_updated, ( str(result) )
def __init__(self, base_path, *args, **kwargs): # Make our base_path a test.lib.path.Path object base_path = Path(base_path) # Store paths related to the virtual environment _virtualenv = kwargs.pop("virtualenv") path_locations = virtualenv.path_locations(_virtualenv) # Make sure we have test.lib.path.Path objects venv, lib, include, bin = map(Path, path_locations) self.venv_path = venv self.lib_path = virtualenv_lib_path(venv, lib) self.include_path = include self.bin_path = bin if hasattr(sys, "pypy_version_info"): self.site_packages_path = self.venv_path.join("site-packages") else: self.site_packages_path = self.lib_path.join("site-packages") 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:], ) if sys.platform == 'win32': if sys.version_info >= (3, 5): scripts_base = self.user_site_path.join('..').normpath else: scripts_base = self.user_base_path self.user_bin_path = scripts_base.join('Scripts') else: self.user_bin_path = self.user_base_path.join(self.bin_path - self.venv_path) # Create a Directory to use as a scratch pad self.scratch_path = base_path.join("scratch").mkdir() # Set our default working directory kwargs.setdefault("cwd", self.scratch_path) # Setup our environment environ = kwargs.get("environ") if environ is None: environ = os.environ.copy() environ["PATH"] = Path.pathsep.join([self.bin_path] + [environ.get("PATH", [])], ) environ["PYTHONUSERBASE"] = self.user_base_path # Writing bytecode can mess up updated file detection environ["PYTHONDONTWRITEBYTECODE"] = "1" # Make sure we get UTF-8 on output, even on Windows... environ["PYTHONIOENCODING"] = "UTF-8" kwargs["environ"] = environ # Call the TestFileEnvironment __init__ super(PipTestEnvironment, self).__init__(base_path, *args, **kwargs) # Expand our absolute path directories into relative for name in [ "base", "venv", "lib", "include", "bin", "site_packages", "user_base", "user_site", "user_bin", "scratch" ]: real_name = "%s_path" % name setattr(self, name, getattr(self, real_name) - self.base_path) # Make sure temp_path is a Path object self.temp_path = Path(self.temp_path) # Ensure the tmp dir exists, things break horribly if it doesn't 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()
import os import sys import re import textwrap import site import shutil import subprocess import pytest import scripttest import six import virtualenv from tests.lib.path import Path, curdir DATA_DIR = Path(__file__).folder.folder.join("data").abspath SRC_DIR = Path(__file__).abspath.folder.folder.folder pyversion = sys.version[:3] pyversion_tuple = sys.version_info def path_to_url(path): """ Convert a path to URI. The path will be made absolute and will not have quoted path parts. (adapted from pip.util) """ path = os.path.normpath(os.path.abspath(path)) drive, path = os.path.splitdrive(path) filepath = path.split(os.path.sep)
def __init__(self, base_path, *args, **kwargs): # Make our base_path a test.lib.path.Path object base_path = Path(base_path) # Store paths related to the virtual environment venv = kwargs.pop("virtualenv") self.venv_path = venv.location self.lib_path = venv.lib self.site_packages_path = venv.site self.bin_path = venv.bin self.user_base_path = self.venv_path.joinpath("user") self.user_site_path = self.venv_path.joinpath( "user", site.USER_SITE[len(site.USER_BASE) + 1:], ) if sys.platform == 'win32': if sys.version_info >= (3, 5): scripts_base = Path( os.path.normpath(self.user_site_path.joinpath('..'))) else: scripts_base = self.user_base_path self.user_bin_path = scripts_base.joinpath('Scripts') else: self.user_bin_path = self.user_base_path.joinpath( os.path.relpath(self.bin_path, self.venv_path)) # Create a Directory to use as a scratch pad self.scratch_path = base_path.joinpath("scratch") self.scratch_path.mkdir() # Set our default working directory kwargs.setdefault("cwd", self.scratch_path) # Setup our environment environ = kwargs.get("environ") if environ is None: environ = os.environ.copy() environ["PATH"] = Path.pathsep.join([self.bin_path] + [environ.get("PATH", [])], ) environ["PYTHONUSERBASE"] = self.user_base_path # Writing bytecode can mess up updated file detection environ["PYTHONDONTWRITEBYTECODE"] = "1" # Make sure we get UTF-8 on output, even on Windows... environ["PYTHONIOENCODING"] = "UTF-8" kwargs["environ"] = environ # Whether all pip invocations should expect stderr # (useful for Python version deprecation) self.pip_expect_warning = kwargs.pop('pip_expect_warning', None) # Call the TestFileEnvironment __init__ super(PipTestEnvironment, self).__init__(base_path, *args, **kwargs) # Expand our absolute path directories into relative for name in [ "base", "venv", "bin", "lib", "site_packages", "user_base", "user_site", "user_bin", "scratch" ]: real_name = "%s_path" % name relative_path = Path( os.path.relpath(getattr(self, real_name), self.base_path)) setattr(self, name, relative_path) # Make sure temp_path is a Path object self.temp_path = Path(self.temp_path) # Ensure the tmp dir exists, things break horribly if it doesn't self.temp_path.mkdir() # create easy-install.pth in user_site, so we always have it updated # instead of created self.user_site_path.mkdir(parents=True) self.user_site_path.joinpath("easy-install.pth").touch()
from pip._internal.collector import LinkCollector from pip._internal.index import PackageFinder from pip._internal.locations import get_major_minor_version from pip._internal.models.search_scope import SearchScope from pip._internal.models.selection_prefs import SelectionPreferences from pip._internal.network.session import PipSession from pip._internal.utils.deprecation import DEPRECATION_MSG_PREFIX from pip._internal.utils.typing import MYPY_CHECK_RUNNING from tests.lib.path import Path, curdir if MYPY_CHECK_RUNNING: from typing import List, Optional from pip._internal.models.target_python import TargetPython DATA_DIR = Path(__file__).parent.parent.joinpath("data").resolve() SRC_DIR = Path(__file__).resolve().parent.parent.parent pyversion = get_major_minor_version() pyversion_tuple = sys.version_info CURRENT_PY_VERSION_INFO = sys.version_info[:3] def assert_paths_equal(actual, expected): assert os.path.normpath(actual) == os.path.normpath(expected) def path_to_url(path): """ Convert a path to URI. The path will be made absolute and
from scripttest import FoundDir, TestFileEnvironment from pip._internal.download import PipSession from pip._internal.index import PackageFinder from pip._internal.models.search_scope import SearchScope from pip._internal.models.selection_prefs import SelectionPreferences from pip._internal.utils.deprecation import DEPRECATION_MSG_PREFIX from pip._internal.utils.typing import MYPY_CHECK_RUNNING from tests.lib.path import Path, curdir if MYPY_CHECK_RUNNING: from typing import Iterable, List, Optional from pip._internal.models.target_python import TargetPython DATA_DIR = Path(__file__).parent.parent.joinpath("data").abspath SRC_DIR = Path(__file__).abspath.parent.parent.parent pyversion = sys.version[:3] pyversion_tuple = sys.version_info CURRENT_PY_VERSION_INFO = sys.version_info[:3] def assert_paths_equal(actual, expected): assert os.path.normpath(actual) == os.path.normpath(expected) def path_to_url(path): """ Convert a path to URI. The path will be made absolute and
def test_download_specify_platform(script, data): """ Test using "pip download --platform" to download a .whl archive supported for a specific platform """ fake_wheel(data, 'fake-1.0-py2.py3-none-any.whl') # Confirm that universal wheels are returned even for specific # platforms. result = script.pip('download', '--no-index', '--find-links', data.find_links, '--only-binary=:all:', '--dest', '.', '--platform', 'linux_x86_64', 'fake') assert (Path('scratch') / 'fake-1.0-py2.py3-none-any.whl' in result.files_created) result = script.pip('download', '--no-index', '--find-links', data.find_links, '--only-binary=:all:', '--dest', '.', '--platform', 'macosx_10_9_x86_64', 'fake') data.reset() fake_wheel(data, 'fake-1.0-py2.py3-none-macosx_10_9_x86_64.whl') fake_wheel(data, 'fake-2.0-py2.py3-none-linux_x86_64.whl') result = script.pip('download', '--no-index', '--find-links', data.find_links, '--only-binary=:all:', '--dest', '.', '--platform', 'macosx_10_10_x86_64', 'fake') assert (Path('scratch') / 'fake-1.0-py2.py3-none-macosx_10_9_x86_64.whl' in result.files_created) # OSX platform wheels are not backward-compatible. result = script.pip( 'download', '--no-index', '--find-links', data.find_links, '--only-binary=:all:', '--dest', '.', '--platform', 'macosx_10_8_x86_64', 'fake', expect_error=True, ) # No linux wheel provided for this version. result = script.pip( 'download', '--no-index', '--find-links', data.find_links, '--only-binary=:all:', '--dest', '.', '--platform', 'linux_x86_64', 'fake==1', expect_error=True, ) result = script.pip('download', '--no-index', '--find-links', data.find_links, '--only-binary=:all:', '--dest', '.', '--platform', 'linux_x86_64', 'fake==2') assert (Path('scratch') / 'fake-2.0-py2.py3-none-linux_x86_64.whl' in result.files_created)
def shared_script(tmpdir_factory, script_factory): tmpdir = Path(str(tmpdir_factory.mktemp("download_shared_script"))) script = script_factory(tmpdir.joinpath("workspace")) return script
def test_download_specify_abi(script, data): """ Test using "pip download --abi" to download a .whl archive supported for a specific abi """ fake_wheel(data, 'fake-1.0-py2.py3-none-any.whl') result = script.pip('download', '--no-index', '--find-links', data.find_links, '--only-binary=:all:', '--dest', '.', '--implementation', 'fk', '--abi', 'fake_abi', 'fake') result.did_create(Path('scratch') / 'fake-1.0-py2.py3-none-any.whl') result = script.pip('download', '--no-index', '--find-links', data.find_links, '--only-binary=:all:', '--dest', '.', '--implementation', 'fk', '--abi', 'none', 'fake') result = script.pip( 'download', '--no-index', '--find-links', data.find_links, '--only-binary=:all:', '--dest', '.', '--abi', 'cp27m', 'fake', ) data.reset() fake_wheel(data, 'fake-1.0-fk2-fakeabi-fake_platform.whl') result = script.pip('download', '--no-index', '--find-links', data.find_links, '--only-binary=:all:', '--dest', '.', '--python-version', '2', '--implementation', 'fk', '--platform', 'fake_platform', '--abi', 'fakeabi', 'fake') result.did_create( Path('scratch') / 'fake-1.0-fk2-fakeabi-fake_platform.whl') result = script.pip( 'download', '--no-index', '--find-links', data.find_links, '--only-binary=:all:', '--dest', '.', '--implementation', 'fk', '--platform', 'fake_platform', '--abi', 'none', 'fake', expect_error=True, ) data.reset() fake_wheel(data, 'fake-1.0-fk2-otherabi-fake_platform.whl') result = script.pip('download', '--no-index', '--find-links', data.find_links, '--only-binary=:all:', '--dest', '.', '--python-version', '2', '--implementation', 'fk', '--platform', 'fake_platform', '--abi', 'fakeabi', '--abi', 'otherabi', '--abi', 'none', 'fake') result.did_create( Path('scratch') / 'fake-1.0-fk2-otherabi-fake_platform.whl')
def clean_project(tmpdir_factory, data): tmpdir = Path(str(tmpdir_factory.mktemp("clean_project"))) new_project_dir = tmpdir.joinpath("FSPkg") path = data.packages.joinpath("FSPkg") shutil.copytree(path, new_project_dir) return new_project_dir
def get_header_scheme_path_for_script(script, dist_name): command = ("from pip._internal.locations import get_scheme;" "scheme = get_scheme({!r});" "print(scheme.headers);").format(dist_name) result = script.run('python', '-c', command).stdout return Path(result.strip())
def make_file(path: str) -> None: Path(path).touch()
def test_download_prefer_binary_when_tarball_higher_than_wheel(script, data): fake_wheel(data, 'source-0.8-py2.py3-none-any.whl') result = script.pip('download', '--prefer-binary', '--no-index', '-f', data.packages, '-d', '.', 'source') result.did_create(Path('scratch') / 'source-0.8-py2.py3-none-any.whl') result.did_not_create(Path('scratch') / 'source-1.0.tar.gz')
def __init__(self, root, source=None): self.source = source or DATA_DIR self.root = Path(root).abspath
def shared_data(tmpdir_factory: pytest.TempdirFactory) -> TestData: return TestData.copy(Path(str(tmpdir_factory.mktemp("data"))))
def shared_data(tmpdir_factory): return TestData.copy(Path(str(tmpdir_factory.mktemp("data"))))
def test_download_prefer_binary_when_only_tarball_exists(script, data): result = script.pip('download', '--prefer-binary', '--no-index', '-f', data.packages, '-d', '.', 'source') result.did_create(Path('scratch') / 'source-1.0.tar.gz')