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)
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)
def test_bad_exit_without_parser(self): """ Test the error exit of the process without a parser. """ utility = ConsoleProgram() with self.assertRaises(SystemExit) as cm: utility.exit(1) self.assertEqual(cm.exception.code, 1)
def test_execution_no_commands(self): """ Assert a utility requires commands on execution """ # Create the utility and mock the exit utility = ConsoleProgram() # Execute the utility without registering commands with self.assertRaises(NotImplementedError): utility.execute()
def test_bad_exit_message_without_parser(self): """ Check stdout, stderr value on a error exit """ utility = ConsoleProgram() with self.assertRaises(SystemExit) as cm: utility.exit(1, "brimstone!") self.assertEqual("", self.read('stdout')) self.assertEqual("brimstone!", self.read('stderr'))
def test_good_exit_message_without_parser(self): """ Check stdout, stderr value on a good exit """ utility = ConsoleProgram() with self.assertRaises(SystemExit) as cm: utility.exit(0, "good") self.assertEqual("good", self.read('stdout')) self.assertEqual("", self.read('stderr'))
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()
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)
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")
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"))
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)
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()