1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71
|
# SPDX-License-Identifier: MIT
from __future__ import annotations
import random
import reprlib
import sys
from typing import Callable, Optional, Sequence
import pytest
import mbedtls
def pytest_report_header(
# pylint: disable=unused-argument
config: object,
startdir: object,
) -> None:
sys.stdout.write(
f"python-mbedtls {mbedtls.__version__}, {mbedtls.version.version}\n"
)
class _Repr(reprlib.Repr):
"""Repr with support for memoryview."""
def repr_memoryview(self, obj: memoryview, _level: object) -> str:
return f"{type(obj).__name__}({self.repr(obj.tobytes())})"
_repr_instance = _Repr()
_repr = _repr_instance.repr
def _compare_memoryviews(
_config: object, _op: object, left: object, right: object
) -> Sequence[str]:
# Adapted from pytest.
summary = [f"{_repr(left)} != {_repr(right)}"]
explanation = []
if isinstance(left, Sequence) and isinstance(right, Sequence):
for i in range(min(len(left), len(right))):
if left[i] != right[i]:
left_value = left[i : i + 1]
right_value = right[i : i + 1]
explanation += [
f"At index {i} diff: {_repr(left_value)} != {_repr(right_value)}"
]
break
return summary + explanation
def pytest_assertrepr_compare(
config: object, op: object, left: object, right: object
) -> Optional[Sequence[str]]:
if op == "==" and any(
(isinstance(left, memoryview), isinstance(right, memoryview))
):
return _compare_memoryviews(config, op, left, right)
return None
@pytest.fixture
def randbytes() -> Callable[[int], bytes]:
def function(length: int) -> bytes:
return bytes(
bytearray(random.randrange(0, 256) for _ in range(length))
)
return function
|