|
| 1 | + |
| 2 | +import unittest |
| 3 | +import glob |
| 4 | +import os |
| 5 | + |
| 6 | +import modm_devices.parser |
| 7 | + |
| 8 | +DEVICE_FILES = None |
| 9 | + |
| 10 | +class DeviceFileTest(unittest.TestCase): |
| 11 | + |
| 12 | + def setUp(self): |
| 13 | + global DEVICE_FILES |
| 14 | + if DEVICE_FILES is None: |
| 15 | + DEVICE_FILES = {} |
| 16 | + device_files = os.path.join(os.path.dirname(__file__), "../../../devices/**/*.xml") |
| 17 | + device_file_names = glob.glob(device_files) |
| 18 | + |
| 19 | + # Parse the files and build the :target enumeration |
| 20 | + parser = modm_devices.parser.DeviceParser() |
| 21 | + for device_file_name in device_file_names: |
| 22 | + for device in parser.parse(device_file_name).get_devices(): |
| 23 | + DEVICE_FILES[device.partname] = device |
| 24 | + self.devices = DEVICE_FILES |
| 25 | + |
| 26 | + |
| 27 | + def tearDown(self): |
| 28 | + self.devices = None |
| 29 | + |
| 30 | + def get_drivers(self, device): |
| 31 | + drivers = [] |
| 32 | + for d in device._properties["driver"]: |
| 33 | + if "instance" in d: |
| 34 | + drivers.extend( (d["name"], i) for i in d["instance"] ) |
| 35 | + else: |
| 36 | + drivers.append( (d["name"],) ) |
| 37 | + return drivers |
| 38 | + |
| 39 | + def test_drivers(self): |
| 40 | + failures = 0 |
| 41 | + for name, device in self.devices.items(): |
| 42 | + def assertIn(key, obj): |
| 43 | + if key not in obj: |
| 44 | + print('{}: Missing "{}" key in "{}"!'.format(name, key, obj)) |
| 45 | + nonlocal failures |
| 46 | + failures += 1 |
| 47 | + return False |
| 48 | + return True |
| 49 | + drivers = self.get_drivers(device) |
| 50 | + gpios = device.get_driver("gpio") |
| 51 | + assertIn("gpio", gpios) |
| 52 | + for gpio in gpios.get("gpio", []): |
| 53 | + signals = [] |
| 54 | + for signal in gpio.get("signal", []): |
| 55 | + # Check for name and driver keys in each signal |
| 56 | + assertIn("name", signal) |
| 57 | + if assertIn("driver", signal): |
| 58 | + # Check if the signal driver is known |
| 59 | + if "instance" in signal: |
| 60 | + driver = (signal["driver"], signal["instance"]) |
| 61 | + else: |
| 62 | + driver = (signal["driver"],) |
| 63 | + signals.append( (*driver, signal["name"]) ) |
| 64 | + # assertIn(driver, drivers) |
| 65 | + |
| 66 | + # Check for duplicate signals |
| 67 | + if not len(signals) == len(set(signals)): |
| 68 | + duplicates = set(x for x in signals if signals.count(x) > 1) |
| 69 | + print("{}: duplicated signals for P{}{}: {}".format( |
| 70 | + name, gpio["port"].upper(), gpio["pin"], duplicates)) |
| 71 | + failures += 1 |
| 72 | + |
| 73 | + self.assertEqual(failures, 0, "Found inconsistencies in the device files!") |
| 74 | + |
| 75 | + |
| 76 | + |
| 77 | + |
| 78 | + |
| 79 | + |
| 80 | + |
0 commit comments