Skip to content

Commit

Permalink
Add endLine and endColumn keys to JSONReporter (#5456)
Browse files Browse the repository at this point in the history
Co-authored-by: Daniël van Noord <[email protected]>
Co-authored-by: Pierre Sassoulas <[email protected]>
  • Loading branch information
3 people authored Dec 3, 2021
1 parent 28a33ef commit 765a0b7
Show file tree
Hide file tree
Showing 5 changed files with 76 additions and 18 deletions.
4 changes: 4 additions & 0 deletions ChangeLog
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,10 @@ Release date: TBA

Closes #5437

* Add ``endLine`` and ``endColumn`` keys to output of ``JSONReporter``.

Closes #5380

* Fixed handling of Google-style parameter specifications where descriptions
are on the line following the parameter name. These were generating
false positives for ``missing-param-doc``.
Expand Down
4 changes: 4 additions & 0 deletions doc/whatsnew/2.12.rst
Original file line number Diff line number Diff line change
Expand Up @@ -246,3 +246,7 @@ Other Changes
``pylint/testutil/`` are still unstable and might be modified in the near future.

Closes #4412 #5287

* Add ``endLine`` and ``endColumn`` keys to output of ``JSONReporter``.

Closes #5380
2 changes: 2 additions & 0 deletions pylint/reporters/json_reporter.py
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,8 @@ def display_messages(self, layout: Optional["Section"]) -> None:
"obj": msg.obj,
"line": msg.line,
"column": msg.column,
"endLine": msg.end_line,
"endColumn": msg.end_column,
"path": msg.path,
"symbol": msg.symbol,
"message": msg.msg or "",
Expand Down
80 changes: 62 additions & 18 deletions tests/unittest_reporters_json.py
Original file line number Diff line number Diff line change
Expand Up @@ -25,29 +25,67 @@
from pylint.reporters.ureports.nodes import EvaluationSection

expected_score_message = "Expected score message"
expected_result = [
[
("column", 0),
("line", 1),
("message", "Line too long (1/2)"),
("message-id", "C0301"),
("module", "0123"),
("obj", ""),
("path", "0123"),
("symbol", "line-too-long"),
("type", "convention"),
]
]


def test_simple_json_output_no_score() -> None:
report = get_linter_result(score=False)
"""Test JSON reporter with no score"""
message = {
"msg": "line-too-long",
"line": 1,
"args": (1, 2),
"end_line": None,
"end_column": None,
}
expected = [
{
"type": "convention",
"module": "0123",
"obj": "",
"line": 1,
"column": 0,
"endLine": None,
"endColumn": None,
"path": "0123",
"symbol": "line-too-long",
"message": "Line too long (1/2)",
"message-id": "C0301",
}
]
report = get_linter_result(score=False, message=message)
assert len(report) == 1
assert json.dumps(report) == json.dumps(expected)


def test_simple_json_output_no_score_with_end_line() -> None:
"""Test JSON reporter with no score with end_line and end_column"""
message = {
"msg": "line-too-long",
"line": 1,
"args": (1, 2),
"end_line": 1,
"end_column": 4,
}
expected = [
{
"type": "convention",
"module": "0123",
"obj": "",
"line": 1,
"column": 0,
"endLine": 1,
"endColumn": 4,
"path": "0123",
"symbol": "line-too-long",
"message": "Line too long (1/2)",
"message-id": "C0301",
}
]
report = get_linter_result(score=False, message=message)
assert len(report) == 1
report_result = [sorted(report[0].items(), key=lambda item: item[0])]
assert report_result == expected_result
assert json.dumps(report) == json.dumps(expected)


def get_linter_result(score: bool) -> List[Dict[str, Any]]:
def get_linter_result(score: bool, message: Dict[str, Any]) -> List[Dict[str, Any]]:
output = StringIO()
reporter = JSONReporter(output)
linter = PyLinter(reporter=reporter)
Expand All @@ -56,7 +94,13 @@ def get_linter_result(score: bool) -> List[Dict[str, Any]]:
linter.config.score = score
linter.open()
linter.set_current_module("0123")
linter.add_message("line-too-long", line=1, args=(1, 2))
linter.add_message(
message["msg"],
line=message["line"],
args=message["args"],
end_lineno=message["end_line"],
end_col_offset=message["end_column"],
)
# we call those methods because we didn't actually run the checkers
if score:
reporter.display_reports(EvaluationSection(expected_score_message))
Expand Down
4 changes: 4 additions & 0 deletions tests/unittest_reporting.py
Original file line number Diff line number Diff line change
Expand Up @@ -219,6 +219,8 @@ def test_multi_format_output(tmp_path):
' "obj": "",\n'
' "line": 1,\n'
' "column": 0,\n'
' "endLine": null,\n'
' "endColumn": null,\n'
f' "path": {escaped_source_file},\n'
' "symbol": "missing-module-docstring",\n'
' "message": "Missing module docstring",\n'
Expand All @@ -230,6 +232,8 @@ def test_multi_format_output(tmp_path):
' "obj": "",\n'
' "line": 1,\n'
' "column": 0,\n'
' "endLine": null,\n'
' "endColumn": null,\n'
f' "path": {escaped_source_file},\n'
' "symbol": "line-too-long",\n'
' "message": "Line too long (1/2)",\n'
Expand Down

0 comments on commit 765a0b7

Please sign in to comment.