Skip to content

Commit

Permalink
Add configurable flake8 executable (#821)
Browse files Browse the repository at this point in the history
  • Loading branch information
ruhulio committed Jun 30, 2020
1 parent 4646633 commit 5fa7ae9
Show file tree
Hide file tree
Showing 2 changed files with 23 additions and 5 deletions.
13 changes: 8 additions & 5 deletions pyls/plugins/flake8_lint.py
Expand Up @@ -41,25 +41,28 @@ def pyls_lint(workspace, document):
log.debug("using flake8 with config: %s", opts['config'])

# Call the flake8 utility then parse diagnostics from stdout
flake8_executable = settings.get('executable', 'flake8')

args = build_args(opts, document.path)
output = run_flake8(args)
output = run_flake8(flake8_executable, args)
return parse_stdout(document, output)


def run_flake8(args):
def run_flake8(flake8_executable, args):
"""Run flake8 with the provided arguments, logs errors
from stderr if any.
"""
# a quick temporary fix to deal with Atom
args = [(i if not i.startswith('--ignore=') else FIX_IGNORES_RE.sub('', i))
for i in args if i is not None]
log.debug("Calling flake8 with args: '%s'", args)

log.debug("Calling %s with args: '%s'", flake8_executable, args)
try:
cmd = ['flake8']
cmd = [flake8_executable]
cmd.extend(args)
p = Popen(cmd, stdout=PIPE, stderr=PIPE)
except IOError:
log.debug("Can't execute flake8. Trying with 'python -m flake8'")
log.debug("Can't execute %s. Trying with 'python -m flake8'", flake8_executable)
cmd = ['python', '-m', 'flake8']
cmd.extend(args)
p = Popen(cmd, stdout=PIPE, stderr=PIPE)
Expand Down
15 changes: 15 additions & 0 deletions test/plugins/test_flake8_lint.py
Expand Up @@ -65,3 +65,18 @@ def test_flake8_config_param(workspace):
call_args = popen_mock.call_args.args[0]
assert 'flake8' in call_args
assert '--config={}'.format(flake8_conf) in call_args


def test_flake8_executable_param(workspace):
with patch('pyls.plugins.flake8_lint.Popen') as popen_mock:
mock_instance = popen_mock.return_value
mock_instance.communicate.return_value = [bytes(), bytes()]

flake8_executable = '/tmp/flake8'
workspace._config.update({'plugins': {'flake8': {'executable': flake8_executable}}})

_name, doc = temp_document(DOC, workspace)
flake8_lint.pyls_lint(workspace, doc)

call_args = popen_mock.call_args.args[0]
assert flake8_executable in call_args

0 comments on commit 5fa7ae9

Please sign in to comment.