diff options
author | Claudiu Popa <pcmanticore@gmail.com> | 2015-01-04 00:57:25 +0200 |
---|---|---|
committer | Claudiu Popa <pcmanticore@gmail.com> | 2015-01-04 00:57:25 +0200 |
commit | 8459080e7459879c74bb7f14c1dc45dea15ae3dc (patch) | |
tree | bb617aec48700ca6cef9ff3e7bf73e735bc0e946 | |
parent | dcf7e19a00fdbdf00d3d3f6b27eb68c21365680d (diff) | |
parent | e0bea04d6990cc8a03a27f77793d6bf0e807b13f (diff) | |
download | pylint-8459080e7459879c74bb7f14c1dc45dea15ae3dc.tar.gz |
Merged in vtemian/pylint (pull request #198)
Added json reporter #ropython-sprints
-rw-r--r-- | reporters/json.py | 58 | ||||
-rw-r--r-- | test/test_self.py | 4 | ||||
-rw-r--r-- | test/unittest_reporters_json.py | 54 |
3 files changed, 115 insertions, 1 deletions
diff --git a/reporters/json.py b/reporters/json.py new file mode 100644 index 0000000..fd4e079 --- /dev/null +++ b/reporters/json.py @@ -0,0 +1,58 @@ +# 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 JSON.""" + + __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, + 'path': message.path, + 'symbol ': message.symbol, + 'message': escape(message.msg or ''), + }) + + 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/test_self.py b/test/test_self.py index 4b5ad29..aa23a2f 100644 --- a/test/test_self.py +++ b/test/test_self.py @@ -23,6 +23,7 @@ from pylint.lint import Run from pylint.reporters import BaseReporter from pylint.reporters.text import * from pylint.reporters.html import HTMLReporter +from pylint.reporters.json import JSONReporter HERE = abspath(dirname(__file__)) @@ -94,7 +95,8 @@ class RunTC(unittest.TestCase): reporters = [ TextReporter(six.StringIO()), HTMLReporter(six.StringIO()), - ColorizedTextReporter(six.StringIO()) + ColorizedTextReporter(six.StringIO()), + JSONReporter(six.StringIO()) ] self._runtest(['pylint/test/functional/arguments.py'], reporter=MultiReporter(reporters), code=1) 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() |