blob: c5748ef31ec511670fe6f73db192a23e8cfbfa63 [file] [log] [blame]
# Copyright (c) 2014 Vlad Temian <[email protected]>
# Copyright (c) 2015-2018, 2020 Claudiu Popa <[email protected]>
# Copyright (c) 2015 Ionel Cristian Maries <[email protected]>
# Copyright (c) 2016 Derek Gustafson <[email protected]>
# Copyright (c) 2017 guillaume2 <[email protected]>
# Copyright (c) 2018 Sushobhit <[email protected]>
# Copyright (c) 2019-2021 Pierre Sassoulas <[email protected]>
# Copyright (c) 2019 Ashley Whetter <[email protected]>
# Copyright (c) 2020 hippo91 <[email protected]>
# Copyright (c) 2021 Marc Mueller <[email protected]>
# Licensed under the GPL: https://www.gnu.org/licenses/old-licenses/gpl-2.0.html
# For details: https://github.com/PyCQA/pylint/blob/main/LICENSE
"""Test for the JSON reporter."""
import json
from io import StringIO
from typing import Any, Dict, List
from pylint import checkers
from pylint.lint import PyLinter
from pylint.reporters import JSONReporter
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)
assert len(report) == 1
report_result = [sorted(report[0].items(), key=lambda item: item[0])]
assert report_result == expected_result
def get_linter_result(score: bool) -> List[Dict[str, Any]]:
output = StringIO()
reporter = JSONReporter(output)
linter = PyLinter(reporter=reporter)
checkers.initialize(linter)
linter.config.persistent = 0
linter.config.score = score
linter.open()
linter.set_current_module("0123")
linter.add_message("line-too-long", line=1, args=(1, 2))
# we call those methods because we didn't actually run the checkers
if score:
reporter.display_reports(EvaluationSection(expected_score_message))
reporter.display_messages(None)
report_result = json.loads(output.getvalue())
return report_result