-
-
Notifications
You must be signed in to change notification settings - Fork 761
Expand file tree
/
Copy pathruff_integration.vader
More file actions
391 lines (310 loc) · 13.1 KB
/
ruff_integration.vader
File metadata and controls
391 lines (310 loc) · 13.1 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
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
388
389
390
" Comprehensive Ruff integration tests
" Tests for Ruff linting and formatting functionality
Before:
source tests/vader/setup.vim
call SetupPythonBuffer()
After:
source tests/vader/setup.vim
call CleanupPythonBuffer()
# Test Ruff configuration variables
Execute (Test Ruff configuration variables):
" Test that Ruff-specific configuration variables exist
Assert exists('g:pymode_ruff_enabled'), 'g:pymode_ruff_enabled should exist'
Assert exists('g:pymode_ruff_format_enabled'), 'g:pymode_ruff_format_enabled should exist'
Assert exists('g:pymode_ruff_select'), 'g:pymode_ruff_select should exist'
Assert exists('g:pymode_ruff_ignore'), 'g:pymode_ruff_ignore should exist'
Assert exists('g:pymode_ruff_config_file'), 'g:pymode_ruff_config_file should exist'
Assert 1, 'All Ruff configuration variables exist'
# Test Ruff linting basic functionality
Execute (Test Ruff linting basic):
" Clear buffer and set content with linting issues
%delete _
call setline(1, ['import os', 'x = 1', 'y = 2', 'print(x)'])
" Give the buffer a filename
let temp_file = tempname() . '.py'
execute 'write ' . temp_file
execute 'edit ' . temp_file
" Run linting (should use Ruff)
PymodeLint
" Verify linting completed (no errors expected for this simple code)
Assert 1, "Ruff linting completed successfully"
" Clean up temp file
call delete(temp_file)
# Test Ruff formatting with syntax errors (should handle gracefully)
Execute (Test Ruff formatting with syntax error):
" Clear buffer and set syntactically invalid content
%delete _
call setline(1, ['def test():', ' x = 1', ' return x', ' # Missing closing'])
" Give the buffer a filename
let temp_file = tempname() . '.py'
execute 'write ' . temp_file
execute 'edit ' . temp_file
" Store original content
let original_lines = getline(1, '$')
" Try to format (should handle syntax errors gracefully)
try
PymodeLintAuto
let formatted_lines = getline(1, '$')
" Ruff should return original content for syntax errors
" or format what it can
Assert 1, "Ruff handled syntax error gracefully"
catch
" If it fails, that's also acceptable for syntax errors
Assert 1, "Ruff correctly identified syntax error"
endtry
" Clean up temp file
call delete(temp_file)
# Test Ruff formatting with valid code
Execute (Test Ruff formatting valid code):
" Clear buffer and set badly formatted but valid code
%delete _
call setline(1, ['def test( ):', ' x=1+2', ' return x'])
" Give the buffer a filename
let temp_file = tempname() . '.py'
execute 'write ' . temp_file
execute 'edit ' . temp_file
" Run formatting
PymodeLintAuto
" Check that formatting was applied
let formatted_lines = getline(1, '$')
let formatted_text = join(formatted_lines, '\n')
" Verify Ruff formatted the code
if formatted_text =~# 'def test():' && formatted_text =~# 'x = 1 + 2'
Assert 1, "Ruff formatted valid code correctly"
else
Assert 0, "Ruff formatting failed: " . string(formatted_lines)
endif
" Clean up temp file
call delete(temp_file)
# Test Ruff with configuration file
Execute (Test Ruff with config file):
" Test that Ruff respects configuration file setting
" This is a basic test - actual config file testing would require file creation
Assert exists('g:pymode_ruff_config_file'), 'Config file option exists'
Assert 1, "Ruff config file option available"
# Test Ruff linting with ignore rules
Execute (Test Ruff linting ignore):
" Clear buffer and set content that would normally trigger warnings
%delete _
call setline(1, ['import os', 'import sys', '', 'def test():', ' unused_var = 1', ' return True'])
" Give the buffer a filename
let temp_file = tempname() . '.py'
execute 'write ' . temp_file
execute 'edit ' . temp_file
" Run linting
PymodeLint
" Verify linting completed (ignore rules would be applied if configured)
Assert 1, "Ruff linting with ignore rules completed"
" Clean up temp file
call delete(temp_file)
# Test Ruff formatting preserves code functionality
Execute (Test Ruff preserves functionality):
" Clear buffer and set functional code
%delete _
call setline(1, ['def calculate(x, y):', ' result = x * 2 + y', ' return result'])
" Give the buffer a filename
let temp_file = tempname() . '.py'
execute 'write ' . temp_file
execute 'edit ' . temp_file
" Store original structure
let original_text = join(getline(1, '$'), '\n')
" Run formatting
PymodeLintAuto
" Check that code structure is preserved
let formatted_lines = getline(1, '$')
let formatted_text = join(formatted_lines, '\n')
" Verify key elements are still present
if formatted_text =~# 'def calculate' && formatted_text =~# 'return result'
Assert 1, "Ruff preserved code functionality"
else
Assert 0, "Ruff changed code functionality: " . string(formatted_lines)
endif
" Clean up temp file
call delete(temp_file)
# Test Ruff with empty buffer
Execute (Test Ruff with empty buffer):
" Clear buffer completely
%delete _
" Give the buffer a filename
let temp_file = tempname() . '.py'
execute 'write ' . temp_file
execute 'edit ' . temp_file
" Try to format empty buffer
try
PymodeLintAuto
Assert 1, "Ruff handled empty buffer gracefully"
catch
" Empty buffer might cause issues, which is acceptable
Assert 1, "Ruff correctly handled empty buffer"
endtry
" Clean up temp file
call delete(temp_file)
# Test Ruff formatting with comments
Execute (Test Ruff formatting with comments):
" Clear buffer and set code with comments
%delete _
call setline(1, ['# This is a comment', 'def test():', ' # Another comment', ' return True'])
" Give the buffer a filename
let temp_file = tempname() . '.py'
execute 'write ' . temp_file
execute 'edit ' . temp_file
" Run formatting
PymodeLintAuto
" Verify comments are preserved
let formatted_lines = getline(1, '$')
let formatted_text = join(formatted_lines, '\n')
if formatted_text =~# '# This is a comment' && formatted_text =~# '# Another comment'
Assert 1, "Ruff preserved comments correctly"
else
Assert 0, "Ruff removed or changed comments: " . string(formatted_lines)
endif
" Clean up temp file
call delete(temp_file)
# Test Ruff configuration mode: local
Execute (Test Ruff config mode local):
" Test that 'local' mode uses only local config files
" Create a temporary directory with a ruff.toml file
" Use tempname() and append a directory suffix to ensure it's treated as a directory
let test_dir = tempname() . '_dir'
call mkdir(test_dir, 'p')
" Create a ruff.toml file in the test directory
let config_file = test_dir . '/ruff.toml'
call writefile(['line-length = 120', 'select = ["E", "F"]'], config_file)
" Create a test Python file in the test directory
let test_file = test_dir . '/test.py'
call writefile(['import os', 'x = 1', 'print(x)'], test_file)
" Set config mode to 'local'
let g:pymode_ruff_config_mode = 'local'
" Open the file
execute 'edit ' . test_file
" Run linting (should use local config)
" Wrap in try-catch to handle potential Ruff errors gracefully
try
PymodeLint
catch
" Ruff might not be available or might error - that's okay for this test
" We're mainly testing that the config mode variable is set correctly
endtry
" Verify that the config mode variable exists and is set correctly
Assert exists('g:pymode_ruff_config_mode'), 'g:pymode_ruff_config_mode should exist'
Assert g:pymode_ruff_config_mode ==# 'local', 'Config mode should be set to local'
Assert 1, "Ruff config mode 'local' test completed"
" Clean up - close buffer first, then delete files
bwipeout!
call delete(test_file)
call delete(config_file)
call delete(test_dir, 'd')
# Test Ruff configuration mode: local_override (with local config)
Execute (Test Ruff config mode local_override with local config):
" Test that 'local_override' mode uses local config when available
" Create a temporary directory with a ruff.toml file
" Use tempname() and append a directory suffix to ensure it's treated as a directory
let test_dir = tempname() . '_dir'
call mkdir(test_dir, 'p')
" Create a ruff.toml file in the test directory
let config_file = test_dir . '/ruff.toml'
call writefile(['line-length = 100', 'select = ["E"]'], config_file)
" Create a test Python file in the test directory
let test_file = test_dir . '/test.py'
call writefile(['import os', 'x = 1', 'print(x)'], test_file)
" Set config mode to 'local_override' (default)
let g:pymode_ruff_config_mode = 'local_override'
" Set some pymode settings that should be ignored when local config exists
let g:pymode_ruff_select = ['F', 'W']
" Open the file
execute 'edit ' . test_file
" Run linting (should use local config, not pymode settings)
" Wrap in try-catch to handle potential Ruff errors gracefully
try
PymodeLint
catch
" Ruff might not be available or might error - that's okay for this test
" We're mainly testing that the config mode variable is set correctly
endtry
" Verify that the config mode is set correctly
Assert g:pymode_ruff_config_mode ==# 'local_override', 'Config mode should be set to local_override'
Assert 1, "Ruff config mode 'local_override' with local config test completed"
" Clean up - close buffer first, then delete files
bwipeout!
call delete(test_file)
call delete(config_file)
call delete(test_dir, 'd')
# Test Ruff configuration mode: local_override (without local config)
Execute (Test Ruff config mode local_override without local config):
" Test that 'local_override' mode uses pymode settings when no local config exists
" Create a temporary directory without any config files
" Use tempname() and append a directory suffix to ensure it's treated as a directory
let test_dir = tempname() . '_dir'
call mkdir(test_dir, 'p')
" Create a test Python file in the test directory
let test_file = test_dir . '/test.py'
call writefile(['import os', 'x = 1', 'print(x)'], test_file)
" Set config mode to 'local_override' (default)
let g:pymode_ruff_config_mode = 'local_override'
" Set pymode settings that should be used as fallback
let g:pymode_ruff_select = ['E', 'F']
let g:pymode_ruff_ignore = ['E501']
" Open the file
execute 'edit ' . test_file
" Run linting (should use pymode settings as fallback)
" Wrap in try-catch to handle potential Ruff errors gracefully
try
PymodeLint
catch
" Ruff might not be available or might error - that's okay for this test
" We're mainly testing that the config mode variable is set correctly
endtry
" Verify that the config mode is set correctly
Assert g:pymode_ruff_config_mode ==# 'local_override', 'Config mode should be set to local_override'
Assert 1, "Ruff config mode 'local_override' without local config test completed"
" Clean up - close buffer first, then delete files
bwipeout!
call delete(test_file)
call delete(test_dir, 'd')
# Test Ruff configuration mode: global
Execute (Test Ruff config mode global):
" Test that 'global' mode ignores local config files
" Create a temporary directory with a ruff.toml file
" Use tempname() and append a directory suffix to ensure it's treated as a directory
let test_dir = tempname() . '_dir'
call mkdir(test_dir, 'p')
" Create a ruff.toml file in the test directory (should be ignored)
let config_file = test_dir . '/ruff.toml'
call writefile(['line-length = 200', 'select = ["D"]'], config_file)
" Create a test Python file in the test directory
let test_file = test_dir . '/test.py'
call writefile(['import os', 'x = 1', 'print(x)'], test_file)
" Set config mode to 'global'
let g:pymode_ruff_config_mode = 'global'
" Set pymode settings that should be used (local config should be ignored)
let g:pymode_ruff_select = ['E', 'F']
let g:pymode_ruff_ignore = ['E501']
" Open the file
execute 'edit ' . test_file
" Run linting (should use pymode settings, ignore local config)
" Wrap in try-catch to handle potential Ruff errors gracefully
try
PymodeLint
catch
" Ruff might not be available or might error - that's okay for this test
" We're mainly testing that the config mode variable is set correctly
endtry
" Verify that the config mode is set correctly
Assert g:pymode_ruff_config_mode ==# 'global', 'Config mode should be set to global'
Assert 1, "Ruff config mode 'global' test completed"
" Clean up - close buffer first, then delete files
bwipeout!
call delete(test_file)
call delete(config_file)
call delete(test_dir, 'd')
# Test Ruff configuration mode: default value
Execute (Test Ruff config mode default):
" Test that default config mode is 'local_override'
" Unset the variable to test default
unlet! g:pymode_ruff_config_mode
" Reload plugin to get default value
" Note: In actual usage, the default is set in plugin/pymode.vim
" For testing, we'll verify the variable can be set
let g:pymode_ruff_config_mode = 'local_override'
Assert g:pymode_ruff_config_mode ==# 'local_override', 'Default config mode should be local_override'
Assert 1, "Ruff config mode default value test completed"