|
11 | 11 |
|
12 | 12 | import pylsp_ruff.plugin as plugin
|
13 | 13 |
|
| 14 | +_UNSORTED_IMPORTS = tw.dedent( |
| 15 | + """ |
| 16 | + from thirdparty import x |
| 17 | + import io |
| 18 | + import asyncio |
| 19 | + """ |
| 20 | +).strip() |
| 21 | + |
| 22 | +_SORTED_IMPORTS = tw.dedent( |
| 23 | + """ |
| 24 | + import asyncio |
| 25 | + import io |
| 26 | +
|
| 27 | + from thirdparty import x |
| 28 | + """ |
| 29 | +).strip() |
| 30 | + |
| 31 | +_UNFORMATTED_CODE = tw.dedent( |
| 32 | + """ |
| 33 | + def foo(): pass |
| 34 | + def bar(): pass |
| 35 | + """ |
| 36 | +).strip() |
| 37 | + |
| 38 | +_FORMATTED_CODE = tw.dedent( |
| 39 | + """ |
| 40 | + def foo(): |
| 41 | + pass |
| 42 | +
|
| 43 | +
|
| 44 | + def bar(): |
| 45 | + pass |
| 46 | + """ |
| 47 | +).strip() |
| 48 | + |
14 | 49 |
|
15 | 50 | @pytest.fixture()
|
16 | 51 | def workspace(tmp_path):
|
@@ -54,40 +89,26 @@ def force_result(self, r):
|
54 | 89 | return pytest.fail()
|
55 | 90 |
|
56 | 91 |
|
57 |
| -def test_ruff_format(workspace): |
58 |
| - # imports incorrectly ordered, |
59 |
| - # body of foo has a line that's too long |
60 |
| - # def bar() line missing whitespace above |
61 |
| - txt = tw.dedent( |
62 |
| - """ |
63 |
| - from thirdparty import x |
64 |
| - import io |
65 |
| - import asyncio |
66 |
| -
|
67 |
| - def foo(): |
68 |
| - print("this is a looooooooooooooooooooooooooooooooooooooooooooong line that should exceed the usual line-length limit which is normally eighty-eight columns") |
69 |
| - def bar(): |
70 |
| - pass |
71 |
| - """ # noqa: E501 |
72 |
| - ).lstrip() |
73 |
| - want = tw.dedent( |
74 |
| - """ |
75 |
| - import asyncio |
76 |
| - import io |
77 |
| -
|
78 |
| - from thirdparty import x |
79 |
| -
|
80 |
| -
|
81 |
| - def foo(): |
82 |
| - print( |
83 |
| - "this is a looooooooooooooooooooooooooooooooooooooooooooong line that should exceed the usual line-length limit which is normally eighty-eight columns" |
84 |
| - ) |
85 |
| -
|
86 |
| -
|
87 |
| - def bar(): |
88 |
| - pass |
89 |
| - """ # noqa: E501 |
90 |
| - ).lstrip() |
| 92 | +def test_ruff_format_only(workspace): |
| 93 | + txt = f"{_UNSORTED_IMPORTS}\n{_UNFORMATTED_CODE}" |
| 94 | + want = f"{_UNSORTED_IMPORTS}\n\n\n{_FORMATTED_CODE}\n" |
| 95 | + _, doc = temp_document(txt, workspace) |
| 96 | + got = run_plugin_format(workspace, doc) |
| 97 | + assert want == got |
| 98 | + |
| 99 | + |
| 100 | +def test_ruff_format_and_sort_imports(workspace): |
| 101 | + txt = f"{_UNSORTED_IMPORTS}\n{_UNFORMATTED_CODE}" |
| 102 | + want = f"{_SORTED_IMPORTS}\n\n\n{_FORMATTED_CODE}\n" |
91 | 103 | _, doc = temp_document(txt, workspace)
|
| 104 | + workspace._config.update( |
| 105 | + { |
| 106 | + "plugins": { |
| 107 | + "ruff": { |
| 108 | + "format": ["I001"], |
| 109 | + } |
| 110 | + } |
| 111 | + } |
| 112 | + ) |
92 | 113 | got = run_plugin_format(workspace, doc)
|
93 |
| - assert want == got, f"want:\n{want}\n\ngot:\n{got}" |
| 114 | + assert want == got |
0 commit comments