From 14ac0512a91848838e6881a263d72f22869b9837 Mon Sep 17 00:00:00 2001 From: Dave Halter Date: Thu, 6 Feb 2020 01:47:34 +0100 Subject: [PATCH] Get rid of cwd modifications in tests --- conftest.py | 7 +++ jedi/inference/imports.py | 5 +- test/helpers.py | 2 + .../test_api_classes_follow_definition.py | 6 ++- test/test_api/test_settings.py | 2 - test/test_inference/test_absolute_import.py | 5 +- .../test_inference/test_buildout_detection.py | 17 +++---- test/test_inference/test_imports.py | 49 ++++++++++--------- test/test_speed.py | 3 +- test/test_utils.py | 5 +- 10 files changed, 55 insertions(+), 46 deletions(-) diff --git a/conftest.py b/conftest.py index 81d0fe54..91e772b3 100644 --- a/conftest.py +++ b/conftest.py @@ -9,6 +9,7 @@ import pytest import jedi from jedi.api.environment import get_system_environment, InterpreterEnvironment from jedi._compatibility import py_version +from test.helpers import test_dir collect_ignore = [ 'setup.py', @@ -109,6 +110,12 @@ def Script(environment): return partial(jedi.Script, environment=environment) +@pytest.fixture(scope='session') +def ScriptWithProject(Script): + project = jedi.Project(test_dir) + return partial(jedi.Script, project=project) + + @pytest.fixture(scope='session') def get_names(Script): return lambda code, **kwargs: Script(code).get_names(**kwargs) diff --git a/jedi/inference/imports.py b/jedi/inference/imports.py index bc1ce0b9..a43e19aa 100644 --- a/jedi/inference/imports.py +++ b/jedi/inference/imports.py @@ -191,18 +191,19 @@ class Importer(object): import_path = base + tuple(import_path) else: path = module_context.py__file__() + project_path = self._inference_state.project._path import_path = list(import_path) if path is None: # If no path is defined, our best guess is that the current # file is edited by a user on the current working # directory. We need to add an initial path, because it # will get removed as the name of the current file. - directory = os.getcwd() + directory = project_path else: directory = os.path.dirname(path) base_import_path, base_directory = _level_to_base_import_path( - self._inference_state.project._path, directory, level, + project_path, directory, level, ) if base_directory is None: # Everything is lost, the relative import does point diff --git a/test/helpers.py b/test/helpers.py index 7e991915..f2782829 100644 --- a/test/helpers.py +++ b/test/helpers.py @@ -16,8 +16,10 @@ import os import pytest from os.path import abspath, dirname, join from functools import partial, wraps +from jedi import Project test_dir = dirname(abspath(__file__)) +test_dir_project = Project(test_dir) root_dir = dirname(test_dir) example_dir = join(test_dir, 'examples') diff --git a/test/test_api/test_api_classes_follow_definition.py b/test/test_api/test_api_classes_follow_definition.py index 83ed1c95..4bf3f254 100644 --- a/test/test_api/test_api_classes_follow_definition.py +++ b/test/test_api/test_api_classes_follow_definition.py @@ -1,7 +1,9 @@ +from os.path import join from itertools import chain +from functools import partial import jedi -from ..helpers import cwd_at +from ..helpers import test_dir def test_import_empty(Script): @@ -47,8 +49,8 @@ def test_follow_import_incomplete(Script, environment): assert alias == ['module'] -@cwd_at('test/completion/import_tree') def test_follow_definition_nested_import(Script): + Script = partial(Script, project=jedi.Project(join(test_dir, 'completion', 'import_tree'))) types = check_follow_definition_types(Script, "import pkg.mod1; pkg") assert types == ['module'] diff --git a/test/test_api/test_settings.py b/test/test_api/test_settings.py index 7a9d0c2c..3a21fc76 100644 --- a/test/test_api/test_settings.py +++ b/test/test_api/test_settings.py @@ -4,11 +4,9 @@ import pytest from jedi import api from jedi.inference import imports -from ..helpers import cwd_at @pytest.mark.skipif('True', reason='Skip for now, test case is not really supported.') -@cwd_at('jedi') def test_add_dynamic_mods(Script): fname = '__main__.py' api.settings.additional_dynamic_modules = [fname] diff --git a/test/test_inference/test_absolute_import.py b/test/test_inference/test_absolute_import.py index 3bf9dd2e..72017dc9 100644 --- a/test/test_inference/test_absolute_import.py +++ b/test/test_inference/test_absolute_import.py @@ -2,10 +2,11 @@ Tests ``from __future__ import absolute_import`` (only important for Python 2.X) """ +from jedi import Project from .. import helpers -@helpers.cwd_at("test/examples/absolute_import") def test_can_complete_when_shadowing(Script): - script = Script(path="unittest.py") + path = helpers.get_example_dir('absolute_import', 'unittest.py') + script = Script(path=path, project=Project(helpers.get_example_dir('absolute_import'))) assert script.complete() diff --git a/test/test_inference/test_buildout_detection.py b/test/test_inference/test_buildout_detection.py index 6815aa9b..aa70eca0 100644 --- a/test/test_inference/test_buildout_detection.py +++ b/test/test_inference/test_buildout_detection.py @@ -5,7 +5,7 @@ from jedi._compatibility import force_unicode from jedi.inference.sys_path import _get_parent_dir_with_file, \ _get_buildout_script_paths, check_sys_path_modifications -from ..helpers import cwd_at +from ..helpers import get_example_dir def check_module_test(Script, code): @@ -13,20 +13,18 @@ def check_module_test(Script, code): return check_sys_path_modifications(module_context) -@cwd_at('test/examples/buildout_project/src/proj_name') def test_parent_dir_with_file(Script): - parent = _get_parent_dir_with_file( - os.path.abspath(os.curdir), 'buildout.cfg') + path = get_example_dir('buildout_project', 'src', 'proj_name') + parent = _get_parent_dir_with_file(path, 'buildout.cfg') assert parent is not None assert parent.endswith(os.path.join('test', 'examples', 'buildout_project')) -@cwd_at('test/examples/buildout_project/src/proj_name') def test_buildout_detection(Script): - scripts = list(_get_buildout_script_paths(os.path.abspath('./module_name.py'))) + path = get_example_dir('buildout_project', 'src', 'proj_name') + scripts = list(_get_buildout_script_paths(os.path.join(path, 'module_name.py'))) assert len(scripts) == 1 - curdir = os.path.abspath(os.curdir) - appdir_path = os.path.normpath(os.path.join(curdir, '../../bin/app')) + appdir_path = os.path.normpath(os.path.join(path, '../../bin/app')) assert scripts[0] == appdir_path @@ -53,13 +51,12 @@ def test_path_from_invalid_sys_path_assignment(Script): assert 'invalid' not in paths -@cwd_at('test/examples/buildout_project/src/proj_name/') def test_sys_path_with_modifications(Script): + path = get_example_dir('buildout_project', 'src', 'proj_name', 'module_name.py') code = dedent(""" import os """) - path = os.path.abspath(os.path.join(os.curdir, 'module_name.py')) paths = Script(code, path=path)._inference_state.get_sys_path() assert '/tmp/.buildout/eggs/important_package.egg' in paths diff --git a/test/test_inference/test_imports.py b/test/test_inference/test_imports.py index 5661371f..963e29a0 100644 --- a/test/test_inference/test_imports.py +++ b/test/test_inference/test_imports.py @@ -6,15 +6,15 @@ Tests". import os import pytest -from jedi.file_io import FileIO, KnownContentFileIO +from jedi.file_io import FileIO, KnownContentFileIO from jedi._compatibility import find_module_py33, find_module from jedi.inference import compiled from jedi.inference import imports from jedi.api.project import Project from jedi.inference.gradual.conversion import _stub_to_python_value_set from jedi.inference.references import get_module_contexts_containing_name -from ..helpers import cwd_at, get_example_dir, test_dir, root_dir +from ..helpers import get_example_dir, test_dir, test_dir_project, root_dir THIS_DIR = os.path.dirname(__file__) @@ -112,19 +112,24 @@ def test_find_module_not_package_zipped(Script, inference_state, environment): assert is_package is False -@cwd_at('test/examples/not_in_sys_path/pkg') -def test_import_not_in_sys_path(Script): +def test_import_not_in_sys_path(Script, environment): """ non-direct imports (not in sys.path) This is in the end just a fallback. """ - a = Script(path='module.py').infer(line=5) + path = get_example_dir() + module_path = os.path.join(path, 'not_in_sys_path', 'pkg', 'module.py') + # This project tests the smart path option of Project. The sys_path is + # explicitly given to make sure that the path is just dumb and only + # includes non-folder dependencies. + project = Project(path, sys_path=environment.get_sys_path()) + a = Script(path=module_path, project=project).infer(line=5) assert a[0].name == 'int' - a = Script(path='module.py').infer(line=6) + a = Script(path=module_path, project=project).infer(line=6) assert a[0].name == 'str' - a = Script(path='module.py').infer(line=7) + a = Script(path=module_path, project=project).infer(line=7) assert a[0].name == 'str' @@ -150,10 +155,9 @@ def test_flask_ext(Script, code, name): assert name in [c.name for c in completions] -@cwd_at('test/test_inference/') def test_not_importable_file(Script): src = 'import not_importable_file as x; x.' - assert not Script(src, path='example.py').complete() + assert not Script(src, path='example.py', project=test_dir_project).complete() def test_import_unique(Script): @@ -200,29 +204,28 @@ def test_goto_definition_on_import(Script): assert len(Script("import sys").infer(1, 8)) == 1 -@cwd_at('jedi') -def test_complete_on_empty_import(Script): - assert Script("from datetime import").complete()[0].name == 'import' +def test_complete_on_empty_import(ScriptWithProject): + assert ScriptWithProject("from datetime import").complete()[0].name == 'import' # should just list the files in the directory - assert 10 < len(Script("from .", path='whatever.py').complete()) < 30 + assert 10 < len(ScriptWithProject("from .", path='whatever.py').complete()) < 30 # Global import - assert len(Script("from . import", 'whatever.py').complete(1, 5)) > 30 + assert len(ScriptWithProject("from . import", 'whatever.py').complete(1, 5)) > 30 # relative import - assert 10 < len(Script("from . import", 'whatever.py').complete(1, 6)) < 30 + assert 10 < len(ScriptWithProject("from . import", 'whatever.py').complete(1, 6)) < 30 # Global import - assert len(Script("from . import classes", 'whatever.py').complete(1, 5)) > 30 + assert len(ScriptWithProject("from . import classes", 'whatever.py').complete(1, 5)) > 30 # relative import - assert 10 < len(Script("from . import classes", 'whatever.py').complete(1, 6)) < 30 + assert 10 < len(ScriptWithProject("from . import classes", 'whatever.py').complete(1, 6)) < 30 wanted = {'ImportError', 'import', 'ImportWarning'} - assert {c.name for c in Script("import").complete()} == wanted - assert len(Script("import import", path='').complete()) > 0 + assert {c.name for c in ScriptWithProject("import").complete()} == wanted + assert len(ScriptWithProject("import import", path='').complete()) > 0 # 111 - assert Script("from datetime import").complete()[0].name == 'import' - assert Script("from datetime import ").complete() + assert ScriptWithProject("from datetime import").complete()[0].name == 'import' + assert ScriptWithProject("from datetime import ").complete() def test_imports_on_global_namespace_without_path(Script): @@ -394,9 +397,9 @@ def test_relative_imports_with_outside_paths(Script): assert not script.complete() -@cwd_at('test/examples/issue1209/api/whatever/') def test_relative_imports_without_path(Script): - project = Project('.', sys_path=[], smart_sys_path=False) + path = get_example_dir('issue1209', 'api', 'whatever') + project = Project(path, sys_path=[], smart_sys_path=False) script = Script("from . ", project=project) assert [c.name for c in script.complete()] == ['api_test1', 'import'] diff --git a/test/test_speed.py b/test/test_speed.py index f08f0644..ea37a154 100644 --- a/test/test_speed.py +++ b/test/test_speed.py @@ -6,7 +6,7 @@ should. import time import functools -from .helpers import cwd_at, get_example_dir +from .helpers import get_example_dir import jedi @@ -44,7 +44,6 @@ def test_scipy_speed(Script): @_check_speed(0.8) -@cwd_at('test') def test_precedence_slowdown(Script): """ Precedence calculation can slow down things significantly in edge diff --git a/test/test_utils.py b/test/test_utils.py index a4c510e0..e91eb62d 100644 --- a/test/test_utils.py +++ b/test/test_utils.py @@ -5,7 +5,7 @@ except ImportError: from jedi import utils -from .helpers import unittest, cwd_at +from .helpers import unittest @unittest.skipIf(not readline, "readline not found") @@ -86,9 +86,8 @@ class TestSetupReadline(unittest.TestCase): # (posix and nt) librariesare included. assert len(difference) < 20 - @cwd_at('test') def test_local_import(self): - s = 'import test_utils' + s = 'import test.test_utils' assert self.complete(s) == [s] def test_preexisting_values(self):