Fix 3941 - add version check to isort fixer (#3942)

This commit is contained in:
Horacio Sanson 2021-10-16 14:02:58 +09:00 committed by GitHub
parent c7e3f1a0dd
commit 16898417e6
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 55 additions and 15 deletions

View File

@ -21,7 +21,7 @@ function! ale#fixers#isort#GetExecutable(buffer) abort
return ale#python#FindExecutable(a:buffer, 'python_isort', ['isort'])
endfunction
function! ale#fixers#isort#Fix(buffer) abort
function! ale#fixers#isort#GetCmd(buffer) abort
let l:executable = ale#fixers#isort#GetExecutable(a:buffer)
let l:cmd = [ale#Escape(l:executable)]
@ -29,7 +29,20 @@ function! ale#fixers#isort#Fix(buffer) abort
call extend(l:cmd, ['run', 'isort'])
endif
call add(l:cmd, '--filename %s')
return join(l:cmd, ' ')
endfunction
function! ale#fixers#isort#FixForVersion(buffer, version) abort
let l:executable = ale#fixers#isort#GetExecutable(a:buffer)
let l:cmd = [ale#Escape(l:executable)]
if l:executable =~? 'pipenv\|poetry$'
call extend(l:cmd, ['run', 'isort'])
endif
if ale#semver#GTE(a:version, [5, 7, 0])
call add(l:cmd, '--filename %s')
endif
let l:options = ale#Var(a:buffer, 'python_isort_options')
@ -41,6 +54,18 @@ function! ale#fixers#isort#Fix(buffer) abort
return {
\ 'cwd': '%s:h',
\ 'command': join(l:cmd, ' ')
\ 'command': join(l:cmd, ' '),
\}
endfunction
function! ale#fixers#isort#Fix(buffer) abort
let l:executable = ale#fixers#isort#GetExecutable(a:buffer)
let l:command = ale#fixers#isort#GetCmd(a:buffer) . ale#Pad('--version')
return ale#semver#RunWithVersionCheck(
\ a:buffer,
\ l:executable,
\ l:command,
\ function('ale#fixers#isort#FixForVersion'),
\)
endfunction

View File

@ -11,45 +11,60 @@ After:
Execute(The isort callback should return the correct default values):
silent execute 'file ' . fnameescape(g:dir . '/../test-files/python/with_virtualenv/subdir/foo/bar.py')
AssertEqual
" --filename option exists only after 5.7.0
GivenCommandOutput ['VERSION 5.7.0']
AssertFixer
\ {
\ 'cwd': '%s:h',
\ 'command': ale#Escape(ale#path#Simplify(g:dir . '/../test-files/python/with_virtualenv/env/' . b:bin_dir . '/isort')) . ' --filename %s' . ' -',
\ },
\ ale#fixers#isort#Fix(bufnr(''))
\ }
Execute(The isort callback should respect custom options):
let g:ale_python_isort_options = '--multi-line=3 --trailing-comma'
silent execute 'file ' . fnameescape(g:dir . '/../test-files/python/with_virtualenv/subdir/foo/bar.py')
AssertEqual
" --filename option exists only after 5.7.0
GivenCommandOutput ['VERSION 5.7.0']
AssertFixer
\ {
\ 'cwd': '%s:h',
\ 'command': ale#Escape(ale#path#Simplify(g:dir . '/../test-files/python/with_virtualenv/env/' . b:bin_dir . '/isort'))
\ . ' --filename %s' . ' --multi-line=3 --trailing-comma -',
\ },
\ ale#fixers#isort#Fix(bufnr(''))
\ }
Execute(Pipenv is detected when python_isort_auto_pipenv is set):
let g:ale_python_isort_auto_pipenv = 1
call ale#test#SetFilename('../test-files/python/pipenv/whatever.py')
AssertEqual
GivenCommandOutput ['VERSION 5.7.0']
AssertFixer
\ {
\ 'cwd': '%s:h',
\ 'command': ale#Escape('pipenv') . ' run isort' . ' --filename %s' . ' -'
\ },
\ ale#fixers#isort#Fix(bufnr(''))
\ }
Execute(Poetry is detected when python_isort_auto_poetry is set):
let g:ale_python_isort_auto_poetry = 1
call ale#test#SetFilename('../test-files/python/poetry/whatever.py')
AssertEqual
GivenCommandOutput ['VERSION 5.7.0']
AssertFixer
\ {
\ 'cwd': '%s:h',
\ 'command': ale#Escape('poetry') . ' run isort' . ' --filename %s' . ' -'
\ },
\ ale#fixers#isort#Fix(bufnr(''))
\ }
Execute(The isort callback should not use --filename for older versions):
silent execute 'file ' . fnameescape(g:dir . '/../test-files/python/with_virtualenv/subdir/foo/bar.py')
" --filename option exists only after 5.7.0
GivenCommandOutput ['VERSION 5.6.0']
AssertFixer
\ {
\ 'cwd': '%s:h',
\ 'command': ale#Escape(ale#path#Simplify(g:dir . '/../test-files/python/with_virtualenv/env/' . b:bin_dir . '/isort')) . ' -',
\ }