Debian Codegraph @

test/test_inference/test_sys_path.py

raw symbols SCIP
1 import os
2 from glob import glob
3 import sys
4 import shutil
5 from pathlib import Path
6
7 import pytest
8
9 from ..helpers import skip_if_windows, skip_if_not_windows, get_example_dir
10 from jedi.inference import sys_path
11 from jedi.api.environment import create_environment
12
13
14 def test_paths_from_assignment(Script):
15 def paths(src):
16 script = Script(src, path='/foo/bar.py')
17 expr_stmt = script._module_node.children[0]
18 return set(sys_path._paths_from_assignment(script._get_module_context(), expr_stmt))
19
20 # Normalize paths for Windows.
21 path_a = Path('/foo/a').absolute()
22 path_b = Path('/foo/b').absolute()
23 path_c = Path('/foo/c').absolute()
24
25 assert paths('sys.path[0:0] = ["a"]') == {path_a}
26 assert paths('sys.path = ["b", 1, x + 3, y, "c"]') == {path_b, path_c}
27 assert paths('sys.path = a = ["a"]') == {path_a}
28
29 # Fail for complicated examples.
30 assert paths('sys.path, other = ["a"], 2') == set()
31
32
33 def test_venv_and_pths(venv_path, environment):
34 pjoin = os.path.join
35
36 if os.name == 'nt':
37 if environment.version_info < (3, 11):
38 site_pkg_path = pjoin(venv_path, 'lib', 'site-packages')
39 else:
40 site_pkg_path = pjoin(venv_path, 'Lib', 'site-packages')
41 else:
42 site_pkg_path = glob(pjoin(venv_path, 'lib', 'python*', 'site-packages'))[0]
43 shutil.rmtree(site_pkg_path)
44 shutil.copytree(get_example_dir('sample_venvs', 'pth_directory'), site_pkg_path)
45
46 virtualenv = create_environment(venv_path)
47 venv_paths = virtualenv.get_sys_path()
48
49 ETALON = [
50 # For now disable egg-links. I have no idea how they work... ~ dave
51 # pjoin('/path', 'from', 'egg-link'),
52 # pjoin(site_pkg_path, '.', 'relative', 'egg-link', 'path'),
53 site_pkg_path,
54 pjoin(site_pkg_path, 'dir-from-foo-pth'),
55 '/foo/smth.py:module',
56 # Not sure why it's added twice. It has to do with site.py which is not
57 # something we can change. However this obviously also doesn't matter.
58 '/foo/smth.py:from_func',
59 '/foo/smth.py:from_func',
60 ]
61
62 # Ensure that pth and egg-link paths were added.
63 assert venv_paths[-len(ETALON):] == ETALON
64
65 # Ensure that none of venv dirs leaked to the interpreter.
66 assert not set(sys.path).intersection(ETALON)
67
68
69 _s = ['/a', '/b', '/c/d/']
70
71
72 @pytest.mark.parametrize(
73 'sys_path_, module_path, expected, is_package', [
74 (_s, '/a/b', ('b',), False),
75 (_s, '/a/b/c', ('b', 'c'), False),
76 (_s, '/a/b.py', ('b',), False),
77 (_s, '/a/b/c.py', ('b', 'c'), False),
78 (_s, '/x/b.py', None, False),
79 (_s, '/c/d/x.py', ('x',), False),
80 (_s, '/c/d/x.py', ('x',), False),
81 (_s, '/c/d/x/y.py', ('x', 'y'), False),
82 # If dots are in there they also resolve. These are obviously illegal
83 # in Python, but Jedi can handle them. Give the user a bit more freedom
84 # that he will have to correct eventually.
85 (_s, '/a/b.c.py', ('b.c',), False),
86 (_s, '/a/b.d/foo.bar.py', ('b.d', 'foo.bar'), False),
87
88 (_s, '/a/.py', None, False),
89 (_s, '/a/c/.py', None, False),
90
91 (['/foo'], '/foo/bar/__init__.py', ('bar',), True),
92 (['/foo'], '/foo/bar/baz/__init__.py', ('bar', 'baz'), True),
93
94 skip_if_windows(['/foo'], '/foo/bar.so', ('bar',), False),
95 skip_if_windows(['/foo'], '/foo/bar/__init__.so', ('bar',), True),
96 skip_if_not_windows(['/foo'], '/foo/bar.pyd', ('bar',), False),
97 skip_if_not_windows(['/foo'], '/foo/bar/__init__.pyd', ('bar',), True),
98
99 (['/foo'], '/x/bar.py', None, False),
100 (['/foo'], '/foo/bar.xyz', ('bar.xyz',), False),
101
102 (['/foo', '/foo/bar'], '/foo/bar/baz', ('baz',), False),
103 (['/foo/bar', '/foo'], '/foo/bar/baz', ('baz',), False),
104
105 (['/'], '/bar/baz.py', ('bar', 'baz',), False),
106 ])
107 def test_transform_path_to_dotted(sys_path_, module_path, expected, is_package):
108 # transform_path_to_dotted expects normalized absolute paths.
109 sys_path_ = [os.path.abspath(path) for path in sys_path_]
110 module_path = os.path.abspath(module_path)
111 assert sys_path.transform_path_to_dotted(sys_path_, Path(module_path)) \
112 == (expected, is_package)