diff --git a/test/conftest.py b/test/conftest.py index 34972793..59f423b3 100644 --- a/test/conftest.py +++ b/test/conftest.py @@ -1,5 +1,6 @@ import os import re +import subprocess import pytest @@ -125,6 +126,33 @@ class StaticAnalysisCase(object): 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() def cwd_tmpdir(monkeypatch, tmpdir): with helpers.set_cwd(tmpdir.strpath): diff --git a/test/test_api/test_environment.py b/test/test_api/test_environment.py index f8fcfd6a..f070750c 100644 --- a/test/test_api/test_environment.py +++ b/test/test_api/test_environment.py @@ -1,5 +1,4 @@ import os -import sys from contextlib import contextmanager import pytest @@ -100,6 +99,13 @@ def set_environment_variable(name, value): os.environ[name] = tmp -def test_virtualenv(): - with set_environment_variable('VIRTUAL_ENV', '/foo/bar/jedi_baz'): - assert get_default_environment()._executable == sys.executable +def test_not_existing_virtualenv(): + """Should not match the path that was given""" + 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 diff --git a/test/test_evaluate/test_sys_path.py b/test/test_evaluate/test_sys_path.py index e9cd2e40..51deea21 100644 --- a/test/test_evaluate/test_sys_path.py +++ b/test/test_evaluate/test_sys_path.py @@ -2,11 +2,8 @@ import os from glob import glob import sys import shutil -import subprocess -import pytest from jedi.evaluate import sys_path -from jedi import find_virtualenvs from jedi.api.environment import Environment @@ -29,35 +26,11 @@ def test_paths_from_assignment(Script): assert paths('sys.path, other = ["a"], 2') == set() -def test_venv_and_pths(tmpdir, environment): - if environment.version_info.major < 3: - pytest.skip("python -m venv does not exist in Python 2") - +def test_venv_and_pths(venv_path): 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' - virtualenv = Environment(dirname, pjoin(dirname, bin_name, 'python')) + virtualenv = Environment(venv_path, pjoin(venv_path, bin_name, 'python')) CUR_DIR = os.path.dirname(__file__) site_pkg_path = pjoin(virtualenv._base_path, 'lib')