Debian Codegraph @

lib/ansible/_internal/_testing.py

raw
1 """
2 Testing utilities for use in integration tests, not unit tests or non-test code.
3 Provides better error behavior than Python's `assert` statement.
4 """
5
6 from __future__ import annotations
7
8 import contextlib
9 import typing as t
10
11
12 class _Checker:
13 @staticmethod
14 def check(value: object, msg: str | None = 'Value is not truthy.') -> None:
15 """Raise an `AssertionError` if the given `value` is not truthy."""
16 if not value:
17 raise AssertionError(msg)
18
19
20 @contextlib.contextmanager
21 def hard_fail_context(msg: str) -> t.Generator[_Checker]:
22 """Enter a context which converts all exceptions to `BaseException` and provides a `Checker` instance for making assertions."""
23 try:
24 yield _Checker()
25 except BaseException as ex:
26 raise BaseException(f"Hard failure: {msg}") from ex