summaryrefslogtreecommitdiffstats
path: root/python/mozlint/test/test_formatters.py
blob: b9e6512b24e3763e238347846daeda596a7bcac3 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
# 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

import json
import sys
from collections import defaultdict

import pytest

from mozlint import ResultContainer
from mozlint import formatters


@pytest.fixture
def results(scope='module'):
    containers = (
        ResultContainer(
            linter='foo',
            path='a/b/c.txt',
            message="oh no foo",
            lineno=1,
        ),
        ResultContainer(
            linter='bar',
            path='d/e/f.txt',
            message="oh no bar",
            hint="try baz instead",
            level='warning',
            lineno=4,
            column=2,
            rule="bar-not-allowed",
        ),
        ResultContainer(
            linter='baz',
            path='a/b/c.txt',
            message="oh no baz",
            lineno=4,
            source="if baz:",
        ),
    )
    results = defaultdict(list)
    for c in containers:
        results[c.path].append(c)
    return results


def test_stylish_formatter(results):
    expected = """
a/b/c.txt
  1  error  oh no foo  (foo)
  4  error  oh no baz  (baz)

d/e/f.txt
  4:2  warning  oh no bar  bar-not-allowed (bar)

\u2716 3 problems (2 errors, 1 warning)
""".strip()

    fmt = formatters.get('stylish', disable_colors=True)
    assert expected == fmt(results)


def test_treeherder_formatter(results):
    expected = """
TEST-UNEXPECTED-ERROR | a/b/c.txt:1 | oh no foo (foo)
TEST-UNEXPECTED-ERROR | a/b/c.txt:4 | oh no baz (baz)
TEST-UNEXPECTED-WARNING | d/e/f.txt:4:2 | oh no bar (bar-not-allowed)
""".strip()

    fmt = formatters.get('treeherder')
    assert expected == fmt(results)


def test_json_formatter(results):
    fmt = formatters.get('json')
    formatted = json.loads(fmt(results))

    assert set(formatted.keys()) == set(results.keys())

    slots = ResultContainer.__slots__
    for errors in formatted.values():
        for err in errors:
            assert all(s in err for s in slots)


if __name__ == '__main__':
    sys.exit(pytest.main(['--verbose', __file__]))