| 1 |
|
| 2 |
|
| 3 |
|
| 4 |
|
| 5 |
"""Test for the JSON reporter.""" |
| 6 |
|
| 7 |
from __future__ import annotations |
| 8 |
|
| 9 |
import json |
| 10 |
from io import StringIO |
| 11 |
from pathlib import Path |
| 12 |
from typing import Any |
| 13 |
|
| 14 |
import pytest |
| 15 |
|
| 16 |
from pylint import checkers |
| 17 |
from pylint.interfaces import HIGH, UNDEFINED |
| 18 |
from pylint.lint import PyLinter |
| 19 |
from pylint.message import Message |
| 20 |
from pylint.reporters.json_reporter import JSON2Reporter, JSONReporter |
| 21 |
from pylint.reporters.ureports.nodes import EvaluationSection |
| 22 |
from pylint.typing import MessageLocationTuple |
| 23 |
|
| 24 |
expected_score_message = "Expected score message" |
| 25 |
|
| 26 |
|
| 27 |
def test_simple_json_output_no_score() -> None: |
| 28 |
"""Test JSON reporter with no score.""" |
| 29 |
message = { |
| 30 |
"msg": "line-too-long", |
| 31 |
"line": 1, |
| 32 |
"args": (1, 2), |
| 33 |
"end_line": None, |
| 34 |
"end_column": None, |
| 35 |
} |
| 36 |
expected = [ |
| 37 |
{ |
| 38 |
"type": "convention", |
| 39 |
"module": "0123", |
| 40 |
"obj": "", |
| 41 |
"line": 1, |
| 42 |
"column": 0, |
| 43 |
"endLine": None, |
| 44 |
"endColumn": None, |
| 45 |
"path": "0123", |
| 46 |
"symbol": "line-too-long", |
| 47 |
"message": "Line too long (1/2)", |
| 48 |
"message-id": "C0301", |
| 49 |
} |
| 50 |
] |
| 51 |
report = get_linter_result(score=False, message=message) |
| 52 |
assert len(report) == 1 |
| 53 |
assert json.dumps(report) == json.dumps(expected) |
| 54 |
|
| 55 |
|
| 56 |
def test_simple_json_output_no_score_with_end_line() -> None: |
| 57 |
"""Test JSON reporter with no score with end_line and end_column.""" |
| 58 |
message = { |
| 59 |
"msg": "line-too-long", |
| 60 |
"line": 1, |
| 61 |
"args": (1, 2), |
| 62 |
"end_line": 1, |
| 63 |
"end_column": 4, |
| 64 |
} |
| 65 |
expected = [ |
| 66 |
{ |
| 67 |
"type": "convention", |
| 68 |
"module": "0123", |
| 69 |
"obj": "", |
| 70 |
"line": 1, |
| 71 |
"column": 0, |
| 72 |
"endLine": 1, |
| 73 |
"endColumn": 4, |
| 74 |
"path": "0123", |
| 75 |
"symbol": "line-too-long", |
| 76 |
"message": "Line too long (1/2)", |
| 77 |
"message-id": "C0301", |
| 78 |
} |
| 79 |
] |
| 80 |
report = get_linter_result(score=False, message=message) |
| 81 |
assert len(report) == 1 |
| 82 |
assert json.dumps(report) == json.dumps(expected) |
| 83 |
|
| 84 |
|
| 85 |
def get_linter_result(score: bool, message: dict[str, Any]) -> list[dict[str, Any]]: |
| 86 |
output = StringIO() |
| 87 |
reporter = JSONReporter(output) |
| 88 |
linter = PyLinter(reporter=reporter) |
| 89 |
checkers.initialize(linter) |
| 90 |
linter.config.persistent = 0 |
| 91 |
linter.config.score = score |
| 92 |
linter.open() |
| 93 |
linter.set_current_module("0123") |
| 94 |
linter.add_message( |
| 95 |
message["msg"], |
| 96 |
line=message["line"], |
| 97 |
args=message["args"], |
| 98 |
end_lineno=message["end_line"], |
| 99 |
end_col_offset=message["end_column"], |
| 100 |
) |
| 101 |
|
| 102 |
if score: |
| 103 |
reporter.display_reports(EvaluationSection(expected_score_message)) |
| 104 |
reporter.display_messages(None) |
| 105 |
report_result = json.loads(output.getvalue()) |
| 106 |
return report_result |
| 107 |
|
| 108 |
|
| 109 |
@pytest.mark.parametrize( |
| 110 |
"message", |
| 111 |
[ |
| 112 |
pytest.param( |
| 113 |
Message( |
| 114 |
msg_id="C0111", |
| 115 |
symbol="missing-docstring", |
| 116 |
location=MessageLocationTuple( |
| 117 |
|
| 118 |
|
| 119 |
abspath=__file__, |
| 120 |
path=__file__, |
| 121 |
module="unittest_json_reporter", |
| 122 |
obj="obj", |
| 123 |
line=1, |
| 124 |
column=3, |
| 125 |
end_line=3, |
| 126 |
end_column=5, |
| 127 |
), |
| 128 |
msg="This is the actual message", |
| 129 |
confidence=UNDEFINED, |
| 130 |
), |
| 131 |
id="everything-defined", |
| 132 |
) |
| 133 |
], |
| 134 |
) |
| 135 |
def test_serialize_deserialize(message: Message) -> None: |
| 136 |
json_message = JSONReporter.serialize(message) |
| 137 |
assert message == JSONReporter.deserialize(json_message) |
| 138 |
|
| 139 |
|
| 140 |
def test_simple_json2_output() -> None: |
| 141 |
"""Test JSON2 reporter.""" |
| 142 |
message = { |
| 143 |
"msg": "line-too-long", |
| 144 |
"line": 1, |
| 145 |
"args": (1, 2), |
| 146 |
"end_line": 1, |
| 147 |
"end_column": 4, |
| 148 |
} |
| 149 |
expected = { |
| 150 |
"messages": [ |
| 151 |
{ |
| 152 |
"type": "convention", |
| 153 |
"symbol": "line-too-long", |
| 154 |
"message": "Line too long (1/2)", |
| 155 |
"messageId": "C0301", |
| 156 |
"confidence": "HIGH", |
| 157 |
"module": "0123", |
| 158 |
"obj": "", |
| 159 |
"line": 1, |
| 160 |
"column": 0, |
| 161 |
"endLine": 1, |
| 162 |
"endColumn": 4, |
| 163 |
"path": "0123", |
| 164 |
"absolutePath": "0123", |
| 165 |
} |
| 166 |
], |
| 167 |
"statistics": { |
| 168 |
"messageTypeCount": { |
| 169 |
"fatal": 0, |
| 170 |
"error": 0, |
| 171 |
"warning": 0, |
| 172 |
"refactor": 0, |
| 173 |
"convention": 1, |
| 174 |
"info": 0, |
| 175 |
}, |
| 176 |
"modulesLinted": 1, |
| 177 |
"score": 5.0, |
| 178 |
}, |
| 179 |
} |
| 180 |
report = get_linter_result_for_v2(message=message) |
| 181 |
assert len(report) == 2 |
| 182 |
assert json.dumps(report) == json.dumps(expected) |
| 183 |
|
| 184 |
|
| 185 |
def get_linter_result_for_v2(message: dict[str, Any]) -> list[dict[str, Any]]: |
| 186 |
output = StringIO() |
| 187 |
reporter = JSON2Reporter(output) |
| 188 |
linter = PyLinter(reporter=reporter) |
| 189 |
checkers.initialize(linter) |
| 190 |
linter.config.persistent = 0 |
| 191 |
linter.open() |
| 192 |
linter.set_current_module("0123") |
| 193 |
linter.add_message( |
| 194 |
message["msg"], |
| 195 |
line=message["line"], |
| 196 |
args=message["args"], |
| 197 |
end_lineno=message["end_line"], |
| 198 |
end_col_offset=message["end_column"], |
| 199 |
confidence=HIGH, |
| 200 |
) |
| 201 |
linter.stats.statement = 2 |
| 202 |
reporter.display_messages(None) |
| 203 |
report_result = json.loads(output.getvalue()) |
| 204 |
return report_result |
| 205 |
|
| 206 |
|
| 207 |
@pytest.mark.parametrize( |
| 208 |
"message", |
| 209 |
[ |
| 210 |
pytest.param( |
| 211 |
Message( |
| 212 |
msg_id="C0111", |
| 213 |
symbol="missing-docstring", |
| 214 |
location=MessageLocationTuple( |
| 215 |
|
| 216 |
abspath=str(Path(__file__).parent), |
| 217 |
path=__file__, |
| 218 |
module="unittest_json_reporter", |
| 219 |
obj="obj", |
| 220 |
line=1, |
| 221 |
column=3, |
| 222 |
end_line=3, |
| 223 |
end_column=5, |
| 224 |
), |
| 225 |
msg="This is the actual message", |
| 226 |
confidence=HIGH, |
| 227 |
), |
| 228 |
id="everything-defined", |
| 229 |
), |
| 230 |
pytest.param( |
| 231 |
Message( |
| 232 |
msg_id="C0111", |
| 233 |
symbol="missing-docstring", |
| 234 |
location=MessageLocationTuple( |
| 235 |
|
| 236 |
abspath=str(Path(__file__).parent), |
| 237 |
path=__file__, |
| 238 |
module="unittest_json_reporter", |
| 239 |
obj="obj", |
| 240 |
line=1, |
| 241 |
column=3, |
| 242 |
end_line=None, |
| 243 |
end_column=None, |
| 244 |
), |
| 245 |
msg="This is the actual message", |
| 246 |
confidence=None, |
| 247 |
), |
| 248 |
id="not-everything-defined", |
| 249 |
), |
| 250 |
], |
| 251 |
) |
| 252 |
def test_serialize_deserialize_for_v2(message: Message) -> None: |
| 253 |
json_message = JSON2Reporter.serialize(message) |
| 254 |
assert message == JSON2Reporter.deserialize(json_message) |
| 255 |
|
| 256 |
|
| 257 |
def test_json2_result_with_broken_score() -> None: |
| 258 |
"""Test that the JSON2 reporter can handle broken score evaluations.""" |
| 259 |
output = StringIO() |
| 260 |
reporter = JSON2Reporter(output) |
| 261 |
linter = PyLinter(reporter=reporter) |
| 262 |
linter.config.evaluation = "1/0" |
| 263 |
reporter.display_messages(None) |
| 264 |
report_result = json.loads(output.getvalue()) |
| 265 |
assert "division by zero" in report_result["statistics"]["score"] |