Secure your code as it's written. Use Snyk Code to scan source code in minutes - no build needed - and fix issues immediately.
def test_create_environment_with_exec_prefix_pointing_to_prefix(tmpdir):
"""Create virtual environment for Python with ``sys.exec_prefix`` pointing
to ``sys.prefix`` or ``sys.base_prefix`` or ``sys.real_prefix`` under a
different name
"""
venvdir = str(tmpdir / "venv")
python_dir = tmpdir / "python"
python_dir.mkdir()
path_key = str("PATH")
old_path = os.environ[path_key]
if hasattr(sys, "real_prefix"):
os.environ[path_key] = os.pathsep.join(
p for p in os.environ[path_key].split(os.pathsep) if not p.startswith(sys.prefix)
)
python = virtualenv.resolve_interpreter(os.path.basename(sys.executable))
try:
subprocess.check_call([sys.executable, "-m", "virtualenv", "-p", python, venvdir])
home_dir, lib_dir, inc_dir, bin_dir = virtualenv.path_locations(venvdir)
assert not os.path.islink(os.path.join(lib_dir, "distutils"))
finally:
os.environ[path_key] = old_path
def test_resolve_interpreter_with_absolute_path(mock_exists, mock_get_installed_pythons, mock_is_executable):
"""Should return absolute path if given and exists"""
test_abs_path = os.path.abspath("/usr/bin/python53")
exe = virtualenv.resolve_interpreter(test_abs_path)
assert exe == test_abs_path, "Absolute path should return as is"
mock_exists.assert_called_with(test_abs_path)
mock_is_executable.assert_called_with(test_abs_path)
def test_resolve_interpreter_with_installed_python(
mock_abspath, mock_exists, mock_get_installed_pythons, mock_is_executable, mock_find_executable
):
test_tag = "foo"
test_path = "/path/to/foo/python.exe"
test_abs_path = "some-abs-path"
test_found_path = "some-found-path"
mock_get_installed_pythons.return_value = {test_tag: test_path, test_tag + "2": test_path + "2"}
mock_abspath.return_value = test_abs_path
mock_find_executable.return_value = test_found_path
exe = virtualenv.resolve_interpreter("foo")
assert exe == test_found_path, "installed python should be accessible by key"
mock_get_installed_pythons.assert_called_once_with()
mock_abspath.assert_called_once_with(test_path)
mock_find_executable.assert_called_once_with(test_path)
mock_exists.assert_called_once_with(test_found_path)
mock_is_executable.assert_called_once_with(test_found_path)
def test_resolve_interpreter_with_nonexistent_interpreter(mock_exists, mock_get_installed_pythons):
"""Should SystemExit with an nonexistent python interpreter path"""
with pytest.raises(SystemExit):
virtualenv.resolve_interpreter("/usr/bin/python53")
mock_exists.assert_called_with("/usr/bin/python53")
def test_resolve_interpreter_with_invalid_interpreter(mock_exists, mock_is_executable):
"""Should exit when with absolute path if not exists"""
invalid = os.path.abspath("/usr/bin/pyt_hon53")
with pytest.raises(SystemExit):
virtualenv.resolve_interpreter(invalid)
mock_exists.assert_called_with(invalid)
mock_is_executable.assert_called_with(invalid)
def adjust_options(options, args):
# TODO-TEST: proper error message with no arguments
venv_path = return_values.venv_path = args[0]
if venv_path == DEFAULT_VIRTUALENV_PATH or options.prompt == '':
from os.path import abspath, basename, dirname
options.prompt = '(%s)' % basename(dirname(abspath(venv_path)))
# end of option munging.
# there are two python interpreters involved here:
# 1) the interpreter we're instructing virtualenv to copy
if options.python is None:
source_python = None
else:
source_python = virtualenv.resolve_interpreter(options.python)
# 2) the interpreter virtualenv will create
destination_python = venv_python(venv_path)
if exists(destination_python):
reason = invalid_virtualenv_reason(venv_path, source_python, destination_python, options)
if reason:
info('Removing invalidated virtualenv. (%s)' % reason)
run(('rm', '-rf', venv_path))
else:
info('Keeping valid virtualenv from previous run.')
raise SystemExit(0) # looks good! we're done here.