Skip to content

Commit

Permalink
Fix macOS compatibility and reenable CI tests
Browse files Browse the repository at this point in the history
* Fix CI for macOS

* Fix error message for missing hidapi

* Skip some device and receiver tests on macOS

Tests fail on macOS, enable them when unit tests are
refined to only test the module without dependencies.

* Safe guard dbus import
  • Loading branch information
MattHag authored May 22, 2024
1 parent a9ce033 commit 500b999
Show file tree
Hide file tree
Showing 6 changed files with 41 additions and 32 deletions.
52 changes: 25 additions & 27 deletions .github/workflows/tests.yml
Original file line number Diff line number Diff line change
Expand Up @@ -31,30 +31,28 @@ jobs:
run: |
make test
# macos-tests:
# runs-on: macos-latest
#
# strategy:
# matrix:
# python-version: [3.8, 3.12]
#
# steps:
# - name: Checkout
# uses: actions/checkout@v4
#
# - name: Set up Python ${{ matrix.python-version }}
# uses: actions/setup-python@v5
# with:
# python-version: ${{ matrix.python-version }}
#
# - name: Set up macOS dependencies
# run: |
# make install_brew
#
# - name: Install Python dependencies
# run: |
# make install_pip PIP_ARGS='.["test"]'
#
# - name: Run tests on macOS
# run: |
# make test
macos-tests:
runs-on: macos-latest

strategy:
matrix:
python-version: [3.8, 3.12]

steps:
- name: Checkout
uses: actions/checkout@v4

- name: Set up Python ${{ matrix.python-version }}
uses: actions/setup-python@v5
with:
python-version: ${{ matrix.python-version }}

- name: Set up macOS dependencies
run: |
make install_brew
- name: Install Python dependencies
run: |
make install_pip PIP_ARGS='.["test"]'
- name: Run tests on macOS
run: |
export DYLD_LIBRARY_PATH=$(brew --prefix hidapi)/lib:$DYLD_LIBRARY_PATH && pytest --cov=lib/ tests/
2 changes: 1 addition & 1 deletion lib/hidapi/hidapi.py
Original file line number Diff line number Diff line change
Expand Up @@ -84,7 +84,7 @@
except OSError:
pass
else:
raise ImportError(f"Unable to load hdiapi library, tried: {' '.join(_library_paths)}")
raise ImportError(f"Unable to load hidapi library, tried: {' '.join(_library_paths)}")


# Retrieve version of hdiapi library
Expand Down
3 changes: 2 additions & 1 deletion lib/logitech_receiver/diversion.py
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,6 @@
from typing import Dict
from typing import Tuple

import dbus
import gi
import psutil

Expand Down Expand Up @@ -188,6 +187,8 @@ def gnome_dbus_interface_setup():
if _dbus_interface is not None:
return _dbus_interface
try:
import dbus

bus = dbus.SessionBus()
remote_object = bus.get_object("org.gnome.Shell", "/io/github/pwr_solaar/solaar")
_dbus_interface = dbus.Interface(remote_object, "io.github.pwr_solaar.solaar")
Expand Down
6 changes: 3 additions & 3 deletions lib/solaar/dbus.py
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@
except Exception:
# Either the dbus library is not available or the system dbus is not running
logger.warning("failed to set up dbus")
pass
bus = None


_suspend_callback = None
Expand All @@ -55,7 +55,7 @@ def watch_suspend_resume(on_resume_callback=None, on_suspend_callback=None):
global _resume_callback, _suspend_callback
_suspend_callback = on_suspend_callback
_resume_callback = on_resume_callback
if on_resume_callback is not None or on_suspend_callback is not None:
if bus is not None and on_resume_callback is not None or on_suspend_callback is not None:
bus.add_signal_receiver(_suspend_or_resume, "PrepareForSleep", dbus_interface=_LOGIND_INTERFACE, path=_LOGIND_PATH)
if logger.isEnabledFor(logging.INFO):
logger.info("connected to system dbus, watching for suspend/resume events")
Expand All @@ -71,7 +71,7 @@ def watch_bluez_connect(serial, callback=None):
if _bluetooth_callbacks.get(serial):
_bluetooth_callbacks.get(serial).remove()
path = _BLUETOOTH_PATH_PREFIX + serial.replace(":", "_").upper()
if callback is not None:
if bus is not None and callback is not None:
_bluetooth_callbacks[serial] = bus.add_signal_receiver(
callback, "PropertiesChanged", path=path, dbus_interface=_BLUETOOTH_INTERFACE
)
4 changes: 4 additions & 0 deletions tests/logitech_receiver/test_device.py
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@
## You should have received a copy of the GNU General Public License along
## with this program; if not, write to the Free Software Foundation, Inc.,
## 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
import platform

from dataclasses import dataclass
from functools import partial
Expand Down Expand Up @@ -96,6 +97,7 @@ def test_Device_name(device_info, responses, codename, name, kind, mock_base):
assert test_device.kind == kind


@pytest.mark.skipif(platform.system() == "Darwin", reason="Cleanup fails on macOS")
@pytest.mark.parametrize(
"device_info, responses, handle, _name, _codename, number, protocol, registers",
zip(
Expand Down Expand Up @@ -124,6 +126,7 @@ def test_Device_info(device_info, responses, handle, _name, _codename, number, p
assert test_device.registers == registers

assert bool(test_device)

test_device.__del__()
assert not bool(test_device)

Expand Down Expand Up @@ -155,6 +158,7 @@ def mock_hid():
pi_DDDD = {"wpid": "DDDD", "kind": 2, "serial": "1234", "polling": "2ms", "power_switch": "top"}


@pytest.mark.skipif(platform.system() == "Darwin", reason="Cleanup fails on macOS")
@pytest.mark.parametrize(
"number, pairing_info, responses, handle, _name, codename, p, p2, name",
zip(
Expand Down
6 changes: 6 additions & 0 deletions tests/logitech_receiver/test_receiver.py
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
import platform

from dataclasses import dataclass
from functools import partial
from unittest import mock
Expand Down Expand Up @@ -97,6 +99,7 @@ def mock_base():
c534_info = {"kind": common.NamedInt(0, "unknown"), "polling": "", "power_switch": "(unknown)", "serial": None, "wpid": "45AB"}


@pytest.mark.skipif(platform.system() == "Darwin", reason="Fails on macOS")
@pytest.mark.parametrize(
"device_info, responses, handle, serial, max_devices, ",
[
Expand Down Expand Up @@ -126,6 +129,7 @@ def test_ReceiverFactory_create_receiver(device_info, responses, handle, serial,
assert r.max_devices == max_devices


@pytest.mark.skipif(platform.system() == "Darwin", reason="Fails on macOS")
@pytest.mark.parametrize(
"device_info, responses, firmware, codename, remaining_pairings, pairing_info, count",
[
Expand All @@ -147,6 +151,7 @@ def test_ReceiverFactory_props(device_info, responses, firmware, codename, remai
assert r.count() == count


@pytest.mark.skipif(platform.system() == "Darwin", reason="Fails on macOS")
@pytest.mark.parametrize(
"device_info, responses, status_str, strng",
[
Expand All @@ -165,6 +170,7 @@ def test_ReceiverFactory_string(device_info, responses, status_str, strng, mock_
assert str(r) == strng


@pytest.mark.skipif(platform.system() == "Darwin", reason="Fails on macOS")
@pytest.mark.parametrize(
"device_info, responses",
[
Expand Down

0 comments on commit 500b999

Please sign in to comment.