Skip to content

Commit

Permalink
Adding tests and cleaning bits up
Browse files Browse the repository at this point in the history
  • Loading branch information
crimsonknave committed Oct 27, 2023
1 parent 9e45bbc commit d84ed81
Show file tree
Hide file tree
Showing 4 changed files with 96 additions and 5 deletions.
5 changes: 5 additions & 0 deletions autoload/ale/fix/registry.vim
Original file line number Diff line number Diff line change
Expand Up @@ -611,6 +611,11 @@ let s:default_registry = {
\ 'suggested_filetypes': ['python'],
\ 'description': 'Fix python files with ruff.',
\ },
\ 'ruff_format': {
\ 'function': 'ale#fixers#ruff_format#Fix',
\ 'suggested_filetypes': ['python'],
\ 'description': 'Fix python files with the ruff formatter.',
\ },
\ 'pycln': {
\ 'function': 'ale#fixers#pycln#Fix',
\ 'suggested_filetypes': ['python'],
Expand Down
8 changes: 4 additions & 4 deletions autoload/ale/fixers/ruff_format.vim
Original file line number Diff line number Diff line change
Expand Up @@ -52,15 +52,15 @@ function! ale#fixers#ruff_format#Fix(buffer) abort

let l:options = ale#Var(a:buffer, 'python_ruff_format_options')

if !empty(l:options)
call add(l:cmd, l:options)
endif

" when --stdin-filename present, ruff will use it for proj root resolution
" https://github.com/charliermarsh/ruff/pull/1281
let l:fname = expand('#' . a:buffer . '...')
call add(l:cmd, 'format')

if !empty(l:options)
call add(l:cmd, l:options)
endif

call add(l:cmd, '--stdin-filename '.ale#Escape(ale#path#Simplify(l:fname)))

call add(l:cmd, '-')
Expand Down
2 changes: 1 addition & 1 deletion doc/ale-python.txt
Original file line number Diff line number Diff line change
Expand Up @@ -1323,7 +1323,7 @@ g:ale_python_ruff_auto_poetry *g:ale_python_ruff_auto_poetry*


===============================================================================
ruff-format *ale-python-fuff-format*
ruff-format *ale-python-ruff-format*

g:ale_python_ruff_format_change_directory
*g:ale_python_ruff_format_change_directory*
Expand Down
86 changes: 86 additions & 0 deletions test/fixers/test_ruff_format_fixer_callback.vader
Original file line number Diff line number Diff line change
@@ -0,0 +1,86 @@
Before:
call ale#assert#SetUpFixerTest('python', 'ruff_format')

let b:bin_dir = has('win32') ? 'Scripts' : 'bin'

After:
call ale#assert#TearDownFixerTest()

unlet! g:dir
unlet! b:bin_dir

Execute(The ruff callback should not change directory if the option is set to 0):
let g:ale_python_ruff_format_change_directory = 0

let file_path = g:dir . '/../test-files/python/with_virtualenv/subdir/foo/bar.py'

silent execute 'file ' . fnameescape(file_path)

let fname = ale#Escape(ale#path#Simplify(file_path))

AssertFixer
\ {
\ 'cwd': '%s:h',
\ 'command': ale#Escape(ale#path#Simplify(g:dir . '/../test-files/python/with_virtualenv/env/' . b:bin_dir . '/ruff')) . ' format --stdin-filename ' . fname . ' -',
\ }

Execute(The ruff callback should respect custom options):
let g:ale_python_ruff_format_options = '--ignore F401 -q'

let file_path = g:dir . '/../test-files/python/with_virtualenv/subdir/foo/bar.py'

silent execute 'file ' . fnameescape(file_path)

let fname = ale#Escape(ale#path#Simplify(file_path))

AssertFixer
\ {
\ 'cwd': ale#path#Simplify(g:dir . '/../test-files/python/with_virtualenv/subdir'),
\ 'command': ale#Escape(ale#path#Simplify(g:dir . '/../test-files/python/with_virtualenv/env/' . b:bin_dir . '/ruff'))
\ . ' format --ignore F401 -q --stdin-filename '. fname . ' -',
\ }

Execute(Pipenv is detected when python_ruff_format_auto_pipenv is set):
let g:ale_python_ruff_format_auto_pipenv = 1
let g:ale_python_ruff_format_change_directory = 0

let file_path = '../test-files/python/pipenv/whatever.py'

call ale#test#SetFilename(file_path)

let fname = ale#Escape(ale#path#Simplify(g:dir . '/'. file_path))

AssertFixer
\ {
\ 'cwd': '%s:h',
\ 'command': ale#Escape('pipenv') . ' run ruff format --stdin-filename ' . fname . ' -'
\ }

Execute(Poetry is detected when python_ruff_auto_poetry is set):
let g:ale_python_ruff_format_auto_poetry = 1
let g:ale_python_ruff_format_change_directory = 0

call ale#test#SetFilename('../test-files/python/poetry/whatever.py')

let fname = ale#Escape(ale#path#Simplify(g:dir .'/../test-files/python/poetry/whatever.py'))

AssertFixer
\ {
\ 'cwd': '%s:h',
\ 'command': ale#Escape('poetry') . ' run ruff format --stdin-filename ' . fname . ' -'
\ }

Execute(Poetry is detected when python_ruff_format_auto_poetry is set, and cwd respects change_directory option):
let g:ale_python_ruff_format_auto_poetry = 1
let g:ale_python_ruff_format_change_directory = 1

call ale#test#SetFilename('../test-files/python/poetry/whatever.py')

let fname = ale#Escape(ale#path#Simplify(g:dir .'/../test-files/python/poetry/whatever.py'))

AssertFixer
\ {
\ 'cwd': ale#path#Simplify(g:dir . '/../test-files/python/poetry'),
\ 'command': ale#Escape('poetry') . ' run ruff format --stdin-filename ' . fname . ' -'
\ }

0 comments on commit d84ed81

Please sign in to comment.