diff options
author | Elena ``of Valhalla'' Grandi <valhalla@trueelena.org> | 2022-02-22 17:03:55 +0100 |
---|---|---|
committer | Elena ``of Valhalla'' Grandi <valhalla@trueelena.org> | 2022-02-22 17:03:55 +0100 |
commit | 3195e3e213084f086dec5d81447e0db16d622337 (patch) | |
tree | 7ba1f2bd5dc0206a30084d16fe998fdede701b1e /tests | |
parent | 0582101ca0e07f5f9b62e2cc0be405f8e4bc2d7c (diff) |
Main Command
Diffstat (limited to 'tests')
-rw-r--r-- | tests/test_command.py | 73 |
1 files changed, 71 insertions, 2 deletions
diff --git a/tests/test_command.py b/tests/test_command.py index f5b824c..eed6e1f 100644 --- a/tests/test_command.py +++ b/tests/test_command.py @@ -1,10 +1,79 @@ +import contextlib +import io +import sys import unittest -from hazwaz import command +import hazwaz + + +class MyCommand(hazwaz.MainCommand): + """ + A command that does things. + + This is a command, but honestly it doesn't really do anything. + """ + commands = () + + def add_arguments(self, parser): + super().add_arguments(parser) + parser.add_argument( + "--foo", + help="foobar things" + ) class testCommand(unittest.TestCase): - pass + def _run_with_argv(self, cmd, argv): + stream = { + 'stdout': io.StringIO(), + 'stderr': io.StringIO(), + } + old_argv = sys.argv + sys.argv = argv + with contextlib.redirect_stdout(stream['stdout']): + with contextlib.redirect_stderr(stream['stderr']): + cmd.main() + sys.argv = old_argv + return stream + + def test_description(self): + cmd = MyCommand() + self.assertEqual( + cmd.parser.description, + "A command that does things." + ) + self.assertEqual( + cmd.parser.epilog, + "This is a command, but honestly it doesn't really do anything." + ) + + def test_description_none(self): + class NoHelpCommand(hazwaz.MainCommand): + pass + + cmd = NoHelpCommand() + self.assertEqual(cmd.parser.description, None) + + def test_description_empty(self): + class NoHelpCommand(hazwaz.MainCommand): + """ + """ + + cmd = NoHelpCommand() + self.assertEqual(cmd.parser.description, '') + + def test_arguments(self): + cmd = MyCommand() + cmd_help = cmd.parser.format_help() + self.assertIn("--verbose", cmd_help) + self.assertIn("--foo", cmd_help) + + def test_run(self): + cmd = MyCommand() + cmd_help = cmd.parser.format_help() + stream = self._run_with_argv(cmd, ["mycommand"]) + self.assertEqual(stream["stdout"].getvalue(), cmd_help) + if __name__ == '__main__': |