Get rid of cwd modifications in tests

This commit is contained in:
Dave Halter
2020-02-06 01:47:34 +01:00
parent f2722952e7
commit 14ac0512a9
10 changed files with 55 additions and 46 deletions

View File

@@ -9,6 +9,7 @@ import pytest
import jedi import jedi
from jedi.api.environment import get_system_environment, InterpreterEnvironment from jedi.api.environment import get_system_environment, InterpreterEnvironment
from jedi._compatibility import py_version from jedi._compatibility import py_version
from test.helpers import test_dir
collect_ignore = [ collect_ignore = [
'setup.py', 'setup.py',
@@ -109,6 +110,12 @@ def Script(environment):
return partial(jedi.Script, environment=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') @pytest.fixture(scope='session')
def get_names(Script): def get_names(Script):
return lambda code, **kwargs: Script(code).get_names(**kwargs) return lambda code, **kwargs: Script(code).get_names(**kwargs)

View File

@@ -191,18 +191,19 @@ class Importer(object):
import_path = base + tuple(import_path) import_path = base + tuple(import_path)
else: else:
path = module_context.py__file__() path = module_context.py__file__()
project_path = self._inference_state.project._path
import_path = list(import_path) import_path = list(import_path)
if path is None: if path is None:
# If no path is defined, our best guess is that the current # If no path is defined, our best guess is that the current
# file is edited by a user on the current working # file is edited by a user on the current working
# directory. We need to add an initial path, because it # directory. We need to add an initial path, because it
# will get removed as the name of the current file. # will get removed as the name of the current file.
directory = os.getcwd() directory = project_path
else: else:
directory = os.path.dirname(path) directory = os.path.dirname(path)
base_import_path, base_directory = _level_to_base_import_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: if base_directory is None:
# Everything is lost, the relative import does point # Everything is lost, the relative import does point

View File

@@ -16,8 +16,10 @@ import os
import pytest import pytest
from os.path import abspath, dirname, join from os.path import abspath, dirname, join
from functools import partial, wraps from functools import partial, wraps
from jedi import Project
test_dir = dirname(abspath(__file__)) test_dir = dirname(abspath(__file__))
test_dir_project = Project(test_dir)
root_dir = dirname(test_dir) root_dir = dirname(test_dir)
example_dir = join(test_dir, 'examples') example_dir = join(test_dir, 'examples')

View File

@@ -1,7 +1,9 @@
from os.path import join
from itertools import chain from itertools import chain
from functools import partial
import jedi import jedi
from ..helpers import cwd_at from ..helpers import test_dir
def test_import_empty(Script): def test_import_empty(Script):
@@ -47,8 +49,8 @@ def test_follow_import_incomplete(Script, environment):
assert alias == ['module'] assert alias == ['module']
@cwd_at('test/completion/import_tree')
def test_follow_definition_nested_import(Script): 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") types = check_follow_definition_types(Script, "import pkg.mod1; pkg")
assert types == ['module'] assert types == ['module']

View File

@@ -4,11 +4,9 @@ import pytest
from jedi import api from jedi import api
from jedi.inference import imports from jedi.inference import imports
from ..helpers import cwd_at
@pytest.mark.skipif('True', reason='Skip for now, test case is not really supported.') @pytest.mark.skipif('True', reason='Skip for now, test case is not really supported.')
@cwd_at('jedi')
def test_add_dynamic_mods(Script): def test_add_dynamic_mods(Script):
fname = '__main__.py' fname = '__main__.py'
api.settings.additional_dynamic_modules = [fname] api.settings.additional_dynamic_modules = [fname]

View File

@@ -2,10 +2,11 @@
Tests ``from __future__ import absolute_import`` (only important for Tests ``from __future__ import absolute_import`` (only important for
Python 2.X) Python 2.X)
""" """
from jedi import Project
from .. import helpers from .. import helpers
@helpers.cwd_at("test/examples/absolute_import")
def test_can_complete_when_shadowing(Script): 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() assert script.complete()

View File

@@ -5,7 +5,7 @@ from jedi._compatibility import force_unicode
from jedi.inference.sys_path import _get_parent_dir_with_file, \ from jedi.inference.sys_path import _get_parent_dir_with_file, \
_get_buildout_script_paths, check_sys_path_modifications _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): def check_module_test(Script, code):
@@ -13,20 +13,18 @@ def check_module_test(Script, code):
return check_sys_path_modifications(module_context) return check_sys_path_modifications(module_context)
@cwd_at('test/examples/buildout_project/src/proj_name')
def test_parent_dir_with_file(Script): def test_parent_dir_with_file(Script):
parent = _get_parent_dir_with_file( path = get_example_dir('buildout_project', 'src', 'proj_name')
os.path.abspath(os.curdir), 'buildout.cfg') parent = _get_parent_dir_with_file(path, 'buildout.cfg')
assert parent is not None assert parent is not None
assert parent.endswith(os.path.join('test', 'examples', 'buildout_project')) assert parent.endswith(os.path.join('test', 'examples', 'buildout_project'))
@cwd_at('test/examples/buildout_project/src/proj_name')
def test_buildout_detection(Script): 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 assert len(scripts) == 1
curdir = os.path.abspath(os.curdir) appdir_path = os.path.normpath(os.path.join(path, '../../bin/app'))
appdir_path = os.path.normpath(os.path.join(curdir, '../../bin/app'))
assert scripts[0] == appdir_path assert scripts[0] == appdir_path
@@ -53,13 +51,12 @@ def test_path_from_invalid_sys_path_assignment(Script):
assert 'invalid' not in paths assert 'invalid' not in paths
@cwd_at('test/examples/buildout_project/src/proj_name/')
def test_sys_path_with_modifications(Script): def test_sys_path_with_modifications(Script):
path = get_example_dir('buildout_project', 'src', 'proj_name', 'module_name.py')
code = dedent(""" code = dedent("""
import os import os
""") """)
path = os.path.abspath(os.path.join(os.curdir, 'module_name.py'))
paths = Script(code, path=path)._inference_state.get_sys_path() paths = Script(code, path=path)._inference_state.get_sys_path()
assert '/tmp/.buildout/eggs/important_package.egg' in paths assert '/tmp/.buildout/eggs/important_package.egg' in paths

View File

@@ -6,15 +6,15 @@ Tests".
import os import os
import pytest 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._compatibility import find_module_py33, find_module
from jedi.inference import compiled from jedi.inference import compiled
from jedi.inference import imports from jedi.inference import imports
from jedi.api.project import Project from jedi.api.project import Project
from jedi.inference.gradual.conversion import _stub_to_python_value_set from jedi.inference.gradual.conversion import _stub_to_python_value_set
from jedi.inference.references import get_module_contexts_containing_name 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__) 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 assert is_package is False
@cwd_at('test/examples/not_in_sys_path/pkg') def test_import_not_in_sys_path(Script, environment):
def test_import_not_in_sys_path(Script):
""" """
non-direct imports (not in sys.path) non-direct imports (not in sys.path)
This is in the end just a fallback. 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' 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' 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' 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] assert name in [c.name for c in completions]
@cwd_at('test/test_inference/')
def test_not_importable_file(Script): def test_not_importable_file(Script):
src = 'import not_importable_file as x; x.' 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): 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 assert len(Script("import sys").infer(1, 8)) == 1
@cwd_at('jedi') def test_complete_on_empty_import(ScriptWithProject):
def test_complete_on_empty_import(Script): assert ScriptWithProject("from datetime import").complete()[0].name == 'import'
assert Script("from datetime import").complete()[0].name == 'import'
# should just list the files in the directory # 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 # 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 # 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 # 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 # 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'} wanted = {'ImportError', 'import', 'ImportWarning'}
assert {c.name for c in Script("import").complete()} == wanted assert {c.name for c in ScriptWithProject("import").complete()} == wanted
assert len(Script("import import", path='').complete()) > 0 assert len(ScriptWithProject("import import", path='').complete()) > 0
# 111 # 111
assert Script("from datetime import").complete()[0].name == 'import' assert ScriptWithProject("from datetime import").complete()[0].name == 'import'
assert Script("from datetime import ").complete() assert ScriptWithProject("from datetime import ").complete()
def test_imports_on_global_namespace_without_path(Script): 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() assert not script.complete()
@cwd_at('test/examples/issue1209/api/whatever/')
def test_relative_imports_without_path(Script): 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) script = Script("from . ", project=project)
assert [c.name for c in script.complete()] == ['api_test1', 'import'] assert [c.name for c in script.complete()] == ['api_test1', 'import']

View File

@@ -6,7 +6,7 @@ should.
import time import time
import functools import functools
from .helpers import cwd_at, get_example_dir from .helpers import get_example_dir
import jedi import jedi
@@ -44,7 +44,6 @@ def test_scipy_speed(Script):
@_check_speed(0.8) @_check_speed(0.8)
@cwd_at('test')
def test_precedence_slowdown(Script): def test_precedence_slowdown(Script):
""" """
Precedence calculation can slow down things significantly in edge Precedence calculation can slow down things significantly in edge

View File

@@ -5,7 +5,7 @@ except ImportError:
from jedi import utils from jedi import utils
from .helpers import unittest, cwd_at from .helpers import unittest
@unittest.skipIf(not readline, "readline not found") @unittest.skipIf(not readline, "readline not found")
@@ -86,9 +86,8 @@ class TestSetupReadline(unittest.TestCase):
# (posix and nt) librariesare included. # (posix and nt) librariesare included.
assert len(difference) < 20 assert len(difference) < 20
@cwd_at('test')
def test_local_import(self): def test_local_import(self):
s = 'import test_utils' s = 'import test.test_utils'
assert self.complete(s) == [s] assert self.complete(s) == [s]
def test_preexisting_values(self): def test_preexisting_values(self):