Debian Codegraph @

tests/plugins/mock_libudev.py

raw symbols SCIP
1 # -*- coding: utf-8 -*-
2 # Copyright (C) 2012 Sebastian Wiesner <[email protected]>
3
4 # This library is free software; you can redistribute it and/or modify it
5 # under the terms of the GNU Lesser General Public License as published by the
6 # Free Software Foundation; either version 2.1 of the License, or (at your
7 # option) any later version.
8
9 # This library is distributed in the hope that it will be useful, but WITHOUT
10 # ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
11 # FITNESS FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License
12 # for more details.
13
14 # You should have received a copy of the GNU Lesser General Public License
15 # along with this library; if not, write to the Free Software Foundation,
16 # Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
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 # isort: STDLIB
29 from collections import namedtuple
30 from contextlib import contextmanager
31 from operator import attrgetter
32
33 # isort: THIRDPARTY
34 import pytest
35
36 try:
37 # isort: STDLIB
38 from unittest import mock
39 except ImportError:
40 # isort: THIRDPARTY
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)