From 83439679c1d863508ac9031cdcfd01fb5622d917 Mon Sep 17 00:00:00 2001 From: Mike Kinney Date: Sat, 15 Jan 2022 14:15:55 -0800 Subject: [PATCH] looks like we needed some of that dead code after all --- meshtastic/__main__.py | 8 ++++++-- meshtastic/tests/test_main.py | 21 +++++++++++++++++++++ meshtastic/tests/test_util.py | 1 + 3 files changed, 28 insertions(+), 2 deletions(-) diff --git a/meshtastic/__main__.py b/meshtastic/__main__.py index 042b3aa..76fb0f8 100644 --- a/meshtastic/__main__.py +++ b/meshtastic/__main__.py @@ -117,6 +117,7 @@ def setPref(attributes, name, valStr): return val = meshtastic.util.fromStr(valStr) + logging.debug(f'valStr:{valStr} val:{val}') enumType = field.enum_type # pylint: disable=C0123 @@ -140,8 +141,11 @@ def setPref(attributes, name, valStr): for temp_name in sorted(names): print(f" {temp_name}") return - - setattr(attributes, snake_name, val) + try: + setattr(attributes, snake_name, val) + except TypeError: + # The setter didn't like our arg type guess try again as a string + setattr(attributes, snake_name, valStr) if Globals.getInstance().get_camel_case(): print(f"Set {camel_name} to {valStr}") diff --git a/meshtastic/tests/test_main.py b/meshtastic/tests/test_main.py index 4521a72..91263f1 100644 --- a/meshtastic/tests/test_main.py +++ b/meshtastic/tests/test_main.py @@ -803,6 +803,27 @@ def test_main_set_valid(capsys): mo.assert_called() +@pytest.mark.unit +@pytest.mark.usefixtures("reset_globals") +def test_main_set_valid_wifi_passwd(capsys): + """Test --set with valid field""" + sys.argv = ['', '--set', 'wifi_password', '123456789'] + Globals.getInstance().set_args(sys.argv) + + mocked_node = MagicMock(autospec=Node) + + iface = MagicMock(autospec=SerialInterface) + iface.getNode.return_value = mocked_node + + with patch('meshtastic.serial_interface.SerialInterface', return_value=iface) as mo: + main() + out, err = capsys.readouterr() + assert re.search(r'Connected to radio', out, re.MULTILINE) + assert re.search(r'Set wifi_password to 123456789', out, re.MULTILINE) + assert err == '' + mo.assert_called() + + @pytest.mark.unit @pytest.mark.usefixtures("reset_globals") def test_main_set_valid_camel_case(capsys): diff --git a/meshtastic/tests/test_util.py b/meshtastic/tests/test_util.py index 515fc76..e6c716c 100644 --- a/meshtastic/tests/test_util.py +++ b/meshtastic/tests/test_util.py @@ -40,6 +40,7 @@ def test_fromStr(): assert fromStr('100.01') == 100.01 assert fromStr('123') == 123 assert fromStr('abc') == 'abc' + assert fromStr('123456789') == 123456789 @pytest.mark.unitslow