Bug 1273634 - [mozlint] Add a treeherder formatter, r=jgraham

This is a really simple and ugly formatter that is compatible with
treeherder's error highlighting mechanism. It is designed to be identical
to the current eslint output on treeherder:
https://dxr.mozilla.org/mozilla-central/rev/4d63dde701b47b8661ab7990f197b6b60e543839/tools/lint/eslint-formatter.js

Eventually eslint will also use this and we can remove that file. Once
bug 1276486 is fixed, we can make this look a little nicer. But for now
it gets the job done.

MozReview-Commit-ID: CwfWPcwWFxF

--HG--
extra : transplant_source : %F3PJ%CB%27%A5%82U%D2%CF%B3%9E%A7%9F%0F%A4%F4%E9%5D%BB
This commit is contained in:
Andrew Halberstadt 2016-05-28 23:38:30 -04:00
Родитель 94051d8d58
Коммит 031698ae90
5 изменённых файлов: 44 добавлений и 2 удалений

Просмотреть файл

@ -6,6 +6,7 @@ import json
from ..result import ResultEncoder
from .stylish import StylishFormatter
from .treeherder import TreeherderFormatter
class JSONFormatter(object):
@ -16,6 +17,7 @@ class JSONFormatter(object):
all_formatters = {
'json': JSONFormatter,
'stylish': StylishFormatter,
'treeherder': TreeherderFormatter,
}

Просмотреть файл

@ -0,0 +1,30 @@
# This Source Code Form is subject to the terms of the Mozilla Public
# License, v. 2.0. If a copy of the MPL was not distributed with this
# file, You can obtain one at http://mozilla.org/MPL/2.0/.
from __future__ import unicode_literals
from ..result import ResultContainer
class TreeherderFormatter(object):
"""Formatter for treeherder friendly output.
This formatter looks ugly, but prints output such that
treeherder is able to highlight the errors and warnings.
This is a stop-gap until bug 1276486 is fixed.
"""
fmt = "TEST-UNEXPECTED-{level} | {path}:{lineno}:{column} | {message} ({rule})"
def __call__(self, result):
message = []
for path, errors in sorted(result.iteritems()):
for err in errors:
assert isinstance(err, ResultContainer)
d = {s: getattr(err, s) for s in err.__slots__}
d['level'] = d['level'].upper()
d['rule'] = d['rule'] or d['linter']
message.append(self.fmt.format(**d))
return "\n".join(message)

Просмотреть файл

@ -42,7 +42,7 @@ def _run_linters(queue, paths, **lintargs):
func = supported_types[linter['type']]
res = func(paths, linter, **lintargs) or []
if isinstance(res, basestring):
if not isinstance(res, (list, tuple)):
continue
for r in res:

Просмотреть файл

@ -68,6 +68,16 @@ d/e/f.txt
fmt = formatters.get('stylish', disable_colors=True)
self.assertEqual(expected, fmt(self.results))
def test_treeherder_formatter(self):
expected = """
TEST-UNEXPECTED-ERROR | a/b/c.txt:1:1 | oh no foo (foo)
TEST-UNEXPECTED-ERROR | a/b/c.txt:4:1 | oh no baz (baz)
TEST-UNEXPECTED-WARNING | d/e/f.txt:4:2 | oh no bar (bar-not-allowed)
""".strip()
fmt = formatters.get('treeherder')
self.assertEqual(expected, fmt(self.results))
def test_json_formatter(self):
fmt = formatters.get('json')
formatted = json.loads(fmt(self.results))

Просмотреть файл

@ -74,7 +74,7 @@ def lint(files, **lintargs):
if not binary:
print(FLAKE8_NOT_FOUND)
return 1
return []
cmdargs = [
binary,