From 386a27282de20fbf4e41e9f9e3640deec7e77963 Mon Sep 17 00:00:00 2001 From: Elena ``of Valhalla'' Grandi Date: Mon, 14 Mar 2022 19:31:44 +0100 Subject: Split logging setup so that it can be overridden --- tests/test_command.py | 42 ++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 42 insertions(+) (limited to 'tests/test_command.py') diff --git a/tests/test_command.py b/tests/test_command.py index 2f9850d..cc64b8f 100644 --- a/tests/test_command.py +++ b/tests/test_command.py @@ -1,5 +1,6 @@ import contextlib import io +import logging import sys import unittest @@ -25,6 +26,17 @@ class MySubCommand(hazwaz.Command): print("Hello World") +class LoggingSubCommand(hazwaz.Command): + """ + A subcommand that logs on various levels. + """ + + def main(self): + logging.debug("This is a DEBUG message") + logging.info("This is an INFO message") + logging.warning("This is a WARNING message") + + class MyCommand(hazwaz.MainCommand): """ A command that does things. @@ -33,6 +45,7 @@ class MyCommand(hazwaz.MainCommand): """ commands = ( MySubCommand(), + LoggingSubCommand(), ) def add_arguments(self, parser): @@ -158,6 +171,35 @@ class testCommand(unittest.TestCase): stream = self._run_with_argv(cmd, ["mycommand"]) self.assertEqual(stream["stdout"].getvalue(), cmd_help) + def test_logging_regular(self): + cmd = MyCommand() + with self.assertLogs() as cm: + self._run_with_argv(cmd, [ + "mycommand", + "loggingsubcommand", + ]) + self.assertEqual(len(cm.output), 1) + + def test_logging_verbose(self): + cmd = MyCommand() + with self.assertLogs() as cm: + self._run_with_argv(cmd, [ + "mycommand", + "--verbose", + "loggingsubcommand", + ]) + self.assertEqual(len(cm.output), 2) + + def test_logging_debug(self): + cmd = MyCommand() + with self.assertLogs() as cm: + self._run_with_argv(cmd, [ + "mycommand", + "--debug", + "loggingsubcommand", + ]) + self.assertEqual(len(cm.output), 3) + if __name__ == '__main__': unittest.main() -- cgit v1.2.3