コード例 #1
0
def test_base_bootstrap_via_pip_invoke(tmp_path, coverage_env,
                                       current_fastest):
    bundle_ver = BUNDLE_SUPPORT[
        PythonInfo.current_system().version_release_str]
    create_cmd = [
        "--seeder",
        "pip",
        str(tmp_path / "env"),
        "--download",
        "--pip",
        bundle_ver["pip"].split("-")[1],
        "--setuptools",
        bundle_ver["setuptools"].split("-")[1],
        "--creator",
        current_fastest,
    ]
    result = run_via_cli(create_cmd)
    coverage_env()
    assert result

    site_package = result.creator.purelib
    pip = site_package / "pip"
    setuptools = site_package / "setuptools"
    wheel = site_package / "wheel"

    files_post_first_create = list(site_package.iterdir())
    assert pip in files_post_first_create
    assert setuptools in files_post_first_create
    assert wheel in files_post_first_create
コード例 #2
0
def test_base_bootstrap_via_pip_invoke(tmp_path, coverage_env, current_fastest, no):
    bundle_ver = BUNDLE_SUPPORT[PythonInfo.current_system().version_release_str]
    create_cmd = [
        "--seeder",
        "pip",
        str(tmp_path / "env"),
        "--download",
        "--pip",
        bundle_ver["pip"].split("-")[1],
        "--setuptools",
        bundle_ver["setuptools"].split("-")[1],
        "--creator",
        current_fastest,
    ]
    if no:
        create_cmd.append("--no-{}".format(no))
    result = cli_run(create_cmd)
    coverage_env()
    assert result

    site_package = result.creator.purelib
    pip = site_package / "pip"
    setuptools = site_package / "setuptools"
    wheel = site_package / "wheel"
    files_post_first_create = list(site_package.iterdir())

    if no:
        no_file = locals()[no]
        assert no not in files_post_first_create

    for key in ("pip", "setuptools", "wheel"):
        if key == no:
            continue
        assert locals()[key] in files_post_first_create
コード例 #3
0
ファイル: test_discovery.py プロジェクト: navytux/virtualenv
def test_relative_path(tmp_path, session_app_data, monkeypatch):
    sys_executable = Path(PythonInfo.current_system(app_data=session_app_data).system_executable)
    cwd = sys_executable.parents[1]
    monkeypatch.chdir(str(cwd))
    relative = str(sys_executable.relative_to(cwd))
    result = get_interpreter(relative, session_app_data)
    assert result is not None
コード例 #4
0
def test_failed_to_find_implementation(of_id, mocker):
    mocker.patch("virtualenv.run.plugin.creators.CreatorSelector._OPTIONS", return_value={})
    with pytest.raises(RuntimeError) as context:
        cli_run(["-p", of_id])
    assert repr(context.value) == repr(
        RuntimeError("No virtualenv implementation for {}".format(PythonInfo.current_system()))
    )
コード例 #5
0
def test_base_bootstrap_via_pip_invoke(tmp_path, coverage_env, mocker,
                                       current_fastest, no):
    bundle_ver = BUNDLE_SUPPORT[
        PythonInfo.current_system().version_release_str]

    extra_search_dir = tmp_path / "extra"
    extra_search_dir.mkdir()

    original = PipInvoke._execute

    def _execute(cmd, env):
        assert set(cmd[-4:]) == {
            "--find-links",
            str(extra_search_dir),
            str(BUNDLE_FOLDER)
        }
        return original(cmd, env)

    run = mocker.patch.object(PipInvoke, "_execute", side_effect=_execute)

    create_cmd = [
        "--seeder",
        "pip",
        str(tmp_path / "env"),
        "--download",
        "--pip",
        bundle_ver["pip"].split("-")[1],
        "--setuptools",
        bundle_ver["setuptools"].split("-")[1],
        "--creator",
        current_fastest,
        "--extra-search-dir",
        str(extra_search_dir),
    ]
    if no:
        create_cmd.append("--no-{}".format(no))
    result = cli_run(create_cmd)
    coverage_env()

    assert result
    assert run.call_count == 1

    site_package = result.creator.purelib
    pip = site_package / "pip"
    setuptools = site_package / "setuptools"
    wheel = site_package / "wheel"
    files_post_first_create = list(site_package.iterdir())

    if no:
        no_file = locals()[no]
        assert no not in files_post_first_create

    for key in ("pip", "setuptools", "wheel"):
        if key == no:
            continue
        assert locals()[key] in files_post_first_create
コード例 #6
0
from __future__ import absolute_import, unicode_literals

import sys
from uuid import uuid4

import pytest

from virtualenv.discovery.py_info import PythonInfo
from virtualenv.run import cli_run


@pytest.mark.slow
def test_failed_to_find_bad_spec():
    of_id = uuid4().hex
    with pytest.raises(RuntimeError) as context:
        cli_run(["-p", of_id])
    msg = repr(RuntimeError("failed to find interpreter for Builtin discover of python_spec={!r}".format(of_id)))
    assert repr(context.value) == msg


@pytest.mark.parametrize("of_id", [sys.executable, PythonInfo.current_system().implementation])
def test_failed_to_find_implementation(of_id, mocker):
    mocker.patch("virtualenv.run.plugin.creators.CreatorSelector._OPTIONS", return_value={})
    with pytest.raises(RuntimeError) as context:
        cli_run(["-p", of_id])
    assert repr(context.value) == repr(
        RuntimeError("No virtualenv implementation for {}".format(PythonInfo.current_system()))
    )
コード例 #7
0
def test_seed_link_via_app_data(tmp_path, coverage_env, current_fastest,
                                copies):
    current = PythonInfo.current_system()
    bundle_ver = BUNDLE_SUPPORT[current.version_release_str]
    create_cmd = [
        ensure_text(
            str(tmp_path / "en v")
        ),  # space in the name to ensure generated scripts work when path has space
        "--seeder",
        "app-data",
        "--extra-search-dir",
        ensure_text(str(BUNDLE_FOLDER)),
        "--download",
        "--pip",
        bundle_ver["pip"].split("-")[1],
        "--setuptools",
        bundle_ver["setuptools"].split("-")[1],
        "--clear-app-data",
        "--creator",
        current_fastest,
        "-vv",
    ]
    if not copies:
        create_cmd.append("--symlink-app-data")
    result = cli_run(create_cmd)
    coverage_env()
    assert result

    # uninstalling pip/setuptools now should leave us with a ensure_safe_to_do env
    site_package = result.creator.purelib
    pip = site_package / "pip"
    setuptools = site_package / "setuptools"

    files_post_first_create = list(site_package.iterdir())
    assert pip in files_post_first_create
    assert setuptools in files_post_first_create
    for pip_exe in [
            result.creator.script_dir /
            "pip{}{}".format(suffix, result.creator.exe.suffix) for suffix in (
                "",
                "{}".format(current.version_info.major),
                "{}.{}".format(current.version_info.major,
                               current.version_info.minor),
                "-{}.{}".format(current.version_info.major,
                                current.version_info.minor),
            )
    ]:
        assert pip_exe.exists()
        process = Popen([
            ensure_text(str(pip_exe)), "--version",
            "--disable-pip-version-check"
        ])
        _, __ = process.communicate()
        assert not process.returncode

    remove_cmd = [
        str(result.creator.script("pip")),
        "--verbose",
        "--disable-pip-version-check",
        "uninstall",
        "-y",
        "setuptools",
    ]
    process = Popen(remove_cmd)
    _, __ = process.communicate()
    assert not process.returncode
    assert site_package.exists()

    files_post_first_uninstall = list(site_package.iterdir())
    assert pip in files_post_first_uninstall
    assert setuptools not in files_post_first_uninstall

    # check we can run it again and will work - checks both overwrite and reuse cache
    result = cli_run(create_cmd)
    coverage_env()
    assert result
    files_post_second_create = list(site_package.iterdir())
    assert files_post_first_create == files_post_second_create

    # Windows does not allow removing a executable while running it, so when uninstalling pip we need to do it via
    # python -m pip
    remove_cmd = [str(result.creator.exe), "-m", "pip"] + remove_cmd[1:]
    process = Popen(remove_cmd + ["pip", "wheel"])
    _, __ = process.communicate()
    assert not process.returncode
    # pip is greedy here, removing all packages removes the site-package too
    if site_package.exists():
        purelib = result.creator.purelib
        patch_files = {
            purelib / "{}.{}".format("_virtualenv", i)
            for i in ("py", "pyc", "pth")
        }
        patch_files.add(purelib / "__pycache__")
        post_run = set(site_package.iterdir()) - patch_files
        assert not post_run, "\n".join(str(i) for i in post_run)

    if sys.version_info[0:2] == (3, 4) and os.environ.get(
            str("PIP_REQ_TRACKER")):
        os.environ.pop(str("PIP_REQ_TRACKER"))
コード例 #8
0
def test_base_bootstrap_link_via_app_data(tmp_path, coverage_env,
                                          current_fastest):
    current = PythonInfo.current_system()
    bundle_ver = BUNDLE_SUPPORT[current.version_release_str]
    create_cmd = [
        six.ensure_text(str(tmp_path / "env")),
        "--seeder",
        "app-data",
        "--extra-search-dir",
        six.ensure_text(str(BUNDLE_FOLDER)),
        "--download",
        "--pip",
        bundle_ver["pip"].split("-")[1],
        "--setuptools",
        bundle_ver["setuptools"].split("-")[1],
        "--clear-app-data",
        "--creator",
        current_fastest,
        "-vv",
    ]
    result = run_via_cli(create_cmd)
    coverage_env()
    assert result

    # uninstalling pip/setuptools now should leave us with a ensure_safe_to_do env
    site_package = result.creator.purelib
    pip = site_package / "pip"
    setuptools = site_package / "setuptools"

    files_post_first_create = list(site_package.iterdir())
    assert pip in files_post_first_create
    assert setuptools in files_post_first_create
    for pip_exe in [
            result.creator.script_dir /
            "pip{}{}".format(suffix, result.creator.exe.suffix) for suffix in (
                "",
                "{}".format(current.version_info.major),
                "-{}.{}".format(current.version_info.major,
                                current.version_info.minor),
            )
    ]:
        assert pip_exe.exists()
        process = Popen([
            six.ensure_text(str(pip_exe)), "--version",
            "--disable-pip-version-check"
        ])
        _, __ = process.communicate()
        assert not process.returncode

    remove_cmd = [
        str(result.creator.exe),
        "-m",
        "pip",
        "--verbose",
        "--disable-pip-version-check",
        "uninstall",
        "-y",
        "setuptools",
    ]
    process = Popen(remove_cmd)
    _, __ = process.communicate()
    assert not process.returncode
    assert site_package.exists()

    files_post_first_uninstall = list(site_package.iterdir())
    assert pip in files_post_first_uninstall
    assert setuptools not in files_post_first_uninstall

    # check we can run it again and will work - checks both overwrite and reuse cache
    result = run_via_cli(create_cmd)
    coverage_env()
    assert result
    files_post_second_create = list(site_package.iterdir())
    assert files_post_first_create == files_post_second_create

    process = Popen(remove_cmd + ["pip", "wheel"])
    _, __ = process.communicate()
    assert not process.returncode
    # pip is greedy here, removing all packages removes the site-package too
    if site_package.exists():
        post_run = list(site_package.iterdir())
        assert not post_run, "\n".join(str(i) for i in post_run)

    if sys.version_info[0:2] == (3, 4) and os.environ.get(
            str("PIP_REQ_TRACKER")):
        os.environ.pop(str("PIP_REQ_TRACKER"))
コード例 #9
0
import pytest

from virtualenv.discovery.builtin import Builtin, get_interpreter
from virtualenv.discovery.py_info import PythonInfo
from virtualenv.info import fs_supports_symlink
from virtualenv.util.path import Path
from virtualenv.util.six import ensure_text


@pytest.mark.skipif(not fs_supports_symlink(), reason="symlink not supported")
@pytest.mark.parametrize("case", ["mixed", "lower", "upper"])
def test_discovery_via_path(monkeypatch, case, tmp_path, caplog,
                            session_app_data):
    caplog.set_level(logging.DEBUG)
    current = PythonInfo.current_system(session_app_data)
    core = "somethingVeryCryptic{}".format(".".join(
        str(i) for i in current.version_info[0:3]))
    name = "somethingVeryCryptic"
    if case == "lower":
        name = name.lower()
    elif case == "upper":
        name = name.upper()
    exe_name = "{}{}{}".format(name, current.version_info.major,
                               ".exe" if sys.platform == "win32" else "")
    target = tmp_path / current.distutils_install["scripts"]
    target.mkdir(parents=True)
    executable = target / exe_name
    os.symlink(sys.executable, ensure_text(str(executable)))
    pyvenv_cfg = Path(sys.executable).parents[1] / "pyvenv.cfg"
    if pyvenv_cfg.exists():
コード例 #10
0
from threading import Thread

import pytest

from virtualenv.__main__ import run, run_with_catch
from virtualenv.create.creator import DEBUG_SCRIPT, Creator, get_env_debug_info
from virtualenv.create.via_global_ref.builtin.python2.python2 import Python2
from virtualenv.discovery.builtin import get_interpreter
from virtualenv.discovery.py_info import PythonInfo
from virtualenv.info import IS_PYPY, IS_WIN, PY2, PY3, fs_is_case_sensitive
from virtualenv.pyenv_cfg import PyEnvCfg
from virtualenv.run import cli_run, session_via_cli
from virtualenv.util.path import Path
from virtualenv.util.six import ensure_str, ensure_text

CURRENT = PythonInfo.current_system()


def test_os_path_sep_not_allowed(tmp_path, capsys):
    target = str(tmp_path / "a{}b".format(os.pathsep))
    err = _non_success_exit_code(capsys, target)
    msg = (
        "destination {!r} must not contain the path separator ({}) as this"
        " would break the activation scripts".format(target, os.pathsep)
    )
    assert msg in err, err


def _non_success_exit_code(capsys, target):
    with pytest.raises(SystemExit) as context:
        run_with_catch(args=[target])
コード例 #11
0
import sys
from uuid import uuid4

import pytest
import six

from virtualenv.discovery.builtin import get_interpreter
from virtualenv.discovery.py_info import PythonInfo
from virtualenv.info import fs_supports_symlink


@pytest.mark.skipif(not fs_supports_symlink(), reason="symlink not supported")
@pytest.mark.parametrize("case", ["mixed", "lower", "upper"])
def test_discovery_via_path(monkeypatch, case, special_name_dir, caplog):
    caplog.set_level(logging.DEBUG)
    current = PythonInfo.current_system()
    core = "somethingVeryCryptic{}".format(".".join(
        str(i) for i in current.version_info[0:3]))
    name = "somethingVeryCryptic"
    if case == "lower":
        name = name.lower()
    elif case == "upper":
        name = name.upper()
    exe_name = "{}{}{}".format(name, current.version_info.major,
                               ".exe" if sys.platform == "win32" else "")
    special_name_dir.mkdir()
    executable = special_name_dir / exe_name
    os.symlink(sys.executable, six.ensure_text(str(executable)))
    new_path = os.pathsep.join(
        [str(special_name_dir)] +
        os.environ.get(str("PATH"), str("")).split(os.pathsep))
コード例 #12
0
def current_creators():
    return PythonInfo.current_system().creators()
コード例 #13
0
def current_creators(session_app_data):
    return PythonInfo.current_system(session_app_data).creators()
コード例 #14
0
from virtualenv.run import cli_run


@pytest.mark.slow
def test_failed_to_find_bad_spec():
    of_id = uuid4().hex
    with pytest.raises(RuntimeError) as context:
        cli_run(["-p", of_id])
    msg = repr(
        RuntimeError(
            "failed to find interpreter for Builtin discover of python_spec={!r}"
            .format(of_id)))
    assert repr(context.value) == msg


SYSTEM = PythonInfo.current_system()


@pytest.mark.parametrize(
    "of_id",
    ({sys.executable} if sys.executable != SYSTEM.executable else set())
    | {SYSTEM.implementation})
def test_failed_to_find_implementation(of_id, mocker):
    mocker.patch("virtualenv.run.plugin.creators.CreatorSelector._OPTIONS",
                 return_value={})
    with pytest.raises(RuntimeError) as context:
        cli_run(["-p", of_id])
    assert repr(context.value) == repr(
        RuntimeError("No virtualenv implementation for {}".format(
            PythonInfo.current_system())))
コード例 #15
0
ファイル: test_creator.py プロジェクト: tucked/virtualenv
from textwrap import dedent
from threading import Thread

import pytest

from virtualenv.__main__ import run, run_with_catch
from virtualenv.create.creator import DEBUG_SCRIPT, Creator, get_env_debug_info
from virtualenv.discovery.builtin import get_interpreter
from virtualenv.discovery.py_info import PythonInfo
from virtualenv.info import IS_PYPY, IS_WIN, PY3, fs_is_case_sensitive, fs_supports_symlink
from virtualenv.pyenv_cfg import PyEnvCfg
from virtualenv.run import cli_run, session_via_cli
from virtualenv.util.path import Path
from virtualenv.util.six import ensure_str, ensure_text

CURRENT = PythonInfo.current_system()


def test_os_path_sep_not_allowed(tmp_path, capsys):
    target = str(tmp_path / "a{}b".format(os.pathsep))
    err = _non_success_exit_code(capsys, target)
    msg = ("destination {!r} must not contain the path separator ({}) as this"
           " would break the activation scripts".format(target, os.pathsep))
    assert msg in err, err


def _non_success_exit_code(capsys, target):
    with pytest.raises(SystemExit) as context:
        run_with_catch(args=[target])
    assert context.value.code != 0
    out, err = capsys.readouterr()
コード例 #16
0
ファイル: test_creator.py プロジェクト: pydawan/virtualenv
from itertools import product
from threading import Thread

import pytest

from virtualenv.__main__ import run, run_with_catch
from virtualenv.create.creator import DEBUG_SCRIPT, Creator, get_env_debug_info
from virtualenv.discovery.builtin import get_interpreter
from virtualenv.discovery.py_info import PythonInfo
from virtualenv.info import IS_PYPY, fs_supports_symlink
from virtualenv.pyenv_cfg import PyEnvCfg
from virtualenv.run import cli_run, session_via_cli
from virtualenv.util.path import Path
from virtualenv.util.six import ensure_text

CURRENT = PythonInfo.current_system()


def test_os_path_sep_not_allowed(tmp_path, capsys):
    target = str(tmp_path / "a{}b".format(os.pathsep))
    err = _non_success_exit_code(capsys, target)
    msg = (
        "destination {!r} must not contain the path separator ({}) as this"
        " would break the activation scripts".format(target, os.pathsep)
    )
    assert msg in err, err


def _non_success_exit_code(capsys, target):
    with pytest.raises(SystemExit) as context:
        run_with_catch(args=[target])
コード例 #17
0
import pytest

from virtualenv.discovery.py_info import PythonInfo
from virtualenv.run import run_via_cli


@pytest.mark.slow
def test_failed_to_find_bad_spec():
    of_id = uuid4().hex
    with pytest.raises(RuntimeError) as context:
        run_via_cli(["-p", of_id])
    msg = repr(
        RuntimeError(
            "failed to find interpreter for Builtin discover of python_spec={!r}"
            .format(of_id)))
    assert repr(context.value) == msg


@pytest.mark.parametrize(
    "of_id", [sys.executable,
              PythonInfo.current_system().implementation])
def test_failed_to_find_implementation(of_id, mocker):
    mocker.patch("virtualenv.run.plugin.creators.CreatorSelector._OPTIONS",
                 return_value={})
    with pytest.raises(RuntimeError) as context:
        run_via_cli(["-p", of_id])
    assert repr(context.value) == repr(
        RuntimeError("No virtualenv implementation for {}".format(
            PythonInfo.current_system())))