Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Unit test parse args #2725

Merged
merged 2 commits into from
Jan 1, 2025
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 3 additions & 1 deletion lib/logitech_receiver/receiver.py
Original file line number Diff line number Diff line change
@@ -41,6 +41,8 @@
if typing.TYPE_CHECKING:
from logitech_receiver import common

from .base import HIDPPNotification

logger = logging.getLogger(__name__)

_hidpp10 = hidpp10.Hidpp10()
@@ -198,7 +200,7 @@ def notify_devices(self):
if not self.write_register(Registers.RECEIVER_CONNECTION, 0x02):
logger.warning("%s: failed to trigger device link notifications", self)

def notification_information(self, number, notification):
def notification_information(self, number, notification: HIDPPNotification) -> tuple[bool, bool, typing.Any, str]:
"""Extract information from unifying-style notification"""
assert notification.address != 0x02
online = not bool(notification.data[0] & 0x40)
6 changes: 5 additions & 1 deletion lib/solaar/gtk.py
Original file line number Diff line number Diff line change
@@ -54,7 +54,7 @@ def _require(module, os_package, gi=None, gi_package=None, gi_version=None):
temp = tempfile.NamedTemporaryFile(prefix="Solaar_", mode="w", delete=True)


def _parse_arguments():
def create_parser():
arg_parser = argparse.ArgumentParser(
prog=NAME.lower(), epilog="For more information see https://pwr-solaar.github.io/Solaar"
)
@@ -99,7 +99,11 @@ def _parse_arguments():
choices=cli.actions,
help="command-line action to perform (optional); append ' --help' to show args",
)
return arg_parser


def _parse_arguments():
arg_parser = create_parser()
args = arg_parser.parse_args()

if args.help_actions:
31 changes: 31 additions & 0 deletions tests/logitech_receiver/test_receiver.py
Original file line number Diff line number Diff line change
@@ -12,6 +12,13 @@
from . import fake_hidpp


@pytest.fixture
def nano_recv():
device_info = DeviceInfo("12", product_id=0xC534)
mock_low_level = LowLevelInterfaceFake(responses_lacking)
yield receiver.create_receiver(mock_low_level, device_info, lambda x: x)


class LowLevelInterfaceFake:
def __init__(self, responses=None):
self.responses = responses
@@ -189,3 +196,27 @@ def test_receiver_factory_no_device(device_info, responses):

with pytest.raises(exceptions.NoSuchDevice):
r.device_pairing_information(1)


@pytest.mark.parametrize(
"address, data, expected_online, expected_encrypted",
[
(0x03, b"\x01\x02\x03", True, False),
(0x10, b"\x61\x02\x03", False, True),
],
)
def test_notification_information_nano_receiver(nano_recv, address, data, expected_online, expected_encrypted):
_number = 0
notification = base.HIDPPNotification(
report_id=0x01,
devnumber=0x52C,
sub_id=0,
address=address,
data=data,
)
online, encrypted, wpid, kind = nano_recv.notification_information(_number, notification)

assert online == expected_online
assert encrypted == expected_encrypted
assert wpid == "0302"
assert kind == "keyboard"
27 changes: 27 additions & 0 deletions tests/solaar/test_gtk.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
from solaar.gtk import create_parser


def test_arg_parse():
parser = create_parser()
res = parser.parse_args([])

assert res.debug == 0
assert res.hidraw_path is None
assert res.restart_on_wake_up is False
assert res.window is None
assert res.battery_icons is None
assert res.tray_icon_size is None


def test_arg_parse_debug():
parser = create_parser()
res = parser.parse_args(["--debug"])

assert res.debug == 1


def test_arg_parse_version():
parser = create_parser()
res = parser.parse_args(["version"])

assert res