Skip to content

Commit 10a8e8e

Browse files
committed
Check if the driver can be loaded, raise an exception or skip the test if it is not loaded.
1 parent 03565db commit 10a8e8e

File tree

3 files changed

+24
-6
lines changed

3 files changed

+24
-6
lines changed

can/bus.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -452,7 +452,7 @@ def fileno(self) -> int:
452452

453453
@classmethod
454454
def list_adapters(cls) -> List[Any]:
455-
""" Lists all adapters for this interface. The adapter identifier can be used to open a specific adapter.
455+
"""Lists all adapters for this interface. The adapter identifier can be used to open a specific adapter.
456456
457457
MAY NOT BE IMPLEMENTED BY ALL INTERFACES
458458
"""

can/interfaces/ixxat/canlib.py

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -774,6 +774,11 @@ def list_adapters(cls):
774774
device_handle = HANDLE()
775775
device_info = structures.VCIDEVICEINFO()
776776

777+
if _canlib is None:
778+
raise CanInterfaceNotImplementedError(
779+
"The IXXAT VCI library has not been initialized. Check the logs for more details."
780+
)
781+
777782
_canlib.vciEnumDeviceOpen(ctypes.byref(device_handle))
778783
while True:
779784
try:

test/test_interface_ixxat.py

Lines changed: 18 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@
99
import sys
1010

1111
from can.interfaces.ixxat import IXXATBus
12+
from can.exceptions import CanInterfaceNotImplementedError
1213

1314

1415
class SoftwareTestCase(unittest.TestCase):
@@ -38,8 +39,12 @@ def test_bus_creation(self):
3839
can.Bus(interface="ixxat", channel=0xFFFF)
3940

4041
def test_adapter_enumeration(self):
41-
# Enumeration of adapters should always work and the result should support len and be iterable
42-
adapters = IXXATBus.list_adapters()
42+
# Enumeration of adapters should always work (if the driver is installed) and the result should support len and be iterable
43+
try:
44+
adapters = IXXATBus.list_adapters()
45+
except CanInterfaceNotImplementedError:
46+
raise unittest.SkipTest("Maybe the driver is not installed.")
47+
4348
n = 0
4449
for adapter in adapters:
4550
n += 1
@@ -58,20 +63,28 @@ def setUp(self):
5863

5964
def test_bus_creation(self):
6065
# Test the enumeration of all adapters by opening and closing each adapter
61-
adapters = IXXATBus.list_adapters()
66+
try:
67+
adapters = IXXATBus.list_adapters()
68+
except CanInterfaceNotImplementedError:
69+
raise unittest.SkipTest("Maybe the driver is not installed.")
70+
6271
for adapter in adapters:
6372
bus = can.Bus(interface="ixxat", adapter=adapter)
6473
bus.shutdown()
6574

6675
def test_send_after_shutdown(self):
6776
# At least one adapter is needed, skip the test if none can be found
68-
adapters = IXXATBus.list_adapters()
77+
try:
78+
adapters = IXXATBus.list_adapters()
79+
except CanInterfaceNotImplementedError:
80+
raise unittest.SkipTest("Maybe the driver is not installed.")
81+
6982
if len(adapters) == 0:
7083
raise unittest.SkipTest("No adapters found")
7184

7285
bus = can.Bus(interface="ixxat", channel=0)
7386
msg = can.Message(arbitration_id=0x3FF, dlc=0)
74-
# Intentionally close the bus now and try to send afterwards. This should lead to an Exception
87+
# Intentionally close the bus now and try to send afterwards. This should lead to an CanOperationError
7588
bus.shutdown()
7689
with self.assertRaises(can.CanOperationError):
7790
bus.send(msg)

0 commit comments

Comments
 (0)