| 1 |
|
| 2 |
|
| 3 |
|
| 4 |
|
| 5 |
|
| 6 |
|
| 7 |
|
| 8 |
|
| 9 |
|
| 10 |
|
| 11 |
|
| 12 |
|
| 13 |
|
| 14 |
|
| 15 |
|
| 16 |
|
| 17 |
"""plugins.mock_libudev |
| 18 |
==================== |
| 19 |
|
| 20 |
Plugin to mock calls to libudev. |
| 21 |
|
| 22 |
This plugin adds :func:`libudev_list()` to the :mod:`pytest` namespace. |
| 23 |
|
| 24 |
.. moduleauthor:: Sebastian Wiesner <[email protected]> |
| 25 |
|
| 26 |
""" |
| 27 |
|
| 28 |
|
| 29 |
from collections import namedtuple |
| 30 |
from contextlib import contextmanager |
| 31 |
from operator import attrgetter |
| 32 |
|
| 33 |
|
| 34 |
import pytest |
| 35 |
|
| 36 |
try: |
| 37 |
|
| 38 |
from unittest import mock |
| 39 |
except ImportError: |
| 40 |
|
| 41 |
import mock |
| 42 |
|
| 43 |
Node = namedtuple("Node", "name value next") |
| 44 |
|
| 45 |
|
| 46 |
class LinkedList: |
| 47 |
""" |
| 48 |
Linked list class to mock libudev list functions. |
| 49 |
""" |
| 50 |
|
| 51 |
@classmethod |
| 52 |
def from_iterable(cls, iterable): |
| 53 |
""" |
| 54 |
Create a list from the given ``iterable``. |
| 55 |
""" |
| 56 |
next_node = None |
| 57 |
for item in reversed(iterable): |
| 58 |
if isinstance(item, tuple): |
| 59 |
name, value = item |
| 60 |
else: |
| 61 |
name, value = item, None |
| 62 |
node = Node(name, value, next_node) |
| 63 |
next_node = node |
| 64 |
return cls(next_node) |
| 65 |
|
| 66 |
def __init__(self, first): |
| 67 |
self.first = first |
| 68 |
|
| 69 |
|
| 70 |
@contextmanager |
| 71 |
def libudev_list(libudev, function, items): |
| 72 |
""" |
| 73 |
Mock a libudev linked list:: |
| 74 |
|
| 75 |
with pytest.libudev_list(device._libudev, 'udev_device_get_tag_list_entry', ['foo', 'bar']): |
| 76 |
assert list(device.tags) == ['foo', 'bar'] |
| 77 |
|
| 78 |
``function`` is a string containing the name of the libudev function that |
| 79 |
returns the list. ``items`` is an iterable yielding items which shall be |
| 80 |
returned by the mocked list function. An item in ``items`` can either be a |
| 81 |
tuple with two components, where the first component is the item name, and |
| 82 |
the second the item value, or a single element, which is the item name. |
| 83 |
The item value is ``None`` in this case. |
| 84 |
""" |
| 85 |
functions_to_patch = [ |
| 86 |
function, |
| 87 |
"udev_list_entry_get_next", |
| 88 |
"udev_list_entry_get_name", |
| 89 |
"udev_list_entry_get_value", |
| 90 |
] |
| 91 |
mocks = dict((f, mock.DEFAULT) for f in functions_to_patch) |
| 92 |
with mock.patch.multiple(libudev, **mocks): |
| 93 |
udev_list = LinkedList.from_iterable(items) |
| 94 |
getattr(libudev, function).return_value = udev_list.first |
| 95 |
libudev.udev_list_entry_get_name.side_effect = attrgetter("name") |
| 96 |
libudev.udev_list_entry_get_value.side_effect = attrgetter("value") |
| 97 |
libudev.udev_list_entry_get_next.side_effect = attrgetter("next") |
| 98 |
yield |
| 99 |
|
| 100 |
|
| 101 |
EXPOSED_FUNCTIONS = [libudev_list] |
| 102 |
|
| 103 |
|
| 104 |
def pytest_configure(): |
| 105 |
for f in EXPOSED_FUNCTIONS: |
| 106 |
setattr(pytest, f.__name__, f) |