diff options
author | Elena ``of Valhalla'' Grandi <valhalla@trueelena.org> | 2022-03-14 19:31:44 +0100 |
---|---|---|
committer | Elena ``of Valhalla'' Grandi <valhalla@trueelena.org> | 2022-03-14 19:31:44 +0100 |
commit | 386a27282de20fbf4e41e9f9e3640deec7e77963 (patch) | |
tree | b2273db55f9f28425fbfa961c24c783955cbbb33 /tests | |
parent | 64147338d7f4f7ead19a618627b8b4ae4e7542ce (diff) |
Split logging setup so that it can be overridden
Diffstat (limited to 'tests')
-rw-r--r-- | tests/test_command.py | 42 |
1 files changed, 42 insertions, 0 deletions
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() |