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 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387
|
# Standard library imports
import types
from pathlib import Path
from unittest.mock import Mock
from importlib.metadata import entry_points
# Third-party imports
import black
import pytest
# Python LSP imports
from pylsp import uris
from pylsp.config.config import Config
from pylsp.workspace import Document, Workspace
# Local imports
from pylsp_black.plugin import (
_load_config,
load_config,
pylsp_format_document,
pylsp_format_range,
pylsp_settings,
)
here = Path(__file__).parent
fixtures_dir = here / "fixtures"
@pytest.fixture
def workspace(tmpdir):
"""Return a workspace."""
return Workspace(uris.from_fs_path(str(tmpdir)), Mock())
@pytest.fixture
def config(workspace):
"""Return a config object."""
cfg = Config(workspace.root_uri, {}, 0, {})
cfg._plugin_settings = {
"plugins": {"black": {"line_length": 88, "cache_config": False}}
}
return cfg
@pytest.fixture
def config_with_skip_options(workspace):
"""Return a config object."""
cfg = Config(workspace.root_uri, {}, 0, {})
cfg._plugin_settings = {
"plugins": {
"black": {
"line_length": 88,
"cache_config": False,
"skip_string_normalization": True,
"skip_magic_trailing_comma": True,
}
}
}
return cfg
@pytest.fixture
def unformatted_document(workspace):
path = fixtures_dir / "unformatted.txt"
uri = f"file:/{path}" # noqa
return Document(uri, workspace)
@pytest.fixture
def unformatted_pyi_document(workspace):
path = fixtures_dir / "unformatted.pyi"
uri = f"file:/{path}" # noqa
return Document(uri, workspace)
@pytest.fixture
def unformatted_crlf_document(workspace):
path = fixtures_dir / "unformatted-crlf.py"
uri = f"file:/{path}" # noqa
with open(path, "r", newline="") as f:
source = f.read()
return Document(uri, workspace, source=source)
@pytest.fixture
def formatted_document(workspace):
path = fixtures_dir / "formatted.txt"
uri = f"file:/{path}" # noqa
return Document(uri, workspace)
@pytest.fixture
def formatted_pyi_document(workspace):
path = fixtures_dir / "formatted.pyi"
uri = f"file:/{path}" # noqa
return Document(uri, workspace)
@pytest.fixture
def formatted_crlf_document(workspace):
path = fixtures_dir / "formatted-crlf.py"
uri = f"file:/{path}" # noqa
with open(path, "r", newline="") as f:
source = f.read()
return Document(uri, workspace, source=source)
@pytest.fixture
def invalid_document(workspace):
path = fixtures_dir / "invalid.txt"
uri = f"file:/{path}" # noqa
return Document(uri, workspace)
@pytest.fixture
def config_document(workspace):
path = fixtures_dir / "config" / "config.txt"
uri = f"file:/{path}" # noqa
return Document(uri, workspace)
@pytest.fixture
def unformatted_line_length(workspace):
path = fixtures_dir / "unformatted-line-length.py"
uri = f"file:/{path}" # noqa
return Document(uri, workspace)
@pytest.fixture
def formatted_line_length(workspace):
path = fixtures_dir / "formatted-line-length.py"
uri = f"file:/{path}" # noqa
return Document(uri, workspace)
def test_pylsp_format_document(config, unformatted_document, formatted_document):
result = pylsp_format_document(config, unformatted_document)
assert result == [
{
"range": {
"start": {"line": 0, "character": 0},
"end": {"line": 3, "character": 0},
},
"newText": formatted_document.source,
}
]
def test_pyls_format_pyi_document(
config, unformatted_pyi_document, formatted_pyi_document
):
result = pylsp_format_document(config, unformatted_pyi_document)
assert result == [
{
"range": {
"start": {"line": 0, "character": 0},
"end": {"line": 5, "character": 0},
},
"newText": formatted_pyi_document.source,
}
]
def test_pylsp_format_document_unchanged(config, formatted_document):
result = pylsp_format_document(config, formatted_document)
assert result == []
def test_pyls_format_pyi_document_unchanged(config, formatted_pyi_document):
result = pylsp_format_document(config, formatted_pyi_document)
assert result == []
def test_pylsp_format_document_syntax_error(config, invalid_document):
result = pylsp_format_document(config, invalid_document)
assert result == []
def test_pylsp_format_document_with_config(config, config_document):
result = pylsp_format_document(config, config_document)
assert result == [
{
"range": {
"start": {"line": 0, "character": 0},
"end": {"line": 1, "character": 0},
},
"newText": (
"run(\n"
" these,\n"
" arguments,\n"
" should,\n"
" be,\n"
" wrapped,\n"
")\n"
),
}
]
@pytest.mark.parametrize(
("start", "end", "expected"),
[
(0, 0, 'a = "hello"\n'),
(
1,
1,
'b = [\n "a",\n "very",\n "very",\n "very",\n "very",\n "very",\n "very",\n "very",\n "very",\n "long",\n "line",\n]\n', # noqa: E501
),
(2, 2, "c = 42\n"),
(
0,
2,
'a = "hello"\nb = [\n "a",\n "very",\n "very",\n "very",\n "very",\n "very",\n "very",\n "very",\n "very",\n "long",\n "line",\n]\nc = 42\n', # noqa: E501
),
],
)
def test_pylsp_format_range(config, unformatted_document, start, end, expected):
range = {
"start": {"line": start, "character": 0},
"end": {"line": end, "character": 0},
}
result = pylsp_format_range(config, unformatted_document, range=range)
assert result == [
{
"range": {
"start": {"line": start, "character": 0},
"end": {"line": end + 1, "character": 0},
},
"newText": expected,
}
]
def test_pylsp_format_range_unchanged(config, formatted_document):
range = {"start": {"line": 0, "character": 0}, "end": {"line": 1, "character": 0}}
result = pylsp_format_range(config, formatted_document, range=range)
assert result == []
def test_pylsp_format_range_syntax_error(config, invalid_document):
range = {"start": {"line": 0, "character": 0}, "end": {"line": 1, "character": 0}}
result = pylsp_format_range(config, invalid_document, range=range)
assert result == []
def test_load_config(config):
config = load_config(str(fixtures_dir / "config" / "example.py"), config)
# TODO split into smaller tests
assert config == {
"line_length": 20,
"target_version": set(),
"pyi": True,
"fast": True,
"skip_magic_trailing_comma": True,
"skip_string_normalization": True,
"preview": True,
}
def test_load_config_target_version(config):
config = load_config(str(fixtures_dir / "target_version" / "example.py"), config)
assert config["target_version"] == {black.TargetVersion.PY39}
def test_load_config_defaults(config):
config = load_config(str(fixtures_dir / "example.py"), config)
assert config == {
"line_length": 88,
"target_version": set(
[
black.TargetVersion.PY38,
black.TargetVersion.PY39,
black.TargetVersion.PY310,
black.TargetVersion.PY311,
]
),
"pyi": False,
"fast": False,
"skip_magic_trailing_comma": False,
"skip_string_normalization": False,
"preview": False,
}
def test_load_config_with_skip_options(config_with_skip_options):
config = load_config(
str(fixtures_dir / "skip_options" / "example.py"), config_with_skip_options
)
assert config == {
"line_length": 88,
"target_version": set(
[
black.TargetVersion.PY38,
black.TargetVersion.PY39,
black.TargetVersion.PY310,
black.TargetVersion.PY311,
]
),
"pyi": False,
"fast": False,
"skip_magic_trailing_comma": True,
"skip_string_normalization": True,
"preview": False,
}
def test_entry_point():
(entry_point,) = entry_points(group="pylsp", name="black")
assert entry_point is not None
module = entry_point.load()
assert isinstance(module, types.ModuleType)
def test_pylsp_format_crlf_document(
config, unformatted_crlf_document, formatted_crlf_document
):
result = pylsp_format_document(config, unformatted_crlf_document)
assert result == [
{
"range": {
"start": {"line": 0, "character": 0},
"end": {"line": 4, "character": 0},
},
"newText": formatted_crlf_document.source,
}
]
def test_pylsp_format_line_length(
config, unformatted_line_length, formatted_line_length
):
config.update({"plugins": {"black": {"line_length": 79}}})
result = pylsp_format_document(config, unformatted_line_length)
assert result == [
{
"range": {
"start": {"line": 0, "character": 0},
"end": {"line": 3, "character": 0},
},
"newText": formatted_line_length.source,
}
]
def test_cache_config(config, unformatted_document):
# Cache should be off by default
for _ in range(5):
pylsp_format_document(config, unformatted_document)
assert _load_config.cache_info().hits == 0
# Enable cache
config.update({"plugins": {"black": {"cache_config": True}}})
# Cache should be working now
for _ in range(5):
pylsp_format_document(config, unformatted_document)
assert _load_config.cache_info().hits == 4
def test_pylsp_settings(config):
plugins = dict(config.plugin_manager.list_name_plugin())
assert "black" in plugins
assert plugins["black"] not in config.disabled_plugins
config.update({"plugins": {"black": {"enabled": False}}})
assert plugins["black"] in config.disabled_plugins
config.update(pylsp_settings())
assert plugins["black"] not in config.disabled_plugins
|