Debian Codegraph @

conftest.py

raw symbols SCIP
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 # The following hooks (pytest_configure, pytest_unconfigure) are used
24 # to modify `jedi.settings.cache_directory` because `clean_jedi_cache`
25 # has no effect during doctests. Without these hooks, doctests uses
26 # user's cache (e.g., ~/.cache/jedi/). We should remove this
27 # workaround once the problem is fixed in pytest.
28 #
29 # See:
30 # - https://github.com/davidhalter/jedi/pull/168
31 # - https://bitbucket.org/hpk42/pytest/issue/275/
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 # This if is just needed to avoid that tests ever skip way more than
165 # they should for all Python versions.
166 pytest.skip()