summaryrefslogtreecommitdiff
path: root/tests/unit/test_debug.py
blob: 2da4bf8975070a9254a5ee176ad52bd9d6b221b3 (plain)
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
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
"""Tests for our debugging module."""
from unittest import mock

import pytest

from flake8.main import debug
from flake8.options import manager


def test_dependencies():
    """Verify that we format our dependencies appropriately."""
    assert [] == debug.dependencies()


@pytest.mark.parametrize(
    "plugins, expected",
    [
        ([], []),
        (
            [manager.PluginVersion("pycodestyle", "2.0.0", False)],
            [
                {
                    "plugin": "pycodestyle",
                    "version": "2.0.0",
                    "is_local": False,
                }
            ],
        ),
        (
            [
                manager.PluginVersion("pycodestyle", "2.0.0", False),
                manager.PluginVersion("mccabe", "0.5.9", False),
            ],
            [
                {"plugin": "mccabe", "version": "0.5.9", "is_local": False},
                {
                    "plugin": "pycodestyle",
                    "version": "2.0.0",
                    "is_local": False,
                },
            ],
        ),
        (
            [
                manager.PluginVersion("pycodestyle", "2.0.0", False),
                manager.PluginVersion("my-local", "0.0.1", True),
                manager.PluginVersion("mccabe", "0.5.9", False),
            ],
            [
                {"plugin": "mccabe", "version": "0.5.9", "is_local": False},
                {"plugin": "my-local", "version": "0.0.1", "is_local": True},
                {
                    "plugin": "pycodestyle",
                    "version": "2.0.0",
                    "is_local": False,
                },
            ],
        ),
    ],
)
def test_plugins_from(plugins, expected):
    """Test that we format plugins appropriately."""
    option_manager = mock.Mock(registered_plugins=set(plugins))
    assert expected == debug.plugins_from(option_manager)


@mock.patch("platform.python_implementation", return_value="CPython")
@mock.patch("platform.python_version", return_value="3.5.3")
@mock.patch("platform.system", return_value="Linux")
def test_information(system, pyversion, pyimpl):
    """Verify that we return all the information we care about."""
    expected = {
        "version": "3.1.0",
        "plugins": [
            {"plugin": "mccabe", "version": "0.5.9", "is_local": False},
            {"plugin": "pycodestyle", "version": "2.0.0", "is_local": False},
        ],
        "dependencies": [],
        "platform": {
            "python_implementation": "CPython",
            "python_version": "3.5.3",
            "system": "Linux",
        },
    }
    option_manager = mock.Mock(
        registered_plugins={
            manager.PluginVersion("pycodestyle", "2.0.0", False),
            manager.PluginVersion("mccabe", "0.5.9", False),
        },
        version="3.1.0",
    )
    assert expected == debug.information(option_manager)
    pyimpl.assert_called_once_with()
    pyversion.assert_called_once_with()
    system.assert_called_once_with()


@mock.patch("flake8.main.debug.print")
@mock.patch("flake8.main.debug.information", return_value={})
@mock.patch("json.dumps", return_value="{}")
def test_print_information_no_plugins(dumps, information, print_mock):
    """Verify we print and exit only when we have plugins."""
    option_manager = mock.Mock(registered_plugins=set())
    action = debug.DebugAction(
        "--bug-report",
        dest="bug_report",
        option_manager=option_manager,
    )
    assert action(None, None, None, None) is None
    assert dumps.called is False
    assert information.called is False
    assert print_mock.called is False


@mock.patch("flake8.main.debug.print")
@mock.patch("flake8.main.debug.information", return_value={})
@mock.patch("json.dumps", return_value="{}")
def test_print_information(dumps, information, print_mock):
    """Verify we print and exit only when we have plugins."""
    plugins = [
        manager.PluginVersion("pycodestyle", "2.0.0", False),
        manager.PluginVersion("mccabe", "0.5.9", False),
    ]
    option_manager = mock.Mock(registered_plugins=set(plugins))
    action = debug.DebugAction(
        "--bug-report",
        dest="bug_report",
        option_manager=option_manager,
    )
    with pytest.raises(SystemExit):
        action(None, None, None, None)
    print_mock.assert_called_once_with("{}")
    dumps.assert_called_once_with({}, indent=2, sort_keys=True)
    information.assert_called_once_with(option_manager)