Skip to content

Commit 6a95243

Browse files
henrikbrixandersenjhedberg
authored andcommitted
tests: drivers: can: host: apply ruff format and fixups
Apply ruff format and fixups to the CAN host test suite pytest files. Signed-off-by: Henrik Brix Andersen <[email protected]>
1 parent 6366bd6 commit 6a95243

File tree

4 files changed

+85
-71
lines changed

4 files changed

+85
-71
lines changed

.ruff-excludes.toml

Lines changed: 0 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -1088,17 +1088,6 @@
10881088
"I001", # https://docs.astral.sh/ruff/rules/unsorted-imports
10891089
"UP031", # https://docs.astral.sh/ruff/rules/printf-string-formatting
10901090
]
1091-
"./tests/drivers/can/host/pytest/can_shell.py" = [
1092-
"I001", # https://docs.astral.sh/ruff/rules/unsorted-imports
1093-
"SIM401", # https://docs.astral.sh/ruff/rules/if-else-block-instead-of-dict-get
1094-
"UP006", # https://docs.astral.sh/ruff/rules/non-pep585-annotation
1095-
"UP035", # https://docs.astral.sh/ruff/rules/deprecated-import
1096-
"UP045", # https://docs.astral.sh/ruff/rules/non-pep604-annotation-optional
1097-
]
1098-
"./tests/drivers/can/host/pytest/test_can.py" = [
1099-
"I001", # https://docs.astral.sh/ruff/rules/unsorted-imports
1100-
"UP039", # https://docs.astral.sh/ruff/rules/unnecessary-class-parentheses
1101-
]
11021091
"./tests/kernel/timer/timer_behavior/pytest/saleae_logic2.py" = [
11031092
"B905", # https://docs.astral.sh/ruff/rules/zip-without-explicit-strict
11041093
"I001", # https://docs.astral.sh/ruff/rules/unsorted-imports
@@ -1481,9 +1470,6 @@ exclude = [
14811470
"./tests/boot/with_mcumgr/pytest/test_downgrade_prevention.py",
14821471
"./tests/boot/with_mcumgr/pytest/test_upgrade.py",
14831472
"./tests/boot/with_mcumgr/pytest/west_sign_wrapper.py",
1484-
"./tests/drivers/can/host/pytest/can_shell.py",
1485-
"./tests/drivers/can/host/pytest/conftest.py",
1486-
"./tests/drivers/can/host/pytest/test_can.py",
14871473
"./tests/kernel/timer/timer_behavior/pytest/saleae_logic2.py",
14881474
"./tests/kernel/timer/timer_behavior/pytest/test_timer.py",
14891475
"./tests/lib/devicetree/memory_region_flags/pytest/test_memory_region_flags.py",

tests/drivers/can/host/pytest/can_shell.py

Lines changed: 33 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -6,25 +6,30 @@
66
Zephyr CAN shell module support for providing a python-can bus interface for testing.
77
"""
88

9-
import re
109
import logging
11-
from typing import Optional, Tuple
10+
import re
1211

1312
from can import BusABC, CanProtocol, Message
1413
from can.exceptions import CanInitializationError, CanOperationError
1514
from can.typechecking import CanFilters
16-
1715
from twister_harness import DeviceAdapter, Shell
1816

1917
logger = logging.getLogger(__name__)
2018

21-
class CanShellBus(BusABC): # pylint: disable=abstract-method
19+
20+
class CanShellBus(BusABC): # pylint: disable=abstract-method
2221
"""
2322
A CAN interface using the Zephyr CAN shell module.
2423
"""
2524

26-
def __init__(self, dut: DeviceAdapter, shell: Shell, channel: str,
27-
can_filters: Optional[CanFilters] = None, **kwargs) -> None:
25+
def __init__(
26+
self,
27+
dut: DeviceAdapter,
28+
shell: Shell,
29+
channel: str,
30+
can_filters: CanFilters | None = None,
31+
**kwargs,
32+
) -> None:
2833
self._dut = dut
2934
self._shell = shell
3035
self._device = channel
@@ -76,7 +81,7 @@ def _start(self):
7681
def _stop(self):
7782
self._shell.exec_command(f'can stop {self._device}')
7883

79-
def send(self, msg: Message, timeout: Optional[float] = None) -> None:
84+
def send(self, msg: Message, timeout: float | None = None) -> None:
8085
logger.debug('sending: %s', msg)
8186

8287
cmd = f'can send {self._device}'
@@ -146,7 +151,7 @@ def _remove_all_filters(self) -> None:
146151
for filter_id in self._filter_ids[:]:
147152
self._remove_filter(filter_id)
148153

149-
def _apply_filters(self, filters: Optional[CanFilters]) -> None:
154+
def _apply_filters(self, filters: CanFilters | None) -> None:
150155
self._remove_all_filters()
151156

152157
if filters:
@@ -155,19 +160,23 @@ def _apply_filters(self, filters: Optional[CanFilters]) -> None:
155160
# Accept all frames if no hardware filters provided
156161
filters = [
157162
{'can_id': 0x0, 'can_mask': 0x0},
158-
{'can_id': 0x0, 'can_mask': 0x0, 'extended': True}
163+
{'can_id': 0x0, 'can_mask': 0x0, 'extended': True},
159164
]
160165
self._is_filtered = False
161166

162167
for can_filter in filters:
163168
can_id = can_filter['can_id']
164169
can_mask = can_filter['can_mask']
165-
extended = can_filter['extended'] if 'extended' in can_filter else False
170+
extended = can_filter.get('extended', False)
166171
self._add_filter(can_id, can_mask, extended)
167172

168-
def _recv_internal(self, timeout: Optional[float]) -> Tuple[Optional[Message], bool]:
169-
frame_regex = r'.*' + re.escape(self._device) + \
170-
r'\s+(?P<brs>\S)(?P<esi>\S)\s+(?P<can_id>\d+)\s+\[(?P<dlc>\d+)\]\s*(?P<data>[a-z0-9 ]*)'
173+
def _recv_internal(self, timeout: float | None) -> tuple[Message | None, bool]:
174+
frame_regex = (
175+
r'.*'
176+
+ re.escape(self._device)
177+
+ r'\s+(?P<brs>\S)(?P<esi>\S)\s+(?P<can_id>\d+)\s+'
178+
+ r'\[(?P<dlc>\d+)\]\s*(?P<data>[a-z0-9 ]*)'
179+
)
171180
lines = self._dut.readlines_until(regex=frame_regex, timeout=timeout)
172181
msg = None
173182

@@ -182,10 +191,17 @@ def _recv_internal(self, timeout: Optional[float]) -> Tuple[Optional[Message], b
182191
brs = m.group('brs') == 'B'
183192
esi = m.group('esi') == 'P'
184193
data = bytearray.fromhex(m.group('data'))
185-
msg = Message(arbitration_id=can_id,is_extended_id=ext,
186-
data=data, dlc=dlc,
187-
is_fd=fd, bitrate_switch=brs, error_state_indicator=esi,
188-
channel=self._device, check=True)
194+
msg = Message(
195+
arbitration_id=can_id,
196+
is_extended_id=ext,
197+
data=data,
198+
dlc=dlc,
199+
is_fd=fd,
200+
bitrate_switch=brs,
201+
error_state_indicator=esi,
202+
channel=self._device,
203+
check=True,
204+
)
189205
logger.debug('received: %s', msg)
190206

191207
return msg, self._is_filtered

tests/drivers/can/host/pytest/conftest.py

Lines changed: 11 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -16,10 +16,15 @@
1616

1717
logger = logging.getLogger(__name__)
1818

19+
1920
def pytest_addoption(parser) -> None:
2021
"""Add local parser options to pytest."""
21-
parser.addoption('--can-context', default=None,
22-
help='Configuration context to use for python-can (default: None)')
22+
parser.addoption(
23+
'--can-context',
24+
default=None,
25+
help='Configuration context to use for python-can (default: None)',
26+
)
27+
2328

2429
@pytest.fixture(name='context', scope='session')
2530
def fixture_context(request, dut: DeviceAdapter) -> str:
@@ -35,6 +40,7 @@ def fixture_context(request, dut: DeviceAdapter) -> str:
3540
logger.info('using python-can configuration context "%s"', ctx)
3641
return ctx
3742

43+
3844
@pytest.fixture(name='chosen', scope='module')
3945
def fixture_chosen(shell: Shell) -> str:
4046
"""Return the name of the zephyr,canbus devicetree chosen device."""
@@ -51,6 +57,7 @@ def fixture_chosen(shell: Shell) -> str:
5157
pytest.fail('zephyr,canbus chosen device not found or not ready')
5258
return None
5359

60+
5461
@pytest.fixture
5562
def can_dut(dut: DeviceAdapter, shell: Shell, chosen: str) -> BusABC:
5663
"""Return DUT CAN bus."""
@@ -59,9 +66,10 @@ def can_dut(dut: DeviceAdapter, shell: Shell, chosen: str) -> BusABC:
5966
bus.shutdown()
6067
dut.clear_buffer()
6168

69+
6270
@pytest.fixture
6371
def can_host(context: str) -> BusABC:
6472
"""Return host CAN bus."""
65-
bus = Bus(config_context = context)
73+
bus = Bus(config_context=context)
6674
yield bus
6775
bus.shutdown()

tests/drivers/can/host/pytest/test_can.py

Lines changed: 41 additions & 37 deletions
Original file line numberDiff line numberDiff line change
@@ -7,50 +7,55 @@
77
"""
88

99
import logging
10-
import pytest
1110

1211
import can
12+
import pytest
1313
from can import BusABC, CanProtocol
1414

1515
# RX/TX timeout in seconds
1616
TIMEOUT = 1.0
1717

1818
logger = logging.getLogger(__name__)
1919

20-
@pytest.mark.parametrize('msg', [
21-
pytest.param(
22-
can.Message(arbitration_id=0x10,
23-
is_extended_id=False),
24-
id='std_id_dlc_0'
25-
),
26-
pytest.param(
27-
can.Message(arbitration_id=0x20,
28-
data=[0xaa, 0xbb, 0xcc, 0xdd],
29-
is_extended_id=False),
30-
id='std_id_dlc_4'
31-
),
32-
pytest.param(
33-
can.Message(arbitration_id=0x30,
34-
data=[0xee, 0xff, 0xee, 0xff, 0xee, 0xff, 0xee, 0xff],
35-
is_extended_id=True),
36-
id='ext_id_dlc_8'
37-
),
38-
pytest.param(
39-
can.Message(arbitration_id=0x40,
40-
data=[0x00, 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07, 0x08, 0x09,
41-
0x10, 0x11],
42-
is_fd=True, is_extended_id=False),
43-
id='std_id_fdf_dlc_9'
44-
),
45-
pytest.param(
46-
can.Message(arbitration_id=0x50,
47-
data=[0x00, 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07, 0x08, 0x09,
48-
0x10, 0x11],
49-
is_fd=True, bitrate_switch=True, is_extended_id=False),
50-
id='std_id_fdf_brs_dlc_9'
51-
),
52-
])
53-
class TestCanRxTx():
20+
21+
@pytest.mark.parametrize(
22+
'msg',
23+
[
24+
pytest.param(can.Message(arbitration_id=0x10, is_extended_id=False), id='std_id_dlc_0'),
25+
pytest.param(
26+
can.Message(arbitration_id=0x20, data=[0xAA, 0xBB, 0xCC, 0xDD], is_extended_id=False),
27+
id='std_id_dlc_4',
28+
),
29+
pytest.param(
30+
can.Message(
31+
arbitration_id=0x30,
32+
data=[0xEE, 0xFF, 0xEE, 0xFF, 0xEE, 0xFF, 0xEE, 0xFF],
33+
is_extended_id=True,
34+
),
35+
id='ext_id_dlc_8',
36+
),
37+
pytest.param(
38+
can.Message(
39+
arbitration_id=0x40,
40+
data=[0x00, 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07, 0x08, 0x09, 0x10, 0x11],
41+
is_fd=True,
42+
is_extended_id=False,
43+
),
44+
id='std_id_fdf_dlc_9',
45+
),
46+
pytest.param(
47+
can.Message(
48+
arbitration_id=0x50,
49+
data=[0x00, 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07, 0x08, 0x09, 0x10, 0x11],
50+
is_fd=True,
51+
bitrate_switch=True,
52+
is_extended_id=False,
53+
),
54+
id='std_id_fdf_brs_dlc_9',
55+
),
56+
],
57+
)
58+
class TestCanRxTx:
5459
"""
5560
Class for testing CAN RX/TX between Zephyr DUT and host.
5661
"""
@@ -64,8 +69,7 @@ def check_rx(tx: can.Message, rx: can.Message) -> None:
6469
if rx is None:
6570
pytest.fail('no message received')
6671

67-
if not tx.equals(rx, timestamp_delta=None, check_channel=False,
68-
check_direction=False):
72+
if not tx.equals(rx, timestamp_delta=None, check_channel=False, check_direction=False):
6973
pytest.fail(f'rx message "{rx}" not equal to tx message "{tx}"')
7074

7175
@staticmethod

0 commit comments

Comments
 (0)