Debian Codegraph @

test/test_api/test_refactoring.py

raw symbols SCIP
1 import os
2 import shutil
3 from textwrap import dedent
4 from pathlib import Path
5 import platform
6
7 import pytest
8
9 import jedi
10 from test.helpers import get_example_dir
11
12
13 @pytest.fixture()
14 def dir_with_content(tmpdir):
15 with open(os.path.join(tmpdir.strpath, 'modx.py'), 'w', newline='') as f:
16 f.write('import modx\nfoo\n') # self reference
17 return Path(tmpdir.strpath)
18
19
20 def test_rename_mod(Script, dir_with_content):
21 script = Script(
22 'import modx; modx\n',
23 path=dir_with_content.joinpath('some_script.py'),
24 project=jedi.Project(dir_with_content),
25 )
26 refactoring = script.rename(line=1, new_name='modr')
27 refactoring.apply()
28
29 p1 = dir_with_content.joinpath('modx.py')
30 p2 = dir_with_content.joinpath('modr.py')
31 expected_code = 'import modr\nfoo\n'
32 assert not p1.exists()
33 with open(p2, newline='') as f:
34 assert f.read() == expected_code
35
36 assert refactoring.get_renames() == [(p1, p2)]
37
38 assert refactoring.get_changed_files()[p1].get_new_code() == expected_code
39
40 assert refactoring.get_diff() == dedent('''\
41 rename from modx.py
42 rename to modr.py
43 --- modx.py
44 +++ modr.py
45 @@ -1,3 +1,3 @@
46 -import modx
47 +import modr
48 foo
49 --- some_script.py
50 +++ some_script.py
51 @@ -1,2 +1,2 @@
52 -import modx; modx
53 +import modr; modr
54 ''').format(dir=dir_with_content)
55
56
57 def test_namespace_package(Script, tmpdir):
58 origin = get_example_dir('implicit_namespace_package')
59 shutil.copytree(origin, tmpdir.strpath, dirs_exist_ok=True)
60 sys_path = [
61 os.path.join(tmpdir.strpath, 'ns1'),
62 os.path.join(tmpdir.strpath, 'ns2')
63 ]
64 script_path = os.path.join(tmpdir.strpath, 'script.py')
65 script = Script(
66 'import pkg\n',
67 path=script_path,
68 project=jedi.Project(os.path.join(tmpdir.strpath, 'does-not-exist'), sys_path=sys_path),
69 )
70 refactoring = script.rename(line=1, new_name='new_pkg')
71 refactoring.apply()
72 old1 = os.path.join(sys_path[0], "pkg")
73 new1 = os.path.join(sys_path[0], "new_pkg")
74 old2 = os.path.join(sys_path[1], "pkg")
75 new2 = os.path.join(sys_path[1], "new_pkg")
76 assert not os.path.exists(old1)
77 assert os.path.exists(new1)
78 assert not os.path.exists(old2)
79 assert os.path.exists(new2)
80
81 changed, = iter(refactoring.get_changed_files().values())
82 assert changed.get_new_code() == "import new_pkg\n"
83
84 assert refactoring.get_diff() == dedent(f'''\
85 rename from {old1}
86 rename to {new1}
87 rename from {old2}
88 rename to {new2}
89 --- {script_path}
90 +++ {script_path}
91 @@ -1,2 +1,2 @@
92 -import pkg
93 +import new_pkg
94 ''').format(dir=dir_with_content)
95
96
97 def test_rename_none_path(Script):
98 refactoring = Script('foo', path=None).rename(new_name='bar')
99 with pytest.raises(jedi.RefactoringError, match='on a Script with path=None'):
100 refactoring.apply()
101 assert refactoring
102
103
104 def test_diff_without_ending_newline(Script):
105 refactoring = Script('a = 1\nb\na').rename(1, 0, new_name='c')
106 assert refactoring.get_diff() == dedent('''\
107 ---
108 +++
109 @@ -1,3 +1,3 @@
110 -a = 1
111 +c = 1
112 b
113 -a
114 +c
115 ''') # noqa: W291
116
117
118 def test_diff_path_outside_of_project(Script):
119 if platform.system().lower() == 'windows':
120 abs_path = r'D:\unknown_dir\file.py'
121 else:
122 abs_path = '/unknown_dir/file.py'
123 script = Script(
124 code='foo = 1',
125 path=abs_path,
126 project=jedi.get_default_project()
127 )
128 diff = script.rename(line=1, column=0, new_name='bar').get_diff()
129 assert diff == dedent(f'''\
130 --- {abs_path}
131 +++ {abs_path}
132 @@ -1 +1 @@
133 -foo = 1
134 +bar = 1
135 ''')