def virtualenv_template(tmpdir_factory): tmpdir = Path(str(tmpdir_factory.mktemp('virtualenv'))) # Copy over our source tree so that each virtual environment is self # contained pip_src = tmpdir.join("pip_src").abspath shutil.copytree( SRC_DIR, pip_src, ignore=shutil.ignore_patterns( "*.pyc", "__pycache__", "contrib", "docs", "tasks", "*.txt", "tests", "pip.egg-info", "build", "dist", ".tox", ".git", ), ) # Create the virtual environment venv = VirtualEnvironment.create( tmpdir.join("venv_orig"), pip_source_dir=pip_src, relocatable=True, ) # Rename original virtualenv directory to make sure # it's not reused by mistake from one of the copies. venv_template = tmpdir / "venv_template" os.rename(venv.location, venv_template) yield venv_template tmpdir.rmtree(noerrors=True)
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") 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_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_bin_path = self.user_base_path.join(self.bin_path - self.venv_path) self.user_site_path = self.venv_path.join( "user", site.USER_SITE[len(site.USER_BASE) + 1:], ) # 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["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 # Writing bytecode can mess up updated file detection environ["PYTHONDONTWRITEBYTECODE"] = "1" 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) # 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()
def virtualenv_template(tmpdir_factory): tmpdir = Path(str(tmpdir_factory.mktemp('virtualenv'))) # Copy over our source tree so that each virtual environment is self # contained pip_src = tmpdir.join("pip_src").abspath shutil.copytree( SRC_DIR, pip_src, ignore=shutil.ignore_patterns( "*.pyc", "__pycache__", "contrib", "docs", "tasks", "*.txt", "tests", "pip.egg-info", "build", "dist", ".tox", ".git", ), ) # Create the virtual environment venv = VirtualEnvironment.create( tmpdir.join("venv_orig"), pip_source_dir=pip_src, relocatable=True, ) if sys.platform == 'win32': # Work around setuptools' easy_install.exe # not working properly after relocation. for exe in os.listdir(venv.bin): if exe.startswith('easy_install'): (venv.bin / exe).remove() with open(venv.bin / 'easy_install.bat', 'w') as fp: fp.write('python.exe -m easy_install %*\n') # Rename original virtualenv directory to make sure # it's not reused by mistake from one of the copies. venv_template = tmpdir / "venv_template" os.rename(venv.location, venv_template) yield venv_template tmpdir.rmtree(noerrors=True)
def virtualenv_template(tmpdir_factory, pip_src): tmpdir = Path(str(tmpdir_factory.mktemp('virtualenv'))) # Create the virtual environment venv = VirtualEnvironment.create( tmpdir.join("venv_orig"), pip_source_dir=pip_src, relocatable=True, ) # Fix `site.py`. site_py = venv.lib / 'site.py' with open(site_py) as fp: site_contents = fp.read() for pattern, replace in ( ( # Ensure `virtualenv.system_site_packages = True` (needed # for testing `--user`) does not result in adding the real # site-packages' directory to `sys.path`. ( '\ndef virtual_addsitepackages(known_paths):\n' ), ( '\ndef virtual_addsitepackages(known_paths):\n' ' return known_paths\n' ), ), ( # Fix sites ordering: user site must be added before system site. ( '\n paths_in_sys = addsitepackages(paths_in_sys)' '\n paths_in_sys = addusersitepackages(paths_in_sys)\n' ), ( '\n paths_in_sys = addusersitepackages(paths_in_sys)' '\n paths_in_sys = addsitepackages(paths_in_sys)\n' ), ), ): assert pattern in site_contents site_contents = site_contents.replace(pattern, replace) with open(site_py, 'w') as fp: fp.write(site_contents) if sys.platform == 'win32': # Work around setuptools' easy_install.exe # not working properly after relocation. for exe in os.listdir(venv.bin): if exe.startswith('easy_install'): (venv.bin / exe).remove() with open(venv.bin / 'easy_install.bat', 'w') as fp: fp.write('python.exe -m easy_install %*\n') # Rename original virtualenv directory to make sure # it's not reused by mistake from one of the copies. venv_template = tmpdir / "venv_template" os.rename(venv.location, venv_template) yield venv_template tmpdir.rmtree(noerrors=True)
def run(self, *args, **kw): if self.verbose: print('>> running %s %s' % (args, kw)) cwd = kw.pop('cwd', None) run_from = kw.pop('run_from', None) assert not cwd or not run_from, "Don't use run_from; it's going away" cwd = Path.string(cwd or run_from or self.cwd) assert not isinstance(cwd, Path) return TestPipResult(super(TestPipEnvironment, self).run(cwd=cwd, *args, **kw), verbose=self.verbose)
class TestData(object): """ Represents a bundle of pre-created test data. This copies a pristine set of test data into a root location that is designed to be test specific. The reason for this is when running the tests concurrently errors can be generated because the related tooling uses the directory as a work space. This leads to two concurrent processes trampling over each other. This class gets around that by copying all data into a directory and operating on the copied data. """ def __init__(self, root, source=None): self.source = source or DATA_DIR self.root = Path(root).abspath @classmethod def copy(cls, root): obj = cls(root) obj.reset() return obj def reset(self): self.root.rmtree() self.source.copytree(self.root) @property def packages(self): return self.root.join("packages") @property def packages2(self): return self.root.join("packages2") @property def indexes(self): return self.root.join("indexes") @property def reqfiles(self): return self.root.join("reqfiles") @property def find_links(self): return path_to_url(self.packages) @property def find_links2(self): return path_to_url(self.packages2) def index_url(self, index="simple"): return path_to_url(self.root.join("indexes", index))
def virtualenv_template(request, tmpdir_factory, pip_src, setuptools_install, common_wheels): if six.PY3 and request.config.getoption('--use-venv'): venv_type = 'venv' else: venv_type = 'virtualenv' # Create the virtual environment tmpdir = Path(str(tmpdir_factory.mktemp('virtualenv'))) venv = VirtualEnvironment(tmpdir.join("venv_orig"), venv_type=venv_type) # Install setuptools and pip. install_egg_link(venv, 'setuptools', setuptools_install) pip_editable = Path(str(tmpdir_factory.mktemp('pip'))) / 'pip' pip_src.copytree(pip_editable) assert compileall.compile_dir(str(pip_editable), quiet=1) subprocess.check_call([venv.bin / 'python', 'setup.py', '-q', 'develop'], cwd=pip_editable) # Drop (non-relocatable) launchers. for exe in os.listdir(venv.bin): if not ( exe.startswith('python') or exe.startswith('libpy') # Don't remove libpypy-c.so... ): (venv.bin / exe).remove() # Enable user site packages. venv.user_site_packages = True # Rename original virtualenv directory to make sure # it's not reused by mistake from one of the copies. venv_template = tmpdir / "venv_template" venv.move(venv_template) yield venv
class PipTestEnvironment(scripttest.TestFileEnvironment): """ A specialized TestFileEnvironment for testing pip """ # # Attribute naming convention # --------------------------- # # Instances of this class have many attributes representing paths # in the filesystem. To keep things straight, absolute paths have # a name of the form xxxx_path and relative paths have a name that # does not end in '_path'. exe = sys.platform == 'win32' and '.exe' or '' verbose = False 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() def _ignore_file(self, fn): if fn.endswith('__pycache__') or fn.endswith(".pyc"): result = True else: result = super(PipTestEnvironment, self)._ignore_file(fn) return result def run(self, *args, **kw): if self.verbose: print('>> running %s %s' % (args, kw)) cwd = kw.pop('cwd', None) run_from = kw.pop('run_from', None) assert not cwd or not run_from, "Don't use run_from; it's going away" cwd = cwd or run_from or self.cwd if sys.platform == 'win32': # Partial fix for ScriptTest.run using `shell=True` on Windows. args = [str(a).replace('^', '^^').replace('&', '^&') for a in args] return TestPipResult( super(PipTestEnvironment, self).run(cwd=cwd, *args, **kw), verbose=self.verbose, ) def pip(self, *args, **kwargs): # On old versions of Python, urllib3/requests will raise a warning # about the lack of an SSLContext. Expect it when running commands # that will touch the outside world. if (pyversion_tuple < (2, 7, 9) and args and args[0] in ('search', 'install', 'download')): kwargs['expect_stderr'] = True if kwargs.pop('use_module', False): exe = 'python' args = ('-m', 'pip') + args else: exe = 'pip' return self.run(exe, *args, **kwargs) def pip_install_local(self, *args, **kwargs): return self.pip( "install", "--no-index", "--find-links", path_to_url(os.path.join(DATA_DIR, "packages")), *args, **kwargs )
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.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 # 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 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()
class TestData(object): """ Represents a bundle of pre-created test data. This copies a pristine set of test data into a root location that is designed to be test specific. The reason for this is when running the tests concurrently errors can be generated because the related tooling uses the directory as a work space. This leads to two concurrent processes trampling over each other. This class gets around that by copying all data into a directory and operating on the copied data. """ def __init__(self, root, source=None): self.source = source or DATA_DIR self.root = Path(root).abspath @classmethod def copy(cls, root): obj = cls(root) obj.reset() return obj def reset(self): # Check explicitly for the target directory to avoid overly-broad # try/except. if self.root.exists(): shutil.rmtree(self.root) shutil.copytree(self.source, self.root, symlinks=True) @property def packages(self): return self.root.joinpath("packages") @property def packages2(self): return self.root.joinpath("packages2") @property def packages3(self): return self.root.joinpath("packages3") @property def src(self): return self.root.joinpath("src") @property def indexes(self): return self.root.joinpath("indexes") @property def reqfiles(self): return self.root.joinpath("reqfiles") @property def completion_paths(self): return self.root.joinpath("completion_paths") @property def find_links(self): return path_to_url(self.packages) @property def find_links2(self): return path_to_url(self.packages2) @property def find_links3(self): return path_to_url(self.packages3) @property def backends(self): return path_to_url(self.root.joinpath("backends")) def index_url(self, index="simple"): return path_to_url(self.root.joinpath("indexes", index))
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 shared_script(tmpdir_factory: pytest.TempdirFactory, script_factory: ScriptFactory) -> PipTestEnvironment: tmpdir = Path(str(tmpdir_factory.mktemp("download_shared_script"))) script = script_factory(tmpdir.joinpath("workspace")) return script
class PipTestEnvironment(scripttest.TestFileEnvironment): """ A specialized TestFileEnvironment for testing pip """ # # Attribute naming convention # --------------------------- # # Instances of this class have many attributes representing paths # in the filesystem. To keep things straight, absolute paths have # a name of the form xxxx_path and relative paths have a name that # does not end in '_path'. exe = sys.platform == 'win32' and '.exe' or '' verbose = False 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) # workaround for https://github.com/pypa/virtualenv/issues/306 if hasattr(sys, "pypy_version_info"): lib = os.path.join(venv, 'lib-python', pyversion) 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_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_bin_path = self.user_base_path.join( self.bin_path - self.venv_path ) self.user_site_path = self.venv_path.join( "user", site.USER_SITE[len(site.USER_BASE) + 1:], ) # 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" 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() def _ignore_file(self, fn): if fn.endswith('__pycache__') or fn.endswith(".pyc"): result = True else: result = super(PipTestEnvironment, self)._ignore_file(fn) return result def run(self, *args, **kw): if self.verbose: print('>> running %s %s' % (args, kw)) cwd = kw.pop('cwd', None) run_from = kw.pop('run_from', None) assert not cwd or not run_from, "Don't use run_from; it's going away" cwd = cwd or run_from or self.cwd return TestPipResult( super(PipTestEnvironment, self).run(cwd=cwd, *args, **kw), verbose=self.verbose, ) def pip(self, *args, **kwargs): # On old versions of Python, urllib3/requests will raise a warning # about the lack of an SSLContext. Expect it when running commands # that will touch the outside world. if (pyversion_tuple < (2, 7, 9) and args and args[0] in ('search', 'install', 'download')): kwargs['expect_stderr'] = True # Python 3.3 is deprecated and we emit a warning on it. if pyversion_tuple[:2] == (3, 3): kwargs['expect_stderr'] = True return self.run("pip", *args, **kwargs) def pip_install_local(self, *args, **kwargs): return self.pip( "install", "--no-index", "--find-links", path_to_url(os.path.join(DATA_DIR, "packages")), *args, **kwargs )
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 make_file(path: str) -> None: Path(path).touch()
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() else: without_egg_link = True 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: '\ '%r\n%s' % (egg_link_path, self)) else: if not egg_link_path in self.files_created: raise TestFailure('expected egg link file missing: '\ '%r\n%s' % (egg_link_path, self)) egg_link_file = self.files_created[egg_link_path] if not (# FIXME: I don't understand why there's a trailing . here egg_link_file.bytes.endswith('.') and egg_link_file.bytes[:-1].strip().endswith(pkg_dir)): raise TestFailure(textwrap.dedent(u('''\ Incorrect egg_link file %r Expected ending: %r ------- Actual contents ------- %s -------------------------------''' % ( egg_link_file, pkg_dir + u('\n.'), egg_link_file.bytes)))) if use_user_site: pth_file = Path.string(e.user_site / 'easy-install.pth') else: pth_file = Path.string(e.site_packages / 'easy-install.pth') if (pth_file in self.files_updated) == without_egg_link: raise TestFailure('%r unexpectedly %supdated by install' % ( pth_file, (not without_egg_link and 'not ' or ''))) if (pkg_dir in self.files_created) == (curdir in without_files): raise TestFailure(textwrap.dedent('''\ expected package directory %r %sto be created actually created: %s ''') % ( Path.string(pkg_dir), (curdir in without_files and 'not ' or ''), sorted(self.files_created.keys()))) 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)) for f in without_files: if (pkg_dir/f).normpath in self.files_created: raise TestFailure('Package directory %r has '\ 'unexpected content %f' % (pkg_dir, f))
def test_download_specify_python_version(script, data): """ Test using "pip download --python-version" to download a .whl archive supported for a specific interpreter """ 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', '.', '--python-version', '2', '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', '.', '--python-version', '3', 'fake' ) result = script.pip( 'download', '--no-index', '--find-links', data.find_links, '--only-binary=:all:', '--dest', '.', '--python-version', '27', 'fake' ) result = script.pip( 'download', '--no-index', '--find-links', data.find_links, '--only-binary=:all:', '--dest', '.', '--python-version', '33', 'fake' ) data.reset() fake_wheel(data, 'fake-1.0-py2-none-any.whl') fake_wheel(data, 'fake-2.0-py3-none-any.whl') # No py3 provided for version 1. result = script.pip( 'download', '--no-index', '--find-links', data.find_links, '--only-binary=:all:', '--dest', '.', '--python-version', '3', 'fake==1.0', expect_error=True, ) result = script.pip( 'download', '--no-index', '--find-links', data.find_links, '--only-binary=:all:', '--dest', '.', '--python-version', '2', 'fake' ) assert ( Path('scratch') / 'fake-1.0-py2-none-any.whl' in result.files_created ) result = script.pip( 'download', '--no-index', '--find-links', data.find_links, '--only-binary=:all:', '--dest', '.', '--python-version', '26', 'fake' ) result = script.pip( 'download', '--no-index', '--find-links', data.find_links, '--only-binary=:all:', '--dest', '.', '--python-version', '3', 'fake' ) assert ( Path('scratch') / 'fake-2.0-py3-none-any.whl' in result.files_created )
def test_download_specify_abi(script: PipTestEnvironment, data: TestData) -> None: """ 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 test_download__python_version(script: PipTestEnvironment, data: TestData) -> None: """ Test using "pip download --python-version" to download a .whl archive supported for a specific interpreter """ 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", ".", "--python-version", "2", "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", ".", "--python-version", "3", "fake", ) result = script.pip( "download", "--no-index", "--find-links", data.find_links, "--only-binary=:all:", "--dest", ".", "--python-version", "27", "fake", ) result = script.pip( "download", "--no-index", "--find-links", data.find_links, "--only-binary=:all:", "--dest", ".", "--python-version", "33", "fake", ) data.reset() fake_wheel(data, "fake-1.0-py2-none-any.whl") fake_wheel(data, "fake-2.0-py3-none-any.whl") # No py3 provided for version 1. result = script.pip( "download", "--no-index", "--find-links", data.find_links, "--only-binary=:all:", "--dest", ".", "--python-version", "3", "fake==1.0", expect_error=True, ) result = script.pip( "download", "--no-index", "--find-links", data.find_links, "--only-binary=:all:", "--dest", ".", "--python-version", "2", "fake", ) result.did_create(Path("scratch") / "fake-1.0-py2-none-any.whl") result = script.pip( "download", "--no-index", "--find-links", data.find_links, "--only-binary=:all:", "--dest", ".", "--python-version", "26", "fake", ) result = script.pip( "download", "--no-index", "--find-links", data.find_links, "--only-binary=:all:", "--dest", ".", "--python-version", "3", "fake", ) result.did_create(Path("scratch") / "fake-2.0-py3-none-any.whl")
def test_download_specify_platform(script: PipTestEnvironment, data: TestData) -> None: """ 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", ) 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", ".", "--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", ) result.did_create( Path("scratch") / "fake-1.0-py2.py3-none-macosx_10_9_x86_64.whl") # 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", ) result.did_create( Path("scratch") / "fake-2.0-py2.py3-none-linux_x86_64.whl") # Test with multiple supported platforms specified. data.reset() fake_wheel(data, "fake-3.0-py2.py3-none-linux_x86_64.whl") result = script.pip( "download", "--no-index", "--find-links", data.find_links, "--only-binary=:all:", "--dest", ".", "--platform", "manylinux1_x86_64", "--platform", "linux_x86_64", "--platform", "any", "fake==3", ) result.did_create( Path("scratch") / "fake-3.0-py2.py3-none-linux_x86_64.whl")
from contextlib import contextmanager import os import sys import re import textwrap import site import shutil import subprocess import pytest from scripttest import FoundDir, TestFileEnvironment 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)
class PipTestEnvironment(TestFileEnvironment): """ A specialized TestFileEnvironment for testing pip """ # # Attribute naming convention # --------------------------- # # Instances of this class have many attributes representing paths # in the filesystem. To keep things straight, absolute paths have # a name of the form xxxx_path and relative paths have a name that # does not end in '_path'. exe = sys.platform == 'win32' and '.exe' or '' verbose = False 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 = self.user_site_path.joinpath('..').normpath 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(self.bin_path - self.venv_path) # Create a Directory to use as a scratch pad self.scratch_path = base_path.joinpath("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 # 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 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.mkdir(parents=True) self.user_site_path.joinpath("easy-install.pth").touch() def _ignore_file(self, fn): if fn.endswith('__pycache__') or fn.endswith(".pyc"): result = True else: result = super(PipTestEnvironment, self)._ignore_file(fn) return result def _find_traverse(self, path, result): # Ignore symlinked directories to avoid duplicates in `run()` # results because of venv `lib64 -> lib/` symlink on Linux. full = os.path.join(self.base_path, path) if os.path.isdir(full) and os.path.islink(full): if not self.temp_path or path != 'tmp': result[path] = FoundDir(self.base_path, path) else: super(PipTestEnvironment, self)._find_traverse(path, result) def run(self, *args, **kw): """ :param allow_stderr_warning: whether a logged warning (or deprecation message) is allowed in stderr. :param allow_stderr_error: whether a logged error is allowed in stderr. Passing True for this argument implies `allow_stderr_warning` since warnings are weaker than errors. :param expect_stderr: whether to allow warnings in stderr (equivalent to `allow_stderr_warning`). This argument is an abbreviated version of `allow_stderr_warning` and is also kept for backwards compatibility. """ if self.verbose: print('>> running %s %s' % (args, kw)) cwd = kw.pop('cwd', None) run_from = kw.pop('run_from', None) assert not cwd or not run_from, "Don't use run_from; it's going away" cwd = cwd or run_from or self.cwd if sys.platform == 'win32': # Partial fix for ScriptTest.run using `shell=True` on Windows. args = [str(a).replace('^', '^^').replace('&', '^&') for a in args] # Remove `allow_stderr_error` and `allow_stderr_warning` before # calling run() because PipTestEnvironment doesn't support them. allow_stderr_error = kw.pop('allow_stderr_error', None) allow_stderr_warning = kw.pop('allow_stderr_warning', None) if kw.get('expect_error'): # Then default to allowing logged errors. if allow_stderr_error is not None and not allow_stderr_error: raise RuntimeError('cannot pass allow_stderr_error=False with ' 'expect_error=True') allow_stderr_error = True elif kw.get('expect_stderr'): # Then default to allowing logged warnings. if allow_stderr_warning is not None and not allow_stderr_warning: raise RuntimeError( 'cannot pass allow_stderr_warning=False with ' 'expect_stderr=True') allow_stderr_warning = True # Pass expect_stderr=True to allow any stderr. We do this because # we do our checking of stderr further on in check_stderr(). kw['expect_stderr'] = True result = super(PipTestEnvironment, self).run(cwd=cwd, *args, **kw) check_stderr( result.stderr, allow_stderr_error=allow_stderr_error, allow_stderr_warning=allow_stderr_warning, ) return TestPipResult(result, verbose=self.verbose) def pip(self, *args, **kwargs): if self.pip_expect_warning: kwargs['allow_stderr_warning'] = True if kwargs.pop('use_module', True): exe = 'python' args = ('-m', 'pip') + args else: exe = 'pip' return self.run(exe, *args, **kwargs) def pip_install_local(self, *args, **kwargs): return self.pip("install", "--no-index", "--find-links", path_to_url(os.path.join(DATA_DIR, "packages")), *args, **kwargs) def easy_install(self, *args, **kwargs): args = ('-m', 'easy_install') + args return self.run('python', *args, **kwargs)
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.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", "bin", "lib", "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()
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) # workaround for https://github.com/pypa/virtualenv/issues/306 if hasattr(sys, "pypy_version_info"): lib = os.path.join(venv, 'lib-python', pyversion) 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_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_bin_path = self.user_base_path.join( self.bin_path - self.venv_path ) self.user_site_path = self.venv_path.join( "user", site.USER_SITE[len(site.USER_BASE) + 1:], ) # 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" 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()
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):])
def test_is_socket(create: Callable[[str], None], result: bool, tmpdir: Path) -> None: target = tmpdir.joinpath("target") create(target) assert os.path.lexists(target) assert is_socket(target) == result
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 from tests.lib.wheel import make_wheel 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
def shared_data(tmpdir_factory): return TestData.copy(Path(str(tmpdir_factory.mktemp("data"))))
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.setdefault("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" # 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().__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 = "{name}_path".format(**locals()) 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()
class PipTestEnvironment(TestFileEnvironment): """ A specialized TestFileEnvironment for testing pip """ # # Attribute naming convention # --------------------------- # # Instances of this class have many attributes representing paths # in the filesystem. To keep things straight, absolute paths have # a name of the form xxxx_path and relative paths have a name that # does not end in '_path'. exe = sys.platform == 'win32' and '.exe' or '' verbose = False 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.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", "bin", "lib", "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() def _ignore_file(self, fn): if fn.endswith('__pycache__') or fn.endswith(".pyc"): result = True else: result = super(PipTestEnvironment, self)._ignore_file(fn) return result def _find_traverse(self, path, result): # Ignore symlinked directories to avoid duplicates in `run()` # results because of venv `lib64 -> lib/` symlink on Linux. full = os.path.join(self.base_path, path) if os.path.isdir(full) and os.path.islink(full): if not self.temp_path or path != 'tmp': result[path] = FoundDir(self.base_path, path) else: super(PipTestEnvironment, self)._find_traverse(path, result) def run(self, *args, **kw): if self.verbose: print('>> running %s %s' % (args, kw)) cwd = kw.pop('cwd', None) run_from = kw.pop('run_from', None) assert not cwd or not run_from, "Don't use run_from; it's going away" cwd = cwd or run_from or self.cwd if sys.platform == 'win32': # Partial fix for ScriptTest.run using `shell=True` on Windows. args = [str(a).replace('^', '^^').replace('&', '^&') for a in args] return TestPipResult( super(PipTestEnvironment, self).run(cwd=cwd, *args, **kw), verbose=self.verbose, ) def pip(self, *args, **kwargs): # On old versions of Python, urllib3/requests will raise a warning # about the lack of an SSLContext. Expect it when running commands # that will touch the outside world. if (pyversion_tuple < (2, 7, 9) and args and args[0] in ('search', 'install', 'download')): kwargs['expect_stderr'] = True if kwargs.pop('use_module', True): exe = 'python' args = ('-m', 'pip') + args else: exe = 'pip' return self.run(exe, *args, **kwargs) def pip_install_local(self, *args, **kwargs): return self.pip("install", "--no-index", "--find-links", path_to_url(os.path.join(DATA_DIR, "packages")), *args, **kwargs) def easy_install(self, *args, **kwargs): args = ('-m', 'easy_install') + args return self.run('python', *args, **kwargs)
def test_correct_pip_version(): """ Check we are importing pip from the right place. """ assert Path(pip.__file__).folder.folder.abspath == SRC_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 shared_data(tmpdir_factory: pytest.TempdirFactory) -> TestData: return TestData.copy(Path(str(tmpdir_factory.mktemp("data"))))
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").abspath SRC_DIR = Path(__file__).abspath.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
def data(tmpdir: Path) -> TestData: return TestData.copy(tmpdir.joinpath("data"))
def __init__(self, root, source=None): self.source = source or DATA_DIR self.root = Path(root).abspath
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')
class PipTestEnvironment(TestFileEnvironment): """ A specialized TestFileEnvironment for testing pip """ # # Attribute naming convention # --------------------------- # # Instances of this class have many attributes representing paths # in the filesystem. To keep things straight, absolute paths have # a name of the form xxxx_path and relative paths have a name that # does not end in '_path'. exe = sys.platform == 'win32' and '.exe' or '' verbose = False 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.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 # 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 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() def _ignore_file(self, fn): if fn.endswith('__pycache__') or fn.endswith(".pyc"): result = True else: result = super(PipTestEnvironment, self)._ignore_file(fn) return result def _find_traverse(self, path, result): # Ignore symlinked directories to avoid duplicates in `run()` # results because of venv `lib64 -> lib/` symlink on Linux. full = os.path.join(self.base_path, path) if os.path.isdir(full) and os.path.islink(full): if not self.temp_path or path != 'tmp': result[path] = FoundDir(self.base_path, path) else: super(PipTestEnvironment, self)._find_traverse(path, result) def run(self, *args, **kw): """ :param allow_stderr_warning: whether a logged warning (or deprecation message) is allowed in stderr. :param allow_stderr_error: whether a logged error is allowed in stderr. Passing True for this argument implies `allow_stderr_warning` since warnings are weaker than errors. :param expect_stderr: whether to allow warnings in stderr (equivalent to `allow_stderr_warning`). This argument is an abbreviated version of `allow_stderr_warning` and is also kept for backwards compatibility. """ if self.verbose: print('>> running %s %s' % (args, kw)) cwd = kw.pop('cwd', None) run_from = kw.pop('run_from', None) assert not cwd or not run_from, "Don't use run_from; it's going away" cwd = cwd or run_from or self.cwd if sys.platform == 'win32': # Partial fix for ScriptTest.run using `shell=True` on Windows. args = [str(a).replace('^', '^^').replace('&', '^&') for a in args] # Remove `allow_stderr_error` and `allow_stderr_warning` before # calling run() because PipTestEnvironment doesn't support them. allow_stderr_error = kw.pop('allow_stderr_error', None) allow_stderr_warning = kw.pop('allow_stderr_warning', None) if kw.get('expect_error'): # Then default to allowing logged errors. if allow_stderr_error is not None and not allow_stderr_error: raise RuntimeError( 'cannot pass allow_stderr_error=False with ' 'expect_error=True' ) allow_stderr_error = True elif kw.get('expect_stderr'): # Then default to allowing logged warnings. if allow_stderr_warning is not None and not allow_stderr_warning: raise RuntimeError( 'cannot pass allow_stderr_warning=False with ' 'expect_stderr=True' ) allow_stderr_warning = True # Pass expect_stderr=True to allow any stderr. We do this because # we do our checking of stderr further on in check_stderr(). kw['expect_stderr'] = True result = super(PipTestEnvironment, self).run(cwd=cwd, *args, **kw) check_stderr( result.stderr, allow_stderr_error=allow_stderr_error, allow_stderr_warning=allow_stderr_warning, ) return TestPipResult(result, verbose=self.verbose) def pip(self, *args, **kwargs): if self.pip_expect_warning: kwargs['allow_stderr_warning'] = True if kwargs.pop('use_module', True): exe = 'python' args = ('-m', 'pip') + args else: exe = 'pip' return self.run(exe, *args, **kwargs) def pip_install_local(self, *args, **kwargs): return self.pip( "install", "--no-index", "--find-links", path_to_url(os.path.join(DATA_DIR, "packages")), *args, **kwargs ) def easy_install(self, *args, **kwargs): args = ('-m', 'easy_install') + args return self.run('python', *args, **kwargs)
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 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')
def shared_script(tmpdir_factory, script_factory): tmpdir = Path(str(tmpdir_factory.mktemp("download_shared_script"))) script = script_factory(tmpdir.joinpath("workspace")) return script
class PipTestEnvironment(scripttest.TestFileEnvironment): """ A specialized TestFileEnvironment for testing pip """ # # Attribute naming convention # --------------------------- # # Instances of this class have many attributes representing paths # in the filesystem. To keep things straight, absolute paths have # a name of the form xxxx_path and relative paths have a name that # does not end in '_path'. exe = sys.platform == 'win32' and '.exe' or '' verbose = False 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") venv, lib, include, bin = virtualenv.path_locations(_virtualenv) # workaround for https://github.com/pypa/virtualenv/issues/306 if hasattr(sys, "pypy_version_info"): lib = os.path.join(venv, 'lib-python', pyversion) 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_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_bin_path = self.user_base_path.join( self.bin_path - self.venv_path ) self.user_site_path = self.venv_path.join( "user", site.USER_SITE[len(site.USER_BASE) + 1:], ) # 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["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 # Writing bytecode can mess up updated file detection environ["PYTHONDONTWRITEBYTECODE"] = "1" 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() def _ignore_file(self, fn): if fn.endswith('__pycache__') or fn.endswith(".pyc"): result = True else: result = super(PipTestEnvironment, self)._ignore_file(fn) return result def run(self, *args, **kw): if self.verbose: print('>> running %s %s' % (args, kw)) cwd = kw.pop('cwd', None) run_from = kw.pop('run_from', None) assert not cwd or not run_from, "Don't use run_from; it's going away" cwd = cwd or run_from or self.cwd return TestPipResult( super(PipTestEnvironment, self).run(cwd=cwd, *args, **kw), verbose=self.verbose, ) def pip(self, *args, **kwargs): return self.run("pip", *args, **kwargs) def pip_install_local(self, *args, **kwargs): return self.pip( "install", "--no-index", "--find-links", path_to_url(os.path.join(DATA_DIR, "packages")), *args, **kwargs )