def virtualenv(tmpdir, monkeypatch): """ Return a virtual environment which is unique to each test function invocation created inside of a sub directory of the test function's temporary directory. The returned object is a ``tests.lib.venv.VirtualEnvironment`` object. """ # Force shutil to use the older method of rmtree that didn't use the fd # functions. These seem to fail on Travis (and only on Travis). monkeypatch.setattr(shutil, "_use_fd_functions", False, raising=False) # 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", "tests", "pip.egg-info", "build", "dist", ".tox", ), ) # Create the virtual environment venv = VirtualEnvironment.create( tmpdir.join("workspace", "venv"), pip_source_dir=pip_src, ) # Undo our monkeypatching of shutil monkeypatch.undo() return venv
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): 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 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)