From 1a157b1cd5f08d652f93defbb10f5bcc1a20a739 Mon Sep 17 00:00:00 2001 From: w0rp Date: Sun, 7 May 2017 16:31:33 +0100 Subject: [PATCH] Support `python -m flake8` for users who are running flake8 that way --- ale_linters/python/flake8.vim | 12 +++++-- .../test_flake8_command_callback.vader | 32 +++++++++++++++++++ 2 files changed, 41 insertions(+), 3 deletions(-) diff --git a/ale_linters/python/flake8.vim b/ale_linters/python/flake8.vim index ab07456..4959583 100644 --- a/ale_linters/python/flake8.vim +++ b/ale_linters/python/flake8.vim @@ -14,8 +14,12 @@ let g:ale_python_flake8_use_global = get(g:, 'ale_python_flake8_use_global', 0) " executables, so we don't have to look up the version number constantly. let s:version_cache = {} +function! s:UsingModule(buffer) abort + return ale#Var(a:buffer, 'python_flake8_options') =~# ' *-m flake8' +endfunction + function! ale_linters#python#flake8#GetExecutable(buffer) abort - if !ale#Var(a:buffer, 'python_flake8_use_global') + if !s:UsingModule(a:buffer) && !ale#Var(a:buffer, 'python_flake8_use_global') let l:virtualenv = ale#python#FindVirtualenv(a:buffer) if !empty(l:virtualenv) @@ -44,8 +48,10 @@ function! ale_linters#python#flake8#VersionCheck(buffer) abort return '' endif - return fnameescape(ale_linters#python#flake8#GetExecutable(a:buffer)) - \ . ' --version' + let l:executable = fnameescape(ale_linters#python#flake8#GetExecutable(a:buffer)) + let l:module_string = s:UsingModule(a:buffer) ? ' -m flake8' : '' + + return l:executable . l:module_string . ' --version' endfunction " Get the flake8 version from the output, or the cache. diff --git a/test/command_callback/test_flake8_command_callback.vader b/test/command_callback/test_flake8_command_callback.vader index bf4dfaf..baec533 100644 --- a/test/command_callback/test_flake8_command_callback.vader +++ b/test/command_callback/test_flake8_command_callback.vader @@ -70,3 +70,35 @@ Execute(The flake8 callbacks should detect virtualenv directories): \ g:dir . '/python_paths/with_virtualenv/env/bin/flake8' \ . ' --stdin-display-name %s -', \ ale_linters#python#flake8#GetCommand(bufnr(''), ['3.0.0']) + +" Some users currently run flake8 this way, so we should support it. +Execute(Using `python -m flake8` should be supported for running flake8): + silent execute 'file ' . fnameescape(g:dir . '/python_paths/with_virtualenv/subdir/foo/bar.py') + + let g:ale_python_flake8_executable = 'python' + let g:ale_python_flake8_options = '-m flake8 --some-option' + + AssertEqual + \ 'python', + \ ale_linters#python#flake8#GetExecutable(bufnr('')) + AssertEqual + \ 'python -m flake8 --version', + \ ale_linters#python#flake8#VersionCheck(bufnr('')) + AssertEqual + \ 'python -m flake8 --some-option -', + \ ale_linters#python#flake8#GetCommand(bufnr(''), ['2.9.9']) + + call ale_linters#python#flake8#ClearVersionCache() + + " Leading spaces shouldn't matter + let g:ale_python_flake8_options = ' -m flake8 --some-option' + + AssertEqual + \ 'python', + \ ale_linters#python#flake8#GetExecutable(bufnr('')) + AssertEqual + \ 'python -m flake8 --version', + \ ale_linters#python#flake8#VersionCheck(bufnr('')) + AssertEqual + \ 'python -m flake8 --some-option -', + \ ale_linters#python#flake8#GetCommand(bufnr(''), ['2.9.9'])