| 1 |
import tempfile |
| 2 |
import shutil |
| 3 |
import os |
| 4 |
import sys |
| 5 |
from functools import partial |
| 6 |
|
| 7 |
import pytest |
| 8 |
|
| 9 |
import jedi |
| 10 |
from jedi.api.environment import get_system_environment, InterpreterEnvironment |
| 11 |
from test.helpers import test_dir |
| 12 |
|
| 13 |
collect_ignore = [ |
| 14 |
'setup.py', |
| 15 |
'jedi/__main__.py', |
| 16 |
'jedi/inference/compiled/subprocess/__main__.py', |
| 17 |
'build/', |
| 18 |
'test/examples', |
| 19 |
'sith.py', |
| 20 |
] |
| 21 |
|
| 22 |
|
| 23 |
|
| 24 |
|
| 25 |
|
| 26 |
|
| 27 |
|
| 28 |
|
| 29 |
|
| 30 |
|
| 31 |
|
| 32 |
|
| 33 |
jedi_cache_directory_orig = None |
| 34 |
jedi_cache_directory_temp = None |
| 35 |
|
| 36 |
|
| 37 |
def pytest_addoption(parser): |
| 38 |
parser.addoption("--jedi-debug", "-D", action='store_true', |
| 39 |
help="Enables Jedi's debug output.") |
| 40 |
|
| 41 |
parser.addoption("--warning-is-error", action='store_true', |
| 42 |
help="Warnings are treated as errors.") |
| 43 |
|
| 44 |
parser.addoption("--env", action='store', |
| 45 |
help="Execute the tests in that environment (e.g. 314 for python3.14).") |
| 46 |
parser.addoption("--interpreter-env", "-I", action='store_true', |
| 47 |
help="Don't use subprocesses to guarantee having safe " |
| 48 |
"code execution. Useful for debugging.") |
| 49 |
|
| 50 |
|
| 51 |
def pytest_configure(config): |
| 52 |
global jedi_cache_directory_orig, jedi_cache_directory_temp |
| 53 |
jedi_cache_directory_orig = jedi.settings.cache_directory |
| 54 |
jedi_cache_directory_temp = tempfile.mkdtemp(prefix='jedi-test-') |
| 55 |
jedi.settings.cache_directory = jedi_cache_directory_temp |
| 56 |
|
| 57 |
if config.option.jedi_debug: |
| 58 |
jedi.set_debug_function() |
| 59 |
|
| 60 |
if config.option.warning_is_error: |
| 61 |
import warnings |
| 62 |
warnings.simplefilter("error") |
| 63 |
|
| 64 |
|
| 65 |
def pytest_unconfigure(config): |
| 66 |
global jedi_cache_directory_orig, jedi_cache_directory_temp |
| 67 |
jedi.settings.cache_directory = jedi_cache_directory_orig |
| 68 |
shutil.rmtree(jedi_cache_directory_temp) |
| 69 |
|
| 70 |
|
| 71 |
@pytest.fixture(scope='session') |
| 72 |
def clean_jedi_cache(request): |
| 73 |
""" |
| 74 |
Set `jedi.settings.cache_directory` to a temporary directory during test. |
| 75 |
|
| 76 |
Note that you can't use built-in `tmpdir` and `monkeypatch` |
| 77 |
fixture here because their scope is 'function', which is not used |
| 78 |
in 'session' scope fixture. |
| 79 |
|
| 80 |
This fixture is activated in ../pytest.ini. |
| 81 |
""" |
| 82 |
from jedi import settings |
| 83 |
old = settings.cache_directory |
| 84 |
tmp = tempfile.mkdtemp(prefix='jedi-test-') |
| 85 |
settings.cache_directory = tmp |
| 86 |
|
| 87 |
@request.addfinalizer |
| 88 |
def restore(): |
| 89 |
settings.cache_directory = old |
| 90 |
shutil.rmtree(tmp) |
| 91 |
|
| 92 |
|
| 93 |
@pytest.fixture(scope='session') |
| 94 |
def environment(request): |
| 95 |
version = request.config.option.env |
| 96 |
if version is None: |
| 97 |
v = str(sys.version_info[0]) + str(sys.version_info[1]) |
| 98 |
version = os.environ.get('JEDI_TEST_ENVIRONMENT', v) |
| 99 |
|
| 100 |
if request.config.option.interpreter_env or version == 'interpreter': |
| 101 |
return InterpreterEnvironment() |
| 102 |
|
| 103 |
if '.' not in version: |
| 104 |
version = version[0] + '.' + version[1:] |
| 105 |
return get_system_environment(version) |
| 106 |
|
| 107 |
|
| 108 |
@pytest.fixture(scope='session') |
| 109 |
def Script(environment): |
| 110 |
return partial(jedi.Script, environment=environment) |
| 111 |
|
| 112 |
|
| 113 |
@pytest.fixture(scope='session') |
| 114 |
def ScriptWithProject(Script): |
| 115 |
project = jedi.Project(test_dir) |
| 116 |
return partial(jedi.Script, project=project) |
| 117 |
|
| 118 |
|
| 119 |
@pytest.fixture(scope='session') |
| 120 |
def get_names(Script): |
| 121 |
return lambda code, **kwargs: Script(code).get_names(**kwargs) |
| 122 |
|
| 123 |
|
| 124 |
@pytest.fixture(scope='session', params=['goto', 'infer']) |
| 125 |
def goto_or_infer(request, Script): |
| 126 |
return lambda code, *args, **kwargs: getattr(Script(code), request.param)(*args, **kwargs) |
| 127 |
|
| 128 |
|
| 129 |
@pytest.fixture(scope='session', params=['goto', 'help']) |
| 130 |
def goto_or_help(request, Script): |
| 131 |
return lambda code, *args, **kwargs: getattr(Script(code), request.param)(*args, **kwargs) |
| 132 |
|
| 133 |
|
| 134 |
@pytest.fixture(scope='session', params=['goto', 'help', 'infer']) |
| 135 |
def goto_or_help_or_infer(request, Script): |
| 136 |
class GotoOrHelpOrInfer: |
| 137 |
def __call__(self, code, *args, **kwargs): |
| 138 |
return getattr(Script(code), request.param)(*args, **kwargs) |
| 139 |
|
| 140 |
type = request.param |
| 141 |
|
| 142 |
return GotoOrHelpOrInfer() |
| 143 |
|
| 144 |
|
| 145 |
@pytest.fixture(scope='session', params=['goto', 'complete', 'help']) |
| 146 |
def goto_or_complete(request, Script): |
| 147 |
return lambda code, *args, **kwargs: getattr(Script(code), request.param)(*args, **kwargs) |
| 148 |
|
| 149 |
|
| 150 |
@pytest.fixture(scope='session') |
| 151 |
def has_django(environment): |
| 152 |
script = jedi.Script('import django', environment=environment) |
| 153 |
return bool(script.infer()) |
| 154 |
|
| 155 |
|
| 156 |
@pytest.fixture(scope='session') |
| 157 |
def jedi_path(): |
| 158 |
return os.path.dirname(__file__) |
| 159 |
|
| 160 |
|
| 161 |
@pytest.fixture() |
| 162 |
def skip_pre_python311(environment): |
| 163 |
if environment.version_info < (3, 11): |
| 164 |
|
| 165 |
|
| 166 |
pytest.skip() |