Debian Codegraph @

test/test_inference/test_buildout_detection.py

raw symbols SCIP
1 import os
2 from textwrap import dedent
3 from pathlib import Path
4
5 from jedi.inference.sys_path import _get_parent_dir_with_file, \
6 _get_buildout_script_paths, check_sys_path_modifications
7
8 from ..helpers import get_example_dir
9
10
11 def check_module_test(Script, code):
12 module_context = Script(code)._get_module_context()
13 return check_sys_path_modifications(module_context)
14
15
16 def test_parent_dir_with_file(Script):
17 path = Path(get_example_dir('buildout_project', 'src', 'proj_name'))
18 parent = _get_parent_dir_with_file(path, 'buildout.cfg')
19 assert parent is not None
20 assert str(parent).endswith(os.path.join('test', 'examples', 'buildout_project'))
21
22
23 def test_buildout_detection(Script):
24 path = Path(get_example_dir('buildout_project', 'src', 'proj_name'))
25 paths = list(_get_buildout_script_paths(path.joinpath('module_name.py')))
26 assert len(paths) == 1
27 appdir_path = os.path.normpath(os.path.join(path, '../../bin/app'))
28 assert str(paths[0]) == appdir_path
29
30
31 def test_append_on_non_sys_path(Script):
32 code = dedent("""
33 class Dummy(object):
34 path = []
35
36 d = Dummy()
37 d.path.append('foo')""")
38
39 paths = check_module_test(Script, code)
40 assert not paths
41 assert 'foo' not in paths
42
43
44 def test_path_from_invalid_sys_path_assignment(Script):
45 code = dedent("""
46 import sys
47 sys.path = 'invalid'""")
48
49 paths = check_module_test(Script, code)
50 assert not paths
51 assert 'invalid' not in paths
52
53
54 def test_sys_path_with_modifications(Script):
55 path = get_example_dir('buildout_project', 'src', 'proj_name', 'module_name.py')
56 code = dedent("""
57 import os
58 """)
59
60 paths = Script(code, path=path)._inference_state.get_sys_path()
61 assert os.path.abspath('/tmp/.buildout/eggs/important_package.egg') in paths
62
63
64 def test_path_from_sys_path_assignment(Script):
65 code = dedent(f"""
66 #!/usr/bin/python
67
68 import sys
69 sys.path[0:0] = [
70 {os.path.abspath('/usr/lib/python3.14/site-packages')!r},
71 {os.path.abspath('/home/test/.buildout/eggs/important_package.egg')!r},
72 ]
73
74 path[0:0] = [1]
75
76 import important_package
77
78 if __name__ == '__main__':
79 sys.exit(important_package.main())""")
80
81 paths = check_module_test(Script, code)
82 assert 1 not in paths
83 assert os.path.abspath('/home/test/.buildout/eggs/important_package.egg') \
84 in map(str, paths)