mirror of
https://github.com/davidhalter/jedi.git
synced 2025-12-06 14:04:26 +08:00
Test if virtualenvs and pth files work
This commit is contained in:
@@ -78,8 +78,8 @@ def find_virtualenvs(paths=None):
|
|||||||
paths = []
|
paths = []
|
||||||
|
|
||||||
for path in paths:
|
for path in paths:
|
||||||
executable = _get_executable_path(path)
|
|
||||||
try:
|
try:
|
||||||
|
executable = _get_executable_path(path)
|
||||||
yield Environment(path, executable)
|
yield Environment(path, executable)
|
||||||
except InvalidPythonEnvironment:
|
except InvalidPythonEnvironment:
|
||||||
pass
|
pass
|
||||||
@@ -123,7 +123,7 @@ def _get_executable_path(path):
|
|||||||
activate = os.path.join(bin_folder, 'activate')
|
activate = os.path.join(bin_folder, 'activate')
|
||||||
python = os.path.join(bin_folder, 'python')
|
python = os.path.join(bin_folder, 'python')
|
||||||
if not all(os.path.exists(p) for p in (activate, python)):
|
if not all(os.path.exists(p) for p in (activate, python)):
|
||||||
return None
|
raise InvalidPythonEnvironment("One of bin/activate and bin/python is missing.")
|
||||||
return python
|
return python
|
||||||
|
|
||||||
|
|
||||||
|
|||||||
@@ -0,0 +1 @@
|
|||||||
|
import smth; smth.extend_path_foo()
|
||||||
6
test/test_evaluate/sample_venvs/pth_directory/smth.py
Normal file
6
test/test_evaluate/sample_venvs/pth_directory/smth.py
Normal file
@@ -0,0 +1,6 @@
|
|||||||
|
import sys
|
||||||
|
sys.path.append('/foo/smth.py:module')
|
||||||
|
|
||||||
|
|
||||||
|
def extend_path_foo():
|
||||||
|
sys.path.append('/foo/smth.py:from_func')
|
||||||
@@ -1 +0,0 @@
|
|||||||
import smth; smth.extend_path()
|
|
||||||
@@ -1,6 +0,0 @@
|
|||||||
import sys
|
|
||||||
sys.path.append('/path/from/smth.py')
|
|
||||||
|
|
||||||
|
|
||||||
def extend_path():
|
|
||||||
sys.path.append('/path/from/smth.py:extend_path')
|
|
||||||
@@ -1,2 +0,0 @@
|
|||||||
# This file is here to force git to create the directory, as *.pth files only
|
|
||||||
# add existing directories.
|
|
||||||
@@ -1 +0,0 @@
|
|||||||
/path/from/egg-link
|
|
||||||
@@ -1 +0,0 @@
|
|||||||
./dir-from-foo-pth
|
|
||||||
@@ -1 +0,0 @@
|
|||||||
import smth; smth.extend_path()
|
|
||||||
@@ -1 +0,0 @@
|
|||||||
./relative/egg-link/path
|
|
||||||
@@ -1,6 +0,0 @@
|
|||||||
import sys
|
|
||||||
sys.path.append('/path/from/smth.py')
|
|
||||||
|
|
||||||
|
|
||||||
def extend_path():
|
|
||||||
sys.path.append('/path/from/smth.py:extend_path')
|
|
||||||
@@ -1,10 +1,12 @@
|
|||||||
import os
|
import os
|
||||||
from glob import glob
|
from glob import glob
|
||||||
import sys
|
import sys
|
||||||
|
import shutil
|
||||||
|
|
||||||
import pytest
|
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
|
||||||
|
|
||||||
|
|
||||||
def test_paths_from_assignment(Script):
|
def test_paths_from_assignment(Script):
|
||||||
@@ -21,37 +23,44 @@ def test_paths_from_assignment(Script):
|
|||||||
assert paths('sys.path, other = ["a"], 2') == set()
|
assert paths('sys.path, other = ["a"], 2') == set()
|
||||||
|
|
||||||
|
|
||||||
# Currently venv site-packages resolution only seeks pythonX.Y/site-packages
|
def test_venv_and_pths(tmpdir, environment):
|
||||||
# that belong to the same version as the interpreter to avoid issues with
|
if environment.version_info.major < 3:
|
||||||
# cross-version imports. "venvs/" dir contains "venv27" and "venv34" that
|
pytest.skip("python -m venv does not exist in Python 2")
|
||||||
# mimic venvs created for py2.7 and py3.4 respectively. If test runner is
|
|
||||||
# invoked with one of those versions, the test below will be run for the
|
|
||||||
# matching directory.
|
|
||||||
CUR_DIR = os.path.dirname(__file__)
|
|
||||||
VENVS = list(glob(
|
|
||||||
os.path.join(CUR_DIR, 'sample_venvs/venv%d%d' % sys.version_info[:2])))
|
|
||||||
|
|
||||||
|
|
||||||
@pytest.mark.parametrize('venv', VENVS)
|
|
||||||
def test_get_venv_path(venv):
|
|
||||||
pjoin = os.path.join
|
pjoin = os.path.join
|
||||||
venv_path = sys_path.get_venv_path(venv)
|
|
||||||
|
|
||||||
site_pkgs = (glob(pjoin(venv, 'lib', 'python*', 'site-packages')) +
|
dirname = pjoin(tmpdir.dirname, 'venv')
|
||||||
glob(pjoin(venv, 'lib', 'site-packages')))[0]
|
|
||||||
|
# Ignore if it fails. It usually fails if it's not able to properly install
|
||||||
|
# pip. However we don't need that for this test.
|
||||||
|
os.system(environment._executable + ' -m venv ' + dirname)
|
||||||
|
|
||||||
|
# We cannot find the virtualenv in some cases, because the virtualenv was
|
||||||
|
# not created correctly.
|
||||||
|
virtualenv = Environment(dirname, pjoin(dirname, 'bin', 'python'))
|
||||||
|
|
||||||
|
CUR_DIR = os.path.dirname(__file__)
|
||||||
|
site_pkg_path = glob(pjoin(virtualenv._base_path, 'lib', 'python*', 'site-packages'))[0]
|
||||||
|
shutil.rmtree(site_pkg_path)
|
||||||
|
shutil.copytree(pjoin(CUR_DIR, 'sample_venvs/pth_directory'), site_pkg_path)
|
||||||
|
|
||||||
|
venv_paths = virtualenv.get_sys_path()
|
||||||
|
|
||||||
ETALON = [
|
ETALON = [
|
||||||
pjoin('/path', 'from', 'egg-link'),
|
# For now disable egg-links. I have no idea how they work... ~ dave
|
||||||
pjoin(site_pkgs, '.', 'relative', 'egg-link', 'path'),
|
#pjoin('/path', 'from', 'egg-link'),
|
||||||
site_pkgs,
|
#pjoin(site_pkg_path, '.', 'relative', 'egg-link', 'path'),
|
||||||
pjoin(site_pkgs, 'dir-from-foo-pth'),
|
site_pkg_path,
|
||||||
|
pjoin(site_pkg_path, 'dir-from-foo-pth'),
|
||||||
|
'/foo/smth.py:module',
|
||||||
|
# Not sure why it's added twice. It has to do with site.py which is not
|
||||||
|
# something we can change. However this obviously also doesn't matter.
|
||||||
|
'/foo/smth.py:from_func',
|
||||||
|
'/foo/smth.py:from_func',
|
||||||
]
|
]
|
||||||
|
|
||||||
# Ensure that pth and egg-link paths were added.
|
# Ensure that pth and egg-link paths were added.
|
||||||
assert venv_path[:len(ETALON)] == ETALON
|
assert venv_paths[-len(ETALON):] == ETALON
|
||||||
|
|
||||||
# Ensure that none of venv dirs leaked to the interpreter.
|
# Ensure that none of venv dirs leaked to the interpreter.
|
||||||
assert not set(sys.path).intersection(ETALON)
|
assert not set(sys.path).intersection(ETALON)
|
||||||
|
|
||||||
# Ensure that "import ..." lines were ignored.
|
|
||||||
assert pjoin('/path', 'from', 'smth.py') not in venv_path
|
|
||||||
assert pjoin('/path', 'from', 'smth.py:extend_path') not in venv_path
|
|
||||||
|
|||||||
Reference in New Issue
Block a user