mirror of
https://github.com/davidhalter/jedi.git
synced 2025-12-06 14:04:26 +08:00
Implement all remaining Path issues and use it instead of strings
This commit is contained in:
@@ -1,5 +1,6 @@
|
||||
import os
|
||||
from textwrap import dedent
|
||||
from pathlib import Path
|
||||
|
||||
from jedi.inference.sys_path import _get_parent_dir_with_file, \
|
||||
_get_buildout_script_paths, check_sys_path_modifications
|
||||
@@ -13,18 +14,18 @@ def check_module_test(Script, code):
|
||||
|
||||
|
||||
def test_parent_dir_with_file(Script):
|
||||
path = get_example_dir('buildout_project', 'src', 'proj_name')
|
||||
path = 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'))
|
||||
assert str(parent).endswith(os.path.join('test', 'examples', 'buildout_project'))
|
||||
|
||||
|
||||
def test_buildout_detection(Script):
|
||||
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
|
||||
path = Path(get_example_dir('buildout_project', 'src', 'proj_name'))
|
||||
paths = list(_get_buildout_script_paths(path.joinpath('module_name.py')))
|
||||
assert len(paths) == 1
|
||||
appdir_path = os.path.normpath(os.path.join(path, '../../bin/app'))
|
||||
assert scripts[0] == appdir_path
|
||||
assert str(paths[0]) == appdir_path
|
||||
|
||||
|
||||
def test_append_on_non_sys_path(Script):
|
||||
@@ -79,4 +80,4 @@ def test_path_from_sys_path_assignment(Script):
|
||||
|
||||
paths = check_module_test(Script, code)
|
||||
assert 1 not in paths
|
||||
assert '/home/test/.buildout/eggs/important_package.egg' in paths
|
||||
assert '/home/test/.buildout/eggs/important_package.egg' in map(str, paths)
|
||||
|
||||
@@ -87,4 +87,4 @@ def test_infer_and_goto(Script, code, full_name, has_stub, has_python, way,
|
||||
assert has_python == (not d.is_stub())
|
||||
assert d.full_name == full_name
|
||||
|
||||
assert d.is_stub() == d.module_path.endswith('.pyi')
|
||||
assert d.is_stub() == (d.module_path.suffix == '.pyi')
|
||||
|
||||
@@ -14,7 +14,7 @@ TYPESHED_PYTHON3 = os.path.join(typeshed.TYPESHED_PATH, 'stdlib', '3')
|
||||
def test_get_typeshed_directories():
|
||||
def get_dirs(version_info):
|
||||
return {
|
||||
p.path.replace(typeshed.TYPESHED_PATH, '').lstrip(os.path.sep)
|
||||
p.path.replace(str(typeshed.TYPESHED_PATH), '').lstrip(os.path.sep)
|
||||
for p in typeshed._get_typeshed_directories(version_info)
|
||||
}
|
||||
|
||||
@@ -52,7 +52,7 @@ def test_keywords_variable(Script):
|
||||
assert seq.name == 'Sequence'
|
||||
# This points towards the typeshed implementation
|
||||
stub_seq, = seq.goto(only_stubs=True)
|
||||
assert typeshed.TYPESHED_PATH in stub_seq.module_path
|
||||
assert str(stub_seq.module_path).startswith(str(typeshed.TYPESHED_PATH))
|
||||
|
||||
|
||||
def test_class(Script):
|
||||
@@ -91,8 +91,12 @@ def test_sys_exc_info(Script):
|
||||
none, def_ = Script(code + '[1]').infer()
|
||||
# It's an optional.
|
||||
assert def_.name == 'BaseException'
|
||||
assert def_.module_path == typeshed.TYPESHED_PATH.joinpath(
|
||||
'stdlib', '2and3', 'builtins.pyi'
|
||||
)
|
||||
assert def_.type == 'instance'
|
||||
assert none.name == 'NoneType'
|
||||
assert none.module_path is None
|
||||
|
||||
none, def_ = Script(code + '[0]').infer()
|
||||
assert def_.name == 'BaseException'
|
||||
@@ -111,7 +115,7 @@ def test_sys_hexversion(Script):
|
||||
script = Script('import sys; sys.hexversion')
|
||||
def_, = script.complete()
|
||||
assert isinstance(def_._name, StubName), def_._name
|
||||
assert typeshed.TYPESHED_PATH in def_.module_path
|
||||
assert str(def_.module_path).startswith(str(typeshed.TYPESHED_PATH))
|
||||
def_, = script.infer()
|
||||
assert def_.name == 'int'
|
||||
|
||||
@@ -138,14 +142,14 @@ def test_type_var(Script):
|
||||
def test_math_is_stub(Script, code, full_name):
|
||||
s = Script(code)
|
||||
cos, = s.infer()
|
||||
wanted = os.path.join('typeshed', 'stdlib', '2and3', 'math.pyi')
|
||||
assert cos.module_path.endswith(wanted)
|
||||
wanted = ('typeshed', 'stdlib', '2and3', 'math.pyi')
|
||||
assert cos.module_path.parts[-4:] == wanted
|
||||
assert cos.is_stub() is True
|
||||
assert cos.goto(only_stubs=True) == [cos]
|
||||
assert cos.full_name == full_name
|
||||
|
||||
cos, = s.goto()
|
||||
assert cos.module_path.endswith(wanted)
|
||||
assert cos.module_path.parts[-4:] == wanted
|
||||
assert cos.goto(only_stubs=True) == [cos]
|
||||
assert cos.is_stub() is True
|
||||
assert cos.full_name == full_name
|
||||
|
||||
@@ -4,6 +4,7 @@ Tests".
|
||||
"""
|
||||
|
||||
import os
|
||||
from pathlib import Path
|
||||
|
||||
import pytest
|
||||
|
||||
@@ -38,11 +39,11 @@ def test_find_module_not_package():
|
||||
assert is_package is False
|
||||
|
||||
|
||||
pkg_zip_path = get_example_dir('zipped_imports', 'pkg.zip')
|
||||
pkg_zip_path = 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]
|
||||
sys_path = environment.get_sys_path() + [str(pkg_zip_path)]
|
||||
|
||||
project = Project('.', sys_path=sys_path)
|
||||
script = Script('import pkg; pkg.mod', project=project)
|
||||
@@ -86,14 +87,14 @@ def test_find_module_package_zipped(Script, inference_state, environment):
|
||||
)
|
||||
def test_correct_zip_package_behavior(Script, inference_state, environment, code,
|
||||
file, package, path):
|
||||
sys_path = environment.get_sys_path() + [pkg_zip_path]
|
||||
sys_path = environment.get_sys_path() + [str(pkg_zip_path)]
|
||||
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 value.py__file__() == pkg_zip_path.joinpath('pkg', file)
|
||||
assert '.'.join(value.py__package__()) == package
|
||||
assert value.is_package() is (path is not None)
|
||||
if path is not None:
|
||||
assert value.py__path__() == [os.path.join(pkg_zip_path, path)]
|
||||
assert value.py__path__() == [str(pkg_zip_path.joinpath(path))]
|
||||
|
||||
|
||||
def test_find_module_not_package_zipped(Script, inference_state, environment):
|
||||
@@ -103,7 +104,7 @@ def test_find_module_not_package_zipped(Script, inference_state, environment):
|
||||
assert len(script.complete()) == 1
|
||||
|
||||
file_io, is_package = inference_state.compiled_subprocess.get_module_info(
|
||||
sys_path=sys_path,
|
||||
sys_path=map(str, sys_path),
|
||||
string='not_pkg',
|
||||
full_name='not_pkg'
|
||||
)
|
||||
@@ -435,8 +436,8 @@ def test_pre_defined_imports_module(Script, environment, name):
|
||||
module = Script('', path=path)._get_module_context()
|
||||
assert module.string_names == (name,)
|
||||
|
||||
assert module.inference_state.builtins_module.py__file__() != path
|
||||
assert module.inference_state.typing_module.py__file__() != path
|
||||
assert str(module.inference_state.builtins_module.py__file__()) != path
|
||||
assert str(module.inference_state.typing_module.py__file__()) != path
|
||||
|
||||
|
||||
@pytest.mark.parametrize('name', ('builtins', 'typing'))
|
||||
@@ -449,8 +450,8 @@ def test_import_needed_modules_by_jedi(Script, environment, tmpdir, name):
|
||||
project=Project('.', sys_path=[tmpdir.strpath] + environment.get_sys_path()),
|
||||
)
|
||||
module, = script.infer()
|
||||
assert module._inference_state.builtins_module.py__file__() != module_path
|
||||
assert module._inference_state.typing_module.py__file__() != module_path
|
||||
assert str(module._inference_state.builtins_module.py__file__()) != module_path
|
||||
assert str(module._inference_state.typing_module.py__file__()) != module_path
|
||||
|
||||
|
||||
def test_import_with_semicolon(Script):
|
||||
|
||||
@@ -2,6 +2,7 @@ import os
|
||||
from glob import glob
|
||||
import sys
|
||||
import shutil
|
||||
from pathlib import Path, PurePath
|
||||
|
||||
import pytest
|
||||
|
||||
@@ -17,9 +18,9 @@ def test_paths_from_assignment(Script):
|
||||
return set(sys_path._paths_from_assignment(script._get_module_context(), expr_stmt))
|
||||
|
||||
# Normalize paths for Windows.
|
||||
path_a = os.path.abspath('/foo/a')
|
||||
path_b = os.path.abspath('/foo/b')
|
||||
path_c = os.path.abspath('/foo/c')
|
||||
path_a = PurePath('/foo/a')
|
||||
path_b = PurePath('/foo/b')
|
||||
path_c = PurePath('/foo/c')
|
||||
|
||||
assert paths('sys.path[0:0] = ["a"]') == {path_a}
|
||||
assert paths('sys.path = ["b", 1, x + 3, y, "c"]') == {path_b, path_c}
|
||||
@@ -105,5 +106,5 @@ def test_transform_path_to_dotted(sys_path_, module_path, expected, is_package):
|
||||
# transform_path_to_dotted expects normalized absolute paths.
|
||||
sys_path_ = [os.path.abspath(path) for path in sys_path_]
|
||||
module_path = os.path.abspath(module_path)
|
||||
assert sys_path.transform_path_to_dotted(sys_path_, module_path) \
|
||||
assert sys_path.transform_path_to_dotted(sys_path_, Path(module_path)) \
|
||||
== (expected, is_package)
|
||||
|
||||
Reference in New Issue
Block a user