2
2
# Copyright 2021- Python Language Server Contributors.
3
3
4
4
import os
5
- import sys
6
5
7
6
import pytest
8
7
from pylsp import uris
9
8
from pylsp .plugins .jedi_rename import pylsp_rename
10
9
from pylsp .workspace import Document
11
10
12
- LT_PY36 = sys .version_info .major < 3 or (
13
- sys .version_info .major == 3 and sys .version_info .minor < 6
14
- )
15
11
16
12
DOC_NAME = "test1.py"
17
13
DOC = """class Test1():
@@ -26,13 +22,17 @@ class Test2(Test1):
26
22
x = Test1()
27
23
"""
28
24
25
+ DOC_NAME_SIMPLE = "test3.py"
26
+ DOC_SIMPLE = "foo = 12"
27
+
29
28
30
29
@pytest .fixture
31
30
def tmp_workspace (temp_workspace_factory ):
32
- return temp_workspace_factory ({DOC_NAME : DOC , DOC_NAME_EXTRA : DOC_EXTRA })
31
+ return temp_workspace_factory (
32
+ {DOC_NAME : DOC , DOC_NAME_EXTRA : DOC_EXTRA , DOC_NAME_SIMPLE : DOC_SIMPLE }
33
+ )
33
34
34
35
35
- @pytest .mark .skipif (LT_PY36 , reason = "Jedi refactoring isnt supported on Python 2.x/3.5" )
36
36
def test_jedi_rename (tmp_workspace , config ): # pylint: disable=redefined-outer-name
37
37
# rename the `Test1` class
38
38
position = {"line" : 0 , "character" : 6 }
@@ -56,13 +56,15 @@ def test_jedi_rename(tmp_workspace, config): # pylint: disable=redefined-outer-
56
56
"newText" : "class ShouldBeRenamed():\n pass\n \n class Test2(ShouldBeRenamed):\n pass\n " ,
57
57
}
58
58
]
59
+
59
60
path = os .path .join (tmp_workspace .root_path , DOC_NAME_EXTRA )
60
61
uri_extra = uris .from_fs_path (path )
61
62
assert changes [1 ]["textDocument" ]["uri" ] == uri_extra
62
63
# This also checks whether documents not yet added via textDocument/didOpen
63
64
# but that do need to be renamed in the project have a `null` version
64
65
# number.
65
66
assert changes [1 ]["textDocument" ]["version" ] is None
67
+
66
68
expected = "from test1 import ShouldBeRenamed\n x = ShouldBeRenamed()\n "
67
69
if os .name == "nt" :
68
70
# The .write method in the temp_workspace_factory functions writes
@@ -77,3 +79,27 @@ def test_jedi_rename(tmp_workspace, config): # pylint: disable=redefined-outer-
77
79
"newText" : expected ,
78
80
}
79
81
]
82
+
83
+ # Regression test for issue python-lsp/python-lsp-server#413
84
+ # rename foo
85
+ position = {"line" : 0 , "character" : 0 }
86
+ DOC_URI = uris .from_fs_path (os .path .join (tmp_workspace .root_path , DOC_NAME_SIMPLE ))
87
+ doc = Document (DOC_URI , tmp_workspace )
88
+
89
+ result = pylsp_rename (config , tmp_workspace , doc , position , "bar" )
90
+ assert len (result .keys ()) == 1
91
+
92
+ changes = result .get ("documentChanges" )
93
+ assert len (changes ) == 1
94
+
95
+ assert changes [0 ]["textDocument" ]["uri" ] == doc .uri
96
+ assert changes [0 ]["textDocument" ]["version" ] == doc .version
97
+ assert changes [0 ].get ("edits" ) == [
98
+ {
99
+ "range" : {
100
+ "start" : {"line" : 0 , "character" : 0 },
101
+ "end" : {"line" : 0 , "character" : 0 },
102
+ },
103
+ "newText" : "bar = 12" ,
104
+ }
105
+ ]
0 commit comments