From 1462de6685d7d71b7adba9f1b5e4f568b40da6b1 Mon Sep 17 00:00:00 2001 From: Konstantin Alekseev Date: Tue, 22 Oct 2019 00:38:54 +0300 Subject: Run flake8 from project root by default. Option `per-file-ignores` was introduced in flake8 version 3.7.0. It allows to ignore specific errors in specific files using glob syntax. For example `per-file-ignores = src/generated/*.py:F401` will ignore `F401` error in all python files in `src/generated`. Thus ale has to run flake8 from project root where .flake8 config is placed otherwise glob won't match linted file. --- ale_linters/python/flake8.vim | 32 +++++++++++++++++++--- doc/ale-python.txt | 13 +++++---- .../test_flake8_command_callback.vader | 23 ++++++++++++++-- 3 files changed, 56 insertions(+), 12 deletions(-) diff --git a/ale_linters/python/flake8.vim b/ale_linters/python/flake8.vim index e2e7b743..85216ae8 100644 --- a/ale_linters/python/flake8.vim +++ b/ale_linters/python/flake8.vim @@ -4,7 +4,7 @@ call ale#Set('python_flake8_executable', 'flake8') call ale#Set('python_flake8_options', '') call ale#Set('python_flake8_use_global', get(g:, 'ale_use_global_executables', 0)) -call ale#Set('python_flake8_change_directory', 1) +call ale#Set('python_flake8_change_directory', 'project') call ale#Set('python_flake8_auto_pipenv', 0) function! s:UsingModule(buffer) abort @@ -38,10 +38,34 @@ function! ale_linters#python#flake8#RunWithVersionCheck(buffer) abort \) endfunction +function! ale_linters#python#flake8#GetCdString(buffer) abort + let l:change_directory = ale#Var(a:buffer, 'python_flake8_change_directory') + + " map legacy options to new ones + if l:change_directory is# 1 + let l:change_directory = 'file' + elseif l:change_directory is# 0 + let l:change_directory = 'off' + endif + + if l:change_directory is# 'file' + return ale#path#BufferCdString(a:buffer) + elseif l:change_directory is# 'off' + return '' + endif + + let l:project_root = ale#python#FindProjectRootIni(a:buffer) + + if !empty(l:project_root) + return ale#path#CdString(l:project_root) + endif + + return ale#path#BufferCdString(a:buffer) +endfunction + function! ale_linters#python#flake8#GetCommand(buffer, version) abort - let l:cd_string = ale#Var(a:buffer, 'python_flake8_change_directory') - \ ? ale#path#BufferCdString(a:buffer) - \ : '' + let l:cd_string = ale_linters#python#flake8#GetCdString(a:buffer) + let l:executable = ale_linters#python#flake8#GetExecutable(a:buffer) let l:exec_args = l:executable =~? 'pipenv$' diff --git a/doc/ale-python.txt b/doc/ale-python.txt index 60b0771d..6b1a6d33 100644 --- a/doc/ale-python.txt +++ b/doc/ale-python.txt @@ -169,13 +169,14 @@ flake8 *ale-python-flake8* g:ale_python_flake8_change_directory *g:ale_python_flake8_change_directory* *b:ale_python_flake8_change_directory* - Type: |Number| - Default: `1` + Type: |String| + Default: `project` - If set to `1`, ALE will switch to the directory the Python file being - checked with `flake8` is in before checking it. This helps `flake8` find - configuration files more easily. This option can be turned off if you want - to control the directory Python is executed from yourself. + If set to `project`, ALE will switch to the project root before checking file. + If set to `file`, ALE will switch to directory the Python file being + checked with `flake8` is in before checking it. + You can turn it off with `off` option if you want to control the directory + Python is executed from yourself. g:ale_python_flake8_executable *g:ale_python_flake8_executable* diff --git a/test/command_callback/test_flake8_command_callback.vader b/test/command_callback/test_flake8_command_callback.vader index f082a63a..0f370983 100644 --- a/test/command_callback/test_flake8_command_callback.vader +++ b/test/command_callback/test_flake8_command_callback.vader @@ -34,13 +34,32 @@ Execute(The flake8 callbacks should return the correct default values): \] Execute(The option for disabling changing directories should work): - let g:ale_python_flake8_change_directory = 0 + let g:ale_python_flake8_change_directory = 'off' AssertLinter 'flake8', [ \ ale#Escape('flake8') . ' --version', \ ale#Escape('flake8') . ' --format=default --stdin-display-name %s -', \] +Execute(The option for changing directory to project root should work): + silent execute 'file ' . fnameescape(g:dir . '/python_paths/namespace_package_tox/namespace/foo/bar.py') + + AssertLinter 'flake8', [ + \ ale#Escape('flake8') . ' --version', + \ ale#path#CdString(ale#python#FindProjectRootIni(bufnr(''))) + \ . ale#Escape('flake8') . ' --format=default --stdin-display-name %s -', + \] + +Execute(The option for changing directory to file dir should work): + let g:ale_python_flake8_change_directory = 'file' + silent execute 'file ' . fnameescape(g:dir . '/python_paths/namespace_package_tox/namespace/foo/bar.py') + + AssertLinter 'flake8', [ + \ ale#Escape('flake8') . ' --version', + \ ale#path#BufferCdString(bufnr('')) + \ . ale#Escape('flake8') . ' --format=default --stdin-display-name %s -', + \] + Execute(The flake8 command callback should let you set options): let g:ale_python_flake8_options = '--some-option' @@ -163,5 +182,5 @@ Execute(Pipenv is detected when python_flake8_auto_pipenv is set): call ale#test#SetFilename('../python_fixtures/pipenv/whatever.py') AssertLinter 'pipenv', - \ ale#path#BufferCdString(bufnr('')) + \ ale#path#CdString(ale#python#FindProjectRootIni(bufnr(''))) \ . ale#Escape('pipenv') . ' run flake8 --format=default --stdin-display-name %s -' -- cgit v1.2.3