1
0
forked from VimPlug/jedi

Write a test for venvs

This commit is contained in:
Dave Halter
2018-04-08 23:04:57 +02:00
parent 0c19219143
commit 81d8c49119
3 changed files with 40 additions and 33 deletions

View File

@@ -1,5 +1,6 @@
import os import os
import re import re
import subprocess
import pytest import pytest
@@ -125,6 +126,33 @@ class StaticAnalysisCase(object):
return "<%s: %s>" % (self.__class__.__name__, os.path.basename(self._path)) return "<%s: %s>" % (self.__class__.__name__, os.path.basename(self._path))
@pytest.fixture()
def venv_path(tmpdir, environment):
if environment.version_info.major < 3:
pytest.skip("python -m venv does not exist in Python 2")
dirname = os.path.join(tmpdir.dirname, 'venv')
# We cannot use the Python from tox because tox creates virtualenvs and
# they have different site.py files that work differently than the default
# ones. Instead, we find the real Python executable by printing the value
# of sys.base_prefix or sys.real_prefix if we are in a virtualenv.
output = subprocess.check_output([
environment._executable, "-c",
"import sys; "
"print(sys.real_prefix if hasattr(sys, 'real_prefix') else sys.base_prefix)"
])
prefix = output.rstrip().decode('utf8')
if os.name == 'nt':
executable_path = os.path.join(prefix, 'python')
else:
executable_name = os.path.basename(environment._executable)
executable_path = os.path.join(prefix, 'bin', executable_name)
subprocess.call([executable_path, '-m', 'venv', dirname])
return dirname
@pytest.fixture() @pytest.fixture()
def cwd_tmpdir(monkeypatch, tmpdir): def cwd_tmpdir(monkeypatch, tmpdir):
with helpers.set_cwd(tmpdir.strpath): with helpers.set_cwd(tmpdir.strpath):

View File

@@ -1,5 +1,4 @@
import os import os
import sys
from contextlib import contextmanager from contextlib import contextmanager
import pytest import pytest
@@ -100,6 +99,13 @@ def set_environment_variable(name, value):
os.environ[name] = tmp os.environ[name] = tmp
def test_virtualenv(): def test_not_existing_virtualenv():
with set_environment_variable('VIRTUAL_ENV', '/foo/bar/jedi_baz'): """Should not match the path that was given"""
assert get_default_environment()._executable == sys.executable path = '/foo/bar/jedi_baz'
with set_environment_variable('VIRTUAL_ENV', path):
assert get_default_environment()._executable != path
def test_working_venv(venv_path):
with set_environment_variable('VIRTUAL_ENV', venv_path):
assert get_default_environment()._base_path == venv_path

View File

@@ -2,11 +2,8 @@ import os
from glob import glob from glob import glob
import sys import sys
import shutil import shutil
import subprocess
import pytest
from jedi.evaluate import sys_path from jedi.evaluate import sys_path
from jedi import find_virtualenvs
from jedi.api.environment import Environment from jedi.api.environment import Environment
@@ -29,35 +26,11 @@ def test_paths_from_assignment(Script):
assert paths('sys.path, other = ["a"], 2') == set() assert paths('sys.path, other = ["a"], 2') == set()
def test_venv_and_pths(tmpdir, environment): def test_venv_and_pths(venv_path):
if environment.version_info.major < 3:
pytest.skip("python -m venv does not exist in Python 2")
pjoin = os.path.join pjoin = os.path.join
dirname = pjoin(tmpdir.dirname, 'venv')
# We cannot use the Python from tox because tox creates virtualenvs and they
# have different site.py files that work differently than the default ones.
# Instead, we find the real Python executable by printing the value of
# sys.base_prefix or sys.real_prefix if we are in a virtualenv.
output = subprocess.check_output([
environment._executable, "-c",
"import sys; "
"print(sys.real_prefix if hasattr(sys, 'real_prefix') else "
"sys.base_prefix)"
])
prefix = output.rstrip().decode('utf8')
if os.name == 'nt':
executable_path = os.path.join(prefix, 'python')
else:
executable_name = os.path.basename(environment._executable)
executable_path = os.path.join(prefix, 'bin', executable_name)
subprocess.call([executable_path, '-m', 'venv', dirname])
bin_name = 'Scripts' if os.name == 'nt' else 'bin' bin_name = 'Scripts' if os.name == 'nt' else 'bin'
virtualenv = Environment(dirname, pjoin(dirname, bin_name, 'python')) virtualenv = Environment(venv_path, pjoin(venv_path, bin_name, 'python'))
CUR_DIR = os.path.dirname(__file__) CUR_DIR = os.path.dirname(__file__)
site_pkg_path = pjoin(virtualenv._base_path, 'lib') site_pkg_path = pjoin(virtualenv._base_path, 'lib')