Repository navigation
Expand file tree
/
Copy pathtest_ruff_format.py
More file actions
129 lines (103 loc) · 2.93 KB
/
Copy pathtest_ruff_format.py
File metadata and controls
129 lines (103 loc) · 2.93 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
import contextlib
import tempfile
import textwrap as tw
from typing import Any, List, Mapping, Optional
from unittest.mock import Mock
import pytest
from pylsp import uris
from pylsp.config.config import Config
from pylsp.workspace import Document, Workspace
import pylsp_ruff.plugin as plugin
_UNSORTED_IMPORTS = tw.dedent(
"""
from thirdparty import x
import io
import asyncio
"""
).strip()
_SORTED_IMPORTS = tw.dedent(
"""
import asyncio
import io
from thirdparty import x
"""
).strip()
_UNFORMATTED_CODE = tw.dedent(
"""
def foo(): pass
def bar(): pass
"""
).strip()
_FORMATTED_CODE = tw.dedent(
"""
def foo():
pass
def bar():
pass
"""
).strip()
@pytest.fixture()
def workspace(tmp_path):
"""Return a workspace."""
ws = Workspace(tmp_path.absolute().as_uri(), Mock())
ws._config = Config(ws.root_uri, {}, 0, {})
ws._config.update({"plugins": {"ruff": {"isolated": True}}})
return ws
def temp_document(doc_text, workspace):
with tempfile.NamedTemporaryFile(
mode="w", dir=workspace.root_path, delete=False
) as temp_file:
name = temp_file.name
temp_file.write(doc_text)
doc = Document(uris.from_fs_path(name), workspace)
return name, doc
def run_plugin_format(workspace: Workspace, doc: Document) -> str:
class TestResult:
result: Optional[List[Mapping[str, Any]]]
def __init__(self):
self.result = None
def get_result(self):
return self.result
def force_result(self, r):
self.result = r
generator = plugin.pylsp_format_document(workspace, doc)
result = TestResult()
with contextlib.suppress(StopIteration):
generator.send(None)
generator.send(result)
if result.result:
return result.result[0]["newText"]
return ""
def test_ruff_format_only(workspace):
txt = f"{_UNSORTED_IMPORTS}\n{_UNFORMATTED_CODE}"
want = f"{_UNSORTED_IMPORTS}\n\n\n{_FORMATTED_CODE}\n"
_, doc = temp_document(txt, workspace)
got = run_plugin_format(workspace, doc)
assert want == got
def test_ruff_format_disabled(workspace):
_, doc = temp_document(_UNFORMATTED_CODE, workspace)
workspace._config.update(
{
"plugins": {
"ruff": {"isolated": True, "format": ["I001"], "formatEnabled": False}
}
}
)
got = run_plugin_format(workspace, doc)
assert got == ""
def test_ruff_format_and_sort_imports(workspace):
txt = f"{_UNSORTED_IMPORTS}\n{_UNFORMATTED_CODE}"
want = f"{_SORTED_IMPORTS}\n\n\n{_FORMATTED_CODE}\n"
_, doc = temp_document(txt, workspace)
workspace._config.update(
{
"plugins": {
"ruff": {
"isolated": True,
"format": ["I001"],
}
}
}
)
got = run_plugin_format(workspace, doc)
assert want == got