Skip to content

Commit bc71fb1

Browse files
committed
tests: add tests for INFO FW_INFO function
Signed-off-by: Filipe Laíns <[email protected]>
1 parent 821d0d5 commit bc71fb1

File tree

2 files changed

+67
-0
lines changed

2 files changed

+67
-0
lines changed

tests/conftest.py

Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,39 @@
11
# SPDX-License-Identifier: MIT
22
# SPDX-FileCopyrightText: 2021 Filipe Laíns <[email protected]>
33

4+
import contextlib
5+
import importlib.util
6+
import os
7+
import os.path
48
import unittest.mock
59

610
import pages
711
import pytest
812
import testsuite
913

1014

15+
root_dir = os.path.abspath(os.path.join(__file__, '..', '..'))
16+
17+
18+
@contextlib.contextmanager
19+
def cd(*path_paths):
20+
old_cwd = os.getcwd()
21+
os.chdir(os.path.join(*path_paths))
22+
try:
23+
yield
24+
finally:
25+
os.chdir(old_cwd)
26+
27+
28+
def import_file(name, path):
29+
spec = importlib.util.spec_from_file_location(name, path)
30+
if not spec.loader:
31+
raise ImportError(f'Unable to import `{path}`: no loader')
32+
module = importlib.util.module_from_spec(spec)
33+
spec.loader.exec_module(module) # type: ignore
34+
return module
35+
36+
1137
@pytest.fixture()
1238
def device(request):
1339
return testsuite.Device(
@@ -29,3 +55,12 @@ def basic_device():
2955
)
3056
device.hid_send = unittest.mock.MagicMock()
3157
return device
58+
59+
60+
@pytest.fixture()
61+
def fw_version():
62+
with cd(root_dir):
63+
configure = import_file('configure', 'configure.py')
64+
version = configure.BuildSystemBuilder.calculate_version()
65+
is_dirty = configure.BuildSystemBuilder._is_git_dirty()
66+
return f'{version}.dirty' if is_dirty else version

tests/test_protocol.py

Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,3 +8,35 @@ def test_dispatch(basic_device):
88
basic_device.protocol_dispatch([0x21]) # invalid length long
99

1010
basic_device.hid_send.assert_not_called()
11+
12+
13+
def test_fw_info_vendor(basic_device):
14+
basic_device.protocol_dispatch([0x20, 0x00, 0x01, 0x00, 0x00, 0x00, 0x00, 0x00])
15+
16+
basic_device.hid_send.assert_called_with(
17+
[0x21, 0x00, 0x01] + list(b'openinput-git') + [0x00] * 16
18+
)
19+
20+
21+
def test_fw_info_version(basic_device, fw_version):
22+
basic_device.protocol_dispatch([0x20, 0x00, 0x01, 0x01, 0x00, 0x00, 0x00, 0x00])
23+
24+
basic_device.hid_send.assert_called_with(
25+
[0x21, 0x00, 0x01] + list(fw_version.encode('ascii')) + [0x00] * (29 - len(fw_version))
26+
)
27+
28+
29+
def test_fw_info_device_name(basic_device):
30+
basic_device.protocol_dispatch([0x20, 0x00, 0x01, 0x02, 0x00, 0x00, 0x00, 0x00])
31+
32+
basic_device.hid_send.assert_called_with(
33+
[0x21, 0x00, 0x01] + list(b'basic test device') + [0x00] * 12
34+
)
35+
36+
37+
def test_fw_info_unsupported(basic_device):
38+
basic_device.protocol_dispatch([0x20, 0x00, 0x01, 0xFF, 0x00, 0x00, 0x00, 0x00])
39+
40+
basic_device.hid_send.assert_called_with(
41+
[0x21, 0xFF, 0x01, 0x00, 0x01] + [0x00] * 27
42+
)

0 commit comments

Comments
 (0)