From 4a1d9a9116c0cb9c386fca89f8e145a6948e76ae Mon Sep 17 00:00:00 2001 From: Dave Halter Date: Thu, 30 Jan 2020 21:02:47 +0100 Subject: [PATCH] Use project instead of sys_path parameter in tests --- test/test_api/test_classes.py | 5 ++-- test/test_api/test_full_name.py | 5 ++-- test/test_api/test_unicode.py | 4 ++- .../test_implicit_namespace_package.py | 25 +++++++++++-------- test/test_inference/test_imports.py | 24 +++++++++++------- test/test_inference/test_namespace_package.py | 7 +++--- 6 files changed, 42 insertions(+), 28 deletions(-) diff --git a/test/test_api/test_classes.py b/test/test_api/test_classes.py index 4a17c572..a037133b 100644 --- a/test/test_api/test_classes.py +++ b/test/test_api/test_classes.py @@ -129,10 +129,11 @@ def test_completion_docstring(Script, jedi_path): Jedi should follow imports in certain conditions """ 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) - 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) docstr('import jedi\njedi.Scr', cleandoc(jedi.Script.__doc__)) diff --git a/test/test_api/test_full_name.py b/test/test_api/test_full_name.py index 6858b6ca..5944ad72 100644 --- a/test/test_api/test_full_name.py +++ b/test/test_api/test_full_name.py @@ -97,9 +97,10 @@ def test_sub_module(Script, jedi_path): 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'] - 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'] diff --git a/test/test_api/test_unicode.py b/test/test_api/test_unicode.py index 015048cd..f7f7ec45 100644 --- a/test/test_api/test_unicode.py +++ b/test/test_api/test_unicode.py @@ -3,6 +3,7 @@ All character set and unicode related tests. """ from jedi._compatibility import u, unicode +from jedi import Project 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). 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' diff --git a/test/test_inference/test_implicit_namespace_package.py b/test/test_inference/test_implicit_namespace_package.py index 2df9de0c..0b3bee71 100644 --- a/test/test_inference/test_implicit_namespace_package.py +++ b/test/test_inference/test_implicit_namespace_package.py @@ -3,6 +3,7 @@ from os.path import dirname import pytest from test.helpers import get_example_dir, example_dir +from jedi import Project @pytest.fixture(autouse=True) @@ -14,9 +15,10 @@ def skip_not_supported_versions(environment): def test_implicit_namespace_package(Script): sys_path = [get_example_dir('implicit_namespace_package', 'ns1'), get_example_dir('implicit_namespace_package', 'ns2')] + project = Project('.', sys_path=sys_path) def script_with_path(*args, **kwargs): - return Script(sys_path=sys_path, *args, **kwargs) + return Script(project=project, *args, **kwargs) # goto definition 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): code = 'from implicit_nested_namespaces.namespace.pkg.module import CONST' - sys_path = [example_dir] - - script = Script(sys_path=sys_path, source=code, line=1, column=61) + project = Project('.', sys_path=[example_dir]) + script = Script(project=project, source=code, line=1, column=61) result = script.infer() 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.module_path is None @@ -71,9 +72,8 @@ def test_implicit_nested_namespace_package(Script): def test_implicit_namespace_package_import_autocomplete(Script): CODE = 'from implicit_name' - sys_path = [example_dir] - - script = Script(sys_path=sys_path, source=CODE) + project = Project('.', sys_path=[example_dir]) + script = Script(project=project, source=CODE) compl = script.complete() 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'), 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() 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' sys_path = [get_example_dir('implicit_namespace_package', 'ns1'), 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() 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'), 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() assert set(c.full_name for c in compl) == set(['pkg']) diff --git a/test/test_inference/test_imports.py b/test/test_inference/test_imports.py index f22f3272..5661371f 100644 --- a/test/test_inference/test_imports.py +++ b/test/test_inference/test_imports.py @@ -44,7 +44,9 @@ pkg_zip_path = get_example_dir('zipped_imports', 'pkg.zip') def test_find_module_package_zipped(Script, inference_state, environment): 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 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, file, package, path, skip_python2): 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() assert value.py__file__() == os.path.join(pkg_zip_path, 'pkg', file) 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): path = get_example_dir('zipped_imports', 'not_pkg.zip') 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 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. """ 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] @@ -166,10 +168,14 @@ def test_cache_works_with_sys_path_param(Script, tmpdir): bar_path = tmpdir.join('bar') foo_path.join('module.py').write('foo = 123', ensure=True) bar_path.join('module.py').write('bar = 123', ensure=True) - foo_completions = Script('import module; module.', - sys_path=[foo_path.strpath]).complete() - bar_completions = Script('import module; module.', - sys_path=[bar_path.strpath]).complete() + foo_completions = Script( + 'import module; module.', + project=Project('.', sys_path=[foo_path.strpath]), + ).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 '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( 'import ' + name, 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() assert module._inference_state.builtins_module.py__file__() != module_path diff --git a/test/test_inference/test_namespace_package.py b/test/test_inference/test_namespace_package.py index f6b34151..0867af85 100644 --- a/test/test_inference/test_namespace_package.py +++ b/test/test_inference/test_namespace_package.py @@ -4,6 +4,7 @@ import pytest import py from ..helpers import get_example_dir, example_dir +from jedi import Project 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): - return Script(sys_path=SYS_PATH, *args, **kwargs) + return Script(project=Project('.', sys_path=SYS_PATH), *args, **kwargs) def test_goto_definition(Script): @@ -69,8 +70,8 @@ def test_nested_namespace_package(Script): code = 'from nested_namespaces.namespace.pkg import CONST' sys_path = [example_dir] - - script = Script(sys_path=sys_path, source=code) + project = Project('.', sys_path=sys_path) + script = Script(project=project, source=code) result = script.infer(line=1, column=45)