|
| 1 | +# Copyright 2017 Palantir Technologies, Inc. |
| 2 | +import os |
| 3 | +import shutil |
| 4 | +import tempfile |
| 5 | +from pyls.workspace import Workspace |
| 6 | +from pyls.providers.lint import PyCodeStyleLinter, PyflakesLinter |
| 7 | + |
| 8 | +DOC_URI = __file__ |
| 9 | +DOC = """import sys |
| 10 | +
|
| 11 | +def hello(): |
| 12 | +\tpass |
| 13 | +
|
| 14 | +import json |
| 15 | +""" |
| 16 | + |
| 17 | +DOC_SYNTAX_ERR = """def hello() |
| 18 | + pass |
| 19 | +""" |
| 20 | + |
| 21 | + |
| 22 | +def test_pycodestyle(workspace): |
| 23 | + workspace.put_document(DOC_URI, DOC) |
| 24 | + provider = PyCodeStyleLinter(workspace) |
| 25 | + |
| 26 | + diags = provider.run(DOC_URI) |
| 27 | + |
| 28 | + assert all([d['source'] == 'pycodestyle' for d in diags]) |
| 29 | + |
| 30 | + # One we're expecting is: |
| 31 | + msg = 'E402 module level import not at top of file' |
| 32 | + mod_import = filter(lambda d: d['message'] == msg, diags)[0] |
| 33 | + |
| 34 | + assert mod_import['code'] == 'E402' |
| 35 | + assert mod_import['range']['start'] == {'line': 5, 'character': 0} |
| 36 | + |
| 37 | + |
| 38 | +def test_pycodestyle_config(): |
| 39 | + """ Test that we load config files properly. |
| 40 | +
|
| 41 | + Config files are loaded in the following order: |
| 42 | + tox.ini pep8.cfg setup.cfg pycodestyle.cfg |
| 43 | +
|
| 44 | + Each overriding the values in the last. |
| 45 | +
|
| 46 | + These files are first looked for in the current document's |
| 47 | + directory and then each parent directory until any one is found |
| 48 | + terminating at the workspace root. |
| 49 | +
|
| 50 | + If any section called 'pycodestyle' exists that will be solely used |
| 51 | + and any config in a 'pep8' section will be ignored |
| 52 | + """ |
| 53 | + # Create a workspace in tmp |
| 54 | + tmp = tempfile.mkdtemp() |
| 55 | + workspace = Workspace(tmp) |
| 56 | + doc_uri = 'file://' + tmp + '/' + 'test.py' |
| 57 | + |
| 58 | + provider = PyCodeStyleLinter(workspace) |
| 59 | + workspace.put_document(doc_uri, DOC) |
| 60 | + |
| 61 | + # Make sure we get a warning for 'indentation contains tabs' |
| 62 | + diags = provider.run(doc_uri) |
| 63 | + assert len(filter(lambda d: d['code'] == 'W191', diags)) > 0 |
| 64 | + |
| 65 | + content = { |
| 66 | + 'setup.cfg': ('[pycodestyle]\nignore = W191', True), |
| 67 | + 'pep8.cfg': ('[pep8]\nignore = W191', True), |
| 68 | + 'tox.ini': ('', False) |
| 69 | + } |
| 70 | + |
| 71 | + for conf_file, (content, working) in content.items(): |
| 72 | + # Now we'll add config file to ignore it |
| 73 | + with open(os.path.join(tmp, conf_file), 'w+') as f: |
| 74 | + f.write(content) |
| 75 | + |
| 76 | + # And make sure we don't get any warnings |
| 77 | + diags = provider.run(doc_uri) |
| 78 | + assert len(filter(lambda d: d['code'] == 'W191', diags)) == 0 if working else 1 |
| 79 | + |
| 80 | + os.unlink(os.path.join(tmp, conf_file)) |
| 81 | + |
| 82 | + shutil.rmtree(tmp) |
| 83 | + |
| 84 | + |
| 85 | +def test_pyflakes(workspace): |
| 86 | + workspace.put_document(DOC_URI, DOC) |
| 87 | + provider = PyflakesLinter(workspace) |
| 88 | + |
| 89 | + diags = provider.run(DOC_URI) |
| 90 | + |
| 91 | + # One we're expecting is: |
| 92 | + msg = '\'sys\' imported but unused' |
| 93 | + unused_import = filter(lambda d: d['message'] == msg, diags)[0] |
| 94 | + |
| 95 | + assert unused_import['range']['start'] == {'line': 0, 'character': 0} |
| 96 | + |
| 97 | + |
| 98 | +def test_syntax_error_pyflakes(workspace): |
| 99 | + workspace.put_document(DOC_URI, DOC_SYNTAX_ERR) |
| 100 | + provider = PyflakesLinter(workspace) |
| 101 | + |
| 102 | + diag = provider.run(DOC_URI)[0] |
| 103 | + |
| 104 | + assert diag['message'] == 'invalid syntax' |
| 105 | + assert diag['range']['start'] == {'line': 0, 'character': 12} |
0 commit comments