diff --git a/pyls/plugins/flake8_lint.py b/pyls/plugins/flake8_lint.py index addb82cc..6189727b 100644 --- a/pyls/plugins/flake8_lint.py +++ b/pyls/plugins/flake8_lint.py @@ -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. """ @@ -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 diff --git a/test/plugins/test_flake8_lint.py b/test/plugins/test_flake8_lint.py index 778a58e0..acc770b2 100644 --- a/test/plugins/test_flake8_lint.py +++ b/test/plugins/test_flake8_lint.py @@ -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):