1
mirror of https://github.com/home-assistant/core synced 2024-09-15 17:29:45 +02:00
ha-core/tests/pylint/__init__.py
epenet c8504bd21d
Add tests for pylint plugins (#65436)
Co-authored-by: epenet <epenet@users.noreply.github.com>
2022-02-03 10:01:02 +01:00

32 lines
1.0 KiB
Python

"""Tests for pylint."""
import contextlib
from pylint.testutils.unittest_linter import UnittestLinter
@contextlib.contextmanager
def assert_no_messages(linter: UnittestLinter):
"""Assert that no messages are added by the given method."""
with assert_adds_messages(linter):
yield
@contextlib.contextmanager
def assert_adds_messages(linter: UnittestLinter, *messages):
"""Assert that exactly the given method adds the given messages.
The list of messages must exactly match *all* the messages added by the
method. Additionally, we check to see whether the args in each message can
actually be substituted into the message string.
"""
yield
got = linter.release_messages()
no_msg = "No message."
expected = "\n".join(repr(m) for m in messages) or no_msg
got_str = "\n".join(repr(m) for m in got) or no_msg
msg = (
"Expected messages did not match actual.\n"
f"\nExpected:\n{expected}\n\nGot:\n{got_str}\n"
)
assert got == list(messages), msg