/mandos/trunk

To get this branch, use:
bzr branch http://bzr.recompile.se/loggerhead/mandos/trunk

« back to all changes in this revision

Viewing changes to mandos-ctl

  • Committer: Teddy Hogeborn
  • Date: 2019-03-06 19:02:46 UTC
  • Revision ID: teddy@recompile.se-20190306190246-5mdmymvq62qpba3y
mandos-ctl: Refactor test

* mandos-ctl (commands_from_args): Removed.
  (TestOptions.assert_command_from_args): New.

Show diffs side-by-side

added added

removed removed

Lines of Context:
1119
1119
    def setUp(self):
1120
1120
        self.parser = argparse.ArgumentParser()
1121
1121
        add_command_line_options(self.parser)
1122
 
    def commands_from_args(self, args):
1123
 
        self.options = self.parser.parse_args(args)
1124
 
        return commands_from_options(self.options)
 
1122
    def assert_command_from_args(self, args, command_cls, **cmd_attrs):
 
1123
        """Assert that parsing ARGS should result in an instance of
 
1124
COMMAND_CLS with (optionally) all supplied attributes (CMD_ATTRS)."""
 
1125
        options = self.parser.parse_args(args)
 
1126
        commands = commands_from_options(options)
 
1127
        self.assertEqual(len(commands), 1)
 
1128
        command = commands[0]
 
1129
        self.assertIsInstance(command, command_cls)
 
1130
        for key, value in cmd_attrs.items():
 
1131
            self.assertEqual(getattr(command, key), value)
1125
1132
    def test_default_is_show_table(self):
1126
 
        commands = self.commands_from_args([])
1127
 
        self.assertEqual(len(commands), 1)
1128
 
        command = commands[0]
1129
 
        self.assertIsInstance(command, PrintTableCmd)
1130
 
        self.assertEqual(command.verbose, False)
 
1133
        self.assert_command_from_args([], PrintTableCmd,
 
1134
                                      verbose=False)
1131
1135
    def test_show_table_verbose(self):
1132
 
        commands = self.commands_from_args(["--verbose"])
1133
 
        self.assertEqual(len(commands), 1)
1134
 
        command = commands[0]
1135
 
        self.assertIsInstance(command, PrintTableCmd)
1136
 
        self.assertEqual(command.verbose, True)
 
1136
        self.assert_command_from_args(["--verbose"], PrintTableCmd,
 
1137
                                      verbose=True)
1137
1138
    def test_enable(self):
1138
 
        commands = self.commands_from_args(["--enable", "foo"])
1139
 
        self.assertEqual(len(commands), 1)
1140
 
        command = commands[0]
1141
 
        self.assertIsInstance(command, EnableCmd)
1142
 
        self.assertEqual(self.options.client, ["foo"])
 
1139
        self.assert_command_from_args(["--enable"], EnableCmd)
1143
1140
    def test_disable(self):
1144
 
        commands = self.commands_from_args(["--disable", "foo"])
1145
 
        self.assertEqual(len(commands), 1)
1146
 
        command = commands[0]
1147
 
        self.assertIsInstance(command, DisableCmd)
1148
 
        self.assertEqual(self.options.client, ["foo"])
 
1141
        self.assert_command_from_args(["--disable"], DisableCmd)
1149
1142
 
1150
1143
 
1151
1144