Debian Codegraph @

python/mozlint/test/test_formatters.py

raw
1 # This Source Code Form is subject to the terms of the Mozilla Public
2 # License, v. 2.0. If a copy of the MPL was not distributed with this
3 # file, You can obtain one at http://mozilla.org/MPL/2.0/.
4
5 import json
6
7 import attr
8 import mozpack.path as mozpath
9 import mozunit
10 import pytest
11
12 from mozlint import formatters
13 from mozlint.result import Issue, ResultSummary
14
15 NORMALISED_PATHS = {
16 "abc": mozpath.normpath("a/b/c.txt"),
17 "def": mozpath.normpath("d/e/f.txt"),
18 "root": mozpath.abspath("/fake/root"),
19 }
20
21 EXPECTED = {
22 "compact": {
23 "kwargs": {},
24 "format": """
25 /fake/root/a/b/c.txt: line 1, Error - oh no foo (foo)
26 /fake/root/a/b/c.txt: line 4, col 10, Error - oh no baz (baz)
27 /fake/root/a/b/c.txt: line 5, Error - oh no foo-diff (foo-diff)
28 /fake/root/d/e/f.txt: line 4, col 2, Warning - oh no bar (bar-not-allowed)
29
30 4 problems
31 """.strip(),
32 },
33 "stylish": {
34 "kwargs": {"disable_colors": True},
35 "format": """
36 /fake/root/a/b/c.txt
37 1 error oh no foo (foo)
38 4:10 error oh no baz (baz)
39 5 error oh no foo-diff (foo-diff)
40 diff 1
41 - hello
42 + hello2
43
44 /fake/root/d/e/f.txt
45 4:2 warning oh no bar bar-not-allowed (bar)
46
47 \u2716 4 problems (3 errors, 1 warning, 0 fixed)
48 """.strip(),
49 },
50 "treeherder": {
51 "kwargs": {},
52 "format": """
53 TEST-UNEXPECTED-ERROR | /fake/root/a/b/c.txt:1 | oh no foo (foo)
54 TEST-UNEXPECTED-ERROR | /fake/root/a/b/c.txt:4:10 | oh no baz (baz)
55 TEST-UNEXPECTED-ERROR | /fake/root/a/b/c.txt:5 | oh no foo-diff (foo-diff)
56 TEST-UNEXPECTED-WARNING | /fake/root/d/e/f.txt:4:2 | oh no bar (bar-not-allowed)
57 """.strip(),
58 },
59 "unix": {
60 "kwargs": {},
61 "format": """
62 {abc}:1: foo error: oh no foo
63 {abc}:4:10: baz error: oh no baz
64 {abc}:5: foo-diff error: oh no foo-diff
65 {def}:4:2: bar-not-allowed warning: oh no bar
66 """.format(
67 **NORMALISED_PATHS
68 ).strip(),
69 },
70 "summary": {
71 "kwargs": {},
72 "format": """
73 {root}/a: 3 errors
74 {root}/d: 0 errors, 1 warning
75 """.format(
76 **NORMALISED_PATHS
77 ).strip(),
78 },
79 }
80
81
82 @pytest.fixture
83 def result(scope="module"):
84 result = ResultSummary("/fake/root")
85 containers = (
86 Issue(linter="foo", path="a/b/c.txt", message="oh no foo", lineno=1),
87 Issue(
88 linter="bar",
89 path="d/e/f.txt",
90 message="oh no bar",
91 hint="try baz instead",
92 level="warning",
93 lineno="4",
94 column="2",
95 rule="bar-not-allowed",
96 ),
97 Issue(
98 linter="baz",
99 path="a/b/c.txt",
100 message="oh no baz",
101 lineno=4,
102 column=10,
103 source="if baz:",
104 ),
105 Issue(
106 linter="foo-diff",
107 path="a/b/c.txt",
108 message="oh no foo-diff",
109 lineno=5,
110 source="if baz:",
111 diff="diff 1\n- hello\n+ hello2",
112 ),
113 )
114 result = ResultSummary("/fake/root")
115 for c in containers:
116 result.issues[c.path].append(c)
117 return result
118
119
120 @pytest.mark.parametrize("name", EXPECTED.keys())
121 def test_formatters(result, name):
122 opts = EXPECTED[name]
123 fmt = formatters.get(name, **opts["kwargs"])
124 # encoding to str bypasses a UnicodeEncodeError in pytest
125 assert fmt(result) == opts["format"]
126
127
128 def test_json_formatter(result):
129 fmt = formatters.get("json")
130 formatted = json.loads(fmt(result))
131
132 assert set(formatted.keys()) == set(result.issues.keys())
133
134 attrs = attr.fields(Issue)
135 for errors in formatted.values():
136 for err in errors:
137 assert all(a.name in err for a in attrs)
138
139
140 if __name__ == "__main__":
141 mozunit.main()