Skip to content

Commit f280c9f

Browse files
committed
Add tests.
1 parent 0e5c0fe commit f280c9f

File tree

6 files changed

+180
-115
lines changed

6 files changed

+180
-115
lines changed

tests/conftest.py

Lines changed: 7 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -5,23 +5,19 @@
55
import pytest
66
from pylsp import uris
77
from pylsp.config.config import Config
8-
from pylsp.workspace import Workspace, Document
9-
8+
from pylsp.workspace import Document, Workspace
109

1110
here = Path(__file__).parent
1211
fixtures_dir = here / "fixtures"
1312

1413

1514
@pytest.fixture
1615
def config(workspace):
17-
"""Return a config object."""
1816
cfg = Config(workspace.root_uri, {}, 0, {})
1917
cfg._plugin_settings = {
2018
"plugins": {
21-
"pylint": {
22-
"enabled": False,
23-
"args": [],
24-
"executable": None,
19+
"isort": {
20+
"enabled": True,
2521
},
2622
},
2723
}
@@ -37,29 +33,13 @@ def workspace(tmpdir):
3733

3834

3935
@pytest.fixture
40-
def document(workspace):
41-
return create_document(workspace, "simple.py")
36+
def unformatted_document(workspace):
37+
return create_document(workspace, "unformatted.py")
4238

4339

4440
@pytest.fixture
45-
def code_action_context():
46-
# https://microsoft.github.io/language-server-protocol/specifications/specification-3-17/#codeActionKind
47-
code_action_kind = [
48-
"",
49-
"quickfix",
50-
"refactor",
51-
"refactor.extract",
52-
"refactor.inline",
53-
"refactor.rewrite",
54-
"source",
55-
"source.organizeImports",
56-
"source.fixAll",
57-
]
58-
59-
return {
60-
"diagnostics": [],
61-
"only": code_action_kind,
62-
}
41+
def formatted_document(workspace):
42+
return create_document(workspace, "formatted.py")
6343

6444

6545
def create_document(workspace, name):

tests/fixtures/formatted.py

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
from __future__ import absolute_import
2+
3+
import os
4+
import sys
5+
6+
from third_party import (lib1, lib2, lib3, lib4, lib5, lib6, lib7, lib8, lib9,
7+
lib10, lib11, lib12, lib13, lib14, lib15)
8+
9+
from pylsp_isort.plugin import Range, isort_config, pylsp_settings

tests/fixtures/formatted_black.py

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
from __future__ import absolute_import
2+
3+
import os
4+
import sys
5+
6+
from third_party import (
7+
lib1,
8+
lib2,
9+
lib3,
10+
lib4,
11+
lib5,
12+
lib6,
13+
lib7,
14+
lib8,
15+
lib9,
16+
lib10,
17+
lib11,
18+
lib12,
19+
lib13,
20+
lib14,
21+
lib15,
22+
)
23+
24+
from pylsp_isort.plugin import Range, isort_config, pylsp_settings

tests/fixtures/simple.py

Lines changed: 0 additions & 3 deletions
This file was deleted.

tests/fixtures/unformatted.py

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
from pylsp_isort.plugin import isort_config
2+
3+
import os
4+
5+
from pylsp_isort.plugin import Range
6+
7+
from pylsp_isort.plugin import pylsp_settings
8+
9+
import sys
10+
11+
from third_party import lib15, lib1, lib2, lib3, lib4, lib5, lib6, lib7, lib8, lib9, lib10, lib11, lib12, lib13, lib14
12+
13+
import sys
14+
15+
from __future__ import absolute_import
16+
17+
from third_party import lib3

tests/test_plugin.py

Lines changed: 123 additions & 85 deletions
Original file line numberDiff line numberDiff line change
@@ -1,96 +1,134 @@
1-
from unittest.mock import ANY
1+
import os
2+
from dataclasses import asdict
3+
4+
import isort
5+
import pytest
26

37
from pylsp_isort import plugin
4-
from test.conftest import *
58

69

7-
def test_definitions(config, workspace, document):
8-
position = {"line": 3, "character": 6}
10+
def _read_content(filename):
11+
path = os.path.join(os.path.dirname(__file__), "fixtures", filename)
12+
with open(path) as f:
13+
return f.read()
14+
15+
16+
@pytest.mark.parametrize(
17+
("text", "settings", "expected"),
18+
[
19+
(
20+
_read_content("unformatted.py"),
21+
{},
22+
_read_content("formatted.py"),
23+
),
24+
(
25+
_read_content("unformatted.py"),
26+
{"profile": "black"},
27+
_read_content("formatted_black.py"),
28+
),
29+
],
30+
)
31+
def test_run_isort(text, settings, expected):
32+
actual = plugin.run_isort(text, settings)
33+
assert actual == expected
934

10-
response = plugin.pylsp_definitions(
11-
config=config,
12-
workspace=workspace,
13-
document=document,
14-
position=position,
15-
)
1635

17-
expected = [
18-
{
19-
"uri": ANY,
20-
"range": {
21-
"start": {
22-
"line": ANY,
23-
"character": ANY,
24-
},
25-
"end": {
26-
"line": ANY,
27-
"character": ANY,
28-
},
29-
},
30-
},
31-
]
32-
33-
assert response == expected
34-
35-
36-
def test_code_action(config, workspace, document, code_action_context):
37-
selection = {
38-
"start": {
39-
"line": 3,
40-
"character": 0,
41-
},
42-
"end": {
43-
"line": 4,
44-
"character": 0,
45-
},
46-
}
47-
48-
response = plugin.pylsp_code_actions(
49-
config=config,
50-
workspace=workspace,
51-
document=document,
52-
range=selection,
53-
context=code_action_context,
36+
@pytest.mark.parametrize(
37+
("settings", "target_path", "expected", "check_sources"),
38+
[
39+
(
40+
{},
41+
None,
42+
isort.Config(),
43+
True,
44+
),
45+
(
46+
{"profile": "black"},
47+
None,
48+
isort.Config(profile="black"),
49+
True,
50+
),
51+
(
52+
{"profile": "black", "line_length": 120},
53+
None,
54+
isort.Config(profile="black", line_length=120),
55+
True,
56+
),
57+
(
58+
{},
59+
__file__,
60+
isort.Config(settings_path=os.path.dirname(__file__)),
61+
True,
62+
),
63+
(
64+
{},
65+
__file__,
66+
isort.Config(profile="black"),
67+
False,
68+
),
69+
],
70+
)
71+
def test_isort_config(settings, target_path, expected, check_sources):
72+
actual = plugin.isort_config(settings, target_path)
73+
actual_dict = asdict(actual)
74+
expected_dict = asdict(expected)
75+
if not check_sources:
76+
del actual_dict["sources"]
77+
del expected_dict["sources"]
78+
assert actual_dict == expected_dict
79+
80+
81+
def test_pylsp_format_document(config, unformatted_document, formatted_document):
82+
actual = _receive(plugin.pylsp_format_document, config, unformatted_document)
83+
84+
text = _read_content(formatted_document.path)
85+
range = plugin.Range(
86+
start={"line": 0, "character": 0},
87+
end={"line": len(unformatted_document.lines), "character": 0},
5488
)
89+
expected = [{"range": range, "newText": text}]
90+
91+
assert actual == expected
5592

56-
expected = [
57-
{
58-
"title": "Extract method",
59-
"kind": "refactor.extract",
60-
"command": {
61-
"command": "example.refactor.extract",
62-
"arguments": [document.uri, selection],
63-
},
64-
},
65-
]
66-
67-
assert response == expected
68-
69-
command = response[0]["command"]["command"]
70-
arguments = response[0]["command"]["arguments"]
71-
72-
response = plugin.pylsp_execute_command(
73-
config=config,
74-
workspace=workspace,
75-
command=command,
76-
arguments=arguments,
93+
94+
def test_pylsp_format_range(config, unformatted_document):
95+
range = plugin.Range(
96+
start={"line": 2, "character": 0},
97+
end={"line": 9, "character": 0},
7798
)
99+
actual = _receive(plugin.pylsp_format_range, config, unformatted_document, range)
78100

79-
workspace._endpoint.request.assert_called_once_with(
80-
"workspace/applyEdit",
81-
{
82-
"edit": {
83-
"changes": {
84-
document.uri: [
85-
{
86-
"range": {
87-
"start": {"line": 3, "character": 0},
88-
"end": {"line": 4, "character": 0},
89-
},
90-
"newText": "replacement text",
91-
},
92-
],
93-
},
94-
},
95-
},
101+
text = "\n".join(
102+
[
103+
"import os",
104+
"import sys",
105+
"",
106+
"from pylsp_isort.plugin import Range, pylsp_settings",
107+
]
96108
)
109+
text += "\n"
110+
expected = [{"range": range, "newText": text}]
111+
112+
assert actual == expected
113+
114+
115+
def _receive(func, *args):
116+
gen = func(*args)
117+
next(gen)
118+
outcome = MockResult([])
119+
try:
120+
gen.send(outcome)
121+
except StopIteration:
122+
pass
123+
return outcome.get_result()
124+
125+
126+
class MockResult:
127+
def __init__(self, result):
128+
self.result = result
129+
130+
def get_result(self):
131+
return self.result
132+
133+
def force_result(self, result):
134+
self.result = result

0 commit comments

Comments
 (0)