Source code for craft_cli.pytest_plugin

# Copyright 2022-2023 Canonical Ltd.
#
# This program is free software; you can redistribute it and/or
# modify it under the terms of the GNU Lesser General Public
# License version 3 as published by the Free Software Foundation.
#
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
# Lesser General Public License for more details.
#
# You should have received a copy of the GNU Lesser General Public License
# along with this program; if not, write to the Free Software Foundation,
# Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301, USA.

"""Different fixtures for easier testability of Craft CLI services."""

from __future__ import annotations

import contextlib
import re
from collections.abc import Generator
from pathlib import Path
from tempfile import NamedTemporaryFile
from typing import TYPE_CHECKING, Any, Literal
from unittest.mock import call

try:
    import pytest
except ImportError as err:
    raise ImportError(
        "craft-cli's pytest plugin requires pytest; install it with 'craft-cli[pytest]'"
    ) from err
from typing_extensions import Self

from craft_cli import messages, printer
from craft_cli.errors import CraftError

if TYPE_CHECKING:
    from unittest.mock import _Call


[docs] def raises_craft_error( expected_exception: type[CraftError] = CraftError, *, match: str | re.Pattern[str] | None = None, details: str | re.Pattern[str] | None = None, resolution: str | re.Pattern[str] | None = None, docs_url: str | re.Pattern[str] | None = None, retcode: int | None = None, ) -> pytest.RaisesExc[CraftError]: """Assert that a `CraftError` is raised, matching on its fields. This is a thin wrapper around `pytest.raises` that, in addition to pytest's `match` (a regex against the error's message), lets you match against the other `CraftError` fields that `match` cannot reach. Like `pytest.raises`, the expected exception type is the first positional argument and defaults to `CraftError`; pass a subclass to be more specific. `match`, `details`, `resolution` and `docs_url` are regular expressions matched with `re.search`; `retcode` is compared for exact equality. Any field left as `None` is not checked. The returned object is a real `pytest.raises` context manager, so the `as` binding and `pytest.RaisesGroup` composition work as usual: with raises_craft_error(CraftCommandError, match="Failed to pull", retcode=2): app.run() On a field mismatch the assertion error names the offending field, e.g. `retcode mismatch: expected 2, got 1`. """ def _check_fields(exc: CraftError) -> bool: for field_name, pattern in ( ("details", details), ("resolution", resolution), ("docs_url", docs_url), ): if pattern is None: continue value = getattr(exc, field_name) if value is None or not re.search(pattern, value): raise AssertionError( f"{field_name} mismatch: expected {pattern!r}, got {value!r}" ) if retcode is not None and exc.retcode != retcode: raise AssertionError( f"retcode mismatch: expected {retcode}, got {exc.retcode}" ) return True return pytest.raises(expected_exception, match=match, check=_check_fields)
[docs] @pytest.fixture(autouse=True) def init_emitter(monkeypatch: pytest.MonkeyPatch) -> Generator[None]: """Ensure ``emit`` is always clean, and initiated (in test mode). Note that the ``init`` is done in the current instance that all modules already acquired. This is an "autouse" fixture, so it just works, no need to declare it in your tests. """ # initiate with a custom log filepath so user directories are not involved here; note that # we're not using pytest's standard tmp_path as Emitter would write logs there, and in # effect we would be polluting that temporary directory (potentially messing with # tests, that may need that empty), so we use another one with NamedTemporaryFile("w", prefix="emitter-logs") as tmp_logfile: monkeypatch.setattr(messages, "TESTMODE", True) monkeypatch.setattr(printer, "TESTMODE", True) messages.emit.init( messages.EmitterMode.QUIET, "test-emitter", "Hello world", log_filepath=Path(tmp_logfile.name), ) yield # end machinery (just in case it was not ended before; note it's ok to "double end") messages.emit.ended_ok()
class _RegexComparingText(str): """A string that compares for equality using regex.match.""" def __eq__(self, other: object) -> bool: return bool(re.match(self, str(other), re.DOTALL)) def __hash__(self) -> int: return str.__hash__(self)
[docs] class RecordingEmitter: """Record what is shown using the emitter and provide a nice API for tests. This class is NOT meant to be used directly, please use the ``emitter`` fixture instead which provides an instance of this class with context properly set up. """ def __init__(self) -> None: self.interactions: list[_Call] = [] self.paused = False
[docs] @contextlib.contextmanager def pause(self) -> Generator[None]: """Mimics the pause context manager, storing the state to simplify tests.""" self.paused = True try: yield finally: self.paused = False
[docs] def record(self, method_name: str, args: Any, kwargs: dict[str, Any]) -> None: """Record the method call and its specific parameters.""" self.interactions.append(call(method_name, *args, **kwargs))
def _check( self, expected_text: str, method_name: str, regex: bool, # noqa: FBT001 *, invert: bool = False, **kwargs: Any, ) -> Any: """Really verify messages.""" if regex: expected_text = _RegexComparingText(expected_text) expected_call = call(method_name, expected_text, **kwargs) found = None for stored_call in self.interactions: if stored_call == expected_call: found = stored_call.args[1] break if invert and found is not None: raise AssertionError( f"Found call {expected_call} when it should have been absent from {self.interactions}." ) if not invert and found is None: raise AssertionError( f"Expected call {expected_call} not found in {self.interactions}" ) return found
[docs] def assert_message( self, expected_text: str, regex: bool = False, # noqa: FBT001 ) -> Any: """Check the 'message' method was properly used. It verifies that the method was called at least once with the expected text. If 'regex' is True, the expected text will be used as a regular expression. """ return self._check(expected_text, "message", regex)
[docs] def assert_no_message( self, expected_text: str, *, regex: bool = False, ) -> Any: """Check the 'message' method was not used. It verifies that the method was never called with the expected text. If 'regex' is True, the expected text will be used as a regular expression. """ return self._check(expected_text, "message", regex, invert=True)
[docs] def assert_progress( self, expected_text: str, permanent: bool | None = None, # noqa: FBT001 regex: bool = False, # noqa: FBT001 ) -> Any: """Check the 'progress' method was properly used. It verifies that the method was called at least once with the expected text (with the given 'permanent' flag). If 'regex' is True, the expected text will be used as a regular expression. """ if permanent is None: result = self._check(expected_text, "progress", regex) else: result = self._check(expected_text, "progress", regex, permanent=permanent) return result
[docs] def assert_no_progress( self, expected_text: str, permanent: bool | None = None, # noqa: FBT001 *, regex: bool = False, ) -> Any: """Check the 'progress' method was not used. It verifies that the method was never called with the expected text (with the given 'permanent' flag). If 'regex' is True, the expected text will be used as a regular expression. """ if permanent is None: result = self._check(expected_text, "progress", regex, invert=True) else: result = self._check( expected_text, "progress", regex, permanent=permanent, invert=True ) return result
[docs] def assert_verbose( self, expected_text: str, regex: bool = False, # noqa: FBT001 ) -> Any: """Check the 'verbose' method was properly used. It verifies that the method was called at least once with the expected text. If 'regex' is True, the expected text will be used as a regular expression. """ return self._check(expected_text, "verbose", regex)
[docs] def assert_no_verbose( self, expected_text: str, *, regex: bool = False, ) -> Any: """Check the 'verbose' method was not used. It verifies that the method was never called with the expected text. If 'regex' is True, the expected text will be used as a regular expression. """ return self._check(expected_text, "verbose", regex, invert=True)
[docs] def assert_warning(self, expected_text: str, *, regex: bool = False) -> Any: """Check the 'warning' method was properly used. It verifies that the method was called at least once with the expected text. If 'regex' is True, the expected text will be used as a regular expression. """ return self._check(expected_text, "warning", regex)
[docs] def assert_no_warning( self, expected_text: str, *, regex: bool = False, ) -> Any: """Check the 'warning' method was not used. It verifies that the method was never called with the expected text. If 'regex' is True, the expected text will be used as a regular expression. """ return self._check(expected_text, "warning", regex, invert=True)
[docs] def assert_debug( self, expected_text: str, regex: bool = False, # noqa: FBT001 ) -> Any: """Check the 'debug' method was properly used. It verifies that the method was called at least once with the expected text. If 'regex' is True, the expected text will be used as a regular expression. """ return self._check(expected_text, "debug", regex)
[docs] def assert_no_debug( self, expected_text: str, *, regex: bool = False, ) -> Any: """Check the 'debug' method was not used. It verifies that the method was never called with the expected text. If 'regex' is True, the expected text will be used as a regular expression. """ return self._check(expected_text, "debug", regex, invert=True)
[docs] def assert_trace( self, expected_text: str, regex: bool = False, # noqa: FBT001 ) -> Any: """Check the 'trace' method was properly used. It verifies that the method was called at least once with the expected text. If 'regex' is True, the expected text will be used as a regular expression. """ return self._check(expected_text, "trace", regex)
[docs] def assert_no_trace( self, expected_text: str, *, regex: bool = False, ) -> Any: """Check the 'trace' method was not used. It verifies that the method was never called with the expected text. If 'regex' is True, the expected text will be used as a regular expression. """ return self._check(expected_text, "trace", regex, invert=True)
[docs] def assert_messages(self, texts: list[str]) -> None: """Check that the 'message' method was called several times with the given texts. This is helper for a common case that happen in multiline commands results where 'message' is called several times. """ self.assert_interactions([call("message", text) for text in texts])
[docs] def assert_interactions(self, expected_call_list: list[_Call] | None) -> None: """Check that the expected call list happen at some point between all stored calls. If None is passed, asserts that no message was emitted. """ if expected_call_list is None: if self.interactions: show_interactions = "\n".join( str(interaction) for interaction in self.interactions ) raise AssertionError( "Expected no call but really got:\n" + show_interactions ) return for _pos, stored_call in enumerate(self.interactions): if stored_call == expected_call_list[0]: pos = _pos break else: pos = 0 end_pos = pos + len(expected_call_list) stored = self.interactions[pos:end_pos] assert stored == expected_call_list
class _RecordingProgresser: def __init__(self, recording_emitter: RecordingEmitter) -> None: self.recording_emitter = recording_emitter def __enter__(self) -> Self: return self def __exit__(self, *_exc_info: object) -> Literal[False]: return False # do not consume any exception def advance(self, amount: float, *a: Any, **k: Any) -> None: """Record the advance usage.""" if amount < 0: raise ValueError("The advance amount cannot be negative") self.recording_emitter.record("advance", (amount, *a), k)
[docs] @pytest.fixture def emitter(monkeypatch: pytest.MonkeyPatch) -> RecordingEmitter: """Provide a helper to test everything that was shown using the Emitter.""" recording_emitter = RecordingEmitter() for method_name in ("message", "progress", "verbose", "debug", "trace", "warning"): def new_method(*a: Any, method_name: str = method_name, **k: Any) -> None: recording_emitter.record(method_name, a, k) monkeypatch.setattr( messages.emit, method_name, new_method, ) # progress bar is special, because it also needs to return a context manager with # something that will record progress calls def fake_progress_bar(*a: Any, **k: Any) -> _RecordingProgresser: recording_emitter.record("progress_bar", a, k) return _RecordingProgresser(recording_emitter) monkeypatch.setattr(messages.emit, "progress_bar", fake_progress_bar) # pause is also special, as it's specifically implemented in the recording emitter monkeypatch.setattr(messages.emit, "pause", recording_emitter.pause) return recording_emitter