From aa786c7ebdc8f300db60c06e6538f50075383c64 Mon Sep 17 00:00:00 2001 From: Crank-Git Date: Sun, 8 Jun 2025 19:51:21 -0400 Subject: [PATCH] Fix linting errors and duplicate function --- meshtastic/tests/test_main.py | 64 +++++++++++++++++++++-------------- meshtastic/tests/test_node.py | 20 +++++------ 2 files changed, 48 insertions(+), 36 deletions(-) diff --git a/meshtastic/tests/test_main.py b/meshtastic/tests/test_main.py index 1d4040c..235829d 100644 --- a/meshtastic/tests/test_main.py +++ b/meshtastic/tests/test_main.py @@ -2713,24 +2713,6 @@ def test_remove_ignored_node(): main() mocked_node.removeIgnored.assert_called_once_with("!12345678") - -@pytest.mark.unit -@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"] - mt_config.args = sys.argv - - iface = MagicMock(autospec=SerialInterface) - with patch("meshtastic.serial_interface.SerialInterface", return_value=iface) as mo: - main() - out, err = capsys.readouterr() - assert re.search(r"Connected to radio", out, re.MULTILINE) - assert re.search(r"Setting device owner short to bob", out, re.MULTILINE) - assert err == "" - mo.assert_called() - - @pytest.mark.unit @pytest.mark.usefixtures("reset_mt_config") def test_main_set_owner_whitespace_only(capsys): @@ -2740,8 +2722,8 @@ def test_main_set_owner_whitespace_only(capsys): with pytest.raises(SystemExit) as excinfo: main() - - out, err = capsys.readouterr() + + out, _ = capsys.readouterr() assert "ERROR: Long Name cannot be empty or contain only whitespace characters" in out assert excinfo.value.code == 1 @@ -2755,8 +2737,8 @@ def test_main_set_owner_empty_string(capsys): with pytest.raises(SystemExit) as excinfo: main() - - out, err = capsys.readouterr() + + out, _ = capsys.readouterr() assert "ERROR: Long Name cannot be empty or contain only whitespace characters" in out assert excinfo.value.code == 1 @@ -2770,8 +2752,8 @@ def test_main_set_owner_short_whitespace_only(capsys): with pytest.raises(SystemExit) as excinfo: main() - - out, err = capsys.readouterr() + + out, _ = capsys.readouterr() assert "ERROR: Short Name cannot be empty or contain only whitespace characters" in out assert excinfo.value.code == 1 @@ -2785,7 +2767,37 @@ def test_main_set_owner_short_empty_string(capsys): with pytest.raises(SystemExit) as excinfo: main() - - out, err = capsys.readouterr() + + out, _ = capsys.readouterr() assert "ERROR: Short Name cannot be empty or contain only whitespace characters" in out assert excinfo.value.code == 1 + + +@pytest.mark.unit +@pytest.mark.usefixtures("reset_mt_config") +def test_main_set_ham_whitespace_only(capsys): + """Test --set-ham with whitespace-only name""" + sys.argv = ["", "--set-ham", " "] + mt_config.args = sys.argv + + with pytest.raises(SystemExit) as excinfo: + main() + + out, _ = capsys.readouterr() + assert "ERROR: Ham radio callsign cannot be empty or contain only whitespace characters" in out + assert excinfo.value.code == 1 + + +@pytest.mark.unit +@pytest.mark.usefixtures("reset_mt_config") +def test_main_set_ham_empty_string(capsys): + """Test --set-ham with empty string""" + sys.argv = ["", "--set-ham", ""] + mt_config.args = sys.argv + + with pytest.raises(SystemExit) as excinfo: + main() + + out, _ = capsys.readouterr() + assert "ERROR: Ham radio callsign cannot be empty or contain only whitespace characters" in out + assert excinfo.value.code == 1 diff --git a/meshtastic/tests/test_node.py b/meshtastic/tests/test_node.py index b1b235c..bb4e2d9 100644 --- a/meshtastic/tests/test_node.py +++ b/meshtastic/tests/test_node.py @@ -1487,8 +1487,8 @@ def test_setOwner_whitespace_only_long_name(capsys): with pytest.raises(SystemExit) as excinfo: anode.setOwner(long_name=" ") - - out, err = capsys.readouterr() + + out, _ = capsys.readouterr() assert "ERROR: Long Name cannot be empty or contain only whitespace characters" in out assert excinfo.value.code == 1 @@ -1501,8 +1501,8 @@ def test_setOwner_empty_long_name(capsys): with pytest.raises(SystemExit) as excinfo: anode.setOwner(long_name="") - - out, err = capsys.readouterr() + + out, _ = capsys.readouterr() assert "ERROR: Long Name cannot be empty or contain only whitespace characters" in out assert excinfo.value.code == 1 @@ -1515,8 +1515,8 @@ def test_setOwner_whitespace_only_short_name(capsys): with pytest.raises(SystemExit) as excinfo: anode.setOwner(short_name=" ") - - out, err = capsys.readouterr() + + out, _ = capsys.readouterr() assert "ERROR: Short Name cannot be empty or contain only whitespace characters" in out assert excinfo.value.code == 1 @@ -1529,8 +1529,8 @@ def test_setOwner_empty_short_name(capsys): with pytest.raises(SystemExit) as excinfo: anode.setOwner(short_name="") - - out, err = capsys.readouterr() + + out, _ = capsys.readouterr() assert "ERROR: Short Name cannot be empty or contain only whitespace characters" in out assert excinfo.value.code == 1 @@ -1540,10 +1540,10 @@ def test_setOwner_valid_names(caplog): """Test setOwner with valid names""" iface = MagicMock(autospec=MeshInterface) anode = Node(iface, 123, noProto=True) - + with caplog.at_level(logging.DEBUG): anode.setOwner(long_name="ValidName", short_name="VN") - + # Should not raise any exceptions and should call _sendAdmin iface._sendAdmin.assert_called_once() assert re.search(r'p.set_owner.long_name:ValidName:', caplog.text, re.MULTILINE)