add unit test for catchAndIgnore()

This commit is contained in:
Mike Kinney
2021-12-23 00:40:21 -08:00
parent 276b2762c8
commit cbd41efb19
3 changed files with 17 additions and 4 deletions

View File

@@ -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

View File

@@ -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)

View File

@@ -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: