Skip to content

Commit

Permalink
Make flake8 use stdin (#830)
Browse files Browse the repository at this point in the history
  • Loading branch information
qbedard authored Jul 2, 2020
1 parent 2faf957 commit 78dee3d
Show file tree
Hide file tree
Showing 2 changed files with 17 additions and 14 deletions.
17 changes: 8 additions & 9 deletions pyls/plugins/flake8_lint.py
Original file line number Diff line number Diff line change
Expand Up @@ -43,12 +43,12 @@ def pyls_lint(workspace, document):
# Call the flake8 utility then parse diagnostics from stdout
flake8_executable = settings.get('executable', 'flake8')

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


def run_flake8(flake8_executable, args):
def run_flake8(flake8_executable, args, document):
"""Run flake8 with the provided arguments, logs errors
from stderr if any.
"""
Expand All @@ -60,26 +60,25 @@ def run_flake8(flake8_executable, args):
try:
cmd = [flake8_executable]
cmd.extend(args)
p = Popen(cmd, stdout=PIPE, stderr=PIPE)
p = Popen(cmd, stdin=PIPE, stdout=PIPE, stderr=PIPE)
except IOError:
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)
(stdout, stderr) = p.communicate()
p = Popen(cmd, stdin=PIPE, stdout=PIPE, stderr=PIPE)
(stdout, stderr) = p.communicate(document.source.encode())
if stderr:
log.error("Error while running flake8 '%s'", stderr.decode())
return stdout.decode()


def build_args(options, doc_path):
def build_args(options):
"""Build arguments for calling flake8.
Args:
options: dictionary of argument names and their values.
doc_path: path of the document to lint.
"""
args = [doc_path]
args = ['-'] # use stdin
for arg_name, arg_val in options.items():
if arg_val is None:
continue
Expand Down
14 changes: 9 additions & 5 deletions test/plugins/test_flake8_lint.py
Original file line number Diff line number Diff line change
Expand Up @@ -28,13 +28,17 @@ def temp_document(doc_text, workspace):
return name, doc


def test_flake8_no_checked_file(workspace):
# A bad uri or a non-saved file may cause the flake8 linter to do nothing.
# In this situtation, the linter will return an empty list.

def test_flake8_unsaved(workspace):
doc = Document('', workspace, DOC)
diags = flake8_lint.pyls_lint(workspace, doc)
assert 'Error' in diags[0]['message']
msg = 'local variable \'a\' is assigned to but never used'
unused_var = [d for d in diags if d['message'] == msg][0]

assert unused_var['source'] == 'flake8'
assert unused_var['code'] == 'F841'
assert unused_var['range']['start'] == {'line': 5, 'character': 1}
assert unused_var['range']['end'] == {'line': 5, 'character': 11}
assert unused_var['severity'] == lsp.DiagnosticSeverity.Warning


def test_flake8_lint(workspace):
Expand Down

0 comments on commit 78dee3d

Please sign in to comment.