Debian Codegraph @

test/test_data_files.py

raw
1 #!/usr/bin/env python3
2 #
3 # Run with pytest
4
5 import configparser
6 import os
7 import re
8 from pathlib import Path
9
10
11 WACOM_RECEIVER_USBIDS = [
12 (0x56A, 0x84),
13 ]
14
15
16 def datadir():
17 return Path(os.getenv("MESON_SOURCE_ROOT") or ".") / "data"
18
19
20 def layoutsdir():
21 return datadir() / "layouts"
22
23
24 def pytest_generate_tests(metafunc):
25 # for any function that takes a "tabletfile" argument return the path to
26 # a tablet file
27 if "tabletfile" in metafunc.fixturenames:
28 files = [f for f in datadir().glob("*.tablet")]
29 metafunc.parametrize("tabletfile", files, ids=[f.name for f in files])
30
31
32 def test_device_match(tabletfile):
33 config = configparser.ConfigParser()
34 config.read(tabletfile)
35
36 # Match format must be bus:vid:pid:name
37 # where bus is 'usb' or 'bluetooth'
38 # where vid/pid is a lowercase hex
39 # where name is optional
40 for match in config["Device"]["DeviceMatch"].split(";"):
41 if not match or match == "generic":
42 continue
43
44 bus, vid, pid = match.split("|")[:3] # skip the name part of the match
45 assert bus in [
46 "usb",
47 "bluetooth",
48 "i2c",
49 "serial",
50 ], f"{tabletfile}: unknown bus type"
51 assert re.match("[0-9a-f]{4}", vid), (
52 f"{tabletfile}: {vid} must be lowercase hex"
53 )
54 assert re.match("[0-9a-f]{4}", pid), (
55 f"{tabletfile}: {pid} must be lowercase hex"
56 )
57
58
59 def test_no_receiver_id(tabletfile):
60 config = configparser.ConfigParser(strict=True)
61 # Don't convert to lowercase
62 config.optionxform = lambda option: option
63 config.read(tabletfile)
64
65 receivers = ["usb|{:04x}|{:04x}".format(*r) for r in WACOM_RECEIVER_USBIDS]
66 for match in config["Device"]["DeviceMatch"].split(";"):
67 assert match not in receivers
68
69
70 def test_svg_exists(tabletfile):
71 config = configparser.ConfigParser(strict=True)
72 # Don't convert to lowercase
73 config.optionxform = lambda option: option
74 config.read(tabletfile)
75
76 try:
77 svg = config["Device"]["Layout"]
78 assert svg != "", "Empty Layout= line not permitted"
79 assert (layoutsdir() / svg).exists()
80
81 except KeyError:
82 pass
83
84
85 def test_button_evcodes(tabletfile):
86 config = configparser.ConfigParser(strict=True)
87 # Don't convert to lowercase
88 config.optionxform = lambda option: option
89 config.read(tabletfile)
90
91 try:
92 nbuttons = 0
93 for where in ["Top", "Bottom", "Left", "Right"]:
94 try:
95 buttons = config["Buttons"][where]
96 nbuttons += len([s for s in buttons.split(";") if s])
97 except KeyError:
98 pass
99 str = config["Buttons"]["EvdevCodes"]
100 codes = [
101 c for c in str.split(";") if c
102 ] # drop empty strings from trailing semicolons
103 assert len(codes) == nbuttons, "Number of buttons mismatches the EvdevCodes"
104 except KeyError:
105 pass