зеркало из https://github.com/mozilla/gecko-dev.git
Bug 1611084 - Add unit tests for perfdocs linter. r=perftest-reviewers,AlexandruIonescu
This patch sets up unit tests for the perfdocs linter and adds a few simple tests for it which will be expanded on in future patches. Differential Revision: https://phabricator.services.mozilla.com/D75480
This commit is contained in:
Родитель
5aad6ff258
Коммит
5f40a745a8
|
@ -9,6 +9,7 @@ from mozbuild.base import MozbuildObject
|
|||
from mozlint.pathutils import findobject
|
||||
from mozlint.parser import Parser
|
||||
from mozlint.result import ResultSummary
|
||||
from mozlog.structuredlog import StructuredLogger
|
||||
from mozpack import path
|
||||
|
||||
import pytest
|
||||
|
@ -134,6 +135,50 @@ def lint(config, root):
|
|||
return wrapper
|
||||
|
||||
|
||||
@pytest.fixture
|
||||
def structuredlog_lint(config, root, logger=None):
|
||||
"""Find and return the 'lint' function for the external linter named in the
|
||||
LINTER global variable. This variant of the lint function is for linters that
|
||||
use the 'structuredlog' type.
|
||||
|
||||
This will automatically pass in the 'config' and 'root' arguments if not
|
||||
specified.
|
||||
"""
|
||||
try:
|
||||
func = findobject(config["payload"])
|
||||
except (ImportError, ValueError):
|
||||
pytest.fail(
|
||||
"could not resolve a lint function from '{}'".format(config["payload"])
|
||||
)
|
||||
|
||||
ResultSummary.root = root
|
||||
|
||||
if not logger:
|
||||
logger = structured_logger()
|
||||
|
||||
def wrapper(
|
||||
paths,
|
||||
config=config,
|
||||
root=root,
|
||||
logger=logger,
|
||||
collapse_results=False,
|
||||
**lintargs,
|
||||
):
|
||||
lintargs["log"] = logging.LoggerAdapter(
|
||||
logger, {"lintname": config.get("name"), "pid": os.getpid()}
|
||||
)
|
||||
results = func(paths, config, root=root, logger=logger, **lintargs)
|
||||
if not collapse_results:
|
||||
return results
|
||||
|
||||
ret = defaultdict(list)
|
||||
for r in results:
|
||||
ret[r.path].append(r)
|
||||
return ret
|
||||
|
||||
return wrapper
|
||||
|
||||
|
||||
@pytest.fixture
|
||||
def create_temp_file(tmpdir):
|
||||
def inner(contents, name=None):
|
||||
|
@ -143,3 +188,8 @@ def create_temp_file(tmpdir):
|
|||
return path.strpath
|
||||
|
||||
return inner
|
||||
|
||||
|
||||
@pytest.fixture
|
||||
def structured_logger():
|
||||
return StructuredLogger("logger")
|
||||
|
|
|
@ -27,6 +27,7 @@ skip-if = os == "win" || os == "mac" # only installed on Linux
|
|||
skip-if = os == "win" || os == "mac" # only installed on Linux
|
||||
[test_clang_format.py]
|
||||
skip-if = os == "win" || os == "mac" # only installed on Linux
|
||||
[test_perfdocs.py]
|
||||
[test_pylint.py]
|
||||
skip-if = os == "win" || os == "mac" # only installed on linux
|
||||
requirements = tools/lint/python/pylint_requirements.txt
|
||||
|
|
|
@ -0,0 +1,86 @@
|
|||
import contextlib
|
||||
import mock
|
||||
import os
|
||||
import pytest
|
||||
import shutil
|
||||
import tempfile
|
||||
|
||||
import mozunit
|
||||
|
||||
LINTER = "perfdocs"
|
||||
|
||||
|
||||
class PerfDocsLoggerMock:
|
||||
LOGGER = None
|
||||
PATHS = []
|
||||
FAILED = True
|
||||
|
||||
|
||||
@contextlib.contextmanager
|
||||
def temp_file(name="temp", content=None):
|
||||
tempdir = tempfile.mkdtemp()
|
||||
path = os.path.join(tempdir, name)
|
||||
if content is not None:
|
||||
with open(path, "w") as f:
|
||||
f.write(content)
|
||||
try:
|
||||
yield path
|
||||
finally:
|
||||
shutil.rmtree(tempdir)
|
||||
|
||||
|
||||
@mock.patch("perfdocs.generator.Generator")
|
||||
@mock.patch("perfdocs.verifier.Verifier")
|
||||
@mock.patch("perfdocs.logger.PerfDocLogger", new=PerfDocsLoggerMock)
|
||||
def test_perfdocs_start_and_fail(generator, verifier, structured_logger, config, paths):
|
||||
from perfdocs.perfdocs import run_perfdocs
|
||||
|
||||
with temp_file("bad", "foo") as temp:
|
||||
run_perfdocs(config, logger=structured_logger, paths=[temp], generate=False)
|
||||
assert PerfDocsLoggerMock.LOGGER == structured_logger
|
||||
assert PerfDocsLoggerMock.PATHS == [temp]
|
||||
assert PerfDocsLoggerMock.FAILED
|
||||
|
||||
assert verifier.validate_tree.assert_called_once()
|
||||
assert generator.generate_perfdocs.assert_not_called()
|
||||
|
||||
|
||||
@mock.patch("perfdocs.generator.Generator")
|
||||
@mock.patch("perfdocs.verifier.Verifier")
|
||||
@mock.patch("perfdocs.logger.PerfDocLogger", new=PerfDocsLoggerMock)
|
||||
def test_perfdocs_start_and_pass(generator, verifier, structured_logger, config, paths):
|
||||
from perfdocs.perfdocs import run_perfdocs
|
||||
|
||||
PerfDocsLoggerMock.FAILED = False
|
||||
with temp_file("bad", "foo") as temp:
|
||||
run_perfdocs(config, logger=structured_logger, paths=[temp], generate=False)
|
||||
assert PerfDocsLoggerMock.LOGGER == structured_logger
|
||||
assert PerfDocsLoggerMock.PATHS == [temp]
|
||||
assert not PerfDocsLoggerMock.FAILED
|
||||
|
||||
assert verifier.validate_tree.assert_called_once()
|
||||
assert generator.generate_perfdocs.assert_called_once()
|
||||
|
||||
|
||||
@mock.patch("perfdocs.logger.PerfDocLogger", new=PerfDocsLoggerMock)
|
||||
def test_perfdocs_bad_paths(structured_logger, config, paths):
|
||||
from perfdocs.perfdocs import run_perfdocs
|
||||
|
||||
with pytest.raises(Exception):
|
||||
run_perfdocs(config, logger=structured_logger, paths=["bad"], generate=False)
|
||||
|
||||
|
||||
def test_perfdocs_logger_failure(config, paths):
|
||||
from perfdocs.logger import PerfDocLogger
|
||||
|
||||
PerfDocLogger.LOGGER = None
|
||||
with pytest.raises(Exception):
|
||||
PerfDocLogger()
|
||||
|
||||
PerfDocLogger.PATHS = []
|
||||
with pytest.raises(Exception):
|
||||
PerfDocLogger()
|
||||
|
||||
|
||||
if __name__ == "__main__":
|
||||
mozunit.main()
|
Загрузка…
Ссылка в новой задаче