def test_arduino_ide_send_code_invalid( self, mock_load_arduino_cli, mock_create_sketch_from_string): """Test an error occurring inside arduino_ide_send_code function call. Because this function basically bridges two functions also tested here we only need to test the error condiction caused if the create_sketch_from_string() function fails. :param mock_load_arduino_cli: Mock for load_arduino_cli() :param mock_create_sketch_from_string: Mock for create_sketch_from_string() :return: None. """ mock_create_sketch_from_string.return_value = None success, ide_mode, std_out, err_out, exit_code = \ actions.arduino_ide_send_code('dummy sketch content here') self.assertTrue(mock_create_sketch_from_string.called) self.assertFalse(mock_load_arduino_cli.called) self.assertFalse(success) self.assertEqual(ide_mode, 'unknown') self.assertIsNone(std_out) self.assertIsNone(err_out) self.assertEqual(exit_code, 51)
def test_arduino_ide_send_code_valid( self, mock_load_arduino_cli, mock_create_sketch_from_string): """Test a valid input to arduino_ide_send_code function. Because this function basically bridges two functions also tested here we only need to test they've been called correctly. :param mock_load_arduino_cli: Mock for load_arduino_cli() :param mock_create_sketch_from_string: Mock for create_sketch_from_string() :return: None. """ actions.arduino_ide_send_code('dummy sketch content here') self.assertTrue(mock_create_sketch_from_string.called) self.assertTrue(mock_load_arduino_cli.called)
def handler_code_post(): """Handle sent Arduino Sketch code. Error codes: 0 - No error 1 - Build or Upload failed. 2 - Sketch not found. 3 - Invalid command line argument 4 - Preference passed to 'get-pref' flag does not exist 5 - Not Clear, but Arduino IDE sometimes errors with this. 50 - Unexpected error code from Arduino IDE 51 - Could not create sketch file 52 - Invalid path to internally created sketch file 53 - Compiler directory not configured in the Settings 54 - Launch IDE option not configured in the Settings 55 - Serial Port configured in Settings not accessible. 56 - Arduino Board not configured in the Settings. 52 - Unexpected server error. 64 - Unable to parse sent JSON. """ success = False ide_mode = 'unknown' std_out, err_out = '', '' exit_code = 52 response_dict = {'response_type': 'ide_output', 'response_state': 'full_response'} try: sketch_code = request.json['sketch_code'] except (TypeError, ValueError, KeyError) as e: exit_code = 64 err_out = 'Unable to parse sent JSON.' print('Error: Unable to parse sent JSON:\n%s' % str(e)) else: try: success, ide_mode, std_out, err_out, exit_code = \ actions.arduino_ide_send_code(sketch_code) except Exception as e: exit_code = 52 err_out += 'Unexpected server error.' print('Error: Exception in arduino_ide_send_code:\n%s' % str(e)) response_dict.update({'success': success, 'ide_mode': ide_mode, 'ide_data': { 'std_output': std_out, 'err_output': err_out, 'exit_code': exit_code}}) if not success: response_dict.update({ 'errors': [{ 'id': exit_code, 'description': 'More info available in the \'ide_data\' value.' }] }) set_header_no_cache() return response_dict