From 2f6e7e17e835d7b659cf3186c17d5975dd503019 Mon Sep 17 00:00:00 2001 From: Hemna Date: Sun, 12 Dec 2021 16:35:26 -0500 Subject: [PATCH] Added unit tests for dev test-plugin Also added a check to make sure that the aprs_login parameter is passed in for use as the fromcallsign. --- aprsd/cmds/dev.py | 13 ++++++++-- tests/cmds/test_dev.py | 58 ++++++++++++++++++++++++++++++++++++++++++ 2 files changed, 69 insertions(+), 2 deletions(-) create mode 100644 tests/cmds/test_dev.py diff --git a/aprsd/cmds/dev.py b/aprsd/cmds/dev.py index e9313b8..383ffcf 100644 --- a/aprsd/cmds/dev.py +++ b/aprsd/cmds/dev.py @@ -68,13 +68,22 @@ def test_plugin( ): """Test an individual APRSD plugin given a python path.""" config = ctx.obj["config"] - fromcall = aprs_login + + if not aprs_login: + if not config.exists("aprs.login"): + click.echo("Must set --aprs_login or APRS_LOGIN") + ctx.exit(-1) + return + else: + fromcall = config.get("aprs.login") + else: + fromcall = aprs_login if not plugin_path: click.echo(ctx.get_help()) click.echo("") ctx.fail("Failed to provide -p option to test a plugin") - ctx.exit() + return if type(message) is tuple: message = " ".join(message) diff --git a/tests/cmds/test_dev.py b/tests/cmds/test_dev.py new file mode 100644 index 0000000..fd2b20a --- /dev/null +++ b/tests/cmds/test_dev.py @@ -0,0 +1,58 @@ +import typing as t +import unittest +from unittest import mock + +from click.testing import CliRunner + +from aprsd import config as aprsd_config +from aprsd.aprsd import cli +from aprsd.cmds import dev # noqa + + +F = t.TypeVar("F", bound=t.Callable[..., t.Any]) + + +class TestDevTestPluginCommand(unittest.TestCase): + + def _build_config(self, login=None, password=None): + config = {"aprs": {}} + if login: + config["aprs"]["login"] = login + + if password: + config["aprs"]["password"] = password + + return aprsd_config.Config(config) + + @mock.patch("aprsd.config.parse_config") + @mock.patch("aprsd.log.setup_logging") + def test_no_login(self, mock_logging, mock_parse_config): + """Make sure we get an error if there is no login and config.""" + + runner = CliRunner() + mock_parse_config.return_value = self._build_config() + + result = runner.invoke( + cli, ["dev", "test-plugin", "bogus command"], + catch_exceptions=False, + ) + # rich.print(f"EXIT CODE {result.exit_code}") + # rich.print(f"Exception {result.exception}") + # rich.print(f"OUTPUT {result.output}") + assert result.exit_code == -1 + assert "Must set --aprs_login or APRS_LOGIN" in result.output + + @mock.patch("aprsd.config.parse_config") + @mock.patch("aprsd.log.setup_logging") + def test_no_plugin_arg(self, mock_logging, mock_parse_config): + """Make sure we get an error if there is no login and config.""" + + runner = CliRunner() + mock_parse_config.return_value = self._build_config(login="something") + + result = runner.invoke( + cli, ["dev", "test-plugin", "bogus command"], + catch_exceptions=False, + ) + assert result.exit_code == 2 + assert "Failed to provide -p option to test a plugin" in result.output