Пример #1
0
    def test_duplicate_command_registration(self):
        """
        Ensure a command cannot be registered twice.
        """
        utility = ConsoleProgram()
        utility.register(GreetingCommand)

        with self.assertRaises(ConsoleError):
            utility.register(GreetingCommand)
Пример #2
0
    def test_duplicate_command_registration(self):
        """
        Ensure a command cannot be registered twice.
        """
        utility = ConsoleProgram()
        utility.register(GreetingCommand)

        with self.assertRaises(ConsoleError):
            utility.register(GreetingCommand)
Пример #3
0
    def test_command_registration(self):
        """
        Ensure that a command can be added to a program correctly.
        """
        utility = ConsoleProgram()
        utility.register(GreetingCommand)

        self.assertIn(GreetingCommand.name, utility.commands)
        self.assertIsNotNone(utility.commands[GreetingCommand.name].parser)
Пример #4
0
    def test_command_registration(self):
        """
        Ensure that a command can be added to a program correctly.
        """
        utility = ConsoleProgram()
        utility.register(GreetingCommand)

        self.assertIn(GreetingCommand.name, utility.commands)
        self.assertIsNotNone(utility.commands[GreetingCommand.name].parser)
Пример #5
0
    def test_handle_defaults_on_execution(self):
        """
        Assert handle default args is called on execute
        """
        # Create the utility and register a command
        utility = ConsoleProgram()
        utility.register(GreetingCommand)

        # Create fake arguments for the parser
        args = utility.parser.parse_args(["greeting", "bob", '--pythonpath', '/tmp/var/mypy'])

        # Mock the exit, handler and parser
        utility.exit = mock.MagicMock()
        utility.parser.parse_args = mock.MagicMock(return_value=args)

        # Execute the command
        utility.execute()

        # Check the execution status
        self.assertIn('/tmp/var/mypy', sys.path)
Пример #6
0
    def test_handle_defaults_on_execution(self):
        """
        Assert handle default args is called on execute
        """
        # Create the utility and register a command
        utility = ConsoleProgram()
        utility.register(GreetingCommand)

        # Create fake arguments for the parser
        args = utility.parser.parse_args(
            ["greeting", "bob", '--pythonpath', '/tmp/var/mypy'])

        # Mock the exit, handler and parser
        utility.exit = mock.MagicMock()
        utility.parser.parse_args = mock.MagicMock(return_value=args)

        # Execute the command
        utility.execute()

        # Check the execution status
        self.assertIn('/tmp/var/mypy', sys.path)
Пример #7
0
    def test_good_execution(self):
        """
        Assert a utility requires commands on execution
        """
        # Create the utility and register a command
        utility = ConsoleProgram()
        GreetingCommand.handle = mock.MagicMock(return_value="Hello Bob!")
        utility.register(GreetingCommand)

        # Create fake arguments for the parser
        args = utility.parser.parse_args(["greeting", "bob"])

        # Mock the exit, handler and parser
        utility.exit = mock.MagicMock()
        utility.parser.parse_args = mock.MagicMock(return_value=args)

        # Execute the command
        utility.execute()

        # Check the execution status
        utility.commands['greeting'].handle.assert_called_once_with(args)
        utility.exit.assert_called_once_with(0, "Hello Bob!\n")
Пример #8
0
    def test_bad_exit_with_parser(self):
        """
        Check that a bad exit is passed to the parser
        """
        # Setup the utility and register a command (forces parser creation)
        utility = ConsoleProgram()
        utility.register(GreetingCommand)

        # Mock the parser exit/error functions
        utility.parser.error = mock.MagicMock()
        utility.parser.error.side_effect = lambda m: sys.exit(1)
        utility.parser.exit = mock.MagicMock()
        utility.parser.exit.side_effect = lambda c,m: sys.exit(0)

        # Perform a bad exit
        with self.assertRaises(SystemExit) as cm:
            utility.exit(1, "brimstone!")

        # Check the exit status
        self.assertEqual(cm.exception.code, 1)
        utility.parser.error.assert_called_once_with("brimstone!")
        utility.parser.exit.assert_not_called()
Пример #9
0
    def test_good_execution(self):
        """
        Assert a utility requires commands on execution
        """
        # Create the utility and register a command
        utility = ConsoleProgram()
        GreetingCommand.handle = mock.MagicMock(return_value="Hello Bob!")
        utility.register(GreetingCommand)

        # Create fake arguments for the parser
        args = utility.parser.parse_args(["greeting", "bob"])

        # Mock the exit, handler and parser
        utility.exit = mock.MagicMock()
        utility.parser.parse_args = mock.MagicMock(return_value=args)

        # Execute the command
        utility.execute()

        # Check the execution status
        utility.commands['greeting'].handle.assert_called_once_with(args)
        utility.exit.assert_called_once_with(0, "Hello Bob!\n")
Пример #10
0
    def test_bad_exit_with_parser(self):
        """
        Check that a bad exit is passed to the parser
        """
        # Setup the utility and register a command (forces parser creation)
        utility = ConsoleProgram()
        utility.register(GreetingCommand)

        # Mock the parser exit/error functions
        utility.parser.error = mock.MagicMock()
        utility.parser.error.side_effect = lambda m: sys.exit(1)
        utility.parser.exit = mock.MagicMock()
        utility.parser.exit.side_effect = lambda c, m: sys.exit(0)

        # Perform a bad exit
        with self.assertRaises(SystemExit) as cm:
            utility.exit(1, "brimstone!")

        # Check the exit status
        self.assertEqual(cm.exception.code, 1)
        utility.parser.error.assert_called_once_with("brimstone!")
        utility.parser.exit.assert_not_called()
Пример #11
0
    def test_traceback_execution(self):
        """
        Assert a utility requires commands on execution
        """
        # Create the utility and register a command
        utility = ConsoleProgram()
        GreetingCommand.handle = mock.MagicMock(side_effect=ValueError("bad"))
        utility.register(GreetingCommand)

        # Create fake arguments for the parser
        args = utility.parser.parse_args(["greeting", "bob", '--traceback'])

        # Mock the exit and parser
        utility.exit = mock.MagicMock()
        utility.parser.parse_args = mock.MagicMock(return_value=args)

        # Execute the utility
        utility.execute()

        # Check the execution status
        utility.exit.assert_called_once_with(1, '\x1b[31mbad\x1b[39m')
        self.assertEqual("", self.read("stdout"))
        self.assertIn("ValueError", self.read("stderr"))
Пример #12
0
    def test_traceback_execution(self):
        """
        Assert a utility requires commands on execution
        """
        # Create the utility and register a command
        utility = ConsoleProgram()
        GreetingCommand.handle = mock.MagicMock(side_effect=ValueError("bad"))
        utility.register(GreetingCommand)

        # Create fake arguments for the parser
        args = utility.parser.parse_args(["greeting", "bob", '--traceback'])

        # Mock the exit and parser
        utility.exit = mock.MagicMock()
        utility.parser.parse_args = mock.MagicMock(return_value=args)

        # Execute the utility
        utility.execute()

        # Check the execution status
        utility.exit.assert_called_once_with(1, '\x1b[31mbad\x1b[39m')
        self.assertEqual("", self.read("stdout"))
        self.assertIn("ValueError", self.read("stderr"))