Debian Codegraph @

tests/_constants.py

raw symbols SCIP
1 # -*- coding: utf-8 -*-
2 # Copyright (C) 2015 mulhern <[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 """
18 pyudev.tests._device_tests
19 ==========================
20
21 Tests for devices.
22
23 .. moduleauthor:: mulhern <[email protected]>
24 """
25
26 # isort: THIRDPARTY
27 import pytest
28 from hypothesis import strategies
29
30 # isort: LOCAL
31 from pyudev import Context, DeviceNotFoundError, Devices
32
33 from .utils import udev
34
35 _CONTEXT = Context()
36
37
38 def _check_device(device):
39 """
40 Check that device exists by getting it.
41 """
42 try:
43 Devices.from_path(_CONTEXT, device.sys_path)
44 return True
45 except DeviceNotFoundError:
46 return False
47
48
49 _DEVICE_DATA = [d for d in udev.DeviceDatabase.db()]
50 _DEVICES = [Devices.from_path(_CONTEXT, d.device_path) for d in _DEVICE_DATA]
51
52
53 def device_strategy(require_existing=True, filter_func=lambda x: True):
54 """
55 Strategy that yields filtered devices.
56
57 The devices are filtered before being sampled to reduce the number
58 of health failures.
59
60 :param bool require_existing: at the very last, verify existance
61 :param filter_func: a function to be used as a filter
62 :type filter_func: Device -> bool
63 """
64 strategy = strategies.sampled_from(
65 [x for x in _CONTEXT.list_devices() if filter_func(x)]
66 )
67
68 if require_existing:
69 strategy = strategy.filter(_check_device)
70
71 return strategy
72
73
74 _CONTEXT_STRATEGY = strategies.just(_CONTEXT)
75
76 _UDEV_VERSION = int(udev.UDevAdm.adm().query_udev_version())
77
78 _SUBSYSTEM_STRATEGY = device_strategy().map(lambda x: x.subsystem)
79
80 # Workaround for issue #181
81 _SUBSYSTEM_STRATEGY = _SUBSYSTEM_STRATEGY.filter(lambda s: s != "i2c")
82
83 _SYSNAME_STRATEGY = device_strategy().map(lambda x: x.sys_name)
84
85 _PROPERTY_STRATEGY = device_strategy().flatmap(
86 lambda d: strategies.sampled_from([p for p in d.properties.items()])
87 )
88
89 _MATCH_PROPERTY_STRATEGY = _PROPERTY_STRATEGY.filter(lambda p: p[0][-4:] != "_ENC")
90
91 if _UDEV_VERSION <= 230:
92 _MATCH_PROPERTY_STRATEGY = _MATCH_PROPERTY_STRATEGY.filter(
93 lambda p: "[" not in p[1]
94 )
95
96 # the attributes object for a given device
97 _ATTRIBUTES_STRATEGY = device_strategy().map(lambda d: d.attributes)
98
99 # an attribute key and value pair
100 _ATTRIBUTE_STRATEGY = _ATTRIBUTES_STRATEGY.flatmap(
101 lambda attrs: strategies.sampled_from([a for a in attrs.available_attributes]).map(
102 lambda key: (key, attrs.get(key))
103 )
104 )
105
106 _ATTRIBUTE_STRATEGY = _ATTRIBUTE_STRATEGY.filter(lambda p: p[1] is not None)
107
108 if _UDEV_VERSION <= 230:
109 _ATTRIBUTE_STRATEGY = _ATTRIBUTE_STRATEGY.filter(
110 lambda p: not p[1].startswith(b"\\")
111 and not p[1][-1:] == b" "
112 and not p[1].startswith(b"[")
113 )
114
115 # the tags object for a given device
116 _TAGS_STRATEGY = device_strategy().map(lambda d: [t for t in d.tags])
117
118 # an arbitrary tag belonging to a given device
119 _TAG_STRATEGY = _TAGS_STRATEGY.filter(lambda t: t != []).flatmap(
120 strategies.sampled_from
121 )
122
123
124 def _UDEV_TEST(version, node=None): # pylint: disable=invalid-name
125 fmt_str = "%s: udev version must be at least %s, is %s"
126 return pytest.mark.skipif(
127 _UDEV_VERSION < version, reason=fmt_str % (node, version, _UDEV_VERSION)
128 )