Esempio n. 1
0
    def test_load_arduino_cli_invalid(self):
        # Test that an path that is not a file returns error
        success, conclusion, out, error, exit_code =\
            actions.load_arduino_cli(os.getcwd())
        self.assertFalse(success)
        self.assertTrue('Provided sketch path is not a valid' in conclusion)

        # Test for error if compiler dir is not set, default is None
        self.delete_default_settings_file()
        success, conclusion, out, error, exit_code = actions.load_arduino_cli()
        self.assertFalse(success)
        self.assertEqual(conclusion, 'Unable to find Arduino IDE')

        # Test for error if compiler dir is not set
        with mock.patch(
                'ardublocklyserver.actions.ServerCompilerSettings.compiler_dir',
                new_callable=mock.PropertyMock) as mock_compiler_dir:
            with mock.patch(
                    'ardublocklyserver.actions.ServerCompilerSettings.'
                    'load_ide_option', new_callable=mock.PropertyMock) as \
                    mock_load_ide_option:
                mock_compiler_dir.return_value = 'true'  # do nothing command
                mock_load_ide_option.return_value = None
                success, conclusion, out, error, exit_code = \
                    actions.load_arduino_cli()
                self.assertFalse(success)
                self.assertEqual(conclusion,
                                 'What should we do with the Sketch?')

        # Test for error if serial port unset, only required when set to upload
        ServerCompilerSettings().load_ide_option = 'upload'
        with mock.patch(
                'ardublocklyserver.actions.ServerCompilerSettings.compiler_dir',
                new_callable=mock.PropertyMock) as mock_compiler_dir:
            with mock.patch(
                    'ardublocklyserver.actions.ServerCompilerSettings.'
                    'get_serial_port_flag') as mock_get_serial_port_flag:
                mock_compiler_dir.return_value = 'true'  # do nothing command
                mock_get_serial_port_flag.return_value = None
                success, conclusion, out, error, exit_code = \
                    actions.load_arduino_cli()
                self.assertFalse(success)
                self.assertEqual(conclusion, 'Serial Port unavailable')

        # Test for error if board type unset, only required when set to upload
        ServerCompilerSettings().load_ide_option = 'upload'
        with mock.patch(
                'ardublocklyserver.actions.ServerCompilerSettings.compiler_dir',
                new_callable=mock.PropertyMock) as mock_compiler_dir:
            with mock.patch(
                    'ardublocklyserver.actions.ServerCompilerSettings.'
                    'get_arduino_board_flag') as mock_get_arduino_board_flag:
                mock_compiler_dir.return_value = 'true'  # do nothing command
                mock_get_arduino_board_flag.return_value = None
                success, conclusion, out, error, exit_code = \
                    actions.load_arduino_cli()
                self.assertFalse(success)
                self.assertEqual(conclusion, 'Unknown Arduino Board')
Esempio n. 2
0
    def test_load_arduino_cli_upload_unicode(
            self, mock_settings, mock_popen, mock_isfile):
        sketch_path = os.path.join(self.temp_folder, 'sketch.ino')
        compiler_dir = 'いろはにほへとちり'
        board_flag = 'whatever:flag'
        port = 'whatever_port'
        mock_settings.return_value.compiler_dir = compiler_dir
        mock_settings.return_value.get_arduino_board_flag = MagicMock()
        mock_settings.return_value.get_arduino_board_flag.return_value = \
            board_flag
        mock_settings.return_value.get_serial_port_flag = MagicMock()
        mock_settings.return_value.get_serial_port_flag.return_value = port
        mock_popen.return_value.communicate.return_value = \
            ('Γαζέες καὶ μυρτιὲς', 'Âne ex aéquo au whist')
        mock_popen.return_value.returncode = 0
        mock_isfile.return_value = True
        mock_settings.return_value.load_ide_option = 'upload'

        success, ide_mode, std_out, err_out, exit_code = \
            actions.load_arduino_cli(sketch_path)

        mock_popen.assert_called_with(
            [compiler_dir, sketch_path, '--upload', '--port', port, '--board',
             board_flag], shell=False, stderr=-1, stdout=-1)
        self.assertTrue(success)
        self.assertEqual(ide_mode, 'upload')
        self.assertEqual(std_out, 'out1')
        self.assertEqual(err_out, 'out2')
        self.assertEqual(exit_code, 0)
Esempio n. 3
0
    def test_load_arduino_cli_upload(
            self, mock_settings, mock_popen, mock_isfile):
        """
        Tests that a compiler path and arduino sketch path can be set
        and that a command line can be launched to open the sketch in the
        Arduino IDE.
        """
        sketch_path = os.path.join(self.temp_folder, 'sketch.ino')
        compiler_dir = 'whatever/arduino'
        board_flag = 'whatever:flag'
        port = 'whatever_port'
        mock_settings.return_value.compiler_dir = compiler_dir
        mock_settings.return_value.get_arduino_board_flag = MagicMock()
        mock_settings.return_value.get_arduino_board_flag.return_value = \
            board_flag
        mock_settings.return_value.get_serial_port_flag = MagicMock()
        mock_settings.return_value.get_serial_port_flag.return_value = port
        mock_popen.return_value.communicate.return_value = \
            ('out1'.encode('latin-1'), 'out2'.encode('latin-1'))
        mock_popen.return_value.returncode = 0
        mock_isfile.return_value = True
        mock_settings.return_value.load_ide_option = 'upload'

        success, ide_mode, std_out, err_out, exit_code = \
            actions.load_arduino_cli(sketch_path)

        mock_popen.assert_called_with(
            [compiler_dir, sketch_path, '--upload', '--port', port, '--board',
             board_flag], shell=False, stderr=-1, stdout=-1)
        self.assertTrue(success)
        self.assertEqual(ide_mode, 'upload')
        self.assertEqual(std_out, 'out1')
        self.assertEqual(err_out, 'out2')
        self.assertEqual(exit_code, 0)
Esempio n. 4
0
    def test_load_arduino_cli_sketch_path_invalid(self):
        invalid_sketch_path = os.path.join(self.temp_folder, 'bad.ino')

        success, ide_mode, std_out, err_out, exit_code = \
            actions.load_arduino_cli(invalid_sketch_path)

        self.assertFalse(success)
        self.assertEqual(ide_mode, 'unknown')
        self.assertEqual(std_out, '')
        self.assertTrue(invalid_sketch_path in err_out)
        self.assertEqual(exit_code, 52)
Esempio n. 5
0
    def test_load_arduino_cli_compiler_dir_invalid(
            self, mock_settings, mock_isfile):
        mock_settings.return_value.compiler_dir = None
        mock_isfile.return_value = True

        success, ide_mode, std_out, err_out, exit_code = \
            actions.load_arduino_cli(os.path.join(self.temp_folder, 's.ino'))

        self.assertFalse(success)
        self.assertEqual(ide_mode, 'unknown')
        self.assertEqual(std_out, '')
        self.assertEqual(exit_code, 53)
Esempio n. 6
0
    def test_load_arduino_cli_board_flag_invalid(
            self, mock_settings, mock_isfile):
        mock_isfile.return_value = True
        mock_settings.return_value.compiler_dir = 'compiler_dir'
        mock_settings.return_value.load_ide_option = 'upload'
        mock_settings.return_value.get_arduino_board_flag = MagicMock()
        mock_settings.return_value.get_arduino_board_flag.return_value = None

        success, ide_mode, std_out, err_out, exit_code = \
            actions.load_arduino_cli(os.path.join(self.temp_folder, 's.ino'))

        self.assertFalse(success)
        self.assertEqual(ide_mode, 'unknown')
        self.assertEqual(std_out, '')
        self.assertEqual(exit_code, 56)
Esempio n. 7
0
    def test_load_arduino_cli_open(
            self, mock_settings, mock_popen, mock_isfile):
        """
        Tests that a compiler path and arduino sketch path can be set
        and that a command line can be launched to open the sketch in the
        Arduino IDE.
        """
        sketch_path = os.path.join(self.temp_folder, 'sketch.ino')
        compiler_dir = 'whatever/arduino'
        mock_settings.return_value.compiler_dir = compiler_dir
        mock_popen.return_value.communicate.return_value = ('out1', 'out2')
        mock_isfile.return_value = True
        mock_settings.return_value.load_ide_option = 'open'

        success, ide_mode, std_out, err_out, exit_code = \
            actions.load_arduino_cli(sketch_path)

        mock_popen.assert_called_with([compiler_dir, sketch_path], shell=False)
        self.assertTrue(success)
        self.assertEqual(ide_mode, 'open')
        self.assertNotEqual(std_out, 'out1')
        self.assertNotEqual(err_out, 'out2')
        self.assertEqual(exit_code, 0)
Esempio n. 8
0
    def test_load_arduino_cli_exit_code_50(
            self, mock_settings, mock_popen, mock_isfile):
        sketch_path = os.path.join(self.temp_folder, 'sketch.ino')
        compiler_dir = 'whatever/arduino'
        board_flag = 'whatever:flag'
        mock_settings.return_value.compiler_dir = compiler_dir
        mock_settings.return_value.get_arduino_board_flag = MagicMock()
        mock_settings.return_value.get_arduino_board_flag.return_value =\
            board_flag
        mock_popen.return_value.communicate.return_value =\
            ('out1'.encode('latin-1'), 'out2'.encode('latin-1'))
        mock_popen.return_value.returncode = 51  # Will be replaced to 50
        mock_isfile.return_value = True
        mock_settings.return_value.load_ide_option = 'verify'

        success, ide_mode, std_out, err_out, exit_code = \
            actions.load_arduino_cli(sketch_path)

        self.assertFalse(success)
        self.assertEqual(ide_mode, 'verify')
        self.assertEqual(std_out, 'out1')
        self.assertTrue('51' in err_out)
        self.assertEqual(exit_code, 50)
Esempio n. 9
0
def handle_sketch(sketch_code):
    """
    Creates an Arduino Sketch and invokes the Arduino CLI.
    Creates a JSON string to return to the page with the following format:
    {"response_type": "ide_output",
     "element" : "div_ide_output",
     "success" : "true",
     "conclusion" : Short text as main conclusion,
     "output" : Output string,
     "error_output" : Output string,
     "exit_code": Exit code}
    """
    sketch_path = actions.create_sketch_from_string(sketch_code)
    success, conclusion, out, error, exit_code =\
        actions.load_arduino_cli(sketch_path)
    json_data = \
        {'response_type': 'ide_output',
         'element': 'div_ide_output',
         'success': success,
         'conclusion': conclusion,
         'output': out,
         'error_output': error,
         'exit_code': exit_code}
    return json.dumps(json_data)
def handle_sketch(sketch_code):
    """
    Creates an Arduino Sketch and invokes the Arduino CLI.
    Creates a JSON string to return to the page with the following format:
    {"response_type": "ide_output",
     "element" : "div_ide_output",
     "success" : "true",
     "conclusion" : Short text as main conclusion,
     "output" : Output string,
     "error_output" : Output string,
     "exit_code": Exit code}
    """
    sketch_path = actions.create_sketch_from_string(sketch_code)
    success, conclusion, out, error, exit_code =\
        actions.load_arduino_cli(sketch_path)
    json_data = \
        {'response_type': 'ide_output',
         'element': 'div_ide_output',
         'success': success,
         'conclusion': conclusion,
         'output': out,
         'error_output': error,
         'exit_code': exit_code}
    return json.dumps(json_data)
Esempio n. 11
0
    def test_load_arduino_cli_valid(self, mock_popen):
        """
        Tests that a compiler path and arduino sketch path can be set
        and that a command line can be launched to open the sketch in the
        Arduino IDE.
        """
        sketch_path = actions.create_sketch_default()

        ServerCompilerSettings().load_ide_option = 'open'
        with patch(
                'ardublocklyserver.actions.ServerCompilerSettings.compiler_dir',
                new_callable=mock.PropertyMock) as mock_compiler_dir:
            mock_compiler_dir.return_value = 'true'  # do nothing command
            expected_command = ['true', sketch_path]
            success, conclusion, out, error, exit_code = \
                actions.load_arduino_cli(sketch_path)
            mock_popen.assert_called_with(expected_command, shell=False)
            self.assertTrue(success)

        ServerCompilerSettings().load_ide_option = 'verify'
        with patch(
                'ardublocklyserver.actions.ServerCompilerSettings.compiler_dir',
                new_callable=mock.PropertyMock) as mock_compiler_dir:
            mock_compiler_dir.return_value = 'true'  # do nothing command
            mock_popen.return_value.communicate.return_value = ('test', 'test')
            mock_popen.return_value.returncode = 0
            expected_command = ['true', '--verify', sketch_path]
            success, conclusion, out, error, exit_code = \
                actions.load_arduino_cli(sketch_path)
            mock_popen.assert_called_with(expected_command,
                                          shell=False,
                                          stderr=-1,
                                          stdout=-1)
            self.assertTrue(success)

        ServerCompilerSettings().load_ide_option = 'upload'
        with patch(
                'ardublocklyserver.actions.ServerCompilerSettings.compiler_dir',
                new_callable=mock.PropertyMock) as mock_compiler_dir:
            mock_compiler_dir.return_value = 'true'  # do nothing command
            mock_popen.return_value.communicate.return_value = ('test', 'test')
            mock_popen.return_value.returncode = 0
            expected_command = [
                'true', '--upload', '--port',
                ServerCompilerSettings().get_serial_port_flag(), '--board',
                ServerCompilerSettings().get_arduino_board_flag(), sketch_path
            ]
            success, conclusion, out, error, exit_code = \
                actions.load_arduino_cli(sketch_path)
            mock_popen.assert_called_with(expected_command,
                                          shell=False,
                                          stderr=-1,
                                          stdout=-1)
            self.assertTrue(success)

        # Test for unicode strings as Py2 can be susceptible to fail there
        ServerCompilerSettings().load_ide_option = 'upload'
        with patch(
                'ardublocklyserver.actions.ServerCompilerSettings.compiler_dir',
                new_callable=mock.PropertyMock) as mock_compiler_dir:
            mock_compiler_dir.return_value = 'いろはにほへとちり'  # unicode
            mock_popen.return_value.communicate.return_value = (
                'Γαζέες καὶ μυρτιὲς', 'Âne ex aéquo au whist')
            mock_popen.return_value.returncode = 0
            expected_command = [
                mock_compiler_dir.return_value, '--upload', '--port',
                ServerCompilerSettings().get_serial_port_flag(), '--board',
                ServerCompilerSettings().get_arduino_board_flag(), sketch_path
            ]
            success, conclusion, out, error, exit_code = \
                actions.load_arduino_cli(sketch_path)
            mock_popen.assert_called_with(expected_command,
                                          shell=False,
                                          stderr=-1,
                                          stdout=-1)
            self.assertTrue(success)
Esempio n. 12
0
    def test_load_arduino_cli_valid(self, mock_popen):
        """
        Tests that a compiler path and arduino sketch path can be set
        and that a command line can be launched to open the sketch in the
        Arduino IDE.
        """
        sketch_path = actions.create_sketch_default()

        ServerCompilerSettings().load_ide_option = 'open'
        with mock.patch(
                'ardublocklyserver.actions.ServerCompilerSettings.compiler_dir',
                new_callable=mock.PropertyMock) as mock_compiler_dir:
            mock_compiler_dir.return_value = 'true'  # do nothing command
            expected_command = ['true', sketch_path]
            success, conclusion, out, error, exit_code = \
                actions.load_arduino_cli(sketch_path)
            mock_popen.assert_called_with(expected_command, shell=False)
            self.assertTrue(success)

        ServerCompilerSettings().load_ide_option = 'verify'
        with mock.patch(
                'ardublocklyserver.actions.ServerCompilerSettings.compiler_dir',
                new_callable=mock.PropertyMock) as mock_compiler_dir:
            mock_compiler_dir.return_value = 'true'  # do nothing command
            mock_popen.return_value.communicate.return_value = ('test', 'test')
            mock_popen.return_value.returncode = 0
            expected_command = ['true', '--verify', sketch_path]
            success, conclusion, out, error, exit_code = \
                actions.load_arduino_cli(sketch_path)
            mock_popen.assert_called_with(expected_command, shell=False,
                                          stderr=-1, stdout=-1)
            self.assertTrue(success)

        ServerCompilerSettings().load_ide_option = 'upload'
        with mock.patch(
                'ardublocklyserver.actions.ServerCompilerSettings.compiler_dir',
                new_callable=mock.PropertyMock) as mock_compiler_dir:
            mock_compiler_dir.return_value = 'true'  # do nothing command
            mock_popen.return_value.communicate.return_value = ('test', 'test')
            mock_popen.return_value.returncode = 0
            expected_command = [
                'true', '--upload', '--port',
                ServerCompilerSettings().get_serial_port_flag(), '--board',
                ServerCompilerSettings().get_arduino_board_flag(), sketch_path]
            success, conclusion, out, error, exit_code = \
                actions.load_arduino_cli(sketch_path)
            mock_popen.assert_called_with(expected_command, shell=False,
                                          stderr=-1, stdout=-1)
            self.assertTrue(success)

        # Test for unicode strings as Py2 can be susceptible to fail there
        ServerCompilerSettings().load_ide_option = 'upload'
        with mock.patch(
                'ardublocklyserver.actions.ServerCompilerSettings.compiler_dir',
                new_callable=mock.PropertyMock) as mock_compiler_dir:
            mock_compiler_dir.return_value = 'いろはにほへとちり'  # unicode
            mock_popen.return_value.communicate.return_value = (
                'Γαζέες καὶ μυρτιὲς', 'Âne ex aéquo au whist')
            mock_popen.return_value.returncode = 0
            expected_command = [
                mock_compiler_dir.return_value, '--upload', '--port',
                ServerCompilerSettings().get_serial_port_flag(), '--board',
                ServerCompilerSettings().get_arduino_board_flag(), sketch_path]
            success, conclusion, out, error, exit_code = \
                actions.load_arduino_cli(sketch_path)
            mock_popen.assert_called_with(expected_command, shell=False,
                                          stderr=-1, stdout=-1)
            self.assertTrue(success)