10
10
11
11
# Python LSP imports
12
12
from pylsp import uris
13
+ from pylsp .config .config import Config
13
14
from pylsp .workspace import Document , Workspace
14
15
15
16
# Local imports
@@ -25,6 +26,14 @@ def workspace(tmpdir):
25
26
return Workspace (uris .from_fs_path (str (tmpdir )), Mock ())
26
27
27
28
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
+
28
37
@pytest .fixture
29
38
def unformatted_document (workspace ):
30
39
path = fixtures_dir / "unformatted.txt"
@@ -85,8 +94,22 @@ def config_document(workspace):
85
94
return Document (uri , workspace )
86
95
87
96
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 )
90
113
91
114
assert result == [
92
115
{
@@ -99,8 +122,10 @@ def test_pylsp_format_document(unformatted_document, formatted_document):
99
122
]
100
123
101
124
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 )
104
129
105
130
assert result == [
106
131
{
@@ -113,26 +138,26 @@ def test_pyls_format_pyi_document(unformatted_pyi_document, formatted_pyi_docume
113
138
]
114
139
115
140
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 )
118
143
119
144
assert result == []
120
145
121
146
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 )
124
149
125
150
assert result == []
126
151
127
152
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 )
130
155
131
156
assert result == []
132
157
133
158
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 )
136
161
137
162
assert result == [
138
163
{
@@ -157,13 +182,13 @@ def test_pylsp_format_document_with_config(config_document):
157
182
("start" , "end" , "expected" ),
158
183
[(0 , 0 , 'a = "hello"\n ' ), (1 , 1 , "b = 42\n " ), (0 , 1 , 'a = "hello"\n b = 42\n ' )],
159
184
)
160
- def test_pylsp_format_range (unformatted_document , start , end , expected ):
185
+ def test_pylsp_format_range (config , unformatted_document , start , end , expected ):
161
186
range = {
162
187
"start" : {"line" : start , "character" : 0 },
163
188
"end" : {"line" : end , "character" : 0 },
164
189
}
165
190
166
- result = pylsp_format_range (unformatted_document , range = range )
191
+ result = pylsp_format_range (config , unformatted_document , range = range )
167
192
168
193
assert result == [
169
194
{
@@ -176,24 +201,24 @@ def test_pylsp_format_range(unformatted_document, start, end, expected):
176
201
]
177
202
178
203
179
- def test_pylsp_format_range_unchanged (formatted_document ):
204
+ def test_pylsp_format_range_unchanged (config , formatted_document ):
180
205
range = {"start" : {"line" : 0 , "character" : 0 }, "end" : {"line" : 1 , "character" : 0 }}
181
206
182
- result = pylsp_format_range (formatted_document , range = range )
207
+ result = pylsp_format_range (config , formatted_document , range = range )
183
208
184
209
assert result == []
185
210
186
211
187
- def test_pylsp_format_range_syntax_error (invalid_document ):
212
+ def test_pylsp_format_range_syntax_error (config , invalid_document ):
188
213
range = {"start" : {"line" : 0 , "character" : 0 }, "end" : {"line" : 1 , "character" : 0 }}
189
214
190
- result = pylsp_format_range (invalid_document , range = range )
215
+ result = pylsp_format_range (config , invalid_document , range = range )
191
216
192
217
assert result == []
193
218
194
219
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 )
197
222
198
223
# TODO split into smaller tests
199
224
assert config == {
@@ -205,14 +230,14 @@ def test_load_config():
205
230
}
206
231
207
232
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 )
210
235
211
236
assert config ["target_version" ] == {black .TargetVersion .PY39 }
212
237
213
238
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 )
216
241
217
242
assert config ["target_version" ] == {
218
243
black .TargetVersion .PY36 ,
@@ -223,8 +248,8 @@ def test_load_config_py36():
223
248
}
224
249
225
250
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 )
228
253
229
254
assert config == {
230
255
"line_length" : 88 ,
@@ -245,8 +270,10 @@ def test_entry_point():
245
270
assert isinstance (module , types .ModuleType )
246
271
247
272
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 )
250
277
251
278
assert result == [
252
279
{
@@ -257,3 +284,20 @@ def test_pylsp_format_crlf_document(unformatted_crlf_document, formatted_crlf_do
257
284
"newText" : formatted_crlf_document .source ,
258
285
}
259
286
]
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