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

A small offering of unit test coverage #595

Open
wants to merge 18 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
18 commits
Select commit Hold shift + click to select a range
5d11ca7
Increasing unit test line coverage a bit for some of the BLE classes;…
nerdenator Jun 14, 2024
3513d1b
Taking linter feedback into account.
nerdenator Jun 14, 2024
3ec7b96
Because GHA is hanging on the final test, I'm commenting it out, and …
nerdenator Jun 14, 2024
f0a7e93
Linting feedback
nerdenator Jun 14, 2024
15770cd
Trying to kick off new build to test things again.
nerdenator Jun 18, 2024
a348a70
Hanging test job again, so eliminating another test to see if it's th…
nerdenator Jun 18, 2024
fc55e79
Fixing lint problems
nerdenator Jun 18, 2024
4842189
More diagnosing of problems with CI/CD
nerdenator Jun 18, 2024
473401a
ble_code_coverage_1: hanging on first test was due to an open client.…
nerdenator Jun 26, 2024
340a9c2
ble_code_coverage_1: trying to suss out the type of error being throw…
nerdenator Jun 26, 2024
445c03a
ble_code_coverage_1: linting
nerdenator Jun 26, 2024
5dad15c
ble_code_coverage_1: additional test cases for _sanitize_address
nerdenator Jun 26, 2024
1bb9128
ble_code_coverage_1: linting
nerdenator Jun 26, 2024
8f0af0d
ble_code_coverage_1: At a stopping point, need to understand the unde…
nerdenator Jun 26, 2024
387320f
Merge branch 'master' into ble_code_coverage_1
nerdenator Jul 8, 2024
2689110
ble_code_coverage_1: adding Linux tests for several new lines; removi…
nerdenator Jul 9, 2024
2f812cd
Merge remote-tracking branch 'upstream/master' into ble_code_coverage_1
nerdenator Jul 15, 2024
d02292a
ble_code_coverage_1: more attempts to get the remaining lines of code…
nerdenator Jul 15, 2024
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
9 changes: 6 additions & 3 deletions meshtastic/ble_interface.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
import asyncio
import atexit
import logging
import platform
import struct
import time
from threading import Thread
Expand Down Expand Up @@ -79,7 +80,8 @@ def __init__(
# We MUST run atexit (if we can) because otherwise (at least on linux) the BLE device is not disconnected
# and future connection attempts will fail. (BlueZ kinda sucks)
# Note: the on disconnected callback will call our self.close which will make us nicely wait for threads to exit
self._exit_handler = atexit.register(self.client.disconnect)
if platform.system() == "Linux":
self._exit_handler = atexit.register(self.client.disconnect)

def from_num_handler(self, _, b): # pylint: disable=C0116
"""Handle callbacks for fromnum notify.
Expand Down Expand Up @@ -207,7 +209,8 @@ def _sendToRadioImpl(self, toRadio):
self.should_read = True

def close(self):
atexit.unregister(self._exit_handler)
if platform.system() == "Linux":
atexit.unregister(self._exit_handler)
try:
MeshInterface.close(self)
except Exception as e:
Expand All @@ -218,7 +221,7 @@ def close(self):
self._receiveThread.join(timeout=2) # If bleak is hung, don't wait for the thread to exit (it is critical we disconnect)
self._receiveThread = None

if self.client:
if hasattr(self, "client"):
self.client.disconnect()
self.client.close()
self.client = None
Expand Down
64 changes: 64 additions & 0 deletions meshtastic/tests/test_ble_interface.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,64 @@
"""Meshtastic unit tests for ble_interface.py"""
import logging
import os
from unittest.mock import patch

import pytest

from meshtastic.ble_interface import BLEClient, BLEInterface


def test_ble_client_no_addr_logs_message(caplog):
"""
We want to see a debug message describing the error
if we try to initialize a BLEClient with no address.
"""
caplog.set_level(level=logging.DEBUG)
test_ble_client = BLEClient(address=None)
test_ble_client.close()
assert "No address provided - only discover method will work." in caplog.text

def test_ble_interface_sanitize_address_returns_lower():
"""
_sanitize_address should only return lower case letters
"""
assert BLEInterface._sanitize_address("HELLO") == "hello"

def test_ble_interface_sanitize_address_returns_no_underscores():
"""
_sanitize_address should only return strings without underscores
"""
assert BLEInterface._sanitize_address("hello_world") == "helloworld"

def test_ble_interface_sanitize_address_returns_no_dash():
"""
_sanitize_address should only return strings without dashes
"""
assert BLEInterface._sanitize_address("hello-world") == "helloworld"

def test_ble_interface_sanitize_address_returns_no_colon():
"""
_sanitize_address should only return strings without colons
"""
assert BLEInterface._sanitize_address("hello:world") == "helloworld"

def test_linux_exit_handler():
"""
Given a platform.system of Linux (or as I like to call it, Ganoo plus Linux),
we should register an exit handler.
"""
with patch("platform.system") as fake_platform:
fake_platform.return_value = "Linux"
test_interface = BLEInterface(address="")
assert test_interface._exit_handler is not None


@pytest.mark.skipif(os.environ.get("CI") == "true", reason="Bluetooth tests are not supported in CI environment")
def test_ble_interface_bogus_addr_exits_process():
"""
If we initialize BLEInterface with a BT address that doesn't
exist, we should exit the process
"""
with pytest.raises(BLEInterface.BLEError) as exc:
BLEInterface(address="bogus")
assert "No Meshtastic BLE peripheral with identifier or address 'bogus' found" in exc.value.args[0]
4 changes: 2 additions & 2 deletions poetry.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 1 addition & 0 deletions pyproject.toml
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@ ppk2-api = "^0.9.2"
pyarrow = "^16.1.0"
platformdirs = "^4.2.2"
print-color = "^0.4.6"
dbus-fast = "^2.22.1"

[tool.poetry.group.dev.dependencies]
hypothesis = "^6.103.2"
Expand Down
Loading