diff options
author | Vlad Temian <vladtemian@gmail.com> | 2014-11-04 22:16:20 +0200 |
---|---|---|
committer | Vlad Temian <vladtemian@gmail.com> | 2014-11-04 22:16:20 +0200 |
commit | 410300e57b2225ca4aab5c25d8b6b85cf728649b (patch) | |
tree | 230e2982a698bff467ea450db76e14a496e632ea | |
parent | dfb7f592f9f6539901942b28d8f8def0ed393e75 (diff) | |
download | pylint-410300e57b2225ca4aab5c25d8b6b85cf728649b.tar.gz |
Added json reporter
-rw-r--r-- | ChangeLog | 1 | ||||
-rw-r--r-- | reporters/json.py | 56 | ||||
-rw-r--r-- | test/unittest_reporters_json.py | 54 |
3 files changed, 111 insertions, 0 deletions
@@ -1671,3 +1671,4 @@ ChangeLog for Pylint 2003-05-19 -- 0.1 * initial release +* Add a simple json reporter. You can use it using `-f json` diff --git a/reporters/json.py b/reporters/json.py new file mode 100644 index 0000000..8a1b6ee --- /dev/null +++ b/reporters/json.py @@ -0,0 +1,56 @@ +# Copyright (c) 2003-2014 LOGILAB S.A. (Paris, FRANCE). +# This program is free software; you can redistribute it and/or modify it under +# the terms of the GNU General Public License as published by the Free Software +# Foundation; either version 2 of the License, or (at your option) any later +# version. +# +# This program is distributed in the hope that it will be useful, but WITHOUT +# ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS +# FOR A PARTICULAR PURPOSE. See the GNU General Public License for more details. +# +# You should have received a copy of the GNU General Public License along with +# this program; if not, write to the Free Software Foundation, Inc., +# 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA. +"""JSON reporter""" +from __future__ import absolute_import, print_function + +import json +import sys +from cgi import escape + +from pylint.interfaces import IReporter +from pylint.reporters import BaseReporter + + +class JSONReporter(BaseReporter): + """Report messages and layouts in HTML.""" + + __implements__ = IReporter + name = 'json' + extension = 'json' + + def __init__(self, output=sys.stdout): + BaseReporter.__init__(self, output) + self.messages = [] + + def handle_message(self, message): + """Manage message of different type and in the context of path.""" + + self.messages.append({ + 'type': message.category, + 'module': message.module, + 'obj': message.obj, + 'line': message.line, + 'column': message.column, + 'message': escape(message.msg), + }) + + def _display(self, layout): + """Launch layouts display""" + if self.messages: + print(json.dumps(self.messages, indent=4), file=self.out) + + +def register(linter): + """Register the reporter classes with the linter.""" + linter.register_reporter(JSONReporter) diff --git a/test/unittest_reporters_json.py b/test/unittest_reporters_json.py new file mode 100644 index 0000000..ed0172c --- /dev/null +++ b/test/unittest_reporters_json.py @@ -0,0 +1,54 @@ +# Copyright (c) 2003-2014 LOGILAB S.A. (Paris, FRANCE). +# This program is free software; you can redistribute it and/or modify it under +# the terms of the GNU General Public License as published by the Free Software +# Foundation; either version 2 of the License, or (at your option) any later +# version. +# +# This program is distributed in the hope that it will be useful, but WITHOUT +# ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS +# FOR A PARTICULAR PURPOSE. See the GNU General Public License for more details. +# +# You should have received a copy of the GNU General Public License along with +# this program; if not, write to the Free Software Foundation, Inc., +# 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA. +import json +import six +import unittest + + +from pylint.lint import PyLinter +from pylint import checkers +from pylint.reporters.json import JSONReporter + + +class TestJSONReporter(unittest.TestCase): + + def test_simple_json_output(self): + output = six.StringIO() + + reporter = JSONReporter() + linter = PyLinter(reporter=reporter) + checkers.initialize(linter) + + linter.config.persistent = 0 + linter.reporter.set_output(output) + linter.open() + linter.set_current_module('0123') + linter.add_message('line-too-long', line=1, args=(1, 2)) + + # we call this method because we didn't actually run the checkers + reporter.display_results(None) + asserted_result = [{ + "obj": "", + "column": 0, + "module": "0123", + "message": "Line too long (1/2)", + "line": 1, + "type": "convention", + }] + report_result = json.loads(output.getvalue()) + self.assertEqual(report_result, asserted_result) + + +if __name__ == '__main__': + unittest.main() |