Skip to content
Open
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Prev Previous commit
Add a test case to test_ruff_format.py
Update existing test text to be compatible with the case that a format
is run with `format` set to `["ALL"]`.
  • Loading branch information
willjow committed Oct 24, 2024
commit fb54915c8abeaf9bdc831d3b31d2bcbfa211d597
33 changes: 29 additions & 4 deletions tests/test_ruff_format.py
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,12 @@
"""
).strip()

_UNUSED_IMPORTS = tw.dedent(
"""
import unused_import
"""
).strip()

_SORTED_IMPORTS = tw.dedent(
"""
import asyncio
Expand All @@ -30,19 +36,19 @@

_UNFORMATTED_CODE = tw.dedent(
"""
def foo(): pass
def bar(): pass
def foo(): return asyncio.subprocess.DEVNULL
def bar(): return x(io.DEFAULT_BUFFER_SIZE)
"""
).strip()

_FORMATTED_CODE = tw.dedent(
"""
def foo():
pass
return asyncio.subprocess.DEVNULL


def bar():
pass
return x(io.DEFAULT_BUFFER_SIZE)
"""
).strip()

Expand Down Expand Up @@ -121,3 +127,22 @@ def test_ruff_format_and_sort_imports(workspace):
)
got = run_plugin_format(workspace, doc)
assert want == got


def test_ruff_format_via_all(workspace):
Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This is somewhat of a fragile thing to test since it's at the mercy of whatever rules ruff might add. The tested text might be reasonably robust, but it also might be best to just leave this out.

# If `pylsp.plugins.ruff.format` contains "ALL", formatting should apply
# the automatic fixes for unsorted-imports (I001) and unused-import (F401)
txt = f"{_UNUSED_IMPORTS}\n{_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": {
"format": ["ALL"],
}
}
}
)
got = run_plugin_format(workspace, doc)
assert want == got