Ejemplo n.º 1
0
def test_flash_failed():
    """
    Ensure things are cleaned up if flashing failed.
    """
    view = mock.MagicMock()
    editor = mock.MagicMock()
    mm = MicrobitMode(editor, view)
    mm.set_buttons = mock.MagicMock()
    mm.flash_thread = mock.MagicMock()
    mm.flash_failed("Boom")
    assert view.show_message.call_count == 1
    mm.set_buttons.assert_called_once_with(flash=True,
                                           repl=True,
                                           files=True,
                                           plotter=True)
    assert mm.flash_thread is None
Ejemplo n.º 2
0
def test_flash_failed():
    """
    Ensure things are cleaned up if flashing failed.
    """
    view = mock.MagicMock()
    editor = mock.MagicMock()
    mm = MicrobitMode(editor, view)
    mock_timer = mock.MagicMock()
    mm.flash_timer = mock_timer
    mm.flash_thread = mock.MagicMock()
    mm.flash_failed('Boom')
    assert view.show_message.call_count == 1
    view.button_bar.slots['flash'].setEnabled.assert_called_once_with(True)
    assert mm.flash_thread is None
    assert mm.flash_timer is None
    mock_timer.stop.assert_called_once_with()
Ejemplo n.º 3
0
def test_flash_failed():
    """
    Ensure things are cleaned up if flashing failed.
    """
    view = mock.MagicMock()
    editor = mock.MagicMock()
    mm = MicrobitMode(editor, view)
    mock_timer = mock.MagicMock()
    mm.flash_timer = mock_timer
    mm.flash_thread = mock.MagicMock()
    mm.flash_failed('Boom')
    assert view.show_message.call_count == 1
    view.button_bar.slots['flash'].setEnabled.assert_called_once_with(True)
    assert mm.flash_thread is None
    assert mm.flash_timer is None
    mock_timer.stop.assert_called_once_with()
Ejemplo n.º 4
0
def test_flash_device_has_latest_firmware_encounters_serial_problem_unix(
    microbit,
):
    """
    If copy_main encounters an IOError on unix-y, revert to old-school
    flashing.
    """
    version_info = {
        "sysname": "microbit",
        "nodename": "microbit",
        "release": uflash.MICROPYTHON_VERSION,
        "version": (
            "micro:bit v0.1.0-b'e10a5ff' on 2018-6-8; MicroPython "
            "v1.9.2-34-gd64154c73 on 2017-09-01"
        ),
        "machine": "micro:bit with nRF51822",
    }
    mock_flasher = mock.MagicMock()
    mock_flasher_class = mock.MagicMock(return_value=mock_flasher)
    mock_timer = mock.MagicMock()
    mock_timer_class = mock.MagicMock(return_value=mock_timer)
    with mock.patch(
        "mu.modes.microbit.uflash.find_microbit", return_value="bar"
    ), mock.patch(
        "mu.modes.microbit.microfs.version", return_value=version_info
    ), mock.patch(
        "mu.modes.microbit.os.path.exists", return_value=True
    ), mock.patch(
        "mu.modes.microbit.DeviceFlasher", mock_flasher_class
    ), mock.patch(
        "mu.modes.microbit.QTimer", mock_timer_class
    ), mock.patch(
        "mu.modes.microbit.sys.platform", "linux"
    ):
        view = mock.MagicMock()
        view.current_tab.text = mock.MagicMock(return_value="foo")
        view.show_message = mock.MagicMock()
        editor = mock.MagicMock()
        editor.minify = False
        editor.microbit_runtime = ""
        editor.current_device = microbit
        mm = MicrobitMode(editor, view)
        mm.flash_failed = mock.MagicMock()
        error = IOError("bang")
        mm.copy_main = mock.MagicMock(side_effect=error)
        mm.set_buttons = mock.MagicMock()
        mm.flash()
        mm.copy_main.assert_called_once_with()
        mock_flasher_class.assert_called_once_with(["bar"], b"foo", None)
        mock_flasher.on_flash_fail.connect.assert_called_once_with(
            mm.flash_failed
        )
        mock_flasher.start.assert_called_once_with()
        assert mm.flash_timer == mock_timer
        mock_timer.timeout.connect.assert_called_once_with(mm.flash_finished)
        mock_timer.setSingleShot.assert_called_once_with(True)
        mock_timer.start.assert_called_once_with(10000)
Ejemplo n.º 5
0
def test_flash_device_has_latest_firmware_encounters_serial_problem_unix():
    """
    If copy_main encounters an IOError on unix-y, revert to old-school
    flashing.
    """
    version_info = {
        'sysname':
        'microbit',
        'nodename':
        'microbit',
        'release':
        uflash.MICROPYTHON_VERSION,
        'version': ("micro:bit v0.1.0-b'e10a5ff' on 2018-6-8; MicroPython "
                    "v1.9.2-34-gd64154c73 on 2017-09-01"),
        'machine':
        'micro:bit with nRF51822',
    }
    mock_flasher = mock.MagicMock()
    mock_flasher_class = mock.MagicMock(return_value=mock_flasher)
    mock_timer = mock.MagicMock()
    mock_timer_class = mock.MagicMock(return_value=mock_timer)
    with mock.patch('mu.modes.microbit.uflash.find_microbit',
                    return_value='bar'),\
            mock.patch('mu.modes.microbit.microfs.version',
                       return_value=version_info),\
            mock.patch('mu.modes.microbit.os.path.exists', return_value=True),\
            mock.patch('mu.modes.microbit.DeviceFlasher',
                       mock_flasher_class), \
            mock.patch('mu.modes.microbit.QTimer', mock_timer_class), \
            mock.patch('mu.modes.microbit.sys.platform', 'linux'):
        view = mock.MagicMock()
        view.current_tab.text = mock.MagicMock(return_value='foo')
        view.show_message = mock.MagicMock()
        editor = mock.MagicMock()
        editor.minify = False
        editor.microbit_runtime = ''
        mm = MicrobitMode(editor, view)
        mm.find_device = mock.MagicMock(return_value=('bar', '12345'))
        mm.flash_failed = mock.MagicMock()
        error = IOError('bang')
        mm.copy_main = mock.MagicMock(side_effect=error)
        mm.set_buttons = mock.MagicMock()
        mm.flash()
        mm.copy_main.assert_called_once_with()
        mock_flasher_class.assert_called_once_with([
            'bar',
        ], b'foo', None)
        mock_flasher.on_flash_fail.connect.\
            assert_called_once_with(mm.flash_failed)
        mock_flasher.start.assert_called_once_with()
        assert mm.flash_timer == mock_timer
        mock_timer.timeout.connect.assert_called_once_with(mm.flash_finished)
        mock_timer.setSingleShot.assert_called_once_with(True)
        mock_timer.start.assert_called_once_with(10000)
Ejemplo n.º 6
0
def test_flash_with_attached_device_has_latest_firmware_encounters_problem(
    microbit,
):
    """
    If copy_main encounters a non-IOError, handle in a helpful manner.
    """
    version_info = {
        "sysname": "microbit",
        "nodename": "microbit",
        "release": uflash.MICROPYTHON_VERSION,
        "version": (
            "micro:bit v0.1.0-b'e10a5ff' on 2018-6-8; MicroPython "
            "v1.9.2-34-gd64154c73 on 2017-09-01"
        ),
        "machine": "micro:bit with nRF51822",
    }
    mock_flasher = mock.MagicMock()
    mock_flasher_class = mock.MagicMock(return_value=mock_flasher)
    with mock.patch(
        "mu.modes.microbit.uflash.find_microbit", return_value="bar"
    ), mock.patch(
        "mu.modes.microbit.microfs.version", return_value=version_info
    ), mock.patch(
        "mu.modes.microbit.os.path.exists", return_value=True
    ), mock.patch(
        "mu.modes.microbit.DeviceFlasher", mock_flasher_class
    ), mock.patch(
        "mu.modes.microbit.sys.platform", "win32"
    ):
        view = mock.MagicMock()
        view.current_tab.text = mock.MagicMock(return_value="foo")
        view.show_message = mock.MagicMock()
        editor = mock.MagicMock()
        editor.minify = False
        editor.microbit_runtime = ""
        editor.current_device = microbit
        mm = MicrobitMode(editor, view)
        mm.flash_failed = mock.MagicMock()
        error = ValueError("bang")
        mm.copy_main = mock.MagicMock(side_effect=error)
        mm.set_buttons = mock.MagicMock()
        mm.flash()
        assert mock_flasher_class.call_count == 0
        mm.copy_main.assert_called_once_with()
        mm.flash_failed.assert_called_once_with(error)
Ejemplo n.º 7
0
def test_flash_with_attached_device_has_latest_firmware_encounters_problem():
    """
    If copy_main encounters an error, handle in a helpful manner.
    """
    version_info = {
        'sysname':
        'microbit',
        'nodename':
        'microbit',
        'release':
        uflash.MICROPYTHON_VERSION,
        'version': ("micro:bit v0.1.0-b'e10a5ff' on 2018-6-8; MicroPython "
                    "v1.9.2-34-gd64154c73 on 2017-09-01"),
        'machine':
        'micro:bit with nRF51822',
    }
    mock_flasher = mock.MagicMock()
    mock_flasher_class = mock.MagicMock(return_value=mock_flasher)
    with mock.patch('mu.modes.microbit.uflash.find_microbit',
                    return_value='bar'),\
            mock.patch('mu.modes.microbit.microfs.find_microbit',
                       return_value=('bar', '12345')),\
            mock.patch('mu.modes.microbit.microfs.version',
                       return_value=version_info),\
            mock.patch('mu.modes.microbit.os.path.exists', return_value=True),\
            mock.patch('mu.modes.microbit.DeviceFlasher',
                       mock_flasher_class), \
            mock.patch('mu.modes.microbit.sys.platform', 'win32'):
        view = mock.MagicMock()
        view.current_tab.text = mock.MagicMock(return_value='foo')
        view.show_message = mock.MagicMock()
        editor = mock.MagicMock()
        editor.minify = False
        editor.microbit_runtime = ''
        mm = MicrobitMode(editor, view)
        mm.flash_failed = mock.MagicMock()
        error = IOError('bang')
        mm.copy_main = mock.MagicMock(side_effect=error)
        mm.set_buttons = mock.MagicMock()
        mm.flash()
        assert mock_flasher_class.call_count == 0
        mm.copy_main.assert_called_once_with()
        mm.flash_failed.assert_called_once_with(error)
Ejemplo n.º 8
0
def test_flash_finished_copy_main_encounters_error():
    """
    If copy_main encounters an error, flash_failed is called.
    """
    view = mock.MagicMock()
    editor = mock.MagicMock()
    mm = MicrobitMode(editor, view)
    mm.flash_failed = mock.MagicMock()
    mm.python_script = 'foo'
    error = IOError('boom')
    mm.copy_main = mock.MagicMock(side_effect=error)
    mm.set_buttons = mock.MagicMock()
    mm.flash_thread = mock.MagicMock()
    mm.flash_timer = mock.MagicMock()
    mm.flash_finished()
    mm.set_buttons.assert_called_once_with(flash=True)
    editor.show_status_message.assert_called_once_with("Finished flashing.")
    assert mm.flash_thread is None
    assert mm.flash_timer is None
    mm.copy_main.assert_called_once_with()
    mm.flash_failed.assert_called_once_with(error)