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_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_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"))