1010
1111# Python LSP imports
1212from pylsp import uris
13+ from pylsp .config .config import Config
1314from pylsp .workspace import Document , Workspace
1415
1516# Local imports
@@ -25,6 +26,14 @@ def workspace(tmpdir):
2526 return Workspace (uris .from_fs_path (str (tmpdir )), Mock ())
2627
2728
29+ @pytest .fixture
30+ def config (workspace ):
31+ """Return a config object."""
32+ cfg = Config (workspace .root_uri , {}, 0 , {})
33+ cfg ._plugin_settings = {"plugins" : {"black" : {"line_length" : 88 }}}
34+ return cfg
35+
36+
2837@pytest .fixture
2938def unformatted_document (workspace ):
3039 path = fixtures_dir / "unformatted.txt"
@@ -85,8 +94,22 @@ def config_document(workspace):
8594 return Document (uri , workspace )
8695
8796
88- def test_pylsp_format_document (unformatted_document , formatted_document ):
89- result = pylsp_format_document (unformatted_document )
97+ @pytest .fixture
98+ def unformatted_line_length (workspace ):
99+ path = fixtures_dir / "unformatted-line-length.py"
100+ uri = f"file:/{ path } "
101+ return Document (uri , workspace )
102+
103+
104+ @pytest .fixture
105+ def formatted_line_length (workspace ):
106+ path = fixtures_dir / "formatted-line-length.py"
107+ uri = f"file:/{ path } "
108+ return Document (uri , workspace )
109+
110+
111+ def test_pylsp_format_document (config , unformatted_document , formatted_document ):
112+ result = pylsp_format_document (config , unformatted_document )
90113
91114 assert result == [
92115 {
@@ -99,8 +122,10 @@ def test_pylsp_format_document(unformatted_document, formatted_document):
99122 ]
100123
101124
102- def test_pyls_format_pyi_document (unformatted_pyi_document , formatted_pyi_document ):
103- result = pylsp_format_document (unformatted_pyi_document )
125+ def test_pyls_format_pyi_document (
126+ config , unformatted_pyi_document , formatted_pyi_document
127+ ):
128+ result = pylsp_format_document (config , unformatted_pyi_document )
104129
105130 assert result == [
106131 {
@@ -113,26 +138,26 @@ def test_pyls_format_pyi_document(unformatted_pyi_document, formatted_pyi_docume
113138 ]
114139
115140
116- def test_pylsp_format_document_unchanged (formatted_document ):
117- result = pylsp_format_document (formatted_document )
141+ def test_pylsp_format_document_unchanged (config , formatted_document ):
142+ result = pylsp_format_document (config , formatted_document )
118143
119144 assert result == []
120145
121146
122- def test_pyls_format_pyi_document_unchanged (formatted_pyi_document ):
123- result = pylsp_format_document (formatted_pyi_document )
147+ def test_pyls_format_pyi_document_unchanged (config , formatted_pyi_document ):
148+ result = pylsp_format_document (config , formatted_pyi_document )
124149
125150 assert result == []
126151
127152
128- def test_pylsp_format_document_syntax_error (invalid_document ):
129- result = pylsp_format_document (invalid_document )
153+ def test_pylsp_format_document_syntax_error (config , invalid_document ):
154+ result = pylsp_format_document (config , invalid_document )
130155
131156 assert result == []
132157
133158
134- def test_pylsp_format_document_with_config (config_document ):
135- result = pylsp_format_document (config_document )
159+ def test_pylsp_format_document_with_config (config , config_document ):
160+ result = pylsp_format_document (config , config_document )
136161
137162 assert result == [
138163 {
@@ -157,13 +182,13 @@ def test_pylsp_format_document_with_config(config_document):
157182 ("start" , "end" , "expected" ),
158183 [(0 , 0 , 'a = "hello"\n ' ), (1 , 1 , "b = 42\n " ), (0 , 1 , 'a = "hello"\n b = 42\n ' )],
159184)
160- def test_pylsp_format_range (unformatted_document , start , end , expected ):
185+ def test_pylsp_format_range (config , unformatted_document , start , end , expected ):
161186 range = {
162187 "start" : {"line" : start , "character" : 0 },
163188 "end" : {"line" : end , "character" : 0 },
164189 }
165190
166- result = pylsp_format_range (unformatted_document , range = range )
191+ result = pylsp_format_range (config , unformatted_document , range = range )
167192
168193 assert result == [
169194 {
@@ -176,24 +201,24 @@ def test_pylsp_format_range(unformatted_document, start, end, expected):
176201 ]
177202
178203
179- def test_pylsp_format_range_unchanged (formatted_document ):
204+ def test_pylsp_format_range_unchanged (config , formatted_document ):
180205 range = {"start" : {"line" : 0 , "character" : 0 }, "end" : {"line" : 1 , "character" : 0 }}
181206
182- result = pylsp_format_range (formatted_document , range = range )
207+ result = pylsp_format_range (config , formatted_document , range = range )
183208
184209 assert result == []
185210
186211
187- def test_pylsp_format_range_syntax_error (invalid_document ):
212+ def test_pylsp_format_range_syntax_error (config , invalid_document ):
188213 range = {"start" : {"line" : 0 , "character" : 0 }, "end" : {"line" : 1 , "character" : 0 }}
189214
190- result = pylsp_format_range (invalid_document , range = range )
215+ result = pylsp_format_range (config , invalid_document , range = range )
191216
192217 assert result == []
193218
194219
195- def test_load_config ():
196- config = load_config (str (fixtures_dir / "config" / "example.py" ))
220+ def test_load_config (config ):
221+ config = load_config (str (fixtures_dir / "config" / "example.py" ), config )
197222
198223 # TODO split into smaller tests
199224 assert config == {
@@ -205,14 +230,14 @@ def test_load_config():
205230 }
206231
207232
208- def test_load_config_target_version ():
209- config = load_config (str (fixtures_dir / "target_version" / "example.py" ))
233+ def test_load_config_target_version (config ):
234+ config = load_config (str (fixtures_dir / "target_version" / "example.py" ), config )
210235
211236 assert config ["target_version" ] == {black .TargetVersion .PY39 }
212237
213238
214- def test_load_config_py36 ():
215- config = load_config (str (fixtures_dir / "py36" / "example.py" ))
239+ def test_load_config_py36 (config ):
240+ config = load_config (str (fixtures_dir / "py36" / "example.py" ), config )
216241
217242 assert config ["target_version" ] == {
218243 black .TargetVersion .PY36 ,
@@ -223,8 +248,8 @@ def test_load_config_py36():
223248 }
224249
225250
226- def test_load_config_defaults ():
227- config = load_config (str (fixtures_dir / "example.py" ))
251+ def test_load_config_defaults (config ):
252+ config = load_config (str (fixtures_dir / "example.py" ), config )
228253
229254 assert config == {
230255 "line_length" : 88 ,
@@ -245,8 +270,10 @@ def test_entry_point():
245270 assert isinstance (module , types .ModuleType )
246271
247272
248- def test_pylsp_format_crlf_document (unformatted_crlf_document , formatted_crlf_document ):
249- result = pylsp_format_document (unformatted_crlf_document )
273+ def test_pylsp_format_crlf_document (
274+ config , unformatted_crlf_document , formatted_crlf_document
275+ ):
276+ result = pylsp_format_document (config , unformatted_crlf_document )
250277
251278 assert result == [
252279 {
@@ -257,3 +284,20 @@ def test_pylsp_format_crlf_document(unformatted_crlf_document, formatted_crlf_do
257284 "newText" : formatted_crlf_document .source ,
258285 }
259286 ]
287+
288+
289+ def test_pylsp_format_line_length (
290+ config , unformatted_line_length , formatted_line_length
291+ ):
292+ config .update ({"plugins" : {"black" : {"line_length" : 79 }}})
293+ result = pylsp_format_document (config , unformatted_line_length )
294+
295+ assert result == [
296+ {
297+ "range" : {
298+ "start" : {"line" : 0 , "character" : 0 },
299+ "end" : {"line" : 3 , "character" : 0 },
300+ },
301+ "newText" : formatted_line_length .source ,
302+ }
303+ ]
0 commit comments