Skip to content

Commit 6be2701

Browse files
committed
Update tests after adding client config
Also, check that line length can be changed from the client
1 parent 53514c2 commit 6be2701

File tree

3 files changed

+73
-28
lines changed

3 files changed

+73
-28
lines changed
Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
def foo(
2+
aaaaa, bbbbb, ccccc, ddddd, eeeee, fffff, ggggg, hhhhh, iiiii, jjjjj, kkkkk
3+
):
4+
return aaaaa
Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
2+
def foo(aaaaa, bbbbb, ccccc, ddddd, eeeee, fffff, ggggg, hhhhh, iiiii, jjjjj, kkkkk):
3+
return aaaaa

tests/test_plugin.py

Lines changed: 66 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@
1010

1111
# Python LSP imports
1212
from pylsp import uris
13+
from pylsp.config.config import Config
1314
from 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
2938
def 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,8 @@ 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(config, unformatted_pyi_document, formatted_pyi_document):
126+
result = pylsp_format_document(config, unformatted_pyi_document)
104127

105128
assert result == [
106129
{
@@ -113,26 +136,26 @@ def test_pyls_format_pyi_document(unformatted_pyi_document, formatted_pyi_docume
113136
]
114137

115138

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)
118141

119142
assert result == []
120143

121144

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)
124147

125148
assert result == []
126149

127150

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)
130153

131154
assert result == []
132155

133156

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)
136159

137160
assert result == [
138161
{
@@ -157,13 +180,13 @@ def test_pylsp_format_document_with_config(config_document):
157180
("start", "end", "expected"),
158181
[(0, 0, 'a = "hello"\n'), (1, 1, "b = 42\n"), (0, 1, 'a = "hello"\nb = 42\n')],
159182
)
160-
def test_pylsp_format_range(unformatted_document, start, end, expected):
183+
def test_pylsp_format_range(config, unformatted_document, start, end, expected):
161184
range = {
162185
"start": {"line": start, "character": 0},
163186
"end": {"line": end, "character": 0},
164187
}
165188

166-
result = pylsp_format_range(unformatted_document, range=range)
189+
result = pylsp_format_range(config, unformatted_document, range=range)
167190

168191
assert result == [
169192
{
@@ -176,24 +199,24 @@ def test_pylsp_format_range(unformatted_document, start, end, expected):
176199
]
177200

178201

179-
def test_pylsp_format_range_unchanged(formatted_document):
202+
def test_pylsp_format_range_unchanged(config, formatted_document):
180203
range = {"start": {"line": 0, "character": 0}, "end": {"line": 1, "character": 0}}
181204

182-
result = pylsp_format_range(formatted_document, range=range)
205+
result = pylsp_format_range(config, formatted_document, range=range)
183206

184207
assert result == []
185208

186209

187-
def test_pylsp_format_range_syntax_error(invalid_document):
210+
def test_pylsp_format_range_syntax_error(config, invalid_document):
188211
range = {"start": {"line": 0, "character": 0}, "end": {"line": 1, "character": 0}}
189212

190-
result = pylsp_format_range(invalid_document, range=range)
213+
result = pylsp_format_range(config, invalid_document, range=range)
191214

192215
assert result == []
193216

194217

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)
197220

198221
# TODO split into smaller tests
199222
assert config == {
@@ -205,14 +228,14 @@ def test_load_config():
205228
}
206229

207230

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)
210233

211234
assert config["target_version"] == {black.TargetVersion.PY39}
212235

213236

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)
216239

217240
assert config["target_version"] == {
218241
black.TargetVersion.PY36,
@@ -223,8 +246,8 @@ def test_load_config_py36():
223246
}
224247

225248

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)
228251

229252
assert config == {
230253
"line_length": 88,
@@ -245,8 +268,8 @@ def test_entry_point():
245268
assert isinstance(module, types.ModuleType)
246269

247270

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)
250273

251274
assert result == [
252275
{
@@ -257,3 +280,18 @@ def test_pylsp_format_crlf_document(unformatted_crlf_document, formatted_crlf_do
257280
"newText": formatted_crlf_document.source,
258281
}
259282
]
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

Comments
 (0)