From cbd41efb19617239cb5124c43f21cfc057d0e1b9 Mon Sep 17 00:00:00 2001 From: Mike Kinney Date: Thu, 23 Dec 2021 00:40:21 -0800 Subject: [PATCH] add unit test for catchAndIgnore() --- meshtastic/tests/test_stream_interface.py | 2 +- meshtastic/tests/test_util.py | 17 +++++++++++++++-- meshtastic/util.py | 2 +- 3 files changed, 17 insertions(+), 4 deletions(-) diff --git a/meshtastic/tests/test_stream_interface.py b/meshtastic/tests/test_stream_interface.py index 57c65d2..a3e400c 100644 --- a/meshtastic/tests/test_stream_interface.py +++ b/meshtastic/tests/test_stream_interface.py @@ -8,7 +8,7 @@ from ..stream_interface import StreamInterface @pytest.mark.unit def test_StreamInterface(): - """Test that we can instantiate a StreamInterface""" + """Test that we cannot instantiate a StreamInterface""" with pytest.raises(Exception) as pytest_wrapped_e: StreamInterface(noProto=True) assert pytest_wrapped_e.type == Exception diff --git a/meshtastic/tests/test_util.py b/meshtastic/tests/test_util.py index b3c6d39..6b9e94b 100644 --- a/meshtastic/tests/test_util.py +++ b/meshtastic/tests/test_util.py @@ -1,10 +1,13 @@ """Meshtastic unit tests for util.py""" import re +import logging import pytest -from meshtastic.util import fixme, stripnl, pskToString, our_exit, support_info, genPSK256, fromStr, fromPSK, quoteBooleans +from meshtastic.util import (fixme, stripnl, pskToString, our_exit, + support_info, genPSK256, fromStr, fromPSK, + quoteBooleans, catchAndIgnore) @pytest.mark.unit @@ -120,7 +123,7 @@ def test_our_exit_non_zero_return_value(): @pytest.mark.unit def test_fixme(): - """Test fixme""" + """Test fixme()""" with pytest.raises(Exception) as pytest_wrapped_e: fixme("some exception") assert pytest_wrapped_e.type == Exception @@ -136,3 +139,13 @@ def test_support_info(capsys): assert re.search(r'Machine', out, re.MULTILINE) assert re.search(r'Executable', out, re.MULTILINE) assert err == '' + + +@pytest.mark.unit +def test_catchAndIgnore(caplog): + """Test catchAndIgnore() does not actually throw an exception, but just logs""" + def some_closure(): + raise Exception('foo') + with caplog.at_level(logging.DEBUG): + catchAndIgnore("something", some_closure) + assert re.search(r'Exception thrown in something', caplog.text, re.MULTILINE) diff --git a/meshtastic/util.py b/meshtastic/util.py index b4376f9..9f9cba8 100644 --- a/meshtastic/util.py +++ b/meshtastic/util.py @@ -102,7 +102,7 @@ def fixme(message): def catchAndIgnore(reason, closure): - """Call a closure but if it throws an excpetion print it and continue""" + """Call a closure but if it throws an exception print it and continue""" try: closure() except BaseException as ex: