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,8 @@ 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 (config , unformatted_pyi_document , formatted_pyi_document ):
126
+ result = pylsp_format_document (config , unformatted_pyi_document )
104
127
105
128
assert result == [
106
129
{
@@ -113,26 +136,26 @@ def test_pyls_format_pyi_document(unformatted_pyi_document, formatted_pyi_docume
113
136
]
114
137
115
138
116
- def test_pylsp_format_document_unchanged (formatted_document ):
117
- result = pylsp_format_document (formatted_document )
139
+ def test_pylsp_format_document_unchanged (config , formatted_document ):
140
+ result = pylsp_format_document (config , formatted_document )
118
141
119
142
assert result == []
120
143
121
144
122
- def test_pyls_format_pyi_document_unchanged (formatted_pyi_document ):
123
- result = pylsp_format_document (formatted_pyi_document )
145
+ def test_pyls_format_pyi_document_unchanged (config , formatted_pyi_document ):
146
+ result = pylsp_format_document (config , formatted_pyi_document )
124
147
125
148
assert result == []
126
149
127
150
128
- def test_pylsp_format_document_syntax_error (invalid_document ):
129
- result = pylsp_format_document (invalid_document )
151
+ def test_pylsp_format_document_syntax_error (config , invalid_document ):
152
+ result = pylsp_format_document (config , invalid_document )
130
153
131
154
assert result == []
132
155
133
156
134
- def test_pylsp_format_document_with_config (config_document ):
135
- result = pylsp_format_document (config_document )
157
+ def test_pylsp_format_document_with_config (config , config_document ):
158
+ result = pylsp_format_document (config , config_document )
136
159
137
160
assert result == [
138
161
{
@@ -157,13 +180,13 @@ def test_pylsp_format_document_with_config(config_document):
157
180
("start" , "end" , "expected" ),
158
181
[(0 , 0 , 'a = "hello"\n ' ), (1 , 1 , "b = 42\n " ), (0 , 1 , 'a = "hello"\n b = 42\n ' )],
159
182
)
160
- def test_pylsp_format_range (unformatted_document , start , end , expected ):
183
+ def test_pylsp_format_range (config , unformatted_document , start , end , expected ):
161
184
range = {
162
185
"start" : {"line" : start , "character" : 0 },
163
186
"end" : {"line" : end , "character" : 0 },
164
187
}
165
188
166
- result = pylsp_format_range (unformatted_document , range = range )
189
+ result = pylsp_format_range (config , unformatted_document , range = range )
167
190
168
191
assert result == [
169
192
{
@@ -176,24 +199,24 @@ def test_pylsp_format_range(unformatted_document, start, end, expected):
176
199
]
177
200
178
201
179
- def test_pylsp_format_range_unchanged (formatted_document ):
202
+ def test_pylsp_format_range_unchanged (config , formatted_document ):
180
203
range = {"start" : {"line" : 0 , "character" : 0 }, "end" : {"line" : 1 , "character" : 0 }}
181
204
182
- result = pylsp_format_range (formatted_document , range = range )
205
+ result = pylsp_format_range (config , formatted_document , range = range )
183
206
184
207
assert result == []
185
208
186
209
187
- def test_pylsp_format_range_syntax_error (invalid_document ):
210
+ def test_pylsp_format_range_syntax_error (config , invalid_document ):
188
211
range = {"start" : {"line" : 0 , "character" : 0 }, "end" : {"line" : 1 , "character" : 0 }}
189
212
190
- result = pylsp_format_range (invalid_document , range = range )
213
+ result = pylsp_format_range (config , invalid_document , range = range )
191
214
192
215
assert result == []
193
216
194
217
195
- def test_load_config ():
196
- config = load_config (str (fixtures_dir / "config" / "example.py" ))
218
+ def test_load_config (config ):
219
+ config = load_config (str (fixtures_dir / "config" / "example.py" ), config )
197
220
198
221
# TODO split into smaller tests
199
222
assert config == {
@@ -205,14 +228,14 @@ def test_load_config():
205
228
}
206
229
207
230
208
- def test_load_config_target_version ():
209
- config = load_config (str (fixtures_dir / "target_version" / "example.py" ))
231
+ def test_load_config_target_version (config ):
232
+ config = load_config (str (fixtures_dir / "target_version" / "example.py" ), config )
210
233
211
234
assert config ["target_version" ] == {black .TargetVersion .PY39 }
212
235
213
236
214
- def test_load_config_py36 ():
215
- config = load_config (str (fixtures_dir / "py36" / "example.py" ))
237
+ def test_load_config_py36 (config ):
238
+ config = load_config (str (fixtures_dir / "py36" / "example.py" ), config )
216
239
217
240
assert config ["target_version" ] == {
218
241
black .TargetVersion .PY36 ,
@@ -223,8 +246,8 @@ def test_load_config_py36():
223
246
}
224
247
225
248
226
- def test_load_config_defaults ():
227
- config = load_config (str (fixtures_dir / "example.py" ))
249
+ def test_load_config_defaults (config ):
250
+ config = load_config (str (fixtures_dir / "example.py" ), config )
228
251
229
252
assert config == {
230
253
"line_length" : 88 ,
@@ -245,8 +268,8 @@ def test_entry_point():
245
268
assert isinstance (module , types .ModuleType )
246
269
247
270
248
- def test_pylsp_format_crlf_document (unformatted_crlf_document , formatted_crlf_document ):
249
- result = pylsp_format_document (unformatted_crlf_document )
271
+ def test_pylsp_format_crlf_document (config , unformatted_crlf_document , formatted_crlf_document ):
272
+ result = pylsp_format_document (config , unformatted_crlf_document )
250
273
251
274
assert result == [
252
275
{
@@ -257,3 +280,18 @@ def test_pylsp_format_crlf_document(unformatted_crlf_document, formatted_crlf_do
257
280
"newText" : formatted_crlf_document .source ,
258
281
}
259
282
]
283
+
284
+
285
+ def test_pylsp_format_line_length (config , unformatted_line_length , formatted_line_length ):
286
+ config .update ({'plugins' : {'black' : {'line_length' : 79 }}})
287
+ result = pylsp_format_document (config , unformatted_line_length )
288
+
289
+ assert result == [
290
+ {
291
+ "range" : {
292
+ "start" : {"line" : 0 , "character" : 0 },
293
+ "end" : {"line" : 3 , "character" : 0 },
294
+ },
295
+ "newText" : formatted_line_length .source ,
296
+ }
297
+ ]
0 commit comments