| 1 |
import os |
| 2 |
import sys |
| 3 |
|
| 4 |
import pytest |
| 5 |
|
| 6 |
import jedi |
| 7 |
from jedi.api.environment import get_default_environment, find_virtualenvs, \ |
| 8 |
InvalidPythonEnvironment, find_system_environments, \ |
| 9 |
get_system_environment, create_environment, InterpreterEnvironment, \ |
| 10 |
get_cached_default_environment |
| 11 |
|
| 12 |
|
| 13 |
def test_sys_path(): |
| 14 |
assert get_default_environment().get_sys_path() |
| 15 |
|
| 16 |
|
| 17 |
def test_find_system_environments(): |
| 18 |
envs = list(find_system_environments()) |
| 19 |
assert len(envs) |
| 20 |
for env in envs: |
| 21 |
assert env.version_info |
| 22 |
assert env.get_sys_path() |
| 23 |
parser_version = env.get_grammar().version_info |
| 24 |
assert parser_version[:2] == env.version_info[:2] |
| 25 |
|
| 26 |
|
| 27 |
@pytest.mark.parametrize( |
| 28 |
'version', |
| 29 |
jedi.api.environment._SUPPORTED_PYTHONS, |
| 30 |
) |
| 31 |
def test_versions(version): |
| 32 |
try: |
| 33 |
env = get_system_environment(version) |
| 34 |
except InvalidPythonEnvironment: |
| 35 |
if version.replace('.', '') == str(sys.version_info[0]) + str(sys.version_info[1]): |
| 36 |
|
| 37 |
raise |
| 38 |
pytest.skip() |
| 39 |
|
| 40 |
assert version == str(env.version_info[0]) + '.' + str(env.version_info[1]) |
| 41 |
assert env.get_sys_path() |
| 42 |
|
| 43 |
|
| 44 |
def test_load_module(inference_state): |
| 45 |
access_path = inference_state.compiled_subprocess.load_module( |
| 46 |
dotted_name='math', |
| 47 |
sys_path=inference_state.get_sys_path() |
| 48 |
) |
| 49 |
name, access_handle = access_path.accesses[0] |
| 50 |
|
| 51 |
assert access_handle.py__bool__() is True |
| 52 |
assert access_handle.get_api_type() == 'module' |
| 53 |
with pytest.raises(AttributeError): |
| 54 |
access_handle.py__mro__() |
| 55 |
|
| 56 |
|
| 57 |
def test_error_in_environment(inference_state, Script, environment): |
| 58 |
if isinstance(environment, InterpreterEnvironment): |
| 59 |
pytest.skip("We don't catch these errors at the moment.") |
| 60 |
|
| 61 |
|
| 62 |
with pytest.raises(jedi.InternalError): |
| 63 |
inference_state.compiled_subprocess._test_raise_error(KeyboardInterrupt) |
| 64 |
|
| 65 |
with pytest.raises(jedi.InternalError): |
| 66 |
inference_state.compiled_subprocess._test_raise_error(KeyboardInterrupt) |
| 67 |
|
| 68 |
def_, = Script('str').infer() |
| 69 |
assert def_.name == 'str' |
| 70 |
|
| 71 |
|
| 72 |
def test_stdout_in_subprocess(inference_state, Script): |
| 73 |
inference_state.compiled_subprocess._test_print(stdout='.') |
| 74 |
Script('1').infer() |
| 75 |
|
| 76 |
|
| 77 |
def test_killed_subprocess(inference_state, Script, environment): |
| 78 |
if isinstance(environment, InterpreterEnvironment): |
| 79 |
pytest.skip("We cannot kill our own process") |
| 80 |
|
| 81 |
inference_state.compiled_subprocess._compiled_subprocess._get_process().kill() |
| 82 |
|
| 83 |
|
| 84 |
with pytest.raises(jedi.InternalError): |
| 85 |
Script('str').infer() |
| 86 |
|
| 87 |
def_, = Script('str').infer() |
| 88 |
|
| 89 |
assert def_.name == 'str' |
| 90 |
|
| 91 |
|
| 92 |
def test_not_existing_virtualenv(monkeypatch): |
| 93 |
"""Should not match the path that was given""" |
| 94 |
path = '/foo/bar/jedi_baz' |
| 95 |
monkeypatch.setenv('VIRTUAL_ENV', path) |
| 96 |
assert get_default_environment().executable != path |
| 97 |
|
| 98 |
|
| 99 |
def test_working_venv(venv_path, monkeypatch): |
| 100 |
monkeypatch.setenv('VIRTUAL_ENV', venv_path) |
| 101 |
assert get_default_environment().path == venv_path |
| 102 |
|
| 103 |
|
| 104 |
def test_scanning_venvs(venv_path): |
| 105 |
parent_dir = os.path.dirname(venv_path) |
| 106 |
assert any(venv.path == venv_path |
| 107 |
for venv in find_virtualenvs([parent_dir])) |
| 108 |
|
| 109 |
|
| 110 |
def test_create_environment_venv_path(venv_path): |
| 111 |
environment = create_environment(venv_path) |
| 112 |
assert environment.path == venv_path |
| 113 |
|
| 114 |
|
| 115 |
def test_create_environment_executable(): |
| 116 |
environment = create_environment(sys.executable) |
| 117 |
assert environment.executable == sys.executable |
| 118 |
|
| 119 |
|
| 120 |
def test_get_default_environment_from_env_does_not_use_safe(tmpdir, monkeypatch): |
| 121 |
fake_python = os.path.join(str(tmpdir), 'fake_python') |
| 122 |
with open(fake_python, 'w', newline='') as f: |
| 123 |
f.write('') |
| 124 |
|
| 125 |
def _get_subprocess(self): |
| 126 |
if self._start_executable != fake_python: |
| 127 |
raise RuntimeError('Should not get called!') |
| 128 |
self.executable = fake_python |
| 129 |
self.path = 'fake' |
| 130 |
|
| 131 |
monkeypatch.setattr('jedi.api.environment.Environment._get_subprocess', |
| 132 |
_get_subprocess) |
| 133 |
|
| 134 |
monkeypatch.setenv('VIRTUAL_ENV', fake_python) |
| 135 |
env = get_default_environment() |
| 136 |
assert env.path == 'fake' |
| 137 |
|
| 138 |
|
| 139 |
@pytest.mark.parametrize('virtualenv', ['', 'fufuuuuu', sys.prefix]) |
| 140 |
def test_get_default_environment_when_embedded(monkeypatch, virtualenv): |
| 141 |
|
| 142 |
|
| 143 |
executable_name = 'RANDOM_EXE' |
| 144 |
monkeypatch.setattr(sys, 'executable', executable_name) |
| 145 |
monkeypatch.setenv('VIRTUAL_ENV', virtualenv) |
| 146 |
env = get_default_environment() |
| 147 |
assert env.executable != executable_name |
| 148 |
|
| 149 |
|
| 150 |
def test_changing_venv(venv_path, monkeypatch): |
| 151 |
monkeypatch.setitem(os.environ, 'VIRTUAL_ENV', venv_path) |
| 152 |
get_cached_default_environment() |
| 153 |
monkeypatch.setitem(os.environ, 'VIRTUAL_ENV', sys.executable) |
| 154 |
assert get_cached_default_environment().executable == sys.executable |