22# Copyright 2021- Python Language Server Contributors.
33
44import os
5- import sys
65
76import pytest
87from pylsp import uris
98from pylsp .plugins .jedi_rename import pylsp_rename
109from pylsp .workspace import Document
1110
12- LT_PY36 = sys .version_info .major < 3 or (
13- sys .version_info .major == 3 and sys .version_info .minor < 6
14- )
1511
1612DOC_NAME = "test1.py"
1713DOC = """class Test1():
@@ -26,13 +22,17 @@ class Test2(Test1):
2622x = Test1()
2723"""
2824
25+ DOC_NAME_SIMPLE = "test3.py"
26+ DOC_SIMPLE = "foo = 12"
27+
2928
3029@pytest .fixture
3130def 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+ )
3334
3435
35- @pytest .mark .skipif (LT_PY36 , reason = "Jedi refactoring isnt supported on Python 2.x/3.5" )
3636def test_jedi_rename (tmp_workspace , config ): # pylint: disable=redefined-outer-name
3737 # rename the `Test1` class
3838 position = {"line" : 0 , "character" : 6 }
@@ -56,13 +56,15 @@ def test_jedi_rename(tmp_workspace, config): # pylint: disable=redefined-outer-
5656 "newText" : "class ShouldBeRenamed():\n pass\n \n class Test2(ShouldBeRenamed):\n pass\n " ,
5757 }
5858 ]
59+
5960 path = os .path .join (tmp_workspace .root_path , DOC_NAME_EXTRA )
6061 uri_extra = uris .from_fs_path (path )
6162 assert changes [1 ]["textDocument" ]["uri" ] == uri_extra
6263 # This also checks whether documents not yet added via textDocument/didOpen
6364 # but that do need to be renamed in the project have a `null` version
6465 # number.
6566 assert changes [1 ]["textDocument" ]["version" ] is None
67+
6668 expected = "from test1 import ShouldBeRenamed\n x = ShouldBeRenamed()\n "
6769 if os .name == "nt" :
6870 # 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-
7779 "newText" : expected ,
7880 }
7981 ]
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