From 067cddd3542e94fc746a56f1482b294201d7620c Mon Sep 17 00:00:00 2001 From: Steve Holden Date: Wed, 10 Apr 2024 09:56:38 +0100 Subject: [PATCH 1/3] Refactor to avoid the use of a special global object. The global object formerly used is now replaced by direct use of the namespace opf the globals module. This eliminates the redundant getters and setters and simplifies the code for future maintainers. Note that the globals module name conflicts (harmlessly at present) with a Python built-in function. A future commit should rename it `config` to remove this clash and better represent its intended purpose. --- meshtastic/__main__.py | 73 ++++++------ meshtastic/globals.py | 114 ++++-------------- meshtastic/tests/conftest.py | 6 +- meshtastic/tests/test_globals.py | 25 ---- meshtastic/tests/test_init.py | 9 +- meshtastic/tests/test_main.py | 192 +++++++++++++++---------------- meshtastic/tests/test_tunnel.py | 13 +-- meshtastic/tunnel.py | 9 +- 8 files changed, 168 insertions(+), 273 deletions(-) delete mode 100644 meshtastic/tests/test_globals.py diff --git a/meshtastic/__main__.py b/meshtastic/__main__.py index 3c0b20b9..ea3dcb43 100644 --- a/meshtastic/__main__.py +++ b/meshtastic/__main__.py @@ -16,16 +16,14 @@ import meshtastic.test import meshtastic.util +from meshtastic import globals from meshtastic import channel_pb2, config_pb2, portnums_pb2, remote_hardware, BROADCAST_ADDR from meshtastic.version import get_active_version from meshtastic.ble_interface import BLEInterface -from meshtastic.globals import Globals - def onReceive(packet, interface): """Callback invoked when a packet arrives""" - our_globals = Globals.getInstance() - args = our_globals.get_args() + args = globals.args try: d = packet.get("decoded") logging.debug(f"in onReceive() d:{d}") @@ -69,7 +67,7 @@ def getPref(node, comp_name): # Note: protobufs has the keys in snake_case, so snake internally snake_name = meshtastic.util.camel_to_snake(name[1]) logging.debug(f"snake_name:{snake_name} camel_name:{camel_name}") - logging.debug(f"use camel:{Globals.getInstance().get_camel_case()}") + logging.debug(f"use camel:{globals.camel_case}") # First validate the input localConfig = node.localConfig @@ -86,7 +84,7 @@ def getPref(node, comp_name): break if not found: - if Globals.getInstance().get_camel_case(): + if globals.camel_case: print( f"{localConfig.__class__.__name__} and {moduleConfig.__class__.__name__} do not have an attribute {snake_name}." ) @@ -105,7 +103,7 @@ def getPref(node, comp_name): config_values = getattr(config, config_type.name) if not wholeField: pref_value = getattr(config_values, pref.name) - if Globals.getInstance().get_camel_case(): + if globals.camel_case: print(f"{str(config_type.name)}.{camel_name}: {str(pref_value)}") logging.debug( f"{str(config_type.name)}.{camel_name}: {str(pref_value)}" @@ -171,7 +169,7 @@ def setPref(config, comp_name, valStr) -> bool: if e: val = e.number else: - if Globals.getInstance().get_camel_case(): + if globals.camel_case: print( f"{name[0]}.{camel_name} does not have an enum called {val}, so you can not set it." ) @@ -210,7 +208,7 @@ def setPref(config, comp_name, valStr) -> bool: config_type.message_type.ignore_incoming.extend([val]) prefix = f"{name[0]}." if config_type.message_type is not None else "" - if Globals.getInstance().get_camel_case(): + if globals.camel_case: print(f"Set {prefix}{camel_name} to {valStr}") else: print(f"Set {prefix}{snake_name} to {valStr}") @@ -225,8 +223,7 @@ def onConnected(interface): False # Should we wait for an acknowledgment if we send to a remote node? ) try: - our_globals = Globals.getInstance() - args = our_globals.get_args() + args = globals.args # do not print this line if we are exporting the config if not args.export_config: @@ -477,7 +474,7 @@ def onConnected(interface): print("Writing modified preferences to device") node.writeConfig(field) else: - if Globals.getInstance().get_camel_case(): + if globals.camel_case: print( f"{node.localConfig.__class__.__name__} and {node.moduleConfig.__class__.__name__} do not have an attribute {pref[0]}." ) @@ -590,7 +587,7 @@ def onConnected(interface): # handle changing channels if args.ch_add: - channelIndex = our_globals.get_channel_index() + channelIndex = globals.channel_index if channelIndex is not None: # Since we set the channel index after adding a channel, don't allow --ch-index meshtastic.util.our_exit( @@ -621,12 +618,12 @@ def onConnected(interface): n.writeChannel(ch.index) if channelIndex is None: print(f"Setting newly-added channel's {ch.index} as '--ch-index' for further modifications") - our_globals.set_channel_index(ch.index) + globals.channel_index = ch.index if args.ch_del: closeNow = True - channelIndex = our_globals.get_channel_index() + channelIndex = globals.channel_index if channelIndex is None: meshtastic.util.our_exit( "Warning: Need to specify '--ch-index' for '--ch-del'.", 1 @@ -642,7 +639,7 @@ def onConnected(interface): def setSimpleConfig(modem_preset): """Set one of the simple modem_config""" - channelIndex = our_globals.get_channel_index() + channelIndex = globals.channel_index if channelIndex is not None and channelIndex > 0: meshtastic.util.our_exit( "Warning: Cannot set modem preset for non-primary channel", 1 @@ -677,7 +674,7 @@ def setSimpleConfig(modem_preset): if args.ch_set or args.ch_enable or args.ch_disable: closeNow = True - channelIndex = our_globals.get_channel_index() + channelIndex = globals.channel_index if channelIndex is None: meshtastic.util.our_exit("Warning: Need to specify '--ch-index'.", 1) ch = interface.getNode(args.dest).channels[channelIndex] @@ -832,7 +829,7 @@ def printConfig(config): names = [] for field in config.message_type.fields: tmp_name = f"{config_section.name}.{field.name}" - if Globals.getInstance().get_camel_case(): + if globals.camel_case: tmp_name = meshtastic.util.snake_to_camel(tmp_name) names.append(tmp_name) for temp_name in sorted(names): @@ -877,7 +874,7 @@ def export_config(interface): if owner_short: configObj["owner_short"] = owner_short if channel_url: - if Globals.getInstance().get_camel_case(): + if globals.camel_case: configObj["channelUrl"] = channel_url else: configObj["channel_url"] = channel_url @@ -889,11 +886,11 @@ def export_config(interface): # Convert inner keys to correct snake/camelCase prefs = {} for pref in config: - if Globals.getInstance().get_camel_case(): + if globals.camel_case: prefs[meshtastic.util.snake_to_camel(pref)] = config[pref] else: prefs[pref] = config[pref] - if Globals.getInstance().get_camel_case(): + if globals.camel_case: configObj["config"] = config else: configObj["config"] = config @@ -905,7 +902,7 @@ def export_config(interface): for pref in module_config: if len(module_config[pref]) > 0: prefs[pref] = module_config[pref] - if Globals.getInstance().get_camel_case(): + if globals.camel_case: configObj["module_config"] = prefs else: configObj["module_config"] = prefs @@ -919,9 +916,8 @@ def export_config(interface): def common(): """Shared code for all of our command line wrappers""" logfile = None - our_globals = Globals.getInstance() - args = our_globals.get_args() - parser = our_globals.get_parser() + args = globals.args + parser = globals.parser logging.basicConfig( level=logging.DEBUG if (args.debug or args.listen) else logging.INFO, format="%(levelname)s file:%(filename)s %(funcName)s line:%(lineno)s %(message)s", @@ -937,7 +933,7 @@ def common(): if args.ch_index is not None: channelIndex = int(args.ch_index) - our_globals.set_channel_index(channelIndex) + globals.channel_index = channelIndex if not args.dest: args.dest = BROADCAST_ADDR @@ -972,7 +968,7 @@ def common(): # Note: using "line buffering" # pylint: disable=R1732 logfile = open(args.seriallog, "w+", buffering=1, encoding="utf8") - our_globals.set_logfile(logfile) + globals.logfile = logfile subscribe() if args.ble_scan: @@ -1063,9 +1059,8 @@ def addConnectionArgs(parser: argparse.ArgumentParser) -> argparse.ArgumentParse def initParser(): """Initialize the command line argument parsing.""" - our_globals = Globals.getInstance() - parser = our_globals.get_parser() - args = our_globals.get_args() + parser = globals.parser + args = globals.args # The "Help" group includes the help option and other informational stuff about the CLI itself outerHelpGroup = parser.add_argument_group('Help') @@ -1286,7 +1281,7 @@ def initParser(): ) group.add_argument( - "--request-telemetry", + "--request-telemetry", help="Request telemetry from a node. " "You need pass the destination ID as argument with '--dest'. " "For repeaters, the nodeNum is required.", @@ -1431,34 +1426,32 @@ def initParser(): args = parser.parse_args() - our_globals.set_args(args) - our_globals.set_parser(parser) + globals.args = args + globals.parser = parser def main(): """Perform command line meshtastic operations""" - our_globals = Globals.getInstance() parser = argparse.ArgumentParser( add_help=False, epilog="If no connection arguments are specified, we search for a compatible serial device, " "and if none is found, then attempt a TCP connection to localhost.") - our_globals.set_parser(parser) + globals.parser = parser initParser() common() - logfile = our_globals.get_logfile() + logfile = globals.logfile if logfile: logfile.close() def tunnelMain(): """Run a meshtastic IP tunnel""" - our_globals = Globals.getInstance() parser = argparse.ArgumentParser(add_help=False) - our_globals.set_parser(parser) + globals.parser = parser initParser() - args = our_globals.get_args() + args = globals.args args.tunnel = True - our_globals.set_args(args) + globals.args = args common() diff --git a/meshtastic/globals.py b/meshtastic/globals.py index 0673de6c..fb379c1d 100644 --- a/meshtastic/globals.py +++ b/meshtastic/globals.py @@ -1,96 +1,28 @@ -"""Globals singleton class. - - Instead of using a global, stuff your variables in this "trash can". - This is not much better than using python's globals, but it allows - us to better test meshtastic. Plus, there are some weird python - global issues/gotcha that we can hopefully avoid by using this - class instead. - """ +Globals singleton class. +The Global object is gone, as are all its setters and getters. Instead the +module itself is the singleton namespace, which can be imported into +whichever module is used. The associated tests have also been removed, +since we now rely on built in Python mechanisms. -class Globals: - """Globals class is a Singleton.""" - - __instance = None - - @staticmethod - def getInstance(): - """Get an instance of the Globals class.""" - if Globals.__instance is None: - Globals() - return Globals.__instance - - def __init__(self): - """Constructor for the Globals CLass""" - if Globals.__instance is not None: - raise Exception("This class is a singleton") # pylint: disable=W0719 - else: - Globals.__instance = self - self.args = None - self.parser = None - self.channel_index = None - self.logfile = None - self.tunnelInstance = None - # TODO: to migrate to camel_case for v1.3 change this value to True - self.camel_case = False - - def reset(self): - """Reset all of our globals. If you add a member, add it to this method, too.""" - self.args = None - self.parser = None - self.channel_index = None - self.logfile = None - self.tunnelInstance = None - # TODO: to migrate to camel_case for v1.3 change this value to True - self.camel_case = False - - # setters - def set_args(self, args): - """Set the args""" - self.args = args - - def set_parser(self, parser): - """Set the parser""" - self.parser = parser - - def set_channel_index(self, channel_index): - """Set the channel_index""" - self.channel_index = channel_index +This is intended to make the Python read more naturally, and to make the +intention of the code clearer and more compact. It is merely a sticking +plaster over the use of shared globals, but the coupling issues wil be dealt +with rather more easily once the code is simplified by this change. - def set_logfile(self, logfile): - """Set the logfile""" - self.logfile = logfile - - def set_tunnelInstance(self, tunnelInstance): - """Set the tunnelInstance""" - self.tunnelInstance = tunnelInstance - - def set_camel_case(self): - """Force using camelCase for things like prefs/set/set""" - self.camel_case = True - - # getters - def get_args(self): - """Get args""" - return self.args - - def get_parser(self): - """Get parser""" - return self.parser - - def get_channel_index(self): - """Get channel_index""" - return self.channel_index - - def get_logfile(self): - """Get logfile""" - return self.logfile - - def get_tunnelInstance(self): - """Get tunnelInstance""" - return self.tunnelInstance +""" - def get_camel_case(self): - """Get whether or not to use camelCase""" - return self.camel_case +def reset(): + """ + """ + global args, parser, channel_index, logfile, tunnelInstance, camel_case + args = None + parser = None + channel_index = None + logfile = None + tunnelInstance = None + # TODO: to migrate to camel_case for v1.3 change this value to True + camel_case = False + +reset() \ No newline at end of file diff --git a/meshtastic/tests/conftest.py b/meshtastic/tests/conftest.py index 4ff2412b..f10cefcc 100644 --- a/meshtastic/tests/conftest.py +++ b/meshtastic/tests/conftest.py @@ -5,7 +5,7 @@ import pytest -from meshtastic.__main__ import Globals +from meshtastic import globals from ..mesh_interface import MeshInterface @@ -15,8 +15,8 @@ def reset_globals(): """Fixture to reset globals.""" parser = None parser = argparse.ArgumentParser(add_help=False) - Globals.getInstance().reset() - Globals.getInstance().set_parser(parser) + globals.reset() + globals.parser = parser @pytest.fixture diff --git a/meshtastic/tests/test_globals.py b/meshtastic/tests/test_globals.py deleted file mode 100644 index da9d08b4..00000000 --- a/meshtastic/tests/test_globals.py +++ /dev/null @@ -1,25 +0,0 @@ -"""Meshtastic unit tests for globals.py -""" - -import pytest - -from ..globals import Globals - - -@pytest.mark.unit -def test_globals_get_instaance(): - """Test that we can instantiate a Globals instance""" - ourglobals = Globals.getInstance() - ourglobals2 = Globals.getInstance() - assert ourglobals == ourglobals2 - - -@pytest.mark.unit -def test_globals_there_can_be_only_one(): - """Test that we can cannot create two Globals instances""" - # if we have an instance, delete it - Globals.getInstance() - with pytest.raises(Exception) as pytest_wrapped_e: - # try to create another instance - Globals() - assert pytest_wrapped_e.type == Exception diff --git a/meshtastic/tests/test_init.py b/meshtastic/tests/test_init.py index c69f0a16..10604d23 100644 --- a/meshtastic/tests/test_init.py +++ b/meshtastic/tests/test_init.py @@ -6,9 +6,8 @@ import pytest -from meshtastic import _onNodeInfoReceive, _onPositionReceive, _onTextReceive +from meshtastic import _onNodeInfoReceive, _onPositionReceive, _onTextReceive, globals -from ..globals import Globals from ..serial_interface import SerialInterface @@ -16,7 +15,7 @@ def test_init_onTextReceive_with_exception(caplog): """Test _onTextReceive""" args = MagicMock() - Globals.getInstance().set_args(args) + globals.args = args iface = MagicMock(autospec=SerialInterface) packet = {} with caplog.at_level(logging.DEBUG): @@ -29,7 +28,7 @@ def test_init_onTextReceive_with_exception(caplog): def test_init_onPositionReceive(caplog): """Test _onPositionReceive""" args = MagicMock() - Globals.getInstance().set_args(args) + globals.args = args iface = MagicMock(autospec=SerialInterface) packet = {"from": "foo", "decoded": {"position": {}}} with caplog.at_level(logging.DEBUG): @@ -41,7 +40,7 @@ def test_init_onPositionReceive(caplog): def test_init_onNodeInfoReceive(caplog, iface_with_nodes): """Test _onNodeInfoReceive""" args = MagicMock() - Globals.getInstance().set_args(args) + globals.args = args iface = iface_with_nodes iface.myInfo.my_node_num = 2475227164 packet = { diff --git a/meshtastic/tests/test_main.py b/meshtastic/tests/test_main.py index 626c58a7..9cc3a5a5 100644 --- a/meshtastic/tests/test_main.py +++ b/meshtastic/tests/test_main.py @@ -11,7 +11,6 @@ import pytest from meshtastic.__main__ import ( - Globals, export_config, initParser, main, @@ -20,6 +19,7 @@ onReceive, tunnelMain, ) +from meshtastic import globals from ..channel_pb2 import Channel # pylint: disable=E0611 @@ -40,7 +40,7 @@ def test_main_init_parser_no_args(capsys): """Test no arguments""" sys.argv = [""] - Globals.getInstance().set_args(sys.argv) + globals.args = sys.argv initParser() out, err = capsys.readouterr() assert out == "" @@ -52,7 +52,7 @@ def test_main_init_parser_no_args(capsys): def test_main_init_parser_version(capsys): """Test --version""" sys.argv = ["", "--version"] - Globals.getInstance().set_args(sys.argv) + globals.args = sys.argv with pytest.raises(SystemExit) as pytest_wrapped_e: initParser() @@ -68,7 +68,7 @@ def test_main_init_parser_version(capsys): def test_main_main_version(capsys): """Test --version""" sys.argv = ["", "--version"] - Globals.getInstance().set_args(sys.argv) + globals.args = sys.argv with pytest.raises(SystemExit) as pytest_wrapped_e: main() @@ -84,7 +84,7 @@ def test_main_main_version(capsys): def test_main_main_no_args(capsys): """Test with no args""" sys.argv = [""] - Globals.getInstance().set_args(sys.argv) + globals.args = sys.argv with pytest.raises(SystemExit) as pytest_wrapped_e: main() @@ -99,7 +99,7 @@ def test_main_main_no_args(capsys): def test_main_support(capsys): """Test --support""" sys.argv = ["", "--support"] - Globals.getInstance().set_args(sys.argv) + globals.args = sys.argv with pytest.raises(SystemExit) as pytest_wrapped_e: main() @@ -119,11 +119,11 @@ def test_main_support(capsys): def test_main_ch_index_no_devices(patched_find_ports, capsys): """Test --ch-index 1""" sys.argv = ["", "--ch-index", "1"] - Globals.getInstance().set_args(sys.argv) + globals.args = sys.argv with pytest.raises(SystemExit) as pytest_wrapped_e: main() - assert Globals.getInstance().get_channel_index() == 1 + assert globals.channel_index == 1 assert pytest_wrapped_e.type == SystemExit assert pytest_wrapped_e.value.code == 1 out, err = capsys.readouterr() @@ -138,7 +138,7 @@ def test_main_ch_index_no_devices(patched_find_ports, capsys): def test_main_test_no_ports(patched_find_ports, capsys): """Test --test with no hardware""" sys.argv = ["", "--test"] - Globals.getInstance().set_args(sys.argv) + globals.args = sys.argv with pytest.raises(SystemExit) as pytest_wrapped_e: main() @@ -158,7 +158,7 @@ def test_main_test_no_ports(patched_find_ports, capsys): def test_main_test_one_port(patched_find_ports, capsys): """Test --test with one fake port""" sys.argv = ["", "--test"] - Globals.getInstance().set_args(sys.argv) + globals.args = sys.argv with pytest.raises(SystemExit) as pytest_wrapped_e: main() @@ -178,7 +178,7 @@ def test_main_test_one_port(patched_find_ports, capsys): def test_main_test_two_ports_success(patched_test_all, capsys): """Test --test two fake ports and testAll() is a simulated success""" sys.argv = ["", "--test"] - Globals.getInstance().set_args(sys.argv) + globals.args = sys.argv with pytest.raises(SystemExit) as pytest_wrapped_e: main() @@ -196,7 +196,7 @@ def test_main_test_two_ports_success(patched_test_all, capsys): def test_main_test_two_ports_fails(patched_test_all, capsys): """Test --test two fake ports and testAll() is a simulated failure""" sys.argv = ["", "--test"] - Globals.getInstance().set_args(sys.argv) + globals.args = sys.argv with pytest.raises(SystemExit) as pytest_wrapped_e: main() @@ -213,7 +213,7 @@ def test_main_test_two_ports_fails(patched_test_all, capsys): def test_main_info(capsys, caplog): """Test --info""" sys.argv = ["", "--info"] - Globals.getInstance().set_args(sys.argv) + globals.args = sys.argv iface = MagicMock(autospec=SerialInterface) @@ -239,7 +239,7 @@ def mock_showInfo(): def test_main_info_with_permission_error(patched_getlogin, capsys, caplog): """Test --info""" sys.argv = ["", "--info"] - Globals.getInstance().set_args(sys.argv) + globals.args = sys.argv patched_getlogin.return_value = "me" @@ -264,7 +264,7 @@ def test_main_info_with_permission_error(patched_getlogin, capsys, caplog): def test_main_info_with_tcp_interface(capsys): """Test --info""" sys.argv = ["", "--info", "--host", "meshtastic.local"] - Globals.getInstance().set_args(sys.argv) + globals.args = sys.argv iface = MagicMock(autospec=TCPInterface) @@ -286,7 +286,7 @@ def mock_showInfo(): def test_main_no_proto(capsys): """Test --noproto (using --info for output)""" sys.argv = ["", "--info", "--noproto"] - Globals.getInstance().set_args(sys.argv) + globals.args = sys.argv iface = MagicMock(autospec=SerialInterface) @@ -317,7 +317,7 @@ def my_sleep(amount): def test_main_info_with_seriallog_stdout(capsys): """Test --info""" sys.argv = ["", "--info", "--seriallog", "stdout"] - Globals.getInstance().set_args(sys.argv) + globals.args = sys.argv iface = MagicMock(autospec=SerialInterface) @@ -339,7 +339,7 @@ def mock_showInfo(): def test_main_info_with_seriallog_output_txt(capsys): """Test --info""" sys.argv = ["", "--info", "--seriallog", "output.txt"] - Globals.getInstance().set_args(sys.argv) + globals.args = sys.argv iface = MagicMock(autospec=SerialInterface) @@ -363,7 +363,7 @@ def mock_showInfo(): def test_main_qr(capsys): """Test --qr""" sys.argv = ["", "--qr"] - Globals.getInstance().set_args(sys.argv) + globals.args = sys.argv iface = MagicMock(autospec=SerialInterface) # TODO: could mock/check url @@ -383,7 +383,7 @@ def test_main_qr(capsys): def test_main_onConnected_exception(capsys): """Test the exception in onConnected""" sys.argv = ["", "--qr"] - Globals.getInstance().set_args(sys.argv) + globals.args = sys.argv def throw_an_exception(junk): raise Exception("Fake exception.") # pylint: disable=W0719 @@ -404,7 +404,7 @@ def throw_an_exception(junk): def test_main_nodes(capsys): """Test --nodes""" sys.argv = ["", "--nodes"] - Globals.getInstance().set_args(sys.argv) + globals.args = sys.argv iface = MagicMock(autospec=SerialInterface) @@ -426,7 +426,7 @@ def mock_showNodes(): def test_main_set_owner_to_bob(capsys): """Test --set-owner bob""" sys.argv = ["", "--set-owner", "bob"] - Globals.getInstance().set_args(sys.argv) + globals.args = sys.argv iface = MagicMock(autospec=SerialInterface) with patch("meshtastic.serial_interface.SerialInterface", return_value=iface) as mo: @@ -443,7 +443,7 @@ def test_main_set_owner_to_bob(capsys): def test_main_set_owner_short_to_bob(capsys): """Test --set-owner-short bob""" sys.argv = ["", "--set-owner-short", "bob"] - Globals.getInstance().set_args(sys.argv) + globals.args = sys.argv iface = MagicMock(autospec=SerialInterface) with patch("meshtastic.serial_interface.SerialInterface", return_value=iface) as mo: @@ -460,7 +460,7 @@ def test_main_set_owner_short_to_bob(capsys): def test_main_set_canned_messages(capsys): """Test --set-canned-message""" sys.argv = ["", "--set-canned-message", "foo"] - Globals.getInstance().set_args(sys.argv) + globals.args = sys.argv iface = MagicMock(autospec=SerialInterface) with patch("meshtastic.serial_interface.SerialInterface", return_value=iface) as mo: @@ -477,7 +477,7 @@ def test_main_set_canned_messages(capsys): def test_main_get_canned_messages(capsys, caplog, iface_with_nodes): """Test --get-canned-message""" sys.argv = ["", "--get-canned-message"] - Globals.getInstance().set_args(sys.argv) + globals.args = sys.argv iface = iface_with_nodes iface.localNode.cannedPluginMessage = "foo" @@ -500,7 +500,7 @@ def test_main_get_canned_messages(capsys, caplog, iface_with_nodes): def test_main_set_ham_to_KI123(capsys): """Test --set-ham KI123""" sys.argv = ["", "--set-ham", "KI123"] - Globals.getInstance().set_args(sys.argv) + globals.args = sys.argv mocked_node = MagicMock(autospec=Node) @@ -536,7 +536,7 @@ def mock_setOwner(name, is_licensed): def test_main_reboot(capsys): """Test --reboot""" sys.argv = ["", "--reboot"] - Globals.getInstance().set_args(sys.argv) + globals.args = sys.argv mocked_node = MagicMock(autospec=Node) @@ -562,7 +562,7 @@ def mock_reboot(): def test_main_shutdown(capsys): """Test --shutdown""" sys.argv = ["", "--shutdown"] - Globals.getInstance().set_args(sys.argv) + globals.args = sys.argv mocked_node = MagicMock(autospec=Node) @@ -588,7 +588,7 @@ def mock_shutdown(): def test_main_sendtext(capsys): """Test --sendtext""" sys.argv = ["", "--sendtext", "hello"] - Globals.getInstance().set_args(sys.argv) + globals.args = sys.argv iface = MagicMock(autospec=SerialInterface) @@ -615,7 +615,7 @@ def mock_sendText( def test_main_sendtext_with_channel(capsys): """Test --sendtext""" sys.argv = ["", "--sendtext", "hello", "--ch-index", "1"] - Globals.getInstance().set_args(sys.argv) + globals.args = sys.argv iface = MagicMock(autospec=SerialInterface) @@ -643,7 +643,7 @@ def mock_sendText( def test_main_sendtext_with_invalid_channel(caplog, capsys): """Test --sendtext""" sys.argv = ["", "--sendtext", "hello", "--ch-index", "-1"] - Globals.getInstance().set_args(sys.argv) + globals.args = sys.argv iface = MagicMock(autospec=SerialInterface) iface.localNode.getChannelByChannelIndex.return_value = None @@ -667,7 +667,7 @@ def test_main_sendtext_with_invalid_channel(caplog, capsys): def test_main_sendtext_with_invalid_channel_nine(caplog, capsys): """Test --sendtext""" sys.argv = ["", "--sendtext", "hello", "--ch-index", "9"] - Globals.getInstance().set_args(sys.argv) + globals.args = sys.argv iface = MagicMock(autospec=SerialInterface) iface.localNode.getChannelByChannelIndex.return_value = None @@ -696,7 +696,7 @@ def test_main_sendtext_with_invalid_channel_nine(caplog, capsys): def test_main_sendtext_with_dest(mock_findPorts, mock_serial, mocked_open, mock_get, mock_set, capsys, caplog, iface_with_nodes): """Test --sendtext with --dest""" sys.argv = ["", "--sendtext", "hello", "--dest", "foo"] - Globals.getInstance().set_args(sys.argv) + globals.args = sys.argv #iface = iface_with_nodes #iface.myInfo.my_node_num = 2475227164 @@ -730,7 +730,7 @@ def test_main_sendtext_with_dest(mock_findPorts, mock_serial, mocked_open, mock_ def test_main_setlat(capsys): """Test --sendlat""" sys.argv = ["", "--setlat", "37.5"] - Globals.getInstance().set_args(sys.argv) + globals.args = sys.argv mocked_node = MagicMock(autospec=Node) @@ -765,7 +765,7 @@ def mock_sendPosition(lat, lon, alt): def test_main_setlon(capsys): """Test --setlon""" sys.argv = ["", "--setlon", "-122.1"] - Globals.getInstance().set_args(sys.argv) + globals.args = sys.argv mocked_node = MagicMock(autospec=Node) @@ -800,7 +800,7 @@ def mock_sendPosition(lat, lon, alt): def test_main_setalt(capsys): """Test --setalt""" sys.argv = ["", "--setalt", "51"] - Globals.getInstance().set_args(sys.argv) + globals.args = sys.argv mocked_node = MagicMock(autospec=Node) @@ -835,7 +835,7 @@ def mock_sendPosition(lat, lon, alt): def test_main_seturl(capsys): """Test --seturl (url used below is what is generated after a factory_reset)""" sys.argv = ["", "--seturl", "https://www.meshtastic.org/d/#CgUYAyIBAQ"] - Globals.getInstance().set_args(sys.argv) + globals.args = sys.argv iface = MagicMock(autospec=SerialInterface) with patch("meshtastic.serial_interface.SerialInterface", return_value=iface) as mo: @@ -856,7 +856,7 @@ def test_main_seturl(capsys): def test_main_set_valid(mocked_findports, mocked_serial, mocked_open, mocked_get, mocked_set, capsys): """Test --set with valid field""" sys.argv = ["", "--set", "network.wifi_ssid", "foo"] - Globals.getInstance().set_args(sys.argv) + globals.args = sys.argv serialInterface = SerialInterface(noProto=True) anode = Node(serialInterface, 1234567890, noProto=True) @@ -881,7 +881,7 @@ def test_main_set_valid(mocked_findports, mocked_serial, mocked_open, mocked_get def test_main_set_valid_wifi_psk(mocked_findports, mocked_serial, mocked_open, mocked_get, mocked_set, capsys): """Test --set with valid field""" sys.argv = ["", "--set", "network.wifi_psk", "123456789"] - Globals.getInstance().set_args(sys.argv) + globals.args = sys.argv serialInterface = SerialInterface(noProto=True) anode = Node(serialInterface, 1234567890, noProto=True) @@ -906,7 +906,7 @@ def test_main_set_valid_wifi_psk(mocked_findports, mocked_serial, mocked_open, m def test_main_set_invalid_wifi_psk(mocked_findports, mocked_serial, mocked_open, mocked_get, mocked_set, capsys): """Test --set with an invalid value (psk must be 8 or more characters)""" sys.argv = ["", "--set", "network.wifi_psk", "1234567"] - Globals.getInstance().set_args(sys.argv) + globals.args = sys.argv serialInterface = SerialInterface(noProto=True) anode = Node(serialInterface, 1234567890, noProto=True) @@ -934,8 +934,8 @@ def test_main_set_invalid_wifi_psk(mocked_findports, mocked_serial, mocked_open, def test_main_set_valid_camel_case(mocked_findports, mocked_serial, mocked_open, mocked_get, mocked_set, capsys): """Test --set with valid field""" sys.argv = ["", "--set", "network.wifi_ssid", "foo"] - Globals.getInstance().set_args(sys.argv) - Globals.getInstance().set_camel_case() + globals.args = sys.argv + globals.camel_case = True serialInterface = SerialInterface(noProto=True) anode = Node(serialInterface, 1234567890, noProto=True) @@ -960,7 +960,7 @@ def test_main_set_valid_camel_case(mocked_findports, mocked_serial, mocked_open, def test_main_set_with_invalid(mocked_findports, mocked_serial, mocked_open, mocked_get, mocked_set, capsys): """Test --set with invalid field""" sys.argv = ["", "--set", "foo", "foo"] - Globals.getInstance().set_args(sys.argv) + globals.args = sys.argv serialInterface = SerialInterface(noProto=True) anode = Node(serialInterface, 1234567890, noProto=True) @@ -986,7 +986,7 @@ def test_main_set_with_invalid(mocked_findports, mocked_serial, mocked_open, moc def test_main_configure_with_snake_case(mocked_findports, mocked_serial, mocked_open, mocked_get, mocked_set, capsys): """Test --configure with valid file""" sys.argv = ["", "--configure", "example_config.yaml"] - Globals.getInstance().set_args(sys.argv) + globals.args = sys.argv serialInterface = SerialInterface(noProto=True) anode = Node(serialInterface, 1234567890, noProto=True) @@ -1019,7 +1019,7 @@ def test_main_configure_with_snake_case(mocked_findports, mocked_serial, mocked_ def test_main_configure_with_camel_case_keys(mocked_findports, mocked_serial, mocked_open, mocked_get, mocked_set, capsys): """Test --configure with valid file""" sys.argv = ["", "--configure", "exampleConfig.yaml"] - Globals.getInstance().set_args(sys.argv) + globals.args = sys.argv serialInterface = SerialInterface(noProto=True) anode = Node(serialInterface, 1234567890, noProto=True) @@ -1046,7 +1046,7 @@ def test_main_configure_with_camel_case_keys(mocked_findports, mocked_serial, mo def test_main_ch_add_valid(capsys): """Test --ch-add with valid channel name, and that channel name does not already exist""" sys.argv = ["", "--ch-add", "testing"] - Globals.getInstance().set_args(sys.argv) + globals.args = sys.argv mocked_channel = MagicMock(autospec=Channel) # TODO: figure out how to get it to print the channel name instead of MagicMock @@ -1074,7 +1074,7 @@ def test_main_ch_add_valid(capsys): def test_main_ch_add_invalid_name_too_long(capsys): """Test --ch-add with invalid channel name, name too long""" sys.argv = ["", "--ch-add", "testingtestingtesting"] - Globals.getInstance().set_args(sys.argv) + globals.args = sys.argv mocked_channel = MagicMock(autospec=Channel) # TODO: figure out how to get it to print the channel name instead of MagicMock @@ -1105,7 +1105,7 @@ def test_main_ch_add_invalid_name_too_long(capsys): def test_main_ch_add_but_name_already_exists(capsys): """Test --ch-add with a channel name that already exists""" sys.argv = ["", "--ch-add", "testing"] - Globals.getInstance().set_args(sys.argv) + globals.args = sys.argv mocked_node = MagicMock(autospec=Node) # set it up so we do not already have a channel named this @@ -1131,7 +1131,7 @@ def test_main_ch_add_but_name_already_exists(capsys): def test_main_ch_add_but_no_more_channels(capsys): """Test --ch-add with but there are no more channels""" sys.argv = ["", "--ch-add", "testing"] - Globals.getInstance().set_args(sys.argv) + globals.args = sys.argv mocked_node = MagicMock(autospec=Node) # set it up so we do not already have a channel named this @@ -1159,7 +1159,7 @@ def test_main_ch_add_but_no_more_channels(capsys): def test_main_ch_del(capsys): """Test --ch-del with valid secondary channel to be deleted""" sys.argv = ["", "--ch-del", "--ch-index", "1"] - Globals.getInstance().set_args(sys.argv) + globals.args = sys.argv mocked_node = MagicMock(autospec=Node) @@ -1180,7 +1180,7 @@ def test_main_ch_del(capsys): def test_main_ch_del_no_ch_index_specified(capsys): """Test --ch-del without a valid ch-index""" sys.argv = ["", "--ch-del"] - Globals.getInstance().set_args(sys.argv) + globals.args = sys.argv mocked_node = MagicMock(autospec=Node) @@ -1204,8 +1204,8 @@ def test_main_ch_del_no_ch_index_specified(capsys): def test_main_ch_del_primary_channel(capsys): """Test --ch-del on ch-index=0""" sys.argv = ["", "--ch-del", "--ch-index", "0"] - Globals.getInstance().set_args(sys.argv) - Globals.getInstance().set_channel_index(1) + globals.args = sys.argv + globals.channel_index = 1 mocked_node = MagicMock(autospec=Node) @@ -1229,7 +1229,7 @@ def test_main_ch_del_primary_channel(capsys): def test_main_ch_enable_valid_secondary_channel(capsys): """Test --ch-enable with --ch-index""" sys.argv = ["", "--ch-enable", "--ch-index", "1"] - Globals.getInstance().set_args(sys.argv) + globals.args = sys.argv mocked_node = MagicMock(autospec=Node) @@ -1242,7 +1242,7 @@ def test_main_ch_enable_valid_secondary_channel(capsys): assert re.search(r"Connected to radio", out, re.MULTILINE) assert re.search(r"Writing modified channels", out, re.MULTILINE) assert err == "" - assert Globals.getInstance().get_channel_index() == 1 + assert globals.channel_index == 1 mo.assert_called() @@ -1251,7 +1251,7 @@ def test_main_ch_enable_valid_secondary_channel(capsys): def test_main_ch_disable_valid_secondary_channel(capsys): """Test --ch-disable with --ch-index""" sys.argv = ["", "--ch-disable", "--ch-index", "1"] - Globals.getInstance().set_args(sys.argv) + globals.args = sys.argv mocked_node = MagicMock(autospec=Node) @@ -1264,7 +1264,7 @@ def test_main_ch_disable_valid_secondary_channel(capsys): assert re.search(r"Connected to radio", out, re.MULTILINE) assert re.search(r"Writing modified channels", out, re.MULTILINE) assert err == "" - assert Globals.getInstance().get_channel_index() == 1 + assert globals.channel_index == 1 mo.assert_called() @@ -1273,7 +1273,7 @@ def test_main_ch_disable_valid_secondary_channel(capsys): def test_main_ch_enable_without_a_ch_index(capsys): """Test --ch-enable without --ch-index""" sys.argv = ["", "--ch-enable"] - Globals.getInstance().set_args(sys.argv) + globals.args = sys.argv mocked_node = MagicMock(autospec=Node) @@ -1289,7 +1289,7 @@ def test_main_ch_enable_without_a_ch_index(capsys): assert re.search(r"Connected to radio", out, re.MULTILINE) assert re.search(r"Warning: Need to specify", out, re.MULTILINE) assert err == "" - assert Globals.getInstance().get_channel_index() is None + assert globals.channel_index is None mo.assert_called() @@ -1298,7 +1298,7 @@ def test_main_ch_enable_without_a_ch_index(capsys): def test_main_ch_enable_primary_channel(capsys): """Test --ch-enable with --ch-index = 0""" sys.argv = ["", "--ch-enable", "--ch-index", "0"] - Globals.getInstance().set_args(sys.argv) + globals.args = sys.argv mocked_node = MagicMock(autospec=Node) @@ -1314,7 +1314,7 @@ def test_main_ch_enable_primary_channel(capsys): assert re.search(r"Connected to radio", out, re.MULTILINE) assert re.search(r"Warning: Cannot enable/disable PRIMARY", out, re.MULTILINE) assert err == "" - assert Globals.getInstance().get_channel_index() == 0 + assert globals.channel_index == 0 mo.assert_called() @@ -1327,7 +1327,7 @@ def test_main_ch_enable_primary_channel(capsys): # '--ch-midfast', '--ch-shortslow', '--ch-shortfast'] # for range_option in range_options: # sys.argv = ['', f"{range_option}" ] -# Globals.getInstance().set_args(sys.argv) +# globals.args = sys.argv # # mocked_node = MagicMock(autospec=Node) # @@ -1348,7 +1348,7 @@ def test_main_ch_enable_primary_channel(capsys): def test_main_ch_longfast_on_non_primary_channel(capsys): """Test --ch-longfast --ch-index 1""" sys.argv = ["", "--ch-longfast", "--ch-index", "1"] - Globals.getInstance().set_args(sys.argv) + globals.args = sys.argv mocked_node = MagicMock(autospec=Node) @@ -1387,7 +1387,7 @@ def test_main_ch_longfast_on_non_primary_channel(capsys): # def test_main_pos_fields_no_args(capsys): # """Test --pos-fields no args (which shows settings)""" # sys.argv = ['', '--pos-fields'] -# Globals.getInstance().set_args(sys.argv) +# globals.args = sys.argv # # pos_flags = MagicMock(autospec=meshtastic.radioconfig_pb2.PositionFlags) # @@ -1419,7 +1419,7 @@ def test_main_ch_longfast_on_non_primary_channel(capsys): # def test_main_pos_fields_arg_of_zero(capsys): # """Test --pos-fields an arg of 0 (which shows list)""" # sys.argv = ['', '--pos-fields', '0'] -# Globals.getInstance().set_args(sys.argv) +# globals.args = sys.argv # # pos_flags = MagicMock(autospec=meshtastic.radioconfig_pb2.PositionFlags) # @@ -1454,7 +1454,7 @@ def test_main_ch_longfast_on_non_primary_channel(capsys): # def test_main_pos_fields_valid_values(capsys): # """Test --pos-fields with valid values""" # sys.argv = ['', '--pos-fields', 'POS_GEO_SEP', 'POS_ALT_MSL'] -# Globals.getInstance().set_args(sys.argv) +# globals.args = sys.argv # # pos_flags = MagicMock(autospec=meshtastic.radioconfig_pb2.PositionFlags) # @@ -1482,7 +1482,7 @@ def test_main_ch_longfast_on_non_primary_channel(capsys): # def test_main_get_with_valid_values(capsys): # """Test --get with valid values (with string, number, boolean)""" # sys.argv = ['', '--get', 'ls_secs', '--get', 'wifi_ssid', '--get', 'fixed_position'] -# Globals.getInstance().set_args(sys.argv) +# globals.args = sys.argv # # with patch('meshtastic.serial_interface.SerialInterface') as mo: # @@ -1508,8 +1508,8 @@ def test_main_ch_longfast_on_non_primary_channel(capsys): #def test_main_get_with_valid_values_camel(capsys, caplog): # """Test --get with valid values (with string, number, boolean)""" # sys.argv = ["", "--get", "lsSecs", "--get", "wifiSsid", "--get", "fixedPosition"] -# Globals.getInstance().set_args(sys.argv) -# Globals.getInstance().set_camel_case() +# globals.args = sys.argv +# globals.camel_case = True # # with caplog.at_level(logging.DEBUG): # with patch("meshtastic.serial_interface.SerialInterface") as mo: @@ -1534,7 +1534,7 @@ def test_main_ch_longfast_on_non_primary_channel(capsys): def test_main_get_with_invalid(capsys): """Test --get with invalid field""" sys.argv = ["", "--get", "foo"] - Globals.getInstance().set_args(sys.argv) + globals.args = sys.argv mocked_user_prefs = MagicMock() mocked_user_prefs.DESCRIPTOR.fields_by_name.get.return_value = None @@ -1561,7 +1561,7 @@ def test_main_get_with_invalid(capsys): def test_main_onReceive_empty(caplog, capsys): """Test onReceive""" args = MagicMock() - Globals.getInstance().set_args(args) + globals.args = args iface = MagicMock(autospec=SerialInterface) packet = {} with caplog.at_level(logging.DEBUG): @@ -1594,7 +1594,7 @@ def test_main_onReceive_with_sendtext(caplog, capsys): is made in onReceive(). """ sys.argv = ["", "--sendtext", "hello"] - Globals.getInstance().set_args(sys.argv) + globals.args = sys.argv # Note: 'TEXT_MESSAGE_APP' value is 1 packet = { @@ -1625,7 +1625,7 @@ def test_main_onReceive_with_text(caplog, capsys): """Test onReceive with text""" args = MagicMock() args.sendtext.return_value = "foo" - Globals.getInstance().set_args(args) + globals.args = args # Note: 'TEXT_MESSAGE_APP' value is 1 # Note: Some of this is faked below. @@ -1659,7 +1659,7 @@ def test_main_onReceive_with_text(caplog, capsys): def test_main_onConnection(capsys): """Test onConnection""" sys.argv = [""] - Globals.getInstance().set_args(sys.argv) + globals.args = sys.argv iface = MagicMock(autospec=SerialInterface) class TempTopic: @@ -1727,7 +1727,7 @@ def test_main_export_config(capsys): #@pytest.mark.usefixtures("reset_globals") #def test_main_export_config_use_camel(capsys): # """Test export_config() function directly""" -# Globals.getInstance().set_camel_case() +# globals.camel_case = True # iface = MagicMock(autospec=SerialInterface) # with patch("meshtastic.serial_interface.SerialInterface", return_value=iface) as mo: # mo.getLongName.return_value = "foo" @@ -1774,7 +1774,7 @@ def test_main_export_config(capsys): #def test_main_export_config_called_from_main(capsys): # """Test --export-config""" # sys.argv = ["", "--export-config"] -# Globals.getInstance().set_args(sys.argv) +# globals.args = sys.argv # # iface = MagicMock(autospec=SerialInterface) # with patch("meshtastic.serial_interface.SerialInterface", return_value=iface) as mo: @@ -1791,7 +1791,7 @@ def test_main_export_config(capsys): def test_main_gpio_rd_no_gpio_channel(capsys): """Test --gpio_rd with no named gpio channel""" sys.argv = ["", "--gpio-rd", "0x10", "--dest", "!foo"] - Globals.getInstance().set_args(sys.argv) + globals.args = sys.argv iface = MagicMock(autospec=SerialInterface) iface.localNode.getChannelByName.return_value = None @@ -1810,7 +1810,7 @@ def test_main_gpio_rd_no_gpio_channel(capsys): def test_main_gpio_rd_no_dest(capsys): """Test --gpio_rd with a named gpio channel but no dest was specified""" sys.argv = ["", "--gpio-rd", "0x2000"] - Globals.getInstance().set_args(sys.argv) + globals.args = sys.argv channel = Channel(index=2, role=2) channel.settings.psk = b"\x8a\x94y\x0e\xc6\xc9\x1e5\x91\x12@\xa60\xa8\xb43\x87\x00\xf2K\x0e\xe7\x7fAz\xcd\xf5\xb0\x900\xa84" @@ -1844,7 +1844,7 @@ def test_main_gpio_rd_no_dest(capsys): # # >>> print(hex(2**13)) # # 0x2000 # sys.argv = ['', '--gpio-rd', '0x1000', '--dest', '!1234'] -# Globals.getInstance().set_args(sys.argv) +# globals.args = sys.argv # # channel = Channel(index=1, role=1) # channel.settings.modem_config = 3 @@ -1892,7 +1892,7 @@ def test_main_gpio_rd_no_dest(capsys): # def test_main_gpio_rd_with_no_gpioMask(caplog, capsys): # """Test --gpio_rd with a named gpio channel""" # sys.argv = ['', '--gpio-rd', '0x1000', '--dest', '!1234'] -# Globals.getInstance().set_args(sys.argv) +# globals.args = sys.argv # # channel = Channel(index=1, role=1) # channel.settings.modem_config = 3 @@ -1939,7 +1939,7 @@ def test_main_gpio_rd_no_dest(capsys): # def test_main_gpio_watch(caplog, capsys): # """Test --gpio_watch with a named gpio channel""" # sys.argv = ['', '--gpio-watch', '0x1000', '--dest', '!1234'] -# Globals.getInstance().set_args(sys.argv) +# globals.args = sys.argv # # def my_sleep(amount): # print(f'{amount}') @@ -1993,7 +1993,7 @@ def test_main_gpio_rd_no_dest(capsys): # def test_main_gpio_wrb(caplog, capsys): # """Test --gpio_wrb with a named gpio channel""" # sys.argv = ['', '--gpio-wrb', '4', '1', '--dest', '!1234'] -# Globals.getInstance().set_args(sys.argv) +# globals.args = sys.argv # # channel = Channel(index=1, role=1) # channel.settings.modem_config = 3 @@ -2057,7 +2057,7 @@ def test_main_gpio_rd_no_dest(capsys): #@pytest.mark.usefixtures("reset_globals") #def test_main_getPref_valid_field_camel(capsys): # """Test getPref() with a valid field""" -# Globals.getInstance().set_camel_case() +# globals.camel_case = True # prefs = MagicMock() # prefs.DESCRIPTOR.fields_by_name.get.return_value = "ls_secs" # prefs.wifi_ssid = "foo" @@ -2090,7 +2090,7 @@ def test_main_gpio_rd_no_dest(capsys): #@pytest.mark.usefixtures("reset_globals") #def test_main_getPref_valid_field_string_camel(capsys): # """Test getPref() with a valid field and value as a string""" -# Globals.getInstance().set_camel_case() +# globals.camel_case = True # prefs = MagicMock() # prefs.DESCRIPTOR.fields_by_name.get.return_value = "wifi_ssid" # prefs.wifi_ssid = "foo" @@ -2123,7 +2123,7 @@ def test_main_gpio_rd_no_dest(capsys): #@pytest.mark.usefixtures("reset_globals") #def test_main_getPref_valid_field_bool_camel(capsys): # """Test getPref() with a valid field and value as a bool""" -# Globals.getInstance().set_camel_case() +# globals.camel_case = True # prefs = MagicMock() # prefs.DESCRIPTOR.fields_by_name.get.return_value = "fixed_position" # prefs.wifi_ssid = "foo" @@ -2172,7 +2172,7 @@ def test_main_gpio_rd_no_dest(capsys): #@pytest.mark.usefixtures("reset_globals") #def test_main_getPref_invalid_field_camel(capsys): # """Test getPref() with an invalid field""" -# Globals.getInstance().set_camel_case() +# globals.camel_case = True # # class Field: # """Simple class for testing.""" @@ -2267,7 +2267,7 @@ def test_main_gpio_rd_no_dest(capsys): # @pytest.mark.usefixtures("reset_globals") # def test_main_setPref_valid_field_invalid_enum_camel(capsys, caplog): # """Test setPref() with a valid field but invalid enum value""" -# Globals.getInstance().set_camel_case() +# globals.camel_case = True # # radioConfig = RadioConfig() # prefs = radioConfig.preferences @@ -2303,7 +2303,7 @@ def test_main_gpio_rd_no_dest(capsys): # @pytest.mark.usefixtures("reset_globals") # def test_main_setPref_valid_field_valid_enum_camel(capsys, caplog): # """Test setPref() with a valid field and valid enum value""" -# Globals.getInstance().set_camel_case() +# globals.camel_case = True # # # charge_current # # some valid values: MA100 MA1000 MA1080 @@ -2354,7 +2354,7 @@ def test_main_gpio_rd_no_dest(capsys): #@pytest.mark.usefixtures("reset_globals") #def test_main_setPref_invalid_field_camel(capsys): # """Test setPref() with a invalid field""" -# Globals.getInstance().set_camel_case() +# globals.camel_case = True # # class Field: # """Simple class for testing.""" @@ -2435,7 +2435,7 @@ def test_main_gpio_rd_no_dest(capsys): def test_main_ch_set_psk_no_ch_index(capsys): """Test --ch-set psk""" sys.argv = ["", "--ch-set", "psk", "foo", "--host", "meshtastic.local"] - Globals.getInstance().set_args(sys.argv) + globals.args = sys.argv iface = MagicMock(autospec=TCPInterface) with patch("meshtastic.tcp_interface.TCPInterface", return_value=iface) as mo: @@ -2464,7 +2464,7 @@ def test_main_ch_set_psk_with_ch_index(capsys): "--ch-index", "0", ] - Globals.getInstance().set_args(sys.argv) + globals.args = sys.argv iface = MagicMock(autospec=TCPInterface) with patch("meshtastic.tcp_interface.TCPInterface", return_value=iface) as mo: @@ -2492,7 +2492,7 @@ def test_main_ch_set_psk_with_ch_index(capsys): # "--ch-index", # "0", # ] -# Globals.getInstance().set_args(sys.argv) +# globals.args = sys.argv # # iface = MagicMock(autospec=TCPInterface) # with patch("meshtastic.tcp_interface.TCPInterface", return_value=iface) as mo: @@ -2520,7 +2520,7 @@ def test_onNode(capsys): def test_tunnel_no_args(capsys): """Test tunnel no arguments""" sys.argv = [""] - Globals.getInstance().set_args(sys.argv) + globals.args = sys.argv with pytest.raises(SystemExit) as pytest_wrapped_e: tunnelMain() assert pytest_wrapped_e.type == SystemExit @@ -2539,7 +2539,7 @@ def test_tunnel_tunnel_arg_with_no_devices(mock_platform_system, caplog, capsys) a_mock.return_value = "Linux" mock_platform_system.side_effect = a_mock sys.argv = ["", "--tunnel"] - Globals.getInstance().set_args(sys.argv) + globals.args = sys.argv print(f"platform.system():{platform.system()}") with caplog.at_level(logging.DEBUG): with pytest.raises(SystemExit) as pytest_wrapped_e: @@ -2562,7 +2562,7 @@ def test_tunnel_subnet_arg_with_no_devices(mock_platform_system, caplog, capsys) a_mock.return_value = "Linux" mock_platform_system.side_effect = a_mock sys.argv = ["", "--subnet", "foo"] - Globals.getInstance().set_args(sys.argv) + globals.args = sys.argv print(f"platform.system():{platform.system()}") with caplog.at_level(logging.DEBUG): with pytest.raises(SystemExit) as pytest_wrapped_e: @@ -2597,7 +2597,7 @@ def my_sleep(amount): a_mock.return_value = "Linux" mock_platform_system.side_effect = a_mock sys.argv = ["", "--tunnel"] - Globals.getInstance().set_args(sys.argv) + globals.args = sys.argv serialInterface = SerialInterface(noProto=True) diff --git a/meshtastic/tests/test_tunnel.py b/meshtastic/tests/test_tunnel.py index ebfbd05b..ea2df711 100644 --- a/meshtastic/tests/test_tunnel.py +++ b/meshtastic/tests/test_tunnel.py @@ -1,5 +1,5 @@ """Meshtastic unit tests for tunnel.py""" - +from meshtastic import globals import logging import re import sys @@ -7,7 +7,6 @@ import pytest -from ..globals import Globals from ..tcp_interface import TCPInterface from ..tunnel import Tunnel, onTunnelReceive @@ -51,7 +50,7 @@ def test_Tunnel_with_interface(mock_platform_system, caplog, iface_with_nodes): with caplog.at_level(logging.WARNING): with patch("socket.socket"): tun = Tunnel(iface) - assert tun == Globals.getInstance().get_tunnelInstance() + assert tun == globals.tunnelInstance iface.close() assert re.search(r"Not creating a TapDevice()", caplog.text, re.MULTILINE) assert re.search(r"Not starting TUN reader", caplog.text, re.MULTILINE) @@ -65,7 +64,7 @@ def test_onTunnelReceive_from_ourselves(mock_platform_system, caplog, iface_with iface = iface_with_nodes iface.myInfo.my_node_num = 2475227164 sys.argv = [""] - Globals.getInstance().set_args(sys.argv) + globals.args = sys.argv packet = {"decoded": {"payload": "foo"}, "from": 2475227164} a_mock = MagicMock() a_mock.return_value = "Linux" @@ -73,7 +72,7 @@ def test_onTunnelReceive_from_ourselves(mock_platform_system, caplog, iface_with with caplog.at_level(logging.DEBUG): with patch("socket.socket"): tun = Tunnel(iface) - Globals.getInstance().set_tunnelInstance(tun) + globals.tunnelInstance = tun onTunnelReceive(packet, iface) assert re.search(r"in onTunnelReceive", caplog.text, re.MULTILINE) assert re.search(r"Ignoring message we sent", caplog.text, re.MULTILINE) @@ -88,7 +87,7 @@ def test_onTunnelReceive_from_someone_else( iface = iface_with_nodes iface.myInfo.my_node_num = 2475227164 sys.argv = [""] - Globals.getInstance().set_args(sys.argv) + globals.args = sys.argv packet = {"decoded": {"payload": "foo"}, "from": 123} a_mock = MagicMock() a_mock.return_value = "Linux" @@ -96,7 +95,7 @@ def test_onTunnelReceive_from_someone_else( with caplog.at_level(logging.DEBUG): with patch("socket.socket"): tun = Tunnel(iface) - Globals.getInstance().set_tunnelInstance(tun) + globals.tunnelInstance = tun onTunnelReceive(packet, iface) assert re.search(r"in onTunnelReceive", caplog.text, re.MULTILINE) diff --git a/meshtastic/tunnel.py b/meshtastic/tunnel.py index 51837c99..21563a1b 100644 --- a/meshtastic/tunnel.py +++ b/meshtastic/tunnel.py @@ -22,16 +22,14 @@ from pubsub import pub # type: ignore[import-untyped] from pytap2 import TapDevice -from meshtastic import portnums_pb2 -from meshtastic.globals import Globals +from meshtastic import portnums_pb2, globals from meshtastic.util import ipstr, readnet_u16 def onTunnelReceive(packet, interface): # pylint: disable=W0613 """Callback for received tunneled messages from mesh.""" logging.debug(f"in onTunnelReceive()") - our_globals = Globals.getInstance() - tunnelInstance = our_globals.get_tunnelInstance() + tunnelInstance = globals.tunnelInstance tunnelInstance.onReceive(packet) @@ -67,8 +65,7 @@ def __init__(self, iface, subnet="10.115", netmask="255.255.0.0"): if platform.system() != "Linux": raise Tunnel.TunnelError("Tunnel() can only be run instantiated on a Linux system") - our_globals = Globals.getInstance() - our_globals.set_tunnelInstance(self) + globals.tunnelInstance = self """A list of chatty UDP services we should never accidentally forward to our slow network""" From 0d574490306f66bfd8f65eec01d8ad1a21b696b8 Mon Sep 17 00:00:00 2001 From: Steve Holden Date: Wed, 10 Apr 2024 14:25:17 +0100 Subject: [PATCH 2/3] Begin to rationalise test data. Also refactor to silence some CI issues. --- meshtastic/globals.py | 10 +++++++++- meshtastic/tests/test_mesh_interface.py | 14 +++++++------- meshtastic/tests/test_tunnel.py | 3 ++- 3 files changed, 18 insertions(+), 9 deletions(-) diff --git a/meshtastic/globals.py b/meshtastic/globals.py index fb379c1d..58fe76ec 100644 --- a/meshtastic/globals.py +++ b/meshtastic/globals.py @@ -15,6 +15,7 @@ def reset(): """ + Restore the namespace to pristine condition. """ global args, parser, channel_index, logfile, tunnelInstance, camel_case args = None @@ -25,4 +26,11 @@ def reset(): # TODO: to migrate to camel_case for v1.3 change this value to True camel_case = False -reset() \ No newline at end of file +# These assignments are used instead of calling reset() +# purely to shut pylint up. +args = None +parser = None +channel_index = None +logfile = None +tunnelInstance = None +camel_case = False diff --git a/meshtastic/tests/test_mesh_interface.py b/meshtastic/tests/test_mesh_interface.py index 06679330..791a92a9 100644 --- a/meshtastic/tests/test_mesh_interface.py +++ b/meshtastic/tests/test_mesh_interface.py @@ -21,11 +21,12 @@ def test_MeshInterface(capsys): """Test that we can instantiate a MeshInterface""" iface = MeshInterface(noProto=True) - nodes = { - "!9388f81c": { - "num": 2475227164, + NODE_ID = "!9388f81c" + NODE_NUM = 2475227164 + node = { + "num": NODE_NUM, "user": { - "id": "!9388f81c", + "id": NODE_ID, "longName": "Unknown f81c", "shortName": "?1C", "macaddr": "RBeTiPgc", @@ -34,10 +35,9 @@ def test_MeshInterface(capsys): "position": {}, "lastHeard": 1640204888, } - } - iface.nodesByNum = {2475227164: nodes["!9388f81c"]} - iface.nodes = nodes + iface.nodes = {NODE_ID: node} + iface.nodesByNum = {NODE_NUM: node} myInfo = MagicMock() iface.myInfo = myInfo diff --git a/meshtastic/tests/test_tunnel.py b/meshtastic/tests/test_tunnel.py index ea2df711..d4a95fc5 100644 --- a/meshtastic/tests/test_tunnel.py +++ b/meshtastic/tests/test_tunnel.py @@ -1,5 +1,4 @@ """Meshtastic unit tests for tunnel.py""" -from meshtastic import globals import logging import re import sys @@ -7,6 +6,8 @@ import pytest +from meshtastic import globals + from ..tcp_interface import TCPInterface from ..tunnel import Tunnel, onTunnelReceive From a07e853f699a0a2f18dae712f5ed2fc6bd386355 Mon Sep 17 00:00:00 2001 From: Steve Holden Date: Wed, 10 Apr 2024 17:42:44 +0100 Subject: [PATCH 3/3] Refactor to remove pylint issues. Since one of pylint's complains was that the globals module was shadowing the built-in, and since the name `config` was already is use in several modules, globals.py was renamed as mt_config.py. All tests now pass, and the only remaining local pylint errors relate to the protobuf code, I'm hoping this will make the PR valid. --- meshtastic/__main__.py | 64 ++-- meshtastic/{globals.py => mt_config.py} | 3 +- meshtastic/tests/conftest.py | 10 +- meshtastic/tests/test_init.py | 8 +- meshtastic/tests/test_main.py | 392 +++++++++++----------- meshtastic/tests/test_mesh_interface.py | 82 ++--- meshtastic/tests/test_stream_interface.py | 4 +- meshtastic/tests/test_tunnel.py | 12 +- meshtastic/tunnel.py | 6 +- 9 files changed, 291 insertions(+), 290 deletions(-) rename meshtastic/{globals.py => mt_config.py} (90%) diff --git a/meshtastic/__main__.py b/meshtastic/__main__.py index ea3dcb43..aa59f95a 100644 --- a/meshtastic/__main__.py +++ b/meshtastic/__main__.py @@ -16,14 +16,14 @@ import meshtastic.test import meshtastic.util -from meshtastic import globals +from meshtastic import mt_config from meshtastic import channel_pb2, config_pb2, portnums_pb2, remote_hardware, BROADCAST_ADDR from meshtastic.version import get_active_version from meshtastic.ble_interface import BLEInterface def onReceive(packet, interface): """Callback invoked when a packet arrives""" - args = globals.args + args = mt_config.args try: d = packet.get("decoded") logging.debug(f"in onReceive() d:{d}") @@ -67,7 +67,7 @@ def getPref(node, comp_name): # Note: protobufs has the keys in snake_case, so snake internally snake_name = meshtastic.util.camel_to_snake(name[1]) logging.debug(f"snake_name:{snake_name} camel_name:{camel_name}") - logging.debug(f"use camel:{globals.camel_case}") + logging.debug(f"use camel:{mt_config.camel_case}") # First validate the input localConfig = node.localConfig @@ -84,7 +84,7 @@ def getPref(node, comp_name): break if not found: - if globals.camel_case: + if mt_config.camel_case: print( f"{localConfig.__class__.__name__} and {moduleConfig.__class__.__name__} do not have an attribute {snake_name}." ) @@ -103,7 +103,7 @@ def getPref(node, comp_name): config_values = getattr(config, config_type.name) if not wholeField: pref_value = getattr(config_values, pref.name) - if globals.camel_case: + if mt_config.camel_case: print(f"{str(config_type.name)}.{camel_name}: {str(pref_value)}") logging.debug( f"{str(config_type.name)}.{camel_name}: {str(pref_value)}" @@ -169,7 +169,7 @@ def setPref(config, comp_name, valStr) -> bool: if e: val = e.number else: - if globals.camel_case: + if mt_config.camel_case: print( f"{name[0]}.{camel_name} does not have an enum called {val}, so you can not set it." ) @@ -208,7 +208,7 @@ def setPref(config, comp_name, valStr) -> bool: config_type.message_type.ignore_incoming.extend([val]) prefix = f"{name[0]}." if config_type.message_type is not None else "" - if globals.camel_case: + if mt_config.camel_case: print(f"Set {prefix}{camel_name} to {valStr}") else: print(f"Set {prefix}{snake_name} to {valStr}") @@ -223,7 +223,7 @@ def onConnected(interface): False # Should we wait for an acknowledgment if we send to a remote node? ) try: - args = globals.args + args = mt_config.args # do not print this line if we are exporting the config if not args.export_config: @@ -474,7 +474,7 @@ def onConnected(interface): print("Writing modified preferences to device") node.writeConfig(field) else: - if globals.camel_case: + if mt_config.camel_case: print( f"{node.localConfig.__class__.__name__} and {node.moduleConfig.__class__.__name__} do not have an attribute {pref[0]}." ) @@ -587,7 +587,7 @@ def onConnected(interface): # handle changing channels if args.ch_add: - channelIndex = globals.channel_index + channelIndex = mt_config.channel_index if channelIndex is not None: # Since we set the channel index after adding a channel, don't allow --ch-index meshtastic.util.our_exit( @@ -618,12 +618,12 @@ def onConnected(interface): n.writeChannel(ch.index) if channelIndex is None: print(f"Setting newly-added channel's {ch.index} as '--ch-index' for further modifications") - globals.channel_index = ch.index + mt_config.channel_index = ch.index if args.ch_del: closeNow = True - channelIndex = globals.channel_index + channelIndex = mt_config.channel_index if channelIndex is None: meshtastic.util.our_exit( "Warning: Need to specify '--ch-index' for '--ch-del'.", 1 @@ -639,7 +639,7 @@ def onConnected(interface): def setSimpleConfig(modem_preset): """Set one of the simple modem_config""" - channelIndex = globals.channel_index + channelIndex = mt_config.channel_index if channelIndex is not None and channelIndex > 0: meshtastic.util.our_exit( "Warning: Cannot set modem preset for non-primary channel", 1 @@ -674,7 +674,7 @@ def setSimpleConfig(modem_preset): if args.ch_set or args.ch_enable or args.ch_disable: closeNow = True - channelIndex = globals.channel_index + channelIndex = mt_config.channel_index if channelIndex is None: meshtastic.util.our_exit("Warning: Need to specify '--ch-index'.", 1) ch = interface.getNode(args.dest).channels[channelIndex] @@ -829,7 +829,7 @@ def printConfig(config): names = [] for field in config.message_type.fields: tmp_name = f"{config_section.name}.{field.name}" - if globals.camel_case: + if mt_config.camel_case: tmp_name = meshtastic.util.snake_to_camel(tmp_name) names.append(tmp_name) for temp_name in sorted(names): @@ -874,7 +874,7 @@ def export_config(interface): if owner_short: configObj["owner_short"] = owner_short if channel_url: - if globals.camel_case: + if mt_config.camel_case: configObj["channelUrl"] = channel_url else: configObj["channel_url"] = channel_url @@ -886,11 +886,11 @@ def export_config(interface): # Convert inner keys to correct snake/camelCase prefs = {} for pref in config: - if globals.camel_case: + if mt_config.camel_case: prefs[meshtastic.util.snake_to_camel(pref)] = config[pref] else: prefs[pref] = config[pref] - if globals.camel_case: + if mt_config.camel_case: configObj["config"] = config else: configObj["config"] = config @@ -902,7 +902,7 @@ def export_config(interface): for pref in module_config: if len(module_config[pref]) > 0: prefs[pref] = module_config[pref] - if globals.camel_case: + if mt_config.camel_case: configObj["module_config"] = prefs else: configObj["module_config"] = prefs @@ -916,8 +916,8 @@ def export_config(interface): def common(): """Shared code for all of our command line wrappers""" logfile = None - args = globals.args - parser = globals.parser + args = mt_config.args + parser = mt_config.parser logging.basicConfig( level=logging.DEBUG if (args.debug or args.listen) else logging.INFO, format="%(levelname)s file:%(filename)s %(funcName)s line:%(lineno)s %(message)s", @@ -933,7 +933,7 @@ def common(): if args.ch_index is not None: channelIndex = int(args.ch_index) - globals.channel_index = channelIndex + mt_config.channel_index = channelIndex if not args.dest: args.dest = BROADCAST_ADDR @@ -968,7 +968,7 @@ def common(): # Note: using "line buffering" # pylint: disable=R1732 logfile = open(args.seriallog, "w+", buffering=1, encoding="utf8") - globals.logfile = logfile + mt_config.logfile = logfile subscribe() if args.ble_scan: @@ -1059,8 +1059,8 @@ def addConnectionArgs(parser: argparse.ArgumentParser) -> argparse.ArgumentParse def initParser(): """Initialize the command line argument parsing.""" - parser = globals.parser - args = globals.args + parser = mt_config.parser + args = mt_config.args # The "Help" group includes the help option and other informational stuff about the CLI itself outerHelpGroup = parser.add_argument_group('Help') @@ -1426,8 +1426,8 @@ def initParser(): args = parser.parse_args() - globals.args = args - globals.parser = parser + mt_config.args = args + mt_config.parser = parser def main(): @@ -1436,10 +1436,10 @@ def main(): add_help=False, epilog="If no connection arguments are specified, we search for a compatible serial device, " "and if none is found, then attempt a TCP connection to localhost.") - globals.parser = parser + mt_config.parser = parser initParser() common() - logfile = globals.logfile + logfile = mt_config.logfile if logfile: logfile.close() @@ -1447,11 +1447,11 @@ def main(): def tunnelMain(): """Run a meshtastic IP tunnel""" parser = argparse.ArgumentParser(add_help=False) - globals.parser = parser + mt_config.parser = parser initParser() - args = globals.args + args = mt_config.args args.tunnel = True - globals.args = args + mt_config.args = args common() diff --git a/meshtastic/globals.py b/meshtastic/mt_config.py similarity index 90% rename from meshtastic/globals.py rename to meshtastic/mt_config.py index 58fe76ec..662a10da 100644 --- a/meshtastic/globals.py +++ b/meshtastic/mt_config.py @@ -8,7 +8,7 @@ This is intended to make the Python read more naturally, and to make the intention of the code clearer and more compact. It is merely a sticking -plaster over the use of shared globals, but the coupling issues wil be dealt +plaster over the use of shared mt_config, but the coupling issues wil be dealt with rather more easily once the code is simplified by this change. """ @@ -17,6 +17,7 @@ def reset(): """ Restore the namespace to pristine condition. """ + # pylint: disable=W0603 global args, parser, channel_index, logfile, tunnelInstance, camel_case args = None parser = None diff --git a/meshtastic/tests/conftest.py b/meshtastic/tests/conftest.py index f10cefcc..f422da07 100644 --- a/meshtastic/tests/conftest.py +++ b/meshtastic/tests/conftest.py @@ -5,18 +5,18 @@ import pytest -from meshtastic import globals +from meshtastic import mt_config from ..mesh_interface import MeshInterface @pytest.fixture -def reset_globals(): - """Fixture to reset globals.""" +def reset_mt_config(): + """Fixture to reset mt_config.""" parser = None parser = argparse.ArgumentParser(add_help=False) - globals.reset() - globals.parser = parser + mt_config.reset() + mt_config.parser = parser @pytest.fixture diff --git a/meshtastic/tests/test_init.py b/meshtastic/tests/test_init.py index 10604d23..0f1dc5b5 100644 --- a/meshtastic/tests/test_init.py +++ b/meshtastic/tests/test_init.py @@ -6,7 +6,7 @@ import pytest -from meshtastic import _onNodeInfoReceive, _onPositionReceive, _onTextReceive, globals +from meshtastic import _onNodeInfoReceive, _onPositionReceive, _onTextReceive, mt_config from ..serial_interface import SerialInterface @@ -15,7 +15,7 @@ def test_init_onTextReceive_with_exception(caplog): """Test _onTextReceive""" args = MagicMock() - globals.args = args + mt_config.args = args iface = MagicMock(autospec=SerialInterface) packet = {} with caplog.at_level(logging.DEBUG): @@ -28,7 +28,7 @@ def test_init_onTextReceive_with_exception(caplog): def test_init_onPositionReceive(caplog): """Test _onPositionReceive""" args = MagicMock() - globals.args = args + mt_config.args = args iface = MagicMock(autospec=SerialInterface) packet = {"from": "foo", "decoded": {"position": {}}} with caplog.at_level(logging.DEBUG): @@ -40,7 +40,7 @@ def test_init_onPositionReceive(caplog): def test_init_onNodeInfoReceive(caplog, iface_with_nodes): """Test _onNodeInfoReceive""" args = MagicMock() - globals.args = args + mt_config.args = args iface = iface_with_nodes iface.myInfo.my_node_num = 2475227164 packet = { diff --git a/meshtastic/tests/test_main.py b/meshtastic/tests/test_main.py index 9cc3a5a5..dba635b4 100644 --- a/meshtastic/tests/test_main.py +++ b/meshtastic/tests/test_main.py @@ -19,7 +19,7 @@ onReceive, tunnelMain, ) -from meshtastic import globals +from meshtastic import mt_config from ..channel_pb2 import Channel # pylint: disable=E0611 @@ -36,11 +36,11 @@ @pytest.mark.unit -@pytest.mark.usefixtures("reset_globals") +@pytest.mark.usefixtures("reset_mt_config") def test_main_init_parser_no_args(capsys): """Test no arguments""" sys.argv = [""] - globals.args = sys.argv + mt_config.args = sys.argv initParser() out, err = capsys.readouterr() assert out == "" @@ -48,11 +48,11 @@ def test_main_init_parser_no_args(capsys): @pytest.mark.unit -@pytest.mark.usefixtures("reset_globals") +@pytest.mark.usefixtures("reset_mt_config") def test_main_init_parser_version(capsys): """Test --version""" sys.argv = ["", "--version"] - globals.args = sys.argv + mt_config.args = sys.argv with pytest.raises(SystemExit) as pytest_wrapped_e: initParser() @@ -64,11 +64,11 @@ def test_main_init_parser_version(capsys): @pytest.mark.unit -@pytest.mark.usefixtures("reset_globals") +@pytest.mark.usefixtures("reset_mt_config") def test_main_main_version(capsys): """Test --version""" sys.argv = ["", "--version"] - globals.args = sys.argv + mt_config.args = sys.argv with pytest.raises(SystemExit) as pytest_wrapped_e: main() @@ -80,11 +80,11 @@ def test_main_main_version(capsys): @pytest.mark.unit -@pytest.mark.usefixtures("reset_globals") +@pytest.mark.usefixtures("reset_mt_config") def test_main_main_no_args(capsys): """Test with no args""" sys.argv = [""] - globals.args = sys.argv + mt_config.args = sys.argv with pytest.raises(SystemExit) as pytest_wrapped_e: main() @@ -95,11 +95,11 @@ def test_main_main_no_args(capsys): @pytest.mark.unit -@pytest.mark.usefixtures("reset_globals") +@pytest.mark.usefixtures("reset_mt_config") def test_main_support(capsys): """Test --support""" sys.argv = ["", "--support"] - globals.args = sys.argv + mt_config.args = sys.argv with pytest.raises(SystemExit) as pytest_wrapped_e: main() @@ -114,16 +114,16 @@ def test_main_support(capsys): @pytest.mark.unit -@pytest.mark.usefixtures("reset_globals") +@pytest.mark.usefixtures("reset_mt_config") @patch("meshtastic.util.findPorts", return_value=[]) def test_main_ch_index_no_devices(patched_find_ports, capsys): """Test --ch-index 1""" sys.argv = ["", "--ch-index", "1"] - globals.args = sys.argv + mt_config.args = sys.argv with pytest.raises(SystemExit) as pytest_wrapped_e: main() - assert globals.channel_index == 1 + assert mt_config.channel_index == 1 assert pytest_wrapped_e.type == SystemExit assert pytest_wrapped_e.value.code == 1 out, err = capsys.readouterr() @@ -133,12 +133,12 @@ def test_main_ch_index_no_devices(patched_find_ports, capsys): @pytest.mark.unit -@pytest.mark.usefixtures("reset_globals") +@pytest.mark.usefixtures("reset_mt_config") @patch("meshtastic.util.findPorts", return_value=[]) def test_main_test_no_ports(patched_find_ports, capsys): """Test --test with no hardware""" sys.argv = ["", "--test"] - globals.args = sys.argv + mt_config.args = sys.argv with pytest.raises(SystemExit) as pytest_wrapped_e: main() @@ -153,12 +153,12 @@ def test_main_test_no_ports(patched_find_ports, capsys): @pytest.mark.unit -@pytest.mark.usefixtures("reset_globals") +@pytest.mark.usefixtures("reset_mt_config") @patch("meshtastic.util.findPorts", return_value=["/dev/ttyFake1"]) def test_main_test_one_port(patched_find_ports, capsys): """Test --test with one fake port""" sys.argv = ["", "--test"] - globals.args = sys.argv + mt_config.args = sys.argv with pytest.raises(SystemExit) as pytest_wrapped_e: main() @@ -173,12 +173,12 @@ def test_main_test_one_port(patched_find_ports, capsys): @pytest.mark.unit -@pytest.mark.usefixtures("reset_globals") +@pytest.mark.usefixtures("reset_mt_config") @patch("meshtastic.test.testAll", return_value=True) def test_main_test_two_ports_success(patched_test_all, capsys): """Test --test two fake ports and testAll() is a simulated success""" sys.argv = ["", "--test"] - globals.args = sys.argv + mt_config.args = sys.argv with pytest.raises(SystemExit) as pytest_wrapped_e: main() @@ -191,12 +191,12 @@ def test_main_test_two_ports_success(patched_test_all, capsys): @pytest.mark.unit -@pytest.mark.usefixtures("reset_globals") +@pytest.mark.usefixtures("reset_mt_config") @patch("meshtastic.test.testAll", return_value=False) def test_main_test_two_ports_fails(patched_test_all, capsys): """Test --test two fake ports and testAll() is a simulated failure""" sys.argv = ["", "--test"] - globals.args = sys.argv + mt_config.args = sys.argv with pytest.raises(SystemExit) as pytest_wrapped_e: main() @@ -209,11 +209,11 @@ def test_main_test_two_ports_fails(patched_test_all, capsys): @pytest.mark.unit -@pytest.mark.usefixtures("reset_globals") +@pytest.mark.usefixtures("reset_mt_config") def test_main_info(capsys, caplog): """Test --info""" sys.argv = ["", "--info"] - globals.args = sys.argv + mt_config.args = sys.argv iface = MagicMock(autospec=SerialInterface) @@ -234,12 +234,12 @@ def mock_showInfo(): @pytest.mark.unit -@pytest.mark.usefixtures("reset_globals") +@pytest.mark.usefixtures("reset_mt_config") @patch("os.getlogin") def test_main_info_with_permission_error(patched_getlogin, capsys, caplog): """Test --info""" sys.argv = ["", "--info"] - globals.args = sys.argv + mt_config.args = sys.argv patched_getlogin.return_value = "me" @@ -260,11 +260,11 @@ def test_main_info_with_permission_error(patched_getlogin, capsys, caplog): @pytest.mark.unit -@pytest.mark.usefixtures("reset_globals") +@pytest.mark.usefixtures("reset_mt_config") def test_main_info_with_tcp_interface(capsys): """Test --info""" sys.argv = ["", "--info", "--host", "meshtastic.local"] - globals.args = sys.argv + mt_config.args = sys.argv iface = MagicMock(autospec=TCPInterface) @@ -282,11 +282,11 @@ def mock_showInfo(): @pytest.mark.unit -@pytest.mark.usefixtures("reset_globals") +@pytest.mark.usefixtures("reset_mt_config") def test_main_no_proto(capsys): """Test --noproto (using --info for output)""" sys.argv = ["", "--info", "--noproto"] - globals.args = sys.argv + mt_config.args = sys.argv iface = MagicMock(autospec=SerialInterface) @@ -313,11 +313,11 @@ def my_sleep(amount): @pytest.mark.unit -@pytest.mark.usefixtures("reset_globals") +@pytest.mark.usefixtures("reset_mt_config") def test_main_info_with_seriallog_stdout(capsys): """Test --info""" sys.argv = ["", "--info", "--seriallog", "stdout"] - globals.args = sys.argv + mt_config.args = sys.argv iface = MagicMock(autospec=SerialInterface) @@ -335,11 +335,11 @@ def mock_showInfo(): @pytest.mark.unit -@pytest.mark.usefixtures("reset_globals") +@pytest.mark.usefixtures("reset_mt_config") def test_main_info_with_seriallog_output_txt(capsys): """Test --info""" sys.argv = ["", "--info", "--seriallog", "output.txt"] - globals.args = sys.argv + mt_config.args = sys.argv iface = MagicMock(autospec=SerialInterface) @@ -359,11 +359,11 @@ def mock_showInfo(): @pytest.mark.unit -@pytest.mark.usefixtures("reset_globals") +@pytest.mark.usefixtures("reset_mt_config") def test_main_qr(capsys): """Test --qr""" sys.argv = ["", "--qr"] - globals.args = sys.argv + mt_config.args = sys.argv iface = MagicMock(autospec=SerialInterface) # TODO: could mock/check url @@ -379,11 +379,11 @@ def test_main_qr(capsys): @pytest.mark.unit -@pytest.mark.usefixtures("reset_globals") +@pytest.mark.usefixtures("reset_mt_config") def test_main_onConnected_exception(capsys): """Test the exception in onConnected""" sys.argv = ["", "--qr"] - globals.args = sys.argv + mt_config.args = sys.argv def throw_an_exception(junk): raise Exception("Fake exception.") # pylint: disable=W0719 @@ -400,11 +400,11 @@ def throw_an_exception(junk): @pytest.mark.unit -@pytest.mark.usefixtures("reset_globals") +@pytest.mark.usefixtures("reset_mt_config") def test_main_nodes(capsys): """Test --nodes""" sys.argv = ["", "--nodes"] - globals.args = sys.argv + mt_config.args = sys.argv iface = MagicMock(autospec=SerialInterface) @@ -422,11 +422,11 @@ def mock_showNodes(): @pytest.mark.unit -@pytest.mark.usefixtures("reset_globals") +@pytest.mark.usefixtures("reset_mt_config") def test_main_set_owner_to_bob(capsys): """Test --set-owner bob""" sys.argv = ["", "--set-owner", "bob"] - globals.args = sys.argv + mt_config.args = sys.argv iface = MagicMock(autospec=SerialInterface) with patch("meshtastic.serial_interface.SerialInterface", return_value=iface) as mo: @@ -439,11 +439,11 @@ def test_main_set_owner_to_bob(capsys): @pytest.mark.unit -@pytest.mark.usefixtures("reset_globals") +@pytest.mark.usefixtures("reset_mt_config") def test_main_set_owner_short_to_bob(capsys): """Test --set-owner-short bob""" sys.argv = ["", "--set-owner-short", "bob"] - globals.args = sys.argv + mt_config.args = sys.argv iface = MagicMock(autospec=SerialInterface) with patch("meshtastic.serial_interface.SerialInterface", return_value=iface) as mo: @@ -456,11 +456,11 @@ def test_main_set_owner_short_to_bob(capsys): @pytest.mark.unit -@pytest.mark.usefixtures("reset_globals") +@pytest.mark.usefixtures("reset_mt_config") def test_main_set_canned_messages(capsys): """Test --set-canned-message""" sys.argv = ["", "--set-canned-message", "foo"] - globals.args = sys.argv + mt_config.args = sys.argv iface = MagicMock(autospec=SerialInterface) with patch("meshtastic.serial_interface.SerialInterface", return_value=iface) as mo: @@ -473,11 +473,11 @@ def test_main_set_canned_messages(capsys): @pytest.mark.unit -@pytest.mark.usefixtures("reset_globals") +@pytest.mark.usefixtures("reset_mt_config") def test_main_get_canned_messages(capsys, caplog, iface_with_nodes): """Test --get-canned-message""" sys.argv = ["", "--get-canned-message"] - globals.args = sys.argv + mt_config.args = sys.argv iface = iface_with_nodes iface.localNode.cannedPluginMessage = "foo" @@ -496,11 +496,11 @@ def test_main_get_canned_messages(capsys, caplog, iface_with_nodes): @pytest.mark.unit -@pytest.mark.usefixtures("reset_globals") +@pytest.mark.usefixtures("reset_mt_config") def test_main_set_ham_to_KI123(capsys): """Test --set-ham KI123""" sys.argv = ["", "--set-ham", "KI123"] - globals.args = sys.argv + mt_config.args = sys.argv mocked_node = MagicMock(autospec=Node) @@ -532,11 +532,11 @@ def mock_setOwner(name, is_licensed): @pytest.mark.unit -@pytest.mark.usefixtures("reset_globals") +@pytest.mark.usefixtures("reset_mt_config") def test_main_reboot(capsys): """Test --reboot""" sys.argv = ["", "--reboot"] - globals.args = sys.argv + mt_config.args = sys.argv mocked_node = MagicMock(autospec=Node) @@ -558,11 +558,11 @@ def mock_reboot(): @pytest.mark.unit -@pytest.mark.usefixtures("reset_globals") +@pytest.mark.usefixtures("reset_mt_config") def test_main_shutdown(capsys): """Test --shutdown""" sys.argv = ["", "--shutdown"] - globals.args = sys.argv + mt_config.args = sys.argv mocked_node = MagicMock(autospec=Node) @@ -584,11 +584,11 @@ def mock_shutdown(): @pytest.mark.unit -@pytest.mark.usefixtures("reset_globals") +@pytest.mark.usefixtures("reset_mt_config") def test_main_sendtext(capsys): """Test --sendtext""" sys.argv = ["", "--sendtext", "hello"] - globals.args = sys.argv + mt_config.args = sys.argv iface = MagicMock(autospec=SerialInterface) @@ -611,11 +611,11 @@ def mock_sendText( @pytest.mark.unit -@pytest.mark.usefixtures("reset_globals") +@pytest.mark.usefixtures("reset_mt_config") def test_main_sendtext_with_channel(capsys): """Test --sendtext""" sys.argv = ["", "--sendtext", "hello", "--ch-index", "1"] - globals.args = sys.argv + mt_config.args = sys.argv iface = MagicMock(autospec=SerialInterface) @@ -639,11 +639,11 @@ def mock_sendText( @pytest.mark.unit -@pytest.mark.usefixtures("reset_globals") +@pytest.mark.usefixtures("reset_mt_config") def test_main_sendtext_with_invalid_channel(caplog, capsys): """Test --sendtext""" sys.argv = ["", "--sendtext", "hello", "--ch-index", "-1"] - globals.args = sys.argv + mt_config.args = sys.argv iface = MagicMock(autospec=SerialInterface) iface.localNode.getChannelByChannelIndex.return_value = None @@ -663,11 +663,11 @@ def test_main_sendtext_with_invalid_channel(caplog, capsys): @pytest.mark.unit -@pytest.mark.usefixtures("reset_globals") +@pytest.mark.usefixtures("reset_mt_config") def test_main_sendtext_with_invalid_channel_nine(caplog, capsys): """Test --sendtext""" sys.argv = ["", "--sendtext", "hello", "--ch-index", "9"] - globals.args = sys.argv + mt_config.args = sys.argv iface = MagicMock(autospec=SerialInterface) iface.localNode.getChannelByChannelIndex.return_value = None @@ -687,7 +687,7 @@ def test_main_sendtext_with_invalid_channel_nine(caplog, capsys): @pytest.mark.unit -@pytest.mark.usefixtures("reset_globals") +@pytest.mark.usefixtures("reset_mt_config") @patch("termios.tcsetattr") @patch("termios.tcgetattr") @patch("builtins.open", new_callable=mock_open, read_data="data") @@ -696,7 +696,7 @@ def test_main_sendtext_with_invalid_channel_nine(caplog, capsys): def test_main_sendtext_with_dest(mock_findPorts, mock_serial, mocked_open, mock_get, mock_set, capsys, caplog, iface_with_nodes): """Test --sendtext with --dest""" sys.argv = ["", "--sendtext", "hello", "--dest", "foo"] - globals.args = sys.argv + mt_config.args = sys.argv #iface = iface_with_nodes #iface.myInfo.my_node_num = 2475227164 @@ -726,11 +726,11 @@ def test_main_sendtext_with_dest(mock_findPorts, mock_serial, mocked_open, mock_ @pytest.mark.unit -@pytest.mark.usefixtures("reset_globals") +@pytest.mark.usefixtures("reset_mt_config") def test_main_setlat(capsys): """Test --sendlat""" sys.argv = ["", "--setlat", "37.5"] - globals.args = sys.argv + mt_config.args = sys.argv mocked_node = MagicMock(autospec=Node) @@ -761,11 +761,11 @@ def mock_sendPosition(lat, lon, alt): @pytest.mark.unit -@pytest.mark.usefixtures("reset_globals") +@pytest.mark.usefixtures("reset_mt_config") def test_main_setlon(capsys): """Test --setlon""" sys.argv = ["", "--setlon", "-122.1"] - globals.args = sys.argv + mt_config.args = sys.argv mocked_node = MagicMock(autospec=Node) @@ -796,11 +796,11 @@ def mock_sendPosition(lat, lon, alt): @pytest.mark.unit -@pytest.mark.usefixtures("reset_globals") +@pytest.mark.usefixtures("reset_mt_config") def test_main_setalt(capsys): """Test --setalt""" sys.argv = ["", "--setalt", "51"] - globals.args = sys.argv + mt_config.args = sys.argv mocked_node = MagicMock(autospec=Node) @@ -831,11 +831,11 @@ def mock_sendPosition(lat, lon, alt): @pytest.mark.unit -@pytest.mark.usefixtures("reset_globals") +@pytest.mark.usefixtures("reset_mt_config") def test_main_seturl(capsys): """Test --seturl (url used below is what is generated after a factory_reset)""" sys.argv = ["", "--seturl", "https://www.meshtastic.org/d/#CgUYAyIBAQ"] - globals.args = sys.argv + mt_config.args = sys.argv iface = MagicMock(autospec=SerialInterface) with patch("meshtastic.serial_interface.SerialInterface", return_value=iface) as mo: @@ -847,7 +847,7 @@ def test_main_seturl(capsys): @pytest.mark.unit -@pytest.mark.usefixtures("reset_globals") +@pytest.mark.usefixtures("reset_mt_config") @patch("termios.tcsetattr") @patch("termios.tcgetattr") @patch("builtins.open", new_callable=mock_open, read_data="data") @@ -856,7 +856,7 @@ def test_main_seturl(capsys): def test_main_set_valid(mocked_findports, mocked_serial, mocked_open, mocked_get, mocked_set, capsys): """Test --set with valid field""" sys.argv = ["", "--set", "network.wifi_ssid", "foo"] - globals.args = sys.argv + mt_config.args = sys.argv serialInterface = SerialInterface(noProto=True) anode = Node(serialInterface, 1234567890, noProto=True) @@ -872,7 +872,7 @@ def test_main_set_valid(mocked_findports, mocked_serial, mocked_open, mocked_get @pytest.mark.unit -@pytest.mark.usefixtures("reset_globals") +@pytest.mark.usefixtures("reset_mt_config") @patch("termios.tcsetattr") @patch("termios.tcgetattr") @patch("builtins.open", new_callable=mock_open, read_data="data") @@ -881,7 +881,7 @@ def test_main_set_valid(mocked_findports, mocked_serial, mocked_open, mocked_get def test_main_set_valid_wifi_psk(mocked_findports, mocked_serial, mocked_open, mocked_get, mocked_set, capsys): """Test --set with valid field""" sys.argv = ["", "--set", "network.wifi_psk", "123456789"] - globals.args = sys.argv + mt_config.args = sys.argv serialInterface = SerialInterface(noProto=True) anode = Node(serialInterface, 1234567890, noProto=True) @@ -897,7 +897,7 @@ def test_main_set_valid_wifi_psk(mocked_findports, mocked_serial, mocked_open, m @pytest.mark.unit -@pytest.mark.usefixtures("reset_globals") +@pytest.mark.usefixtures("reset_mt_config") @patch("termios.tcsetattr") @patch("termios.tcgetattr") @patch("builtins.open", new_callable=mock_open, read_data="data") @@ -906,7 +906,7 @@ def test_main_set_valid_wifi_psk(mocked_findports, mocked_serial, mocked_open, m def test_main_set_invalid_wifi_psk(mocked_findports, mocked_serial, mocked_open, mocked_get, mocked_set, capsys): """Test --set with an invalid value (psk must be 8 or more characters)""" sys.argv = ["", "--set", "network.wifi_psk", "1234567"] - globals.args = sys.argv + mt_config.args = sys.argv serialInterface = SerialInterface(noProto=True) anode = Node(serialInterface, 1234567890, noProto=True) @@ -925,7 +925,7 @@ def test_main_set_invalid_wifi_psk(mocked_findports, mocked_serial, mocked_open, @pytest.mark.unit -@pytest.mark.usefixtures("reset_globals") +@pytest.mark.usefixtures("reset_mt_config") @patch("termios.tcsetattr") @patch("termios.tcgetattr") @patch("builtins.open", new_callable=mock_open, read_data="data") @@ -934,8 +934,8 @@ def test_main_set_invalid_wifi_psk(mocked_findports, mocked_serial, mocked_open, def test_main_set_valid_camel_case(mocked_findports, mocked_serial, mocked_open, mocked_get, mocked_set, capsys): """Test --set with valid field""" sys.argv = ["", "--set", "network.wifi_ssid", "foo"] - globals.args = sys.argv - globals.camel_case = True + mt_config.args = sys.argv + mt_config.camel_case = True serialInterface = SerialInterface(noProto=True) anode = Node(serialInterface, 1234567890, noProto=True) @@ -951,7 +951,7 @@ def test_main_set_valid_camel_case(mocked_findports, mocked_serial, mocked_open, @pytest.mark.unit -@pytest.mark.usefixtures("reset_globals") +@pytest.mark.usefixtures("reset_mt_config") @patch("termios.tcsetattr") @patch("termios.tcgetattr") @patch("builtins.open", new_callable=mock_open, read_data="data") @@ -960,7 +960,7 @@ def test_main_set_valid_camel_case(mocked_findports, mocked_serial, mocked_open, def test_main_set_with_invalid(mocked_findports, mocked_serial, mocked_open, mocked_get, mocked_set, capsys): """Test --set with invalid field""" sys.argv = ["", "--set", "foo", "foo"] - globals.args = sys.argv + mt_config.args = sys.argv serialInterface = SerialInterface(noProto=True) anode = Node(serialInterface, 1234567890, noProto=True) @@ -977,7 +977,7 @@ def test_main_set_with_invalid(mocked_findports, mocked_serial, mocked_open, moc # TODO: write some negative --configure tests @pytest.mark.unit -@pytest.mark.usefixtures("reset_globals") +@pytest.mark.usefixtures("reset_mt_config") @patch("termios.tcsetattr") @patch("termios.tcgetattr") @patch("builtins.open", new_callable=mock_open, read_data="data") @@ -986,7 +986,7 @@ def test_main_set_with_invalid(mocked_findports, mocked_serial, mocked_open, moc def test_main_configure_with_snake_case(mocked_findports, mocked_serial, mocked_open, mocked_get, mocked_set, capsys): """Test --configure with valid file""" sys.argv = ["", "--configure", "example_config.yaml"] - globals.args = sys.argv + mt_config.args = sys.argv serialInterface = SerialInterface(noProto=True) anode = Node(serialInterface, 1234567890, noProto=True) @@ -1010,7 +1010,7 @@ def test_main_configure_with_snake_case(mocked_findports, mocked_serial, mocked_ @pytest.mark.unit -@pytest.mark.usefixtures("reset_globals") +@pytest.mark.usefixtures("reset_mt_config") @patch("termios.tcsetattr") @patch("termios.tcgetattr") @patch("builtins.open", new_callable=mock_open, read_data="data") @@ -1019,7 +1019,7 @@ def test_main_configure_with_snake_case(mocked_findports, mocked_serial, mocked_ def test_main_configure_with_camel_case_keys(mocked_findports, mocked_serial, mocked_open, mocked_get, mocked_set, capsys): """Test --configure with valid file""" sys.argv = ["", "--configure", "exampleConfig.yaml"] - globals.args = sys.argv + mt_config.args = sys.argv serialInterface = SerialInterface(noProto=True) anode = Node(serialInterface, 1234567890, noProto=True) @@ -1042,11 +1042,11 @@ def test_main_configure_with_camel_case_keys(mocked_findports, mocked_serial, mo @pytest.mark.unit -@pytest.mark.usefixtures("reset_globals") +@pytest.mark.usefixtures("reset_mt_config") def test_main_ch_add_valid(capsys): """Test --ch-add with valid channel name, and that channel name does not already exist""" sys.argv = ["", "--ch-add", "testing"] - globals.args = sys.argv + mt_config.args = sys.argv mocked_channel = MagicMock(autospec=Channel) # TODO: figure out how to get it to print the channel name instead of MagicMock @@ -1070,11 +1070,11 @@ def test_main_ch_add_valid(capsys): @pytest.mark.unit -@pytest.mark.usefixtures("reset_globals") +@pytest.mark.usefixtures("reset_mt_config") def test_main_ch_add_invalid_name_too_long(capsys): """Test --ch-add with invalid channel name, name too long""" sys.argv = ["", "--ch-add", "testingtestingtesting"] - globals.args = sys.argv + mt_config.args = sys.argv mocked_channel = MagicMock(autospec=Channel) # TODO: figure out how to get it to print the channel name instead of MagicMock @@ -1101,11 +1101,11 @@ def test_main_ch_add_invalid_name_too_long(capsys): @pytest.mark.unit -@pytest.mark.usefixtures("reset_globals") +@pytest.mark.usefixtures("reset_mt_config") def test_main_ch_add_but_name_already_exists(capsys): """Test --ch-add with a channel name that already exists""" sys.argv = ["", "--ch-add", "testing"] - globals.args = sys.argv + mt_config.args = sys.argv mocked_node = MagicMock(autospec=Node) # set it up so we do not already have a channel named this @@ -1127,11 +1127,11 @@ def test_main_ch_add_but_name_already_exists(capsys): @pytest.mark.unit -@pytest.mark.usefixtures("reset_globals") +@pytest.mark.usefixtures("reset_mt_config") def test_main_ch_add_but_no_more_channels(capsys): """Test --ch-add with but there are no more channels""" sys.argv = ["", "--ch-add", "testing"] - globals.args = sys.argv + mt_config.args = sys.argv mocked_node = MagicMock(autospec=Node) # set it up so we do not already have a channel named this @@ -1155,11 +1155,11 @@ def test_main_ch_add_but_no_more_channels(capsys): @pytest.mark.unit -@pytest.mark.usefixtures("reset_globals") +@pytest.mark.usefixtures("reset_mt_config") def test_main_ch_del(capsys): """Test --ch-del with valid secondary channel to be deleted""" sys.argv = ["", "--ch-del", "--ch-index", "1"] - globals.args = sys.argv + mt_config.args = sys.argv mocked_node = MagicMock(autospec=Node) @@ -1176,11 +1176,11 @@ def test_main_ch_del(capsys): @pytest.mark.unit -@pytest.mark.usefixtures("reset_globals") +@pytest.mark.usefixtures("reset_mt_config") def test_main_ch_del_no_ch_index_specified(capsys): """Test --ch-del without a valid ch-index""" sys.argv = ["", "--ch-del"] - globals.args = sys.argv + mt_config.args = sys.argv mocked_node = MagicMock(autospec=Node) @@ -1200,12 +1200,12 @@ def test_main_ch_del_no_ch_index_specified(capsys): @pytest.mark.unit -@pytest.mark.usefixtures("reset_globals") +@pytest.mark.usefixtures("reset_mt_config") def test_main_ch_del_primary_channel(capsys): """Test --ch-del on ch-index=0""" sys.argv = ["", "--ch-del", "--ch-index", "0"] - globals.args = sys.argv - globals.channel_index = 1 + mt_config.args = sys.argv + mt_config.channel_index = 1 mocked_node = MagicMock(autospec=Node) @@ -1225,11 +1225,11 @@ def test_main_ch_del_primary_channel(capsys): @pytest.mark.unit -@pytest.mark.usefixtures("reset_globals") +@pytest.mark.usefixtures("reset_mt_config") def test_main_ch_enable_valid_secondary_channel(capsys): """Test --ch-enable with --ch-index""" sys.argv = ["", "--ch-enable", "--ch-index", "1"] - globals.args = sys.argv + mt_config.args = sys.argv mocked_node = MagicMock(autospec=Node) @@ -1242,16 +1242,16 @@ def test_main_ch_enable_valid_secondary_channel(capsys): assert re.search(r"Connected to radio", out, re.MULTILINE) assert re.search(r"Writing modified channels", out, re.MULTILINE) assert err == "" - assert globals.channel_index == 1 + assert mt_config.channel_index == 1 mo.assert_called() @pytest.mark.unit -@pytest.mark.usefixtures("reset_globals") +@pytest.mark.usefixtures("reset_mt_config") def test_main_ch_disable_valid_secondary_channel(capsys): """Test --ch-disable with --ch-index""" sys.argv = ["", "--ch-disable", "--ch-index", "1"] - globals.args = sys.argv + mt_config.args = sys.argv mocked_node = MagicMock(autospec=Node) @@ -1264,16 +1264,16 @@ def test_main_ch_disable_valid_secondary_channel(capsys): assert re.search(r"Connected to radio", out, re.MULTILINE) assert re.search(r"Writing modified channels", out, re.MULTILINE) assert err == "" - assert globals.channel_index == 1 + assert mt_config.channel_index == 1 mo.assert_called() @pytest.mark.unit -@pytest.mark.usefixtures("reset_globals") +@pytest.mark.usefixtures("reset_mt_config") def test_main_ch_enable_without_a_ch_index(capsys): """Test --ch-enable without --ch-index""" sys.argv = ["", "--ch-enable"] - globals.args = sys.argv + mt_config.args = sys.argv mocked_node = MagicMock(autospec=Node) @@ -1289,16 +1289,16 @@ def test_main_ch_enable_without_a_ch_index(capsys): assert re.search(r"Connected to radio", out, re.MULTILINE) assert re.search(r"Warning: Need to specify", out, re.MULTILINE) assert err == "" - assert globals.channel_index is None + assert mt_config.channel_index is None mo.assert_called() @pytest.mark.unit -@pytest.mark.usefixtures("reset_globals") +@pytest.mark.usefixtures("reset_mt_config") def test_main_ch_enable_primary_channel(capsys): """Test --ch-enable with --ch-index = 0""" sys.argv = ["", "--ch-enable", "--ch-index", "0"] - globals.args = sys.argv + mt_config.args = sys.argv mocked_node = MagicMock(autospec=Node) @@ -1314,20 +1314,20 @@ def test_main_ch_enable_primary_channel(capsys): assert re.search(r"Connected to radio", out, re.MULTILINE) assert re.search(r"Warning: Cannot enable/disable PRIMARY", out, re.MULTILINE) assert err == "" - assert globals.channel_index == 0 + assert mt_config.channel_index == 0 mo.assert_called() # TODO # @pytest.mark.unit -# @pytest.mark.usefixtures("reset_globals") +# @pytest.mark.usefixtures("reset_mt_config") # def test_main_ch_range_options(capsys): # """Test changing the various range options.""" # range_options = ['--ch-vlongslow', '--ch-longslow', '--ch-longfast', '--ch-midslow', # '--ch-midfast', '--ch-shortslow', '--ch-shortfast'] # for range_option in range_options: # sys.argv = ['', f"{range_option}" ] -# globals.args = sys.argv +# mt_config.args = sys.argv # # mocked_node = MagicMock(autospec=Node) # @@ -1344,11 +1344,11 @@ def test_main_ch_enable_primary_channel(capsys): @pytest.mark.unit -@pytest.mark.usefixtures("reset_globals") +@pytest.mark.usefixtures("reset_mt_config") def test_main_ch_longfast_on_non_primary_channel(capsys): """Test --ch-longfast --ch-index 1""" sys.argv = ["", "--ch-longfast", "--ch-index", "1"] - globals.args = sys.argv + mt_config.args = sys.argv mocked_node = MagicMock(autospec=Node) @@ -1383,11 +1383,11 @@ def test_main_ch_longfast_on_non_primary_channel(capsys): # TODO # @pytest.mark.unit -# @pytest.mark.usefixtures("reset_globals") +# @pytest.mark.usefixtures("reset_mt_config") # def test_main_pos_fields_no_args(capsys): # """Test --pos-fields no args (which shows settings)""" # sys.argv = ['', '--pos-fields'] -# globals.args = sys.argv +# mt_config.args = sys.argv # # pos_flags = MagicMock(autospec=meshtastic.radioconfig_pb2.PositionFlags) # @@ -1415,11 +1415,11 @@ def test_main_ch_longfast_on_non_primary_channel(capsys): # TODO # @pytest.mark.unit -# @pytest.mark.usefixtures("reset_globals") +# @pytest.mark.usefixtures("reset_mt_config") # def test_main_pos_fields_arg_of_zero(capsys): # """Test --pos-fields an arg of 0 (which shows list)""" # sys.argv = ['', '--pos-fields', '0'] -# globals.args = sys.argv +# mt_config.args = sys.argv # # pos_flags = MagicMock(autospec=meshtastic.radioconfig_pb2.PositionFlags) # @@ -1450,11 +1450,11 @@ def test_main_ch_longfast_on_non_primary_channel(capsys): # TODO # @pytest.mark.unit -# @pytest.mark.usefixtures("reset_globals") +# @pytest.mark.usefixtures("reset_mt_config") # def test_main_pos_fields_valid_values(capsys): # """Test --pos-fields with valid values""" # sys.argv = ['', '--pos-fields', 'POS_GEO_SEP', 'POS_ALT_MSL'] -# globals.args = sys.argv +# mt_config.args = sys.argv # # pos_flags = MagicMock(autospec=meshtastic.radioconfig_pb2.PositionFlags) # @@ -1478,11 +1478,11 @@ def test_main_ch_longfast_on_non_primary_channel(capsys): # TODO # @pytest.mark.unit -# @pytest.mark.usefixtures("reset_globals") +# @pytest.mark.usefixtures("reset_mt_config") # def test_main_get_with_valid_values(capsys): # """Test --get with valid values (with string, number, boolean)""" # sys.argv = ['', '--get', 'ls_secs', '--get', 'wifi_ssid', '--get', 'fixed_position'] -# globals.args = sys.argv +# mt_config.args = sys.argv # # with patch('meshtastic.serial_interface.SerialInterface') as mo: # @@ -1504,12 +1504,12 @@ def test_main_ch_longfast_on_non_primary_channel(capsys): # TODO #@pytest.mark.unit -#@pytest.mark.usefixtures("reset_globals") +#@pytest.mark.usefixtures("reset_mt_config") #def test_main_get_with_valid_values_camel(capsys, caplog): # """Test --get with valid values (with string, number, boolean)""" # sys.argv = ["", "--get", "lsSecs", "--get", "wifiSsid", "--get", "fixedPosition"] -# globals.args = sys.argv -# globals.camel_case = True +# mt_config.args = sys.argv +# mt_config.camel_case = True # # with caplog.at_level(logging.DEBUG): # with patch("meshtastic.serial_interface.SerialInterface") as mo: @@ -1530,11 +1530,11 @@ def test_main_ch_longfast_on_non_primary_channel(capsys): @pytest.mark.unit -@pytest.mark.usefixtures("reset_globals") +@pytest.mark.usefixtures("reset_mt_config") def test_main_get_with_invalid(capsys): """Test --get with invalid field""" sys.argv = ["", "--get", "foo"] - globals.args = sys.argv + mt_config.args = sys.argv mocked_user_prefs = MagicMock() mocked_user_prefs.DESCRIPTOR.fields_by_name.get.return_value = None @@ -1557,11 +1557,11 @@ def test_main_get_with_invalid(capsys): @pytest.mark.unit -@pytest.mark.usefixtures("reset_globals") +@pytest.mark.usefixtures("reset_mt_config") def test_main_onReceive_empty(caplog, capsys): """Test onReceive""" args = MagicMock() - globals.args = args + mt_config.args = args iface = MagicMock(autospec=SerialInterface) packet = {} with caplog.at_level(logging.DEBUG): @@ -1587,14 +1587,14 @@ def test_main_onReceive_empty(caplog, capsys): @pytest.mark.unit -@pytest.mark.usefixtures("reset_globals") +@pytest.mark.usefixtures("reset_mt_config") def test_main_onReceive_with_sendtext(caplog, capsys): """Test onReceive with sendtext The entire point of this test is to make sure the interface.close() call is made in onReceive(). """ sys.argv = ["", "--sendtext", "hello"] - globals.args = sys.argv + mt_config.args = sys.argv # Note: 'TEXT_MESSAGE_APP' value is 1 packet = { @@ -1620,12 +1620,12 @@ def test_main_onReceive_with_sendtext(caplog, capsys): @pytest.mark.unit -@pytest.mark.usefixtures("reset_globals") +@pytest.mark.usefixtures("reset_mt_config") def test_main_onReceive_with_text(caplog, capsys): """Test onReceive with text""" args = MagicMock() args.sendtext.return_value = "foo" - globals.args = args + mt_config.args = args # Note: 'TEXT_MESSAGE_APP' value is 1 # Note: Some of this is faked below. @@ -1655,11 +1655,11 @@ def test_main_onReceive_with_text(caplog, capsys): @pytest.mark.unit -@pytest.mark.usefixtures("reset_globals") +@pytest.mark.usefixtures("reset_mt_config") def test_main_onConnection(capsys): """Test onConnection""" sys.argv = [""] - globals.args = sys.argv + mt_config.args = sys.argv iface = MagicMock(autospec=SerialInterface) class TempTopic: @@ -1677,7 +1677,7 @@ def getName(self): @pytest.mark.unit -@pytest.mark.usefixtures("reset_globals") +@pytest.mark.usefixtures("reset_mt_config") def test_main_export_config(capsys): """Test export_config() function directly""" iface = MagicMock(autospec=SerialInterface) @@ -1724,10 +1724,10 @@ def test_main_export_config(capsys): # TODO # recursion depth exceeded error #@pytest.mark.unit -#@pytest.mark.usefixtures("reset_globals") +#@pytest.mark.usefixtures("reset_mt_config") #def test_main_export_config_use_camel(capsys): # """Test export_config() function directly""" -# globals.camel_case = True +# mt_config.camel_case = True # iface = MagicMock(autospec=SerialInterface) # with patch("meshtastic.serial_interface.SerialInterface", return_value=iface) as mo: # mo.getLongName.return_value = "foo" @@ -1770,11 +1770,11 @@ def test_main_export_config(capsys): # TODO # maximum recursion depth error #@pytest.mark.unit -#@pytest.mark.usefixtures("reset_globals") +#@pytest.mark.usefixtures("reset_mt_config") #def test_main_export_config_called_from_main(capsys): # """Test --export-config""" # sys.argv = ["", "--export-config"] -# globals.args = sys.argv +# mt_config.args = sys.argv # # iface = MagicMock(autospec=SerialInterface) # with patch("meshtastic.serial_interface.SerialInterface", return_value=iface) as mo: @@ -1787,11 +1787,11 @@ def test_main_export_config(capsys): @pytest.mark.unit -@pytest.mark.usefixtures("reset_globals") +@pytest.mark.usefixtures("reset_mt_config") def test_main_gpio_rd_no_gpio_channel(capsys): """Test --gpio_rd with no named gpio channel""" sys.argv = ["", "--gpio-rd", "0x10", "--dest", "!foo"] - globals.args = sys.argv + mt_config.args = sys.argv iface = MagicMock(autospec=SerialInterface) iface.localNode.getChannelByName.return_value = None @@ -1806,11 +1806,11 @@ def test_main_gpio_rd_no_gpio_channel(capsys): @pytest.mark.unit -@pytest.mark.usefixtures("reset_globals") +@pytest.mark.usefixtures("reset_mt_config") def test_main_gpio_rd_no_dest(capsys): """Test --gpio_rd with a named gpio channel but no dest was specified""" sys.argv = ["", "--gpio-rd", "0x2000"] - globals.args = sys.argv + mt_config.args = sys.argv channel = Channel(index=2, role=2) channel.settings.psk = b"\x8a\x94y\x0e\xc6\xc9\x1e5\x91\x12@\xa60\xa8\xb43\x87\x00\xf2K\x0e\xe7\x7fAz\xcd\xf5\xb0\x900\xa84" @@ -1830,7 +1830,7 @@ def test_main_gpio_rd_no_dest(capsys): # TODO # @pytest.mark.unit -# @pytest.mark.usefixtures("reset_globals") +# @pytest.mark.usefixtures("reset_mt_config") # @patch('time.sleep') # def test_main_gpio_rd(caplog, capsys): # """Test --gpio_rd with a named gpio channel""" @@ -1844,7 +1844,7 @@ def test_main_gpio_rd_no_dest(capsys): # # >>> print(hex(2**13)) # # 0x2000 # sys.argv = ['', '--gpio-rd', '0x1000', '--dest', '!1234'] -# globals.args = sys.argv +# mt_config.args = sys.argv # # channel = Channel(index=1, role=1) # channel.settings.modem_config = 3 @@ -1887,12 +1887,12 @@ def test_main_gpio_rd_no_dest(capsys): # TODO # @pytest.mark.unit -# @pytest.mark.usefixtures("reset_globals") +# @pytest.mark.usefixtures("reset_mt_config") # @patch('time.sleep') # def test_main_gpio_rd_with_no_gpioMask(caplog, capsys): # """Test --gpio_rd with a named gpio channel""" # sys.argv = ['', '--gpio-rd', '0x1000', '--dest', '!1234'] -# globals.args = sys.argv +# mt_config.args = sys.argv # # channel = Channel(index=1, role=1) # channel.settings.modem_config = 3 @@ -1935,11 +1935,11 @@ def test_main_gpio_rd_no_dest(capsys): # TODO # @pytest.mark.unit -# @pytest.mark.usefixtures("reset_globals") +# @pytest.mark.usefixtures("reset_mt_config") # def test_main_gpio_watch(caplog, capsys): # """Test --gpio_watch with a named gpio channel""" # sys.argv = ['', '--gpio-watch', '0x1000', '--dest', '!1234'] -# globals.args = sys.argv +# mt_config.args = sys.argv # # def my_sleep(amount): # print(f'{amount}') @@ -1989,11 +1989,11 @@ def test_main_gpio_rd_no_dest(capsys): # TODO # @pytest.mark.unit -# @pytest.mark.usefixtures("reset_globals") +# @pytest.mark.usefixtures("reset_mt_config") # def test_main_gpio_wrb(caplog, capsys): # """Test --gpio_wrb with a named gpio channel""" # sys.argv = ['', '--gpio-wrb', '4', '1', '--dest', '!1234'] -# globals.args = sys.argv +# mt_config.args = sys.argv # # channel = Channel(index=1, role=1) # channel.settings.modem_config = 3 @@ -2038,7 +2038,7 @@ def test_main_gpio_rd_no_dest(capsys): # TODO # need to restructure these for nested configs #@pytest.mark.unit -#@pytest.mark.usefixtures("reset_globals") +#@pytest.mark.usefixtures("reset_mt_config") #def test_main_getPref_valid_field(capsys): # """Test getPref() with a valid field""" # prefs = MagicMock() @@ -2054,10 +2054,10 @@ def test_main_gpio_rd_no_dest(capsys): # # #@pytest.mark.unit -#@pytest.mark.usefixtures("reset_globals") +#@pytest.mark.usefixtures("reset_mt_config") #def test_main_getPref_valid_field_camel(capsys): # """Test getPref() with a valid field""" -# globals.camel_case = True +# mt_config.camel_case = True # prefs = MagicMock() # prefs.DESCRIPTOR.fields_by_name.get.return_value = "ls_secs" # prefs.wifi_ssid = "foo" @@ -2071,7 +2071,7 @@ def test_main_gpio_rd_no_dest(capsys): # # #@pytest.mark.unit -#@pytest.mark.usefixtures("reset_globals") +#@pytest.mark.usefixtures("reset_mt_config") #def test_main_getPref_valid_field_string(capsys): # """Test getPref() with a valid field and value as a string""" # prefs = MagicMock() @@ -2087,10 +2087,10 @@ def test_main_gpio_rd_no_dest(capsys): # # #@pytest.mark.unit -#@pytest.mark.usefixtures("reset_globals") +#@pytest.mark.usefixtures("reset_mt_config") #def test_main_getPref_valid_field_string_camel(capsys): # """Test getPref() with a valid field and value as a string""" -# globals.camel_case = True +# mt_config.camel_case = True # prefs = MagicMock() # prefs.DESCRIPTOR.fields_by_name.get.return_value = "wifi_ssid" # prefs.wifi_ssid = "foo" @@ -2104,7 +2104,7 @@ def test_main_gpio_rd_no_dest(capsys): # # #@pytest.mark.unit -#@pytest.mark.usefixtures("reset_globals") +#@pytest.mark.usefixtures("reset_mt_config") #def test_main_getPref_valid_field_bool(capsys): # """Test getPref() with a valid field and value as a bool""" # prefs = MagicMock() @@ -2120,10 +2120,10 @@ def test_main_gpio_rd_no_dest(capsys): # # #@pytest.mark.unit -#@pytest.mark.usefixtures("reset_globals") +#@pytest.mark.usefixtures("reset_mt_config") #def test_main_getPref_valid_field_bool_camel(capsys): # """Test getPref() with a valid field and value as a bool""" -# globals.camel_case = True +# mt_config.camel_case = True # prefs = MagicMock() # prefs.DESCRIPTOR.fields_by_name.get.return_value = "fixed_position" # prefs.wifi_ssid = "foo" @@ -2137,7 +2137,7 @@ def test_main_gpio_rd_no_dest(capsys): # # #@pytest.mark.unit -#@pytest.mark.usefixtures("reset_globals") +#@pytest.mark.usefixtures("reset_mt_config") #def test_main_getPref_invalid_field(capsys): # """Test getPref() with an invalid field""" # @@ -2169,10 +2169,10 @@ def test_main_gpio_rd_no_dest(capsys): # # #@pytest.mark.unit -#@pytest.mark.usefixtures("reset_globals") +#@pytest.mark.usefixtures("reset_mt_config") #def test_main_getPref_invalid_field_camel(capsys): # """Test getPref() with an invalid field""" -# globals.camel_case = True +# mt_config.camel_case = True # # class Field: # """Simple class for testing.""" @@ -2202,7 +2202,7 @@ def test_main_gpio_rd_no_dest(capsys): # # #@pytest.mark.unit -#@pytest.mark.usefixtures("reset_globals") +#@pytest.mark.usefixtures("reset_mt_config") #def test_main_setPref_valid_field_int_as_string(capsys): # """Test setPref() with a valid field""" # @@ -2226,7 +2226,7 @@ def test_main_gpio_rd_no_dest(capsys): # TODO # @pytest.mark.unit -# @pytest.mark.usefixtures("reset_globals") +# @pytest.mark.usefixtures("reset_mt_config") # def test_main_setPref_valid_field_invalid_enum(capsys, caplog): # """Test setPref() with a valid field but invalid enum value""" # @@ -2245,7 +2245,7 @@ def test_main_gpio_rd_no_dest(capsys): # TODO # @pytest.mark.unit -# @pytest.mark.usefixtures("reset_globals") +# @pytest.mark.usefixtures("reset_mt_config") # def test_main_setPref_valid_field_invalid_enum_where_enums_are_camel_cased_values(capsys, caplog): # """Test setPref() with a valid field but invalid enum value""" # @@ -2264,10 +2264,10 @@ def test_main_gpio_rd_no_dest(capsys): # TODO # @pytest.mark.unit -# @pytest.mark.usefixtures("reset_globals") +# @pytest.mark.usefixtures("reset_mt_config") # def test_main_setPref_valid_field_invalid_enum_camel(capsys, caplog): # """Test setPref() with a valid field but invalid enum value""" -# globals.camel_case = True +# mt_config.camel_case = True # # radioConfig = RadioConfig() # prefs = radioConfig.preferences @@ -2281,7 +2281,7 @@ def test_main_gpio_rd_no_dest(capsys): # TODO # @pytest.mark.unit -# @pytest.mark.usefixtures("reset_globals") +# @pytest.mark.usefixtures("reset_mt_config") # def test_main_setPref_valid_field_valid_enum(capsys, caplog): # """Test setPref() with a valid field and valid enum value""" # @@ -2300,10 +2300,10 @@ def test_main_gpio_rd_no_dest(capsys): # TODO # @pytest.mark.unit -# @pytest.mark.usefixtures("reset_globals") +# @pytest.mark.usefixtures("reset_mt_config") # def test_main_setPref_valid_field_valid_enum_camel(capsys, caplog): # """Test setPref() with a valid field and valid enum value""" -# globals.camel_case = True +# mt_config.camel_case = True # # # charge_current # # some valid values: MA100 MA1000 MA1080 @@ -2320,7 +2320,7 @@ def test_main_gpio_rd_no_dest(capsys): # TODO # need to update for nested configs #@pytest.mark.unit -#@pytest.mark.usefixtures("reset_globals") +#@pytest.mark.usefixtures("reset_mt_config") #def test_main_setPref_invalid_field(capsys): # """Test setPref() with a invalid field""" # @@ -2351,10 +2351,10 @@ def test_main_gpio_rd_no_dest(capsys): # # #@pytest.mark.unit -#@pytest.mark.usefixtures("reset_globals") +#@pytest.mark.usefixtures("reset_mt_config") #def test_main_setPref_invalid_field_camel(capsys): # """Test setPref() with a invalid field""" -# globals.camel_case = True +# mt_config.camel_case = True # # class Field: # """Simple class for testing.""" @@ -2383,7 +2383,7 @@ def test_main_gpio_rd_no_dest(capsys): # # #@pytest.mark.unit -#@pytest.mark.usefixtures("reset_globals") +#@pytest.mark.usefixtures("reset_mt_config") #def test_main_setPref_ignore_incoming_123(capsys): # """Test setPref() with ignore_incoming""" # @@ -2407,7 +2407,7 @@ def test_main_gpio_rd_no_dest(capsys): # # #@pytest.mark.unit -#@pytest.mark.usefixtures("reset_globals") +#@pytest.mark.usefixtures("reset_mt_config") #def test_main_setPref_ignore_incoming_0(capsys): # """Test setPref() with ignore_incoming""" # @@ -2431,11 +2431,11 @@ def test_main_gpio_rd_no_dest(capsys): @pytest.mark.unit -@pytest.mark.usefixtures("reset_globals") +@pytest.mark.usefixtures("reset_mt_config") def test_main_ch_set_psk_no_ch_index(capsys): """Test --ch-set psk""" sys.argv = ["", "--ch-set", "psk", "foo", "--host", "meshtastic.local"] - globals.args = sys.argv + mt_config.args = sys.argv iface = MagicMock(autospec=TCPInterface) with patch("meshtastic.tcp_interface.TCPInterface", return_value=iface) as mo: @@ -2451,7 +2451,7 @@ def test_main_ch_set_psk_no_ch_index(capsys): @pytest.mark.unit -@pytest.mark.usefixtures("reset_globals") +@pytest.mark.usefixtures("reset_mt_config") def test_main_ch_set_psk_with_ch_index(capsys): """Test --ch-set psk""" sys.argv = [ @@ -2464,7 +2464,7 @@ def test_main_ch_set_psk_with_ch_index(capsys): "--ch-index", "0", ] - globals.args = sys.argv + mt_config.args = sys.argv iface = MagicMock(autospec=TCPInterface) with patch("meshtastic.tcp_interface.TCPInterface", return_value=iface) as mo: @@ -2479,7 +2479,7 @@ def test_main_ch_set_psk_with_ch_index(capsys): # TODO # doesn't work properly with nested/module config stuff #@pytest.mark.unit -#@pytest.mark.usefixtures("reset_globals") +#@pytest.mark.usefixtures("reset_mt_config") #def test_main_ch_set_name_with_ch_index(capsys): # """Test --ch-set setting other than psk""" # sys.argv = [ @@ -2492,7 +2492,7 @@ def test_main_ch_set_psk_with_ch_index(capsys): # "--ch-index", # "0", # ] -# globals.args = sys.argv +# mt_config.args = sys.argv # # iface = MagicMock(autospec=TCPInterface) # with patch("meshtastic.tcp_interface.TCPInterface", return_value=iface) as mo: @@ -2506,7 +2506,7 @@ def test_main_ch_set_psk_with_ch_index(capsys): @pytest.mark.unit -@pytest.mark.usefixtures("reset_globals") +@pytest.mark.usefixtures("reset_mt_config") def test_onNode(capsys): """Test onNode""" onNode("foo") @@ -2516,11 +2516,11 @@ def test_onNode(capsys): @pytest.mark.unit -@pytest.mark.usefixtures("reset_globals") +@pytest.mark.usefixtures("reset_mt_config") def test_tunnel_no_args(capsys): """Test tunnel no arguments""" sys.argv = [""] - globals.args = sys.argv + mt_config.args = sys.argv with pytest.raises(SystemExit) as pytest_wrapped_e: tunnelMain() assert pytest_wrapped_e.type == SystemExit @@ -2530,7 +2530,7 @@ def test_tunnel_no_args(capsys): @pytest.mark.unit -@pytest.mark.usefixtures("reset_globals") +@pytest.mark.usefixtures("reset_mt_config") @patch("meshtastic.util.findPorts", return_value=[]) @patch("platform.system") def test_tunnel_tunnel_arg_with_no_devices(mock_platform_system, caplog, capsys): @@ -2539,7 +2539,7 @@ def test_tunnel_tunnel_arg_with_no_devices(mock_platform_system, caplog, capsys) a_mock.return_value = "Linux" mock_platform_system.side_effect = a_mock sys.argv = ["", "--tunnel"] - globals.args = sys.argv + mt_config.args = sys.argv print(f"platform.system():{platform.system()}") with caplog.at_level(logging.DEBUG): with pytest.raises(SystemExit) as pytest_wrapped_e: @@ -2553,7 +2553,7 @@ def test_tunnel_tunnel_arg_with_no_devices(mock_platform_system, caplog, capsys) @pytest.mark.unit -@pytest.mark.usefixtures("reset_globals") +@pytest.mark.usefixtures("reset_mt_config") @patch("meshtastic.util.findPorts", return_value=[]) @patch("platform.system") def test_tunnel_subnet_arg_with_no_devices(mock_platform_system, caplog, capsys): @@ -2562,7 +2562,7 @@ def test_tunnel_subnet_arg_with_no_devices(mock_platform_system, caplog, capsys) a_mock.return_value = "Linux" mock_platform_system.side_effect = a_mock sys.argv = ["", "--subnet", "foo"] - globals.args = sys.argv + mt_config.args = sys.argv print(f"platform.system():{platform.system()}") with caplog.at_level(logging.DEBUG): with pytest.raises(SystemExit) as pytest_wrapped_e: @@ -2576,7 +2576,7 @@ def test_tunnel_subnet_arg_with_no_devices(mock_platform_system, caplog, capsys) @pytest.mark.unit -@pytest.mark.usefixtures("reset_globals") +@pytest.mark.usefixtures("reset_mt_config") @patch("platform.system") @patch("termios.tcsetattr") @patch("termios.tcgetattr") @@ -2597,7 +2597,7 @@ def my_sleep(amount): a_mock.return_value = "Linux" mock_platform_system.side_effect = a_mock sys.argv = ["", "--tunnel"] - globals.args = sys.argv + mt_config.args = sys.argv serialInterface = SerialInterface(noProto=True) diff --git a/meshtastic/tests/test_mesh_interface.py b/meshtastic/tests/test_mesh_interface.py index 791a92a9..7de9c923 100644 --- a/meshtastic/tests/test_mesh_interface.py +++ b/meshtastic/tests/test_mesh_interface.py @@ -16,7 +16,7 @@ @pytest.mark.unit -@pytest.mark.usefixtures("reset_globals") +@pytest.mark.usefixtures("reset_mt_config") def test_MeshInterface(capsys): """Test that we can instantiate a MeshInterface""" iface = MeshInterface(noProto=True) @@ -57,7 +57,7 @@ def test_MeshInterface(capsys): @pytest.mark.unit -@pytest.mark.usefixtures("reset_globals") +@pytest.mark.usefixtures("reset_mt_config") def test_getMyUser(iface_with_nodes): """Test getMyUser()""" iface = iface_with_nodes @@ -68,7 +68,7 @@ def test_getMyUser(iface_with_nodes): @pytest.mark.unit -@pytest.mark.usefixtures("reset_globals") +@pytest.mark.usefixtures("reset_mt_config") def test_getLongName(iface_with_nodes): """Test getLongName()""" iface = iface_with_nodes @@ -78,7 +78,7 @@ def test_getLongName(iface_with_nodes): @pytest.mark.unit -@pytest.mark.usefixtures("reset_globals") +@pytest.mark.usefixtures("reset_mt_config") def test_getShortName(iface_with_nodes): """Test getShortName().""" iface = iface_with_nodes @@ -88,7 +88,7 @@ def test_getShortName(iface_with_nodes): @pytest.mark.unit -@pytest.mark.usefixtures("reset_globals") +@pytest.mark.usefixtures("reset_mt_config") def test_handlePacketFromRadio_no_from(capsys): """Test _handlePacketFromRadio with no 'from' in the mesh packet.""" iface = MeshInterface(noProto=True) @@ -100,7 +100,7 @@ def test_handlePacketFromRadio_no_from(capsys): @pytest.mark.unit -@pytest.mark.usefixtures("reset_globals") +@pytest.mark.usefixtures("reset_mt_config") def test_handlePacketFromRadio_with_a_portnum(caplog): """Test _handlePacketFromRadio with a portnum Since we have an attribute called 'from', we cannot simply 'set' it. @@ -116,7 +116,7 @@ def test_handlePacketFromRadio_with_a_portnum(caplog): @pytest.mark.unit -@pytest.mark.usefixtures("reset_globals") +@pytest.mark.usefixtures("reset_mt_config") def test_handlePacketFromRadio_no_portnum(caplog): """Test _handlePacketFromRadio without a portnum""" iface = MeshInterface(noProto=True) @@ -128,7 +128,7 @@ def test_handlePacketFromRadio_no_portnum(caplog): @pytest.mark.unit -@pytest.mark.usefixtures("reset_globals") +@pytest.mark.usefixtures("reset_mt_config") def test_getNode_with_local(): """Test getNode""" iface = MeshInterface(noProto=True) @@ -137,7 +137,7 @@ def test_getNode_with_local(): @pytest.mark.unit -@pytest.mark.usefixtures("reset_globals") +@pytest.mark.usefixtures("reset_mt_config") def test_getNode_not_local(caplog): """Test getNode not local""" iface = MeshInterface(noProto=True) @@ -150,7 +150,7 @@ def test_getNode_not_local(caplog): @pytest.mark.unit -@pytest.mark.usefixtures("reset_globals") +@pytest.mark.usefixtures("reset_mt_config") def test_getNode_not_local_timeout(capsys): """Test getNode not local, simulate timeout""" iface = MeshInterface(noProto=True) @@ -167,7 +167,7 @@ def test_getNode_not_local_timeout(capsys): @pytest.mark.unit -@pytest.mark.usefixtures("reset_globals") +@pytest.mark.usefixtures("reset_mt_config") def test_sendPosition(caplog): """Test sendPosition""" iface = MeshInterface(noProto=True) @@ -179,7 +179,7 @@ def test_sendPosition(caplog): # TODO # @pytest.mark.unit -# @pytest.mark.usefixtures("reset_globals") +# @pytest.mark.usefixtures("reset_mt_config") # def test_close_with_heartbeatTimer(caplog): # """Test close() with heartbeatTimer""" # iface = MeshInterface(noProto=True) @@ -197,7 +197,7 @@ def test_sendPosition(caplog): # TODO # @pytest.mark.unit -# @pytest.mark.usefixtures("reset_globals") +# @pytest.mark.usefixtures("reset_mt_config") # def test_handleFromRadio_empty_payload(caplog): # """Test _handleFromRadio""" # iface = MeshInterface(noProto=True) @@ -208,7 +208,7 @@ def test_sendPosition(caplog): @pytest.mark.unit -@pytest.mark.usefixtures("reset_globals") +@pytest.mark.usefixtures("reset_mt_config") def test_handleFromRadio_with_my_info(caplog): """Test _handleFromRadio with my_info""" # Note: I captured the '--debug --info' for the bytes below. @@ -233,7 +233,7 @@ def test_handleFromRadio_with_my_info(caplog): @pytest.mark.unit -@pytest.mark.usefixtures("reset_globals") +@pytest.mark.usefixtures("reset_mt_config") def test_handleFromRadio_with_node_info(caplog, capsys): """Test _handleFromRadio with node_info""" # Note: I captured the '--debug --info' for the bytes below. @@ -269,7 +269,7 @@ def test_handleFromRadio_with_node_info(caplog, capsys): @pytest.mark.unit -@pytest.mark.usefixtures("reset_globals") +@pytest.mark.usefixtures("reset_mt_config") def test_handleFromRadio_with_node_info_tbeam1(caplog, capsys): """Test _handleFromRadio with node_info""" # Note: Captured the '--debug --info' for the bytes below. @@ -293,7 +293,7 @@ def test_handleFromRadio_with_node_info_tbeam1(caplog, capsys): @pytest.mark.unit -@pytest.mark.usefixtures("reset_globals") +@pytest.mark.usefixtures("reset_mt_config") def test_handleFromRadio_with_node_info_tbeam_with_bad_data(caplog): """Test _handleFromRadio with node_info with some bad data (issue#172) - ensure we do not throw exception""" # Note: Captured the '--debug --info' for the bytes below. @@ -305,7 +305,7 @@ def test_handleFromRadio_with_node_info_tbeam_with_bad_data(caplog): @pytest.mark.unit -@pytest.mark.usefixtures("reset_globals") +@pytest.mark.usefixtures("reset_mt_config") def test_MeshInterface_sendToRadioImpl(caplog): """Test _sendToRadioImp()""" iface = MeshInterface(noProto=True) @@ -316,7 +316,7 @@ def test_MeshInterface_sendToRadioImpl(caplog): @pytest.mark.unit -@pytest.mark.usefixtures("reset_globals") +@pytest.mark.usefixtures("reset_mt_config") def test_MeshInterface_sendToRadio_no_proto(caplog): """Test sendToRadio()""" iface = MeshInterface() @@ -327,7 +327,7 @@ def test_MeshInterface_sendToRadio_no_proto(caplog): @pytest.mark.unit -@pytest.mark.usefixtures("reset_globals") +@pytest.mark.usefixtures("reset_mt_config") def test_sendData_too_long(caplog): """Test when data payload is too big""" iface = MeshInterface(noProto=True) @@ -352,7 +352,7 @@ def test_sendData_too_long(caplog): @pytest.mark.unit -@pytest.mark.usefixtures("reset_globals") +@pytest.mark.usefixtures("reset_mt_config") def test_sendData_unknown_app(capsys): """Test sendData when unknown app""" iface = MeshInterface(noProto=True) @@ -366,7 +366,7 @@ def test_sendData_unknown_app(capsys): @pytest.mark.unit -@pytest.mark.usefixtures("reset_globals") +@pytest.mark.usefixtures("reset_mt_config") def test_sendPosition_with_a_position(caplog): """Test sendPosition when lat/long/alt""" iface = MeshInterface(noProto=True) @@ -378,7 +378,7 @@ def test_sendPosition_with_a_position(caplog): @pytest.mark.unit -@pytest.mark.usefixtures("reset_globals") +@pytest.mark.usefixtures("reset_mt_config") def test_sendPacket_with_no_destination(capsys): """Test _sendPacket()""" iface = MeshInterface(noProto=True) @@ -392,7 +392,7 @@ def test_sendPacket_with_no_destination(capsys): @pytest.mark.unit -@pytest.mark.usefixtures("reset_globals") +@pytest.mark.usefixtures("reset_mt_config") def test_sendPacket_with_destination_as_int(caplog): """Test _sendPacket() with int as a destination""" iface = MeshInterface(noProto=True) @@ -403,7 +403,7 @@ def test_sendPacket_with_destination_as_int(caplog): @pytest.mark.unit -@pytest.mark.usefixtures("reset_globals") +@pytest.mark.usefixtures("reset_mt_config") def test_sendPacket_with_destination_starting_with_a_bang(caplog): """Test _sendPacket() with int as a destination""" iface = MeshInterface(noProto=True) @@ -414,7 +414,7 @@ def test_sendPacket_with_destination_starting_with_a_bang(caplog): @pytest.mark.unit -@pytest.mark.usefixtures("reset_globals") +@pytest.mark.usefixtures("reset_mt_config") def test_sendPacket_with_destination_as_BROADCAST_ADDR(caplog): """Test _sendPacket() with BROADCAST_ADDR as a destination""" iface = MeshInterface(noProto=True) @@ -425,7 +425,7 @@ def test_sendPacket_with_destination_as_BROADCAST_ADDR(caplog): @pytest.mark.unit -@pytest.mark.usefixtures("reset_globals") +@pytest.mark.usefixtures("reset_mt_config") def test_sendPacket_with_destination_as_LOCAL_ADDR_no_myInfo(capsys): """Test _sendPacket() with LOCAL_ADDR as a destination with no myInfo""" iface = MeshInterface(noProto=True) @@ -440,7 +440,7 @@ def test_sendPacket_with_destination_as_LOCAL_ADDR_no_myInfo(capsys): @pytest.mark.unit -@pytest.mark.usefixtures("reset_globals") +@pytest.mark.usefixtures("reset_mt_config") def test_sendPacket_with_destination_as_LOCAL_ADDR_with_myInfo(caplog): """Test _sendPacket() with LOCAL_ADDR as a destination with myInfo""" iface = MeshInterface(noProto=True) @@ -454,7 +454,7 @@ def test_sendPacket_with_destination_as_LOCAL_ADDR_with_myInfo(caplog): @pytest.mark.unit -@pytest.mark.usefixtures("reset_globals") +@pytest.mark.usefixtures("reset_mt_config") def test_sendPacket_with_destination_is_blank_with_nodes(capsys, iface_with_nodes): """Test _sendPacket() with '' as a destination with myInfo""" iface = iface_with_nodes @@ -469,7 +469,7 @@ def test_sendPacket_with_destination_is_blank_with_nodes(capsys, iface_with_node @pytest.mark.unit -@pytest.mark.usefixtures("reset_globals") +@pytest.mark.usefixtures("reset_mt_config") def test_sendPacket_with_destination_is_blank_without_nodes(caplog, iface_with_nodes): """Test _sendPacket() with '' as a destination with myInfo""" iface = iface_with_nodes @@ -481,7 +481,7 @@ def test_sendPacket_with_destination_is_blank_without_nodes(caplog, iface_with_n @pytest.mark.unit -@pytest.mark.usefixtures("reset_globals") +@pytest.mark.usefixtures("reset_mt_config") def test_getMyNodeInfo(): """Test getMyNodeInfo()""" iface = MeshInterface(noProto=True) @@ -496,7 +496,7 @@ def test_getMyNodeInfo(): @pytest.mark.unit -@pytest.mark.usefixtures("reset_globals") +@pytest.mark.usefixtures("reset_mt_config") def test_generatePacketId(capsys): """Test _generatePacketId() when no currentPacketId (not connected)""" iface = MeshInterface(noProto=True) @@ -514,7 +514,7 @@ def test_generatePacketId(capsys): @pytest.mark.unit -@pytest.mark.usefixtures("reset_globals") +@pytest.mark.usefixtures("reset_mt_config") def test_fixupPosition_empty_pos(): """Test _fixupPosition()""" iface = MeshInterface(noProto=True) @@ -524,7 +524,7 @@ def test_fixupPosition_empty_pos(): @pytest.mark.unit -@pytest.mark.usefixtures("reset_globals") +@pytest.mark.usefixtures("reset_mt_config") def test_fixupPosition_no_changes_needed(): """Test _fixupPosition()""" iface = MeshInterface(noProto=True) @@ -534,7 +534,7 @@ def test_fixupPosition_no_changes_needed(): @pytest.mark.unit -@pytest.mark.usefixtures("reset_globals") +@pytest.mark.usefixtures("reset_mt_config") def test_fixupPosition(): """Test _fixupPosition()""" iface = MeshInterface(noProto=True) @@ -549,7 +549,7 @@ def test_fixupPosition(): @pytest.mark.unit -@pytest.mark.usefixtures("reset_globals") +@pytest.mark.usefixtures("reset_mt_config") def test_nodeNumToId(iface_with_nodes): """Test _nodeNumToId()""" iface = iface_with_nodes @@ -559,7 +559,7 @@ def test_nodeNumToId(iface_with_nodes): @pytest.mark.unit -@pytest.mark.usefixtures("reset_globals") +@pytest.mark.usefixtures("reset_mt_config") def test_nodeNumToId_not_found(iface_with_nodes): """Test _nodeNumToId()""" iface = iface_with_nodes @@ -569,7 +569,7 @@ def test_nodeNumToId_not_found(iface_with_nodes): @pytest.mark.unit -@pytest.mark.usefixtures("reset_globals") +@pytest.mark.usefixtures("reset_mt_config") def test_nodeNumToId_to_all(iface_with_nodes): """Test _nodeNumToId()""" iface = iface_with_nodes @@ -579,7 +579,7 @@ def test_nodeNumToId_to_all(iface_with_nodes): @pytest.mark.unit -@pytest.mark.usefixtures("reset_globals") +@pytest.mark.usefixtures("reset_mt_config") def test_getOrCreateByNum_minimal(iface_with_nodes): """Test _getOrCreateByNum()""" iface = iface_with_nodes @@ -589,7 +589,7 @@ def test_getOrCreateByNum_minimal(iface_with_nodes): @pytest.mark.unit -@pytest.mark.usefixtures("reset_globals") +@pytest.mark.usefixtures("reset_mt_config") def test_getOrCreateByNum_not_found(iface_with_nodes): """Test _getOrCreateByNum()""" iface = iface_with_nodes @@ -600,7 +600,7 @@ def test_getOrCreateByNum_not_found(iface_with_nodes): @pytest.mark.unit -@pytest.mark.usefixtures("reset_globals") +@pytest.mark.usefixtures("reset_mt_config") def test_getOrCreateByNum(iface_with_nodes): """Test _getOrCreateByNum()""" iface = iface_with_nodes diff --git a/meshtastic/tests/test_stream_interface.py b/meshtastic/tests/test_stream_interface.py index 7520559c..3411ffb8 100644 --- a/meshtastic/tests/test_stream_interface.py +++ b/meshtastic/tests/test_stream_interface.py @@ -20,7 +20,7 @@ def test_StreamInterface(): # Note: This takes a bit, so moving from unit to slow @pytest.mark.unitslow -@pytest.mark.usefixtures("reset_globals") +@pytest.mark.usefixtures("reset_mt_config") def test_StreamInterface_with_noProto(caplog): """Test that we can instantiate a StreamInterface based on nonProto and we can read/write bytes from a mocked stream @@ -41,7 +41,7 @@ def test_StreamInterface_with_noProto(caplog): ### Tip: If you want to see the print output, run with '-s' flag: ### pytest -s meshtastic/tests/test_stream_interface.py::test_sendToRadioImpl # @pytest.mark.unitslow -# @pytest.mark.usefixtures("reset_globals") +# @pytest.mark.usefixtures("reset_mt_config") # def test_sendToRadioImpl(caplog): # """Test _sendToRadioImpl()""" # diff --git a/meshtastic/tests/test_tunnel.py b/meshtastic/tests/test_tunnel.py index d4a95fc5..690e99a4 100644 --- a/meshtastic/tests/test_tunnel.py +++ b/meshtastic/tests/test_tunnel.py @@ -6,7 +6,7 @@ import pytest -from meshtastic import globals +from meshtastic import mt_config from ..tcp_interface import TCPInterface from ..tunnel import Tunnel, onTunnelReceive @@ -51,7 +51,7 @@ def test_Tunnel_with_interface(mock_platform_system, caplog, iface_with_nodes): with caplog.at_level(logging.WARNING): with patch("socket.socket"): tun = Tunnel(iface) - assert tun == globals.tunnelInstance + assert tun == mt_config.tunnelInstance iface.close() assert re.search(r"Not creating a TapDevice()", caplog.text, re.MULTILINE) assert re.search(r"Not starting TUN reader", caplog.text, re.MULTILINE) @@ -65,7 +65,7 @@ def test_onTunnelReceive_from_ourselves(mock_platform_system, caplog, iface_with iface = iface_with_nodes iface.myInfo.my_node_num = 2475227164 sys.argv = [""] - globals.args = sys.argv + mt_config.args = sys.argv packet = {"decoded": {"payload": "foo"}, "from": 2475227164} a_mock = MagicMock() a_mock.return_value = "Linux" @@ -73,7 +73,7 @@ def test_onTunnelReceive_from_ourselves(mock_platform_system, caplog, iface_with with caplog.at_level(logging.DEBUG): with patch("socket.socket"): tun = Tunnel(iface) - globals.tunnelInstance = tun + mt_config.tunnelInstance = tun onTunnelReceive(packet, iface) assert re.search(r"in onTunnelReceive", caplog.text, re.MULTILINE) assert re.search(r"Ignoring message we sent", caplog.text, re.MULTILINE) @@ -88,7 +88,7 @@ def test_onTunnelReceive_from_someone_else( iface = iface_with_nodes iface.myInfo.my_node_num = 2475227164 sys.argv = [""] - globals.args = sys.argv + mt_config.args = sys.argv packet = {"decoded": {"payload": "foo"}, "from": 123} a_mock = MagicMock() a_mock.return_value = "Linux" @@ -96,7 +96,7 @@ def test_onTunnelReceive_from_someone_else( with caplog.at_level(logging.DEBUG): with patch("socket.socket"): tun = Tunnel(iface) - globals.tunnelInstance = tun + mt_config.tunnelInstance = tun onTunnelReceive(packet, iface) assert re.search(r"in onTunnelReceive", caplog.text, re.MULTILINE) diff --git a/meshtastic/tunnel.py b/meshtastic/tunnel.py index 21563a1b..40a1c2eb 100644 --- a/meshtastic/tunnel.py +++ b/meshtastic/tunnel.py @@ -22,14 +22,14 @@ from pubsub import pub # type: ignore[import-untyped] from pytap2 import TapDevice -from meshtastic import portnums_pb2, globals +from meshtastic import portnums_pb2, mt_config from meshtastic.util import ipstr, readnet_u16 def onTunnelReceive(packet, interface): # pylint: disable=W0613 """Callback for received tunneled messages from mesh.""" logging.debug(f"in onTunnelReceive()") - tunnelInstance = globals.tunnelInstance + tunnelInstance = mt_config.tunnelInstance tunnelInstance.onReceive(packet) @@ -65,7 +65,7 @@ def __init__(self, iface, subnet="10.115", netmask="255.255.0.0"): if platform.system() != "Linux": raise Tunnel.TunnelError("Tunnel() can only be run instantiated on a Linux system") - globals.tunnelInstance = self + mt_config.tunnelInstance = self """A list of chatty UDP services we should never accidentally forward to our slow network"""