File: test_commands.py

package info (click to toggle)
python-lsp-rope 0.1.17-1
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid, trixie
  • size: 340 kB
  • sloc: python: 2,319; makefile: 2
file content (68 lines) | stat: -rw-r--r-- 1,968 bytes parent folder | download | duplicates (2)
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
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
from unittest.mock import patch

from pylsp.lsp import MessageType

from pylsp_rope import commands, plugin
from pylsp_rope.plugin import pylsp_commands, pylsp_execute_command
from pylsp_rope.text import Position
from test.conftest import create_document
from test.helpers import assert_no_execute_command


def test_command_registration(config, workspace):
    commands = pylsp_commands(config, workspace)

    assert isinstance(commands, list)
    assert all(isinstance(cmd, str) for cmd in commands)
    assert all(cmd.startswith("pylsp_rope.") for cmd in commands)


def test_command_error_handling(caplog, config, workspace, document):
    """
    pylsp_execute_command should never raise an error when executeCommand().

    Instead, we'll show an error message to the user.
    """

    arguments = [
        {
            "document_uri": document.uri,
            "position": Position(1),
        },
    ]

    with patch(
        "pylsp_rope.refactoring.CommandRefactorInline.__call__",
        side_effect=Exception("some unexpected exception"),
    ):
        pylsp_execute_command(
            config,
            workspace,
            command=commands.COMMAND_REFACTOR_INLINE,
            arguments=arguments,
        )

    workspace._endpoint.notify.assert_called_once_with(
        "window/showMessage",
        params={
            "type": MessageType.Error,
            "message": "pylsp-rope: some unexpected exception",
        },
    )
    assert "Traceback (most recent call last):" in caplog.text


def test_command_nothing_to_modify(config, workspace, document, code_action_context):
    document = create_document(workspace, "simple.py")

    command = commands.COMMAND_SOURCE_ORGANIZE_IMPORT
    arguments = [{"document_uri": document.uri}]

    response = plugin.pylsp_execute_command(
        config=config,
        workspace=workspace,
        command=command,
        arguments=arguments,
    )

    assert_no_execute_command(workspace)