mirror of
https://github.com/davidhalter/jedi.git
synced 2025-12-06 22:14:27 +08:00
Use project instead of sys_path parameter in tests
This commit is contained in:
@@ -129,10 +129,11 @@ def test_completion_docstring(Script, jedi_path):
|
|||||||
Jedi should follow imports in certain conditions
|
Jedi should follow imports in certain conditions
|
||||||
"""
|
"""
|
||||||
def docstr(src, result):
|
def docstr(src, result):
|
||||||
c = Script(src, sys_path=[jedi_path]).complete()[0]
|
c = Script(src, project=project).complete()[0]
|
||||||
assert c.docstring(raw=True, fast=False) == cleandoc(result)
|
assert c.docstring(raw=True, fast=False) == cleandoc(result)
|
||||||
|
|
||||||
c = Script('import jedi\njed', sys_path=[jedi_path]).complete()[0]
|
project = jedi.Project('.', sys_path=[jedi_path])
|
||||||
|
c = Script('import jedi\njed', project=project).complete()[0]
|
||||||
assert c.docstring(fast=False) == cleandoc(jedi_doc)
|
assert c.docstring(fast=False) == cleandoc(jedi_doc)
|
||||||
|
|
||||||
docstr('import jedi\njedi.Scr', cleandoc(jedi.Script.__doc__))
|
docstr('import jedi\njedi.Scr', cleandoc(jedi.Script.__doc__))
|
||||||
|
|||||||
@@ -97,9 +97,10 @@ def test_sub_module(Script, jedi_path):
|
|||||||
path.
|
path.
|
||||||
"""
|
"""
|
||||||
sys_path = [jedi_path]
|
sys_path = [jedi_path]
|
||||||
defs = Script('from jedi.api import classes; classes', sys_path=sys_path).infer()
|
project = jedi.Project('.', sys_path=sys_path)
|
||||||
|
defs = Script('from jedi.api import classes; classes', project=project).infer()
|
||||||
assert [d.full_name for d in defs] == ['jedi.api.classes']
|
assert [d.full_name for d in defs] == ['jedi.api.classes']
|
||||||
defs = Script('import jedi.api; jedi.api', sys_path=sys_path).infer()
|
defs = Script('import jedi.api; jedi.api', project=project).infer()
|
||||||
assert [d.full_name for d in defs] == ['jedi.api']
|
assert [d.full_name for d in defs] == ['jedi.api']
|
||||||
|
|
||||||
|
|
||||||
|
|||||||
@@ -3,6 +3,7 @@
|
|||||||
All character set and unicode related tests.
|
All character set and unicode related tests.
|
||||||
"""
|
"""
|
||||||
from jedi._compatibility import u, unicode
|
from jedi._compatibility import u, unicode
|
||||||
|
from jedi import Project
|
||||||
|
|
||||||
|
|
||||||
def test_unicode_script(Script):
|
def test_unicode_script(Script):
|
||||||
@@ -70,7 +71,8 @@ def test_wrong_encoding(Script, tmpdir):
|
|||||||
# Use both latin-1 and utf-8 (a really broken file).
|
# Use both latin-1 and utf-8 (a really broken file).
|
||||||
x.write_binary(u'foobar = 1\nä'.encode('latin-1') + u'ä'.encode('utf-8'))
|
x.write_binary(u'foobar = 1\nä'.encode('latin-1') + u'ä'.encode('utf-8'))
|
||||||
|
|
||||||
c, = Script('import x; x.foo', sys_path=[tmpdir.strpath]).complete()
|
project = Project('.', sys_path=[tmpdir.strpath])
|
||||||
|
c, = Script('import x; x.foo', project=project).complete()
|
||||||
assert c.name == 'foobar'
|
assert c.name == 'foobar'
|
||||||
|
|
||||||
|
|
||||||
|
|||||||
@@ -3,6 +3,7 @@ from os.path import dirname
|
|||||||
import pytest
|
import pytest
|
||||||
|
|
||||||
from test.helpers import get_example_dir, example_dir
|
from test.helpers import get_example_dir, example_dir
|
||||||
|
from jedi import Project
|
||||||
|
|
||||||
|
|
||||||
@pytest.fixture(autouse=True)
|
@pytest.fixture(autouse=True)
|
||||||
@@ -14,9 +15,10 @@ def skip_not_supported_versions(environment):
|
|||||||
def test_implicit_namespace_package(Script):
|
def test_implicit_namespace_package(Script):
|
||||||
sys_path = [get_example_dir('implicit_namespace_package', 'ns1'),
|
sys_path = [get_example_dir('implicit_namespace_package', 'ns1'),
|
||||||
get_example_dir('implicit_namespace_package', 'ns2')]
|
get_example_dir('implicit_namespace_package', 'ns2')]
|
||||||
|
project = Project('.', sys_path=sys_path)
|
||||||
|
|
||||||
def script_with_path(*args, **kwargs):
|
def script_with_path(*args, **kwargs):
|
||||||
return Script(sys_path=sys_path, *args, **kwargs)
|
return Script(project=project, *args, **kwargs)
|
||||||
|
|
||||||
# goto definition
|
# goto definition
|
||||||
assert script_with_path('from pkg import ns1_file').infer()
|
assert script_with_path('from pkg import ns1_file').infer()
|
||||||
@@ -55,15 +57,14 @@ def test_implicit_namespace_package(Script):
|
|||||||
def test_implicit_nested_namespace_package(Script):
|
def test_implicit_nested_namespace_package(Script):
|
||||||
code = 'from implicit_nested_namespaces.namespace.pkg.module import CONST'
|
code = 'from implicit_nested_namespaces.namespace.pkg.module import CONST'
|
||||||
|
|
||||||
sys_path = [example_dir]
|
project = Project('.', sys_path=[example_dir])
|
||||||
|
script = Script(project=project, source=code, line=1, column=61)
|
||||||
script = Script(sys_path=sys_path, source=code, line=1, column=61)
|
|
||||||
|
|
||||||
result = script.infer()
|
result = script.infer()
|
||||||
|
|
||||||
assert len(result) == 1
|
assert len(result) == 1
|
||||||
|
|
||||||
implicit_pkg, = Script(code, sys_path=sys_path).infer(column=10)
|
implicit_pkg, = Script(code, project=project).infer(column=10)
|
||||||
assert implicit_pkg.type == 'module'
|
assert implicit_pkg.type == 'module'
|
||||||
assert implicit_pkg.module_path is None
|
assert implicit_pkg.module_path is None
|
||||||
|
|
||||||
@@ -71,9 +72,8 @@ def test_implicit_nested_namespace_package(Script):
|
|||||||
def test_implicit_namespace_package_import_autocomplete(Script):
|
def test_implicit_namespace_package_import_autocomplete(Script):
|
||||||
CODE = 'from implicit_name'
|
CODE = 'from implicit_name'
|
||||||
|
|
||||||
sys_path = [example_dir]
|
project = Project('.', sys_path=[example_dir])
|
||||||
|
script = Script(project=project, source=CODE)
|
||||||
script = Script(sys_path=sys_path, source=CODE)
|
|
||||||
compl = script.complete()
|
compl = script.complete()
|
||||||
assert [c.name for c in compl] == ['implicit_namespace_package']
|
assert [c.name for c in compl] == ['implicit_namespace_package']
|
||||||
|
|
||||||
@@ -83,7 +83,8 @@ def test_namespace_package_in_multiple_directories_autocompletion(Script):
|
|||||||
sys_path = [get_example_dir('implicit_namespace_package', 'ns1'),
|
sys_path = [get_example_dir('implicit_namespace_package', 'ns1'),
|
||||||
get_example_dir('implicit_namespace_package', 'ns2')]
|
get_example_dir('implicit_namespace_package', 'ns2')]
|
||||||
|
|
||||||
script = Script(sys_path=sys_path, source=CODE)
|
project = Project('.', sys_path=sys_path)
|
||||||
|
script = Script(project=project, source=CODE)
|
||||||
compl = script.complete()
|
compl = script.complete()
|
||||||
assert set(c.name for c in compl) == set(['ns1_file', 'ns2_file'])
|
assert set(c.name for c in compl) == set(['ns1_file', 'ns2_file'])
|
||||||
|
|
||||||
@@ -92,7 +93,8 @@ def test_namespace_package_in_multiple_directories_goto_definition(Script):
|
|||||||
CODE = 'from pkg import ns1_file'
|
CODE = 'from pkg import ns1_file'
|
||||||
sys_path = [get_example_dir('implicit_namespace_package', 'ns1'),
|
sys_path = [get_example_dir('implicit_namespace_package', 'ns1'),
|
||||||
get_example_dir('implicit_namespace_package', 'ns2')]
|
get_example_dir('implicit_namespace_package', 'ns2')]
|
||||||
script = Script(sys_path=sys_path, source=CODE)
|
project = Project('.', sys_path=sys_path)
|
||||||
|
script = Script(project=project, source=CODE)
|
||||||
result = script.infer()
|
result = script.infer()
|
||||||
assert len(result) == 1
|
assert len(result) == 1
|
||||||
|
|
||||||
@@ -102,6 +104,7 @@ def test_namespace_name_autocompletion_full_name(Script):
|
|||||||
sys_path = [get_example_dir('implicit_namespace_package', 'ns1'),
|
sys_path = [get_example_dir('implicit_namespace_package', 'ns1'),
|
||||||
get_example_dir('implicit_namespace_package', 'ns2')]
|
get_example_dir('implicit_namespace_package', 'ns2')]
|
||||||
|
|
||||||
script = Script(sys_path=sys_path, source=CODE)
|
project = Project('.', sys_path=sys_path)
|
||||||
|
script = Script(project=project, source=CODE)
|
||||||
compl = script.complete()
|
compl = script.complete()
|
||||||
assert set(c.full_name for c in compl) == set(['pkg'])
|
assert set(c.full_name for c in compl) == set(['pkg'])
|
||||||
|
|||||||
@@ -44,7 +44,9 @@ pkg_zip_path = get_example_dir('zipped_imports', 'pkg.zip')
|
|||||||
|
|
||||||
def test_find_module_package_zipped(Script, inference_state, environment):
|
def test_find_module_package_zipped(Script, inference_state, environment):
|
||||||
sys_path = environment.get_sys_path() + [pkg_zip_path]
|
sys_path = environment.get_sys_path() + [pkg_zip_path]
|
||||||
script = Script('import pkg; pkg.mod', sys_path=sys_path)
|
|
||||||
|
project = Project('.', sys_path=sys_path)
|
||||||
|
script = Script('import pkg; pkg.mod', project=project)
|
||||||
assert len(script.complete()) == 1
|
assert len(script.complete()) == 1
|
||||||
|
|
||||||
file_io, is_package = inference_state.compiled_subprocess.get_module_info(
|
file_io, is_package = inference_state.compiled_subprocess.get_module_info(
|
||||||
@@ -86,7 +88,7 @@ def test_find_module_package_zipped(Script, inference_state, environment):
|
|||||||
def test_correct_zip_package_behavior(Script, inference_state, environment, code,
|
def test_correct_zip_package_behavior(Script, inference_state, environment, code,
|
||||||
file, package, path, skip_python2):
|
file, package, path, skip_python2):
|
||||||
sys_path = environment.get_sys_path() + [pkg_zip_path]
|
sys_path = environment.get_sys_path() + [pkg_zip_path]
|
||||||
pkg, = Script(code, sys_path=sys_path).infer()
|
pkg, = Script(code, project=Project('.', sys_path=sys_path)).infer()
|
||||||
value, = pkg._name.infer()
|
value, = pkg._name.infer()
|
||||||
assert value.py__file__() == os.path.join(pkg_zip_path, 'pkg', file)
|
assert value.py__file__() == os.path.join(pkg_zip_path, 'pkg', file)
|
||||||
assert '.'.join(value.py__package__()) == package
|
assert '.'.join(value.py__package__()) == package
|
||||||
@@ -98,7 +100,7 @@ def test_correct_zip_package_behavior(Script, inference_state, environment, code
|
|||||||
def test_find_module_not_package_zipped(Script, inference_state, environment):
|
def test_find_module_not_package_zipped(Script, inference_state, environment):
|
||||||
path = get_example_dir('zipped_imports', 'not_pkg.zip')
|
path = get_example_dir('zipped_imports', 'not_pkg.zip')
|
||||||
sys_path = environment.get_sys_path() + [path]
|
sys_path = environment.get_sys_path() + [path]
|
||||||
script = Script('import not_pkg; not_pkg.val', sys_path=sys_path)
|
script = Script('import not_pkg; not_pkg.val', project=Project('.', sys_path=sys_path))
|
||||||
assert len(script.complete()) == 1
|
assert len(script.complete()) == 1
|
||||||
|
|
||||||
file_io, is_package = inference_state.compiled_subprocess.get_module_info(
|
file_io, is_package = inference_state.compiled_subprocess.get_module_info(
|
||||||
@@ -144,7 +146,7 @@ def test_flask_ext(Script, code, name):
|
|||||||
"""flask.ext.foo is really imported from flaskext.foo or flask_foo.
|
"""flask.ext.foo is really imported from flaskext.foo or flask_foo.
|
||||||
"""
|
"""
|
||||||
path = get_example_dir('flask-site-packages')
|
path = get_example_dir('flask-site-packages')
|
||||||
completions = Script(code, sys_path=[path]).complete()
|
completions = Script(code, project=Project('.', sys_path=[path])).complete()
|
||||||
assert name in [c.name for c in completions]
|
assert name in [c.name for c in completions]
|
||||||
|
|
||||||
|
|
||||||
@@ -166,10 +168,14 @@ def test_cache_works_with_sys_path_param(Script, tmpdir):
|
|||||||
bar_path = tmpdir.join('bar')
|
bar_path = tmpdir.join('bar')
|
||||||
foo_path.join('module.py').write('foo = 123', ensure=True)
|
foo_path.join('module.py').write('foo = 123', ensure=True)
|
||||||
bar_path.join('module.py').write('bar = 123', ensure=True)
|
bar_path.join('module.py').write('bar = 123', ensure=True)
|
||||||
foo_completions = Script('import module; module.',
|
foo_completions = Script(
|
||||||
sys_path=[foo_path.strpath]).complete()
|
'import module; module.',
|
||||||
bar_completions = Script('import module; module.',
|
project=Project('.', sys_path=[foo_path.strpath]),
|
||||||
sys_path=[bar_path.strpath]).complete()
|
).complete()
|
||||||
|
bar_completions = Script(
|
||||||
|
'import module; module.',
|
||||||
|
project=Project('.', sys_path=[bar_path.strpath]),
|
||||||
|
).complete()
|
||||||
assert 'foo' in [c.name for c in foo_completions]
|
assert 'foo' in [c.name for c in foo_completions]
|
||||||
assert 'bar' not in [c.name for c in foo_completions]
|
assert 'bar' not in [c.name for c in foo_completions]
|
||||||
|
|
||||||
@@ -460,7 +466,7 @@ def test_import_needed_modules_by_jedi(Script, environment, tmpdir, name):
|
|||||||
script = Script(
|
script = Script(
|
||||||
'import ' + name,
|
'import ' + name,
|
||||||
path=tmpdir.join('something.py').strpath,
|
path=tmpdir.join('something.py').strpath,
|
||||||
sys_path=[tmpdir.strpath] + environment.get_sys_path(),
|
project=Project('.', sys_path=[tmpdir.strpath] + environment.get_sys_path()),
|
||||||
)
|
)
|
||||||
module, = script.infer()
|
module, = script.infer()
|
||||||
assert module._inference_state.builtins_module.py__file__() != module_path
|
assert module._inference_state.builtins_module.py__file__() != module_path
|
||||||
|
|||||||
@@ -4,6 +4,7 @@ import pytest
|
|||||||
import py
|
import py
|
||||||
|
|
||||||
from ..helpers import get_example_dir, example_dir
|
from ..helpers import get_example_dir, example_dir
|
||||||
|
from jedi import Project
|
||||||
|
|
||||||
|
|
||||||
SYS_PATH = [get_example_dir('namespace_package', 'ns1'),
|
SYS_PATH = [get_example_dir('namespace_package', 'ns1'),
|
||||||
@@ -11,7 +12,7 @@ SYS_PATH = [get_example_dir('namespace_package', 'ns1'),
|
|||||||
|
|
||||||
|
|
||||||
def script_with_path(Script, *args, **kwargs):
|
def script_with_path(Script, *args, **kwargs):
|
||||||
return Script(sys_path=SYS_PATH, *args, **kwargs)
|
return Script(project=Project('.', sys_path=SYS_PATH), *args, **kwargs)
|
||||||
|
|
||||||
|
|
||||||
def test_goto_definition(Script):
|
def test_goto_definition(Script):
|
||||||
@@ -69,8 +70,8 @@ def test_nested_namespace_package(Script):
|
|||||||
code = 'from nested_namespaces.namespace.pkg import CONST'
|
code = 'from nested_namespaces.namespace.pkg import CONST'
|
||||||
|
|
||||||
sys_path = [example_dir]
|
sys_path = [example_dir]
|
||||||
|
project = Project('.', sys_path=sys_path)
|
||||||
script = Script(sys_path=sys_path, source=code)
|
script = Script(project=project, source=code)
|
||||||
|
|
||||||
result = script.infer(line=1, column=45)
|
result = script.infer(line=1, column=45)
|
||||||
|
|
||||||
|
|||||||
Reference in New Issue
Block a user