Debian Codegraph @

test/test_api/test_project.py

raw symbols SCIP
1 import os
2 from pathlib import Path
3
4 import pytest
5
6 from ..helpers import get_example_dir, set_cwd, root_dir, test_dir
7 from jedi import Interpreter
8 from jedi.api import Project, get_default_project
9 from jedi.api.project import _is_potential_project, _CONTAINS_POTENTIAL_PROJECT
10
11
12 def test_django_default_project(Script):
13 dir = get_example_dir('django')
14
15 script = Script(
16 "from app import models\nmodels.SomeMo",
17 path=os.path.join(dir, 'models/x.py')
18 )
19 c, = script.complete()
20 assert c.name == "SomeModel"
21
22 project = script._inference_state.project
23 assert project._django is True
24 assert project.sys_path is None
25 assert project.smart_sys_path is True
26 assert project.load_unsafe_extensions is False
27
28
29 def test_django_default_project_of_file(Script):
30 project = get_default_project(__file__)
31 assert project._path == Path(__file__).parent.parent.parent
32
33
34 def test_interpreter_project_path():
35 # Run from anywhere it should be the cwd.
36 dir = Path(root_dir).joinpath('test')
37 with set_cwd(dir):
38 project = Interpreter('', [locals()])._inference_state.project
39 assert project._path == dir
40
41
42 def test_added_sys_path(inference_state):
43 project = get_default_project()
44 p = '/some_random_path'
45 project.added_sys_path = [p]
46 assert p in project._get_sys_path(inference_state)
47
48
49 def test_load_save_project(tmpdir):
50 project = Project(tmpdir.strpath, added_sys_path=['/foo'])
51 project.save()
52
53 loaded = Project.load(tmpdir.strpath)
54 assert loaded.added_sys_path == ['/foo']
55
56
57 @pytest.mark.parametrize(
58 'string, full_names, kwargs', [
59 ('test_load_save_project', ['test_api.test_project.test_load_save_project'], {}),
60 ('test_load_savep', [], dict(complete=True)),
61 ('test_load_save_p', ['test_api.test_project.test_load_save_project'],
62 dict(complete=True)),
63 ('test_load_save_p', ['test_api.test_project.test_load_save_project'],
64 dict(complete=True, all_scopes=True)),
65
66 ('some_search_test_var', [], {}),
67 ('some_search_test_var', ['test_api.test_project.test_search.some_search_test_var'],
68 dict(all_scopes=True)),
69 ('some_search_test_var', ['test_api.test_project.test_search.some_search_test_var'],
70 dict(complete=True, all_scopes=True)),
71 # Make sure that the searched name is not part of the file, by
72 # splitting it up.
73 ('some_search_test_v' + 'a', ['test_api.test_project.test_search.some_search_test_var'],
74 dict(complete=True, all_scopes=True)),
75
76 ('sample_int', ['helpers.sample_int'], {}),
77 ('sample_int', ['helpers.sample_int'], dict(all_scopes=True)),
78 ('sample_int.real', ['stub:builtins.int.real'], {}),
79
80 ('class sample_int.real', [], {}),
81 ('foo sample_int.real', [], {}),
82 ('def sample_int.to_bytes', ['stub:builtins.int.to_bytes'], {}),
83 ('function sample_int.to_bytes', ['stub:builtins.int.to_bytes'], {}),
84 ('property sample_int.real', ['stub:builtins.int.real'], {}),
85
86 # With modules
87 ('test_project.test_search', ['test_api.test_project.test_search'], {}),
88 ('test_project.test_searc', ['test_api.test_project.test_search'], dict(complete=True)),
89 ('test_api.test_project.test_search', ['test_api.test_project.test_search'], {}),
90 ('test_api.test_project.test_sear', ['test_api.test_project.test_search'],
91 dict(complete=True)),
92
93 # With namespace
94 ('implicit_namespace_package.ns1.pkg',
95 ['examples.implicit_namespace_package.ns1.pkg'], {}),
96 ('implicit_namespace_package.ns1.pkg.ns1_file',
97 ['examples.implicit_namespace_package.ns1.pkg.ns1_file'], {}),
98 ('examples.implicit_namespace_package.ns1.pkg.ns1_file',
99 ['examples.implicit_namespace_package.ns1.pkg.ns1_file'], {}),
100 ('implicit_namespace_package.ns1.pkg.',
101 ['examples.implicit_namespace_package.ns1.pkg.ns1_file'],
102 dict(complete=True)),
103 ('implicit_namespace_package.',
104 ['examples.implicit_namespace_package.ns1',
105 'examples.implicit_namespace_package.ns2'],
106 dict(complete=True)),
107
108 # With stubs
109 ('with_python.module', ['examples.stub_packages.with_python.module'], {}),
110 ('with_python.modul', ['examples.stub_packages.with_python.module'],
111 dict(complete=True)),
112 ('no_python.foo', ['stub:examples.stub_packages.no_python.foo'], {}),
113 ('no_python.fo', ['stub:examples.stub_packages.no_python.foo'],
114 dict(complete=True)),
115 ('with_python-stubs.module', [], {}),
116 ('no_python-stubs.foo', [], {}),
117 # Both locations are given, because they live in separate folders (one
118 # suffixed with -stubs.
119 ('with_python', ['examples.stub_packages.with_python'], {}),
120 ('no_python', ['stub:examples.stub_packages.no_python'], {}),
121 # Completion stubs
122 ('stub_only', ['stub:completion.stub_folder.stub_only',
123 'stub:examples.stub_packages.with_python.stub_only'], {}),
124 ('with_stub', ['completion.stub_folder.with_stub'], {}),
125 ('with_stub.in_with_stub_both',
126 ['completion.stub_folder.with_stub.in_with_stub_both'], {}),
127 ('with_stub.in_with_stub_python',
128 ['completion.stub_folder.with_stub.in_with_stub_python'], {}),
129 ('with_stub.in_with_stub_stub',
130 ['stub:completion.stub_folder.with_stub.in_with_stub_stub'], {}),
131 # Completion stubs: Folder
132 ('with_stub_folder', ['completion.stub_folder.with_stub_folder'], {}),
133 ('with_stub_folder.nested_with_stub',
134 ['completion.stub_folder.with_stub_folder.nested_with_stub'], {}),
135 ('nested_with_stub',
136 ['completion.stub_folder.stub_only_folder.nested_with_stub',
137 'completion.stub_folder.with_stub_folder.nested_with_stub'], {}),
138
139 # On sys path
140 ('sys.path', ['stub:sys.path'], {}),
141 ('json.dumps', ['json.dumps'], {}), # stdlib + stub
142 ('multiprocessing', ['multiprocessing'], {}),
143 ('multiprocessin', ['multiprocessing'], dict(complete=True)),
144 ]
145 )
146 def test_search(string, full_names, kwargs):
147 some_search_test_var = 1.0 # noqa: F841
148 project = Project(test_dir)
149 if kwargs.pop('complete', False) is True:
150 defs = project.complete_search(string, **kwargs)
151 else:
152 defs = project.search(string, **kwargs)
153 actual = sorted([
154 ('stub:' if d.is_stub() else '') + (d.full_name or d.name)
155 for d in defs
156 ])
157 assert actual == full_names
158
159
160 @pytest.mark.parametrize(
161 'string, completions, all_scopes', [
162 ('SomeCl', ['ass'], False),
163 ('twic', [], False),
164 ('twic', ['e', 'e'], True),
165 ('test_load_save_p', ['roject'], False),
166 ]
167 )
168 def test_complete_search(Script, string, completions, all_scopes):
169 project = Project(test_dir)
170 defs = project.complete_search(string, all_scopes=all_scopes)
171 assert [d.complete for d in defs] == completions
172
173
174 @pytest.mark.parametrize(
175 'path,expected', [
176 (Path(__file__).parents[2], True), # The path of the project
177 (Path(__file__).parents[1], False), # The path of the tests, not a project
178 (Path.home(), None)
179 ]
180 )
181 def test_is_potential_project(path, expected):
182
183 if expected is None:
184 try:
185 expected = bool(set(_CONTAINS_POTENTIAL_PROJECT) & set(os.listdir(path)))
186 except OSError:
187 expected = False
188
189 assert _is_potential_project(path) == expected