|
| 1 | +" Comprehensive Ruff integration tests |
| 2 | +" Tests for Ruff linting and formatting functionality |
| 3 | + |
| 4 | +Before: |
| 5 | + source tests/vader/setup.vim |
| 6 | + call SetupPythonBuffer() |
| 7 | + |
| 8 | +After: |
| 9 | + source tests/vader/setup.vim |
| 10 | + call CleanupPythonBuffer() |
| 11 | + |
| 12 | +# Test Ruff configuration variables |
| 13 | +Execute (Test Ruff configuration variables): |
| 14 | + " Test that Ruff-specific configuration variables exist |
| 15 | + Assert exists('g:pymode_ruff_enabled'), 'g:pymode_ruff_enabled should exist' |
| 16 | + Assert exists('g:pymode_ruff_format_enabled'), 'g:pymode_ruff_format_enabled should exist' |
| 17 | + Assert exists('g:pymode_ruff_select'), 'g:pymode_ruff_select should exist' |
| 18 | + Assert exists('g:pymode_ruff_ignore'), 'g:pymode_ruff_ignore should exist' |
| 19 | + Assert exists('g:pymode_ruff_config_file'), 'g:pymode_ruff_config_file should exist' |
| 20 | + Assert 1, 'All Ruff configuration variables exist' |
| 21 | + |
| 22 | +# Test Ruff linting basic functionality |
| 23 | +Execute (Test Ruff linting basic): |
| 24 | + " Clear buffer and set content with linting issues |
| 25 | + %delete _ |
| 26 | + call setline(1, ['import os', 'x = 1', 'y = 2', 'print(x)']) |
| 27 | + |
| 28 | + " Give the buffer a filename |
| 29 | + let temp_file = tempname() . '.py' |
| 30 | + execute 'write ' . temp_file |
| 31 | + execute 'edit ' . temp_file |
| 32 | + |
| 33 | + " Run linting (should use Ruff) |
| 34 | + PymodeLint |
| 35 | + |
| 36 | + " Verify linting completed (no errors expected for this simple code) |
| 37 | + Assert 1, "Ruff linting completed successfully" |
| 38 | + |
| 39 | + " Clean up temp file |
| 40 | + call delete(temp_file) |
| 41 | + |
| 42 | +# Test Ruff formatting with syntax errors (should handle gracefully) |
| 43 | +Execute (Test Ruff formatting with syntax error): |
| 44 | + " Clear buffer and set syntactically invalid content |
| 45 | + %delete _ |
| 46 | + call setline(1, ['def test():', ' x = 1', ' return x', ' # Missing closing']) |
| 47 | + |
| 48 | + " Give the buffer a filename |
| 49 | + let temp_file = tempname() . '.py' |
| 50 | + execute 'write ' . temp_file |
| 51 | + execute 'edit ' . temp_file |
| 52 | + |
| 53 | + " Store original content |
| 54 | + let original_lines = getline(1, '$') |
| 55 | + |
| 56 | + " Try to format (should handle syntax errors gracefully) |
| 57 | + try |
| 58 | + PymodeLintAuto |
| 59 | + let formatted_lines = getline(1, '$') |
| 60 | + |
| 61 | + " Ruff should return original content for syntax errors |
| 62 | + " or format what it can |
| 63 | + Assert 1, "Ruff handled syntax error gracefully" |
| 64 | + catch |
| 65 | + " If it fails, that's also acceptable for syntax errors |
| 66 | + Assert 1, "Ruff correctly identified syntax error" |
| 67 | + endtry |
| 68 | + |
| 69 | + " Clean up temp file |
| 70 | + call delete(temp_file) |
| 71 | + |
| 72 | +# Test Ruff formatting with valid code |
| 73 | +Execute (Test Ruff formatting valid code): |
| 74 | + " Clear buffer and set badly formatted but valid code |
| 75 | + %delete _ |
| 76 | + call setline(1, ['def test( ):', ' x=1+2', ' return x']) |
| 77 | + |
| 78 | + " Give the buffer a filename |
| 79 | + let temp_file = tempname() . '.py' |
| 80 | + execute 'write ' . temp_file |
| 81 | + execute 'edit ' . temp_file |
| 82 | + |
| 83 | + " Run formatting |
| 84 | + PymodeLintAuto |
| 85 | + |
| 86 | + " Check that formatting was applied |
| 87 | + let formatted_lines = getline(1, '$') |
| 88 | + let formatted_text = join(formatted_lines, '\n') |
| 89 | + |
| 90 | + " Verify Ruff formatted the code |
| 91 | + if formatted_text =~# 'def test():' && formatted_text =~# 'x = 1 + 2' |
| 92 | + Assert 1, "Ruff formatted valid code correctly" |
| 93 | + else |
| 94 | + Assert 0, "Ruff formatting failed: " . string(formatted_lines) |
| 95 | + endif |
| 96 | + |
| 97 | + " Clean up temp file |
| 98 | + call delete(temp_file) |
| 99 | + |
| 100 | +# Test Ruff with configuration file |
| 101 | +Execute (Test Ruff with config file): |
| 102 | + " Test that Ruff respects configuration file setting |
| 103 | + " This is a basic test - actual config file testing would require file creation |
| 104 | + Assert exists('g:pymode_ruff_config_file'), 'Config file option exists' |
| 105 | + Assert 1, "Ruff config file option available" |
| 106 | + |
| 107 | +# Test Ruff linting with ignore rules |
| 108 | +Execute (Test Ruff linting ignore): |
| 109 | + " Clear buffer and set content that would normally trigger warnings |
| 110 | + %delete _ |
| 111 | + call setline(1, ['import os', 'import sys', '', 'def test():', ' unused_var = 1', ' return True']) |
| 112 | + |
| 113 | + " Give the buffer a filename |
| 114 | + let temp_file = tempname() . '.py' |
| 115 | + execute 'write ' . temp_file |
| 116 | + execute 'edit ' . temp_file |
| 117 | + |
| 118 | + " Run linting |
| 119 | + PymodeLint |
| 120 | + |
| 121 | + " Verify linting completed (ignore rules would be applied if configured) |
| 122 | + Assert 1, "Ruff linting with ignore rules completed" |
| 123 | + |
| 124 | + " Clean up temp file |
| 125 | + call delete(temp_file) |
| 126 | + |
| 127 | +# Test Ruff formatting preserves code functionality |
| 128 | +Execute (Test Ruff preserves functionality): |
| 129 | + " Clear buffer and set functional code |
| 130 | + %delete _ |
| 131 | + call setline(1, ['def calculate(x, y):', ' result = x * 2 + y', ' return result']) |
| 132 | + |
| 133 | + " Give the buffer a filename |
| 134 | + let temp_file = tempname() . '.py' |
| 135 | + execute 'write ' . temp_file |
| 136 | + execute 'edit ' . temp_file |
| 137 | + |
| 138 | + " Store original structure |
| 139 | + let original_text = join(getline(1, '$'), '\n') |
| 140 | + |
| 141 | + " Run formatting |
| 142 | + PymodeLintAuto |
| 143 | + |
| 144 | + " Check that code structure is preserved |
| 145 | + let formatted_lines = getline(1, '$') |
| 146 | + let formatted_text = join(formatted_lines, '\n') |
| 147 | + |
| 148 | + " Verify key elements are still present |
| 149 | + if formatted_text =~# 'def calculate' && formatted_text =~# 'return result' |
| 150 | + Assert 1, "Ruff preserved code functionality" |
| 151 | + else |
| 152 | + Assert 0, "Ruff changed code functionality: " . string(formatted_lines) |
| 153 | + endif |
| 154 | + |
| 155 | + " Clean up temp file |
| 156 | + call delete(temp_file) |
| 157 | + |
| 158 | +# Test Ruff with empty buffer |
| 159 | +Execute (Test Ruff with empty buffer): |
| 160 | + " Clear buffer completely |
| 161 | + %delete _ |
| 162 | + |
| 163 | + " Give the buffer a filename |
| 164 | + let temp_file = tempname() . '.py' |
| 165 | + execute 'write ' . temp_file |
| 166 | + execute 'edit ' . temp_file |
| 167 | + |
| 168 | + " Try to format empty buffer |
| 169 | + try |
| 170 | + PymodeLintAuto |
| 171 | + Assert 1, "Ruff handled empty buffer gracefully" |
| 172 | + catch |
| 173 | + " Empty buffer might cause issues, which is acceptable |
| 174 | + Assert 1, "Ruff correctly handled empty buffer" |
| 175 | + endtry |
| 176 | + |
| 177 | + " Clean up temp file |
| 178 | + call delete(temp_file) |
| 179 | + |
| 180 | +# Test Ruff formatting with comments |
| 181 | +Execute (Test Ruff formatting with comments): |
| 182 | + " Clear buffer and set code with comments |
| 183 | + %delete _ |
| 184 | + call setline(1, ['# This is a comment', 'def test():', ' # Another comment', ' return True']) |
| 185 | + |
| 186 | + " Give the buffer a filename |
| 187 | + let temp_file = tempname() . '.py' |
| 188 | + execute 'write ' . temp_file |
| 189 | + execute 'edit ' . temp_file |
| 190 | + |
| 191 | + " Run formatting |
| 192 | + PymodeLintAuto |
| 193 | + |
| 194 | + " Verify comments are preserved |
| 195 | + let formatted_lines = getline(1, '$') |
| 196 | + let formatted_text = join(formatted_lines, '\n') |
| 197 | + |
| 198 | + if formatted_text =~# '# This is a comment' && formatted_text =~# '# Another comment' |
| 199 | + Assert 1, "Ruff preserved comments correctly" |
| 200 | + else |
| 201 | + Assert 0, "Ruff removed or changed comments: " . string(formatted_lines) |
| 202 | + endif |
| 203 | + |
| 204 | + " Clean up temp file |
| 205 | + call delete(temp_file) |
| 206 | + |
0 commit comments