From d84ed818199708b03d87ed6acdc3c51bc494fdbe Mon Sep 17 00:00:00 2001 From: crimsonknave Date: Thu, 26 Oct 2023 22:16:04 -0400 Subject: [PATCH] Adding tests and cleaning bits up --- autoload/ale/fix/registry.vim | 5 ++ autoload/ale/fixers/ruff_format.vim | 8 +- doc/ale-python.txt | 2 +- .../test_ruff_format_fixer_callback.vader | 86 +++++++++++++++++++ 4 files changed, 96 insertions(+), 5 deletions(-) create mode 100644 test/fixers/test_ruff_format_fixer_callback.vader diff --git a/autoload/ale/fix/registry.vim b/autoload/ale/fix/registry.vim index 3c4ccbdec2..97346fb9e2 100644 --- a/autoload/ale/fix/registry.vim +++ b/autoload/ale/fix/registry.vim @@ -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'], diff --git a/autoload/ale/fixers/ruff_format.vim b/autoload/ale/fixers/ruff_format.vim index 2c2d1a1273..86858745d5 100644 --- a/autoload/ale/fixers/ruff_format.vim +++ b/autoload/ale/fixers/ruff_format.vim @@ -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, '-') diff --git a/doc/ale-python.txt b/doc/ale-python.txt index 41c0a5354c..ec118c5ad6 100644 --- a/doc/ale-python.txt +++ b/doc/ale-python.txt @@ -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* diff --git a/test/fixers/test_ruff_format_fixer_callback.vader b/test/fixers/test_ruff_format_fixer_callback.vader new file mode 100644 index 0000000000..3cf5fd5272 --- /dev/null +++ b/test/fixers/test_ruff_format_fixer_callback.vader @@ -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 . ' -' + \ } +