File: test_mccabe_lint.py

package info (click to toggle)
python-lsp-server 1.12.0-3
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid
  • size: 796 kB
  • sloc: python: 7,791; sh: 12; makefile: 4
file content (39 lines) | stat: -rw-r--r-- 1,269 bytes parent folder | download
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
# Copyright 2017-2020 Palantir Technologies, Inc.
# Copyright 2021- Python Language Server Contributors.

from pylsp import lsp, uris
from pylsp.plugins import mccabe_lint
from pylsp.workspace import Document

DOC_URI = uris.from_fs_path(__file__)
DOC = """def hello():
\tpass
"""

DOC_SYNTAX_ERR = """def hello()
\tpass"""


def test_mccabe(config, workspace) -> None:
    old_settings = config.settings
    try:
        config.update({"plugins": {"mccabe": {"threshold": 1}}})
        doc = Document(DOC_URI, workspace, DOC)
        diags = mccabe_lint.pylsp_lint(config, workspace, doc)

        assert all(d["source"] == "mccabe" for d in diags)

        # One we're expecting is:
        msg = "Cyclomatic complexity too high: 1 (threshold 1)"
        mod_import = [d for d in diags if d["message"] == msg][0]

        assert mod_import["severity"] == lsp.DiagnosticSeverity.Warning
        assert mod_import["range"]["start"] == {"line": 0, "character": 0}
        assert mod_import["range"]["end"] == {"line": 0, "character": 6}
    finally:
        config._settings = old_settings


def test_mccabe_syntax_error(config, workspace) -> None:
    doc = Document(DOC_URI, workspace, DOC_SYNTAX_ERR)
    assert mccabe_lint.pylsp_lint(config, workspace, doc) is None