Bug 1569882 - Fix mozlint unit tests for relative path outputs, r=ahal

Differential Revision: https://phabricator.services.mozilla.com/D39877

--HG--
extra : moz-landing-system : lando
This commit is contained in:
Bastien Abadie 2019-07-30 23:30:15 +00:00
parent 723b502233
commit 74eca00f1f
6 changed files with 34 additions and 25 deletions

View File

@ -4,8 +4,6 @@
from __future__ import absolute_import, unicode_literals
import os
from ..result import Issue
@ -25,7 +23,7 @@ class UnixFormatter(object):
assert isinstance(err, Issue)
slots = {s: getattr(err, s) for s in err.__slots__}
slots["path"] = os.path.relpath(slots["path"])
slots["path"] = slots['relpath']
slots["column"] = "%d:" % slots["column"] if slots["column"] else ""
slots["rule"] = slots["rule"] or slots["linter"]

View File

@ -7,6 +7,7 @@ from __future__ import absolute_import
from collections import defaultdict
from json import dumps, JSONEncoder
import os
import mozpack.path as mozpath
class ResultSummary(object):
@ -19,7 +20,7 @@ class ResultSummary(object):
# Store the repository root folder to be able to build
# Issues relative paths to that folder
if ResultSummary.root is None:
ResultSummary.root = root
ResultSummary.root = mozpath.normpath(root)
def reset(self):
self.issues = defaultdict(list)
@ -117,11 +118,14 @@ class Issue(object):
root = ResultSummary.root
assert root is not None, 'Missing ResultSummary.root'
if os.path.isabs(path):
self.path = path
self.relpath = os.path.relpath(path, root)
self.path = mozpath.normpath(path)
if self.path.startswith(root):
self.relpath = mozpath.relpath(self.path, root)
else:
self.relpath = self.path
else:
self.path = os.path.join(root, path)
self.relpath = path
self.path = mozpath.join(root, path)
self.relpath = mozpath.normpath(path)
def __repr__(self):
s = dumps(self, cls=IssueEncoder, indent=2)

View File

@ -5,7 +5,6 @@
from __future__ import absolute_import, unicode_literals
import json
import os
import mozunit
import mozpack.path as mozpath
@ -15,19 +14,19 @@ from mozlint.result import Issue, ResultSummary
from mozlint import formatters
NORMALISED_PATHS = {
"abc": os.path.normpath("a/b/c.txt"),
"def": os.path.normpath("d/e/f.txt"),
"cwd": mozpath.normpath(os.getcwd()),
"abc": mozpath.normpath("a/b/c.txt"),
"def": mozpath.normpath("d/e/f.txt"),
"root": mozpath.abspath("/fake/root"),
}
EXPECTED = {
"compact": {
"kwargs": {},
"format": """
a/b/c.txt: line 1, Error - oh no foo (foo)
a/b/c.txt: line 4, col 10, Error - oh no baz (baz)
a/b/c.txt: line 5, Error - oh no foo-diff (foo-diff)
d/e/f.txt: line 4, col 2, Warning - oh no bar (bar-not-allowed)
/fake/root/a/b/c.txt: line 1, Error - oh no foo (foo)
/fake/root/a/b/c.txt: line 4, col 10, Error - oh no baz (baz)
/fake/root/a/b/c.txt: line 5, Error - oh no foo-diff (foo-diff)
/fake/root/d/e/f.txt: line 4, col 2, Warning - oh no bar (bar-not-allowed)
4 problems
""".strip(),
@ -35,7 +34,7 @@ d/e/f.txt: line 4, col 2, Warning - oh no bar (bar-not-allowed)
"stylish": {
"kwargs": {"disable_colors": True},
"format": """
a/b/c.txt
/fake/root/a/b/c.txt
1 error oh no foo (foo)
4:10 error oh no baz (baz)
5 error oh no foo-diff (foo-diff)
@ -43,7 +42,7 @@ a/b/c.txt
- hello
+ hello2
d/e/f.txt
/fake/root/d/e/f.txt
4:2 warning oh no bar bar-not-allowed (bar)
\u2716 4 problems (3 errors, 1 warning)
@ -52,10 +51,10 @@ d/e/f.txt
"treeherder": {
"kwargs": {},
"format": """
TEST-UNEXPECTED-ERROR | a/b/c.txt:1 | oh no foo (foo)
TEST-UNEXPECTED-ERROR | a/b/c.txt:4:10 | oh no baz (baz)
TEST-UNEXPECTED-ERROR | a/b/c.txt:5 | oh no foo-diff (foo-diff)
TEST-UNEXPECTED-WARNING | d/e/f.txt:4:2 | oh no bar (bar-not-allowed)
TEST-UNEXPECTED-ERROR | /fake/root/a/b/c.txt:1 | oh no foo (foo)
TEST-UNEXPECTED-ERROR | /fake/root/a/b/c.txt:4:10 | oh no baz (baz)
TEST-UNEXPECTED-ERROR | /fake/root/a/b/c.txt:5 | oh no foo-diff (foo-diff)
TEST-UNEXPECTED-WARNING | /fake/root/d/e/f.txt:4:2 | oh no bar (bar-not-allowed)
""".strip(),
},
"unix": {
@ -72,8 +71,8 @@ TEST-UNEXPECTED-WARNING | d/e/f.txt:4:2 | oh no bar (bar-not-allowed)
"summary": {
"kwargs": {},
"format": """
{cwd}/a: 3 errors
{cwd}/d: 0 errors, 1 warning
{root}/a: 3 errors
{root}/d: 0 errors, 1 warning
""".format(
**NORMALISED_PATHS
).strip(),
@ -83,6 +82,7 @@ TEST-UNEXPECTED-WARNING | d/e/f.txt:4:2 | oh no bar (bar-not-allowed)
@pytest.fixture
def result(scope="module"):
result = ResultSummary('/fake/root')
containers = (
Issue(linter="foo", path="a/b/c.txt", message="oh no foo", lineno=1),
Issue(

View File

@ -7,9 +7,12 @@ from __future__ import absolute_import
import mozunit
from mozlint.result import Issue
from mozlint.result import ResultSummary
def test_issue_defaults():
ResultSummary.root = '/fake/root'
issue = Issue('linter', 'path', 'message', None)
assert issue.lineno == 0
assert issue.column is None

View File

@ -8,6 +8,7 @@ import os
import mozunit
import pytest
import mozpack.path as mozpath
from mozlint.result import Issue, ResultSummary
@ -15,7 +16,7 @@ from mozlint.result import Issue, ResultSummary
@pytest.fixture
def path(filedir):
def _path(name):
return os.path.join(filedir, name)
return mozpath.join(filedir, name)
return _path

View File

@ -7,6 +7,7 @@ from collections import defaultdict
from mozbuild.base import MozbuildObject
from mozlint.pathutils import findobject
from mozlint.parser import Parser
from mozlint.result import ResultSummary
import pytest
@ -87,6 +88,8 @@ def lint(config, root):
except (ImportError, ValueError):
pytest.fail("could not resolve a lint function from '{}'".format(config['payload']))
ResultSummary.root = root
def wrapper(paths, config=config, root=root, collapse_results=False, **lintargs):
results = func(paths, config, root=root, **lintargs)
if not collapse_results: