1
0
forked from VimPlug/jedi

Implement all remaining Path issues and use it instead of strings

This commit is contained in:
Dave Halter
2020-07-12 01:14:00 +02:00
parent db0e90763b
commit 480a464179
23 changed files with 131 additions and 97 deletions

View File

@@ -22,7 +22,7 @@ def test_preload_modules():
# Filter the typeshed parser cache.
typeshed_cache_count = sum(
1 for path in grammar_cache
if path is not None and path.startswith(typeshed.TYPESHED_PATH)
if path is not None and str(path).startswith(str(typeshed.TYPESHED_PATH))
)
# +1 for None module (currently used)
assert len(grammar_cache) - typeshed_cache_count == len(modules) + 1
@@ -210,11 +210,11 @@ def test_goto_follow_imports(Script):
import inspect
inspect.isfunction""")
definition, = Script(code).goto(column=0, follow_imports=True)
assert 'inspect.py' in definition.module_path
assert definition.module_path.name == 'inspect.py'
assert (definition.line, definition.column) == (1, 0)
definition, = Script(code).goto(follow_imports=True)
assert 'inspect.py' in definition.module_path
assert definition.module_path.name == 'inspect.py'
assert (definition.line, definition.column) > (1, 0)
code = '''def param(p): pass\nparam(1)'''
@@ -245,11 +245,11 @@ def test_goto_module(Script):
assert module.module_path == expected
base_path = get_example_dir('simple_import')
path = os.path.join(base_path, '__init__.py')
path = base_path.joinpath('__init__.py')
check(1, os.path.join(base_path, 'module.py'))
check(1, os.path.join(base_path, 'module.py'), follow_imports=True)
check(5, os.path.join(base_path, 'module2.py'))
check(1, base_path.joinpath('module.py'))
check(1, base_path.joinpath('module.py'), follow_imports=True)
check(5, base_path.joinpath('module2.py'))
def test_goto_definition_cursor(Script):

View File

@@ -3,7 +3,6 @@
from textwrap import dedent
from inspect import cleandoc
import os
import pytest
@@ -534,8 +533,8 @@ def test_execute(Script, code, description):
('from pkg import Foo; Foo().bar', 'bar', 'module.py'),
])
def test_inheritance_module_path(Script, goto, code, name, file_name):
base_path = os.path.join(get_example_dir('inheritance'), 'pkg')
whatever_path = os.path.join(base_path, 'NOT_EXISTING.py')
base_path = get_example_dir('inheritance', 'pkg')
whatever_path = base_path.joinpath('NOT_EXISTING.py')
script = Script(code, path=whatever_path)
if goto is None:
@@ -544,7 +543,7 @@ def test_inheritance_module_path(Script, goto, code, name, file_name):
func, = script.goto(follow_imports=goto)
assert func.type == 'function'
assert func.name == name
assert func.module_path == os.path.join(base_path, file_name)
assert func.module_path == base_path.joinpath(file_name)
def test_definition_goto_follow_imports(Script):

View File

@@ -221,8 +221,8 @@ current_dirname = os.path.basename(dirname(dirname(dirname(__file__))))
('example.py', 'rb"' + join('..', current_dirname, 'tes'), None, ['t' + s]),
# Absolute paths
(None, '"' + join(root_dir, 'test', 'test_ca'), None, ['che.py"']),
(None, '"%s"' % join(root_dir, 'test', 'test_ca'), len(root_dir) + 14, ['che.py']),
(None, f'"{root_dir.joinpath("test", "test_ca")}', None, ['che.py"']),
(None, f'"{root_dir.joinpath("test", "test_ca")}"', len(str(root_dir)) + 14, ['che.py']),
# Longer quotes
('example.py', 'r"""test', None, [s]),

View File

@@ -41,7 +41,7 @@ def test_keyword_attributes(Script):
assert def_.line is def_.column is None
assert def_.in_builtin_module() is True
assert def_.module_name == 'builtins'
assert 'typeshed' in def_.module_path
assert 'typeshed' in def_.module_path.parts
assert def_.type == 'keyword'

View File

@@ -1,4 +1,5 @@
import os
from pathlib import Path
import pytest
@@ -21,13 +22,12 @@ def test_django_default_project(Script):
def test_django_default_project_of_file(Script):
project = get_default_project(__file__)
d = os.path.dirname
assert project._path == d(d(d(__file__)))
assert project._path == Path(__file__).parent.parent.parent
def test_interpreter_project_path():
# Run from anywhere it should be the cwd.
dir = os.path.join(root_dir, 'test')
dir = Path(root_dir).joinpath('test')
with set_cwd(dir):
project = Interpreter('', [locals()])._inference_state.project
assert project._path == dir

View File

@@ -1,5 +1,6 @@
import os
from textwrap import dedent
from pathlib import Path
import pytest
@@ -10,22 +11,22 @@ import jedi
def dir_with_content(tmpdir):
with open(os.path.join(tmpdir.strpath, 'modx.py'), 'w', newline='') as f:
f.write('import modx\nfoo\n') # self reference
return tmpdir.strpath
return Path(tmpdir.strpath)
def test_rename_mod(Script, dir_with_content):
script = Script(
'import modx; modx\n',
path=os.path.join(dir_with_content, 'some_script.py'),
path=dir_with_content.joinpath('some_script.py'),
project=jedi.Project(dir_with_content),
)
refactoring = script.rename(line=1, new_name='modr')
refactoring.apply()
p1 = os.path.join(dir_with_content, 'modx.py')
p2 = os.path.join(dir_with_content, 'modr.py')
p1 = dir_with_content.joinpath('modx.py')
p2 = dir_with_content.joinpath('modr.py')
expected_code = 'import modr\nfoo\n'
assert not os.path.exists(p1)
assert not p1.exists()
with open(p2, newline='') as f:
assert f.read() == expected_code