Skip to content

Commit

Permalink
Check if the driver can be loaded, raise an exception or skip the tes…
Browse files Browse the repository at this point in the history
…t if it is not loaded.
  • Loading branch information
marcel-kanter committed Dec 3, 2021
1 parent 03565db commit 10a8e8e
Show file tree
Hide file tree
Showing 3 changed files with 24 additions and 6 deletions.
2 changes: 1 addition & 1 deletion can/bus.py
Original file line number Diff line number Diff line change
Expand Up @@ -452,7 +452,7 @@ def fileno(self) -> int:

@classmethod
def list_adapters(cls) -> List[Any]:
""" Lists all adapters for this interface. The adapter identifier can be used to open a specific adapter.
"""Lists all adapters for this interface. The adapter identifier can be used to open a specific adapter.
MAY NOT BE IMPLEMENTED BY ALL INTERFACES
"""
Expand Down
5 changes: 5 additions & 0 deletions can/interfaces/ixxat/canlib.py
Original file line number Diff line number Diff line change
Expand Up @@ -774,6 +774,11 @@ def list_adapters(cls):
device_handle = HANDLE()
device_info = structures.VCIDEVICEINFO()

if _canlib is None:
raise CanInterfaceNotImplementedError(
"The IXXAT VCI library has not been initialized. Check the logs for more details."
)

_canlib.vciEnumDeviceOpen(ctypes.byref(device_handle))
while True:
try:
Expand Down
23 changes: 18 additions & 5 deletions test/test_interface_ixxat.py
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@
import sys

from can.interfaces.ixxat import IXXATBus
from can.exceptions import CanInterfaceNotImplementedError


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

def test_adapter_enumeration(self):
# Enumeration of adapters should always work and the result should support len and be iterable
adapters = IXXATBus.list_adapters()
# Enumeration of adapters should always work (if the driver is installed) and the result should support len and be iterable
try:
adapters = IXXATBus.list_adapters()
except CanInterfaceNotImplementedError:
raise unittest.SkipTest("Maybe the driver is not installed.")

n = 0
for adapter in adapters:
n += 1
Expand All @@ -58,20 +63,28 @@ def setUp(self):

def test_bus_creation(self):
# Test the enumeration of all adapters by opening and closing each adapter
adapters = IXXATBus.list_adapters()
try:
adapters = IXXATBus.list_adapters()
except CanInterfaceNotImplementedError:
raise unittest.SkipTest("Maybe the driver is not installed.")

for adapter in adapters:
bus = can.Bus(interface="ixxat", adapter=adapter)
bus.shutdown()

def test_send_after_shutdown(self):
# At least one adapter is needed, skip the test if none can be found
adapters = IXXATBus.list_adapters()
try:
adapters = IXXATBus.list_adapters()
except CanInterfaceNotImplementedError:
raise unittest.SkipTest("Maybe the driver is not installed.")

if len(adapters) == 0:
raise unittest.SkipTest("No adapters found")

bus = can.Bus(interface="ixxat", channel=0)
msg = can.Message(arbitration_id=0x3FF, dlc=0)
# Intentionally close the bus now and try to send afterwards. This should lead to an Exception
# Intentionally close the bus now and try to send afterwards. This should lead to an CanOperationError
bus.shutdown()
with self.assertRaises(can.CanOperationError):
bus.send(msg)
Expand Down

0 comments on commit 10a8e8e

Please sign in to comment.