示例#1
0
def test_flash_force_with_unsupported_microbit(microbit_incompatible):
    """
    If Mu is supposed to flash the device, but the device is, in fact, not
    one that's supported by the version of MicroPython built into Mu, then
    display a warning message to the user.
    """
    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",
                        side_effect=ValueError("bang")), mock.patch(
                            "mu.modes.microbit.os.path.isfile",
                            return_value=True), mock.patch(
                                "mu.modes.microbit.DeviceFlasher",
                                mock_flasher_class):
        view = mock.MagicMock()
        # Empty file to force flashing.
        view.current_tab.text = mock.MagicMock(return_value="foo")
        view.show_message = mock.MagicMock()
        editor = mock.MagicMock()
        editor.microbit_runtime = ""
        editor.minify = False
        editor.current_device = microbit_incompatible
        mm = MicrobitMode(editor, view)
        mm.set_buttons = mock.MagicMock()
        mm.flash()
        assert view.show_message.call_count == 1
示例#2
0
def test_flash_minify(microbit_v1_5):
    view = mock.MagicMock()
    script = "#" + ("x" * 8193) + "\n"
    view.current_tab.text = mock.MagicMock(return_value=script)
    view.current_tab.label = "foo"
    view.show_message = mock.MagicMock()
    editor = mock.MagicMock()
    editor.minify = True
    editor.current_device = microbit_v1_5
    mm = MicrobitMode(editor, view)
    mm.set_buttons = mock.MagicMock()
    with mock.patch("mu.modes.microbit.DeviceFlasher"), mock.patch(
            "mu.contrib.uflash._MAX_SIZE",
            8188), mock.patch("nudatus.mangle", return_value="") as m:
        mm.flash()
        m.assert_called_once_with(script)

    ex = TokenError("Bad", (1, 0))
    with mock.patch("nudatus.mangle", side_effect=ex) as m, mock.patch(
            "mu.contrib.uflash._MAX_SIZE", 8188):
        mm.flash()
        view.show_message.assert_called_with(
            'Unable to flash "foo"',
            "Problem minifying script\nBad [1:0]",
            "Warning",
        )
示例#3
0
def test_flash_force_with_attached_device(microbit):
    """
    Ensure the expected calls are made to DeviceFlasher and a helpful status
    message is enacted.
    """
    version_info = {
        "sysname":
        "microbit",
        "nodename":
        "microbit",
        "release":
        "1.0",
        "version": ("micro:bit v0.0.9-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="/foo/microbit/"), 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.os.path.isfile",
                                return_value=True), mock.patch(
                                    "mu.modes.microbit.DeviceFlasher",
                                    mock_flasher_class):
        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 = "/foo/bar.hex"
        editor.current_device = microbit
        mm = MicrobitMode(editor, view)
        mm.set_buttons = mock.MagicMock()
        mm.flash_and_send = mock.MagicMock(side_effect=mm.flash_and_send)
        mm.flash()
        assert mm.flash_thread == mock_flasher
        assert editor.show_status_message.call_count == 1
        mm.set_buttons.assert_called_once_with(flash=False,
                                               repl=False,
                                               files=False,
                                               plotter=False)
        mm.flash_and_send.assert_called_once_with(b"foo", "/foo/microbit/",
                                                  "/foo/bar.hex")
        mock_flasher_class.assert_called_once_with(
            "/foo/microbit/",
            python_script=None,
            path_to_runtime="/foo/bar.hex",
        )
        mock_flasher.finished.connect.assert_called_once_with(
            mm.flash_finished)
        mock_flasher.on_flash_fail.connect.assert_called_once_with(
            mm.flash_failed)
        mock_flasher.start.assert_called_once_with()
示例#4
0
def test_flash_forced_with_attached_device_as_not_windows(
    microbit_old_firmware,
):
    """
    Ensure the expected calls are made to DeviceFlasher and a helpful status
    message is enacted as if not on Windows.
    """
    version_info = {
        "sysname": "microbit",
        "nodename": "microbit",
        "release": "1.0",
        "version": (
            "micro:bit v0.0.9-b'e10a5ff' on 2018-6-8; MicroPython "
            "v1.9.2-34-gd64154c73 on 2017-09-01"
        ),
        "machine": "micro:bit with nRF51822",
    }
    mock_timer = mock.MagicMock()
    mock_timer_class = mock.MagicMock(return_value=mock_timer)
    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", "linux"
    ), mock.patch(
        "mu.modes.microbit.QTimer", mock_timer_class
    ):
        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_old_firmware
        mm = MicrobitMode(editor, view)
        mm.set_buttons = mock.MagicMock()
        mm.copy_main = mock.MagicMock()
        mm.flash()
        assert mm.flash_timer == mock_timer
        assert editor.show_status_message.call_count == 1
        mm.set_buttons.assert_called_once_with(flash=False)
        mock_flasher_class.assert_called_once_with(["bar"], b"", None)
        assert mock_flasher.finished.connect.call_count == 0
        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)
        mock_flasher.on_flash_fail.connect.assert_called_once_with(
            mm.flash_failed
        )
        mock_flasher.start.assert_called_once_with()
        assert mm.python_script == b"foo"
示例#5
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)
示例#6
0
def test_flash_forced_with_attached_device_as_not_windows():
    """
    Ensure the expected calls are made to DeviceFlasher and a helpful status
    message is enacted as if not on Windows.
    """
    version_info = {
        'sysname':
        'microbit',
        'nodename':
        'microbit',
        'release':
        '1.0',
        'version': ("micro:bit v0.0.9-b'e10a5ff' on 2018-6-8; MicroPython "
                    "v1.9.2-34-gd64154c73 on 2017-09-01"),
        'machine':
        'micro:bit with nRF51822',
    }
    mock_timer = mock.MagicMock()
    mock_timer_class = mock.MagicMock(return_value=mock_timer)
    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=('COM0', '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', 'linux'), \
            mock.patch('mu.modes.microbit.QTimer', mock_timer_class):
        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.set_buttons = mock.MagicMock()
        mm.copy_main = mock.MagicMock()
        mm.flash()
        assert mm.flash_timer == mock_timer
        assert editor.show_status_message.call_count == 1
        mm.set_buttons.assert_called_once_with(flash=False)
        mock_flasher_class.assert_called_once_with([
            'bar',
        ], b'', None)
        assert mock_flasher.finished.connect.call_count == 0
        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)
        mock_flasher.on_flash_fail.connect.\
            assert_called_once_with(mm.flash_failed)
        mock_flasher.start.assert_called_once_with()
        assert mm.python_script == b'foo'
示例#7
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)
示例#8
0
def test_on_data_flood():
    """
    Ensure the "Files" button is re-enabled before calling the base method.
    """
    view = mock.MagicMock()
    editor = mock.MagicMock()
    mm = MicrobitMode(editor, view)
    mm.set_buttons = mock.MagicMock()
    with mock.patch('builtins.super') as mock_super:
        mm.on_data_flood()
        mm.set_buttons.assert_called_once_with(files=True)
        mock_super().on_data_flood.assert_called_once_with()
示例#9
0
def test_flash_finished():
    """
    Ensure state is set back as expected when the flashing thread is finished.
    """
    view = mock.MagicMock()
    editor = mock.MagicMock()
    mm = MicrobitMode(editor, view)
    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
示例#10
0
def test_flash_with_attached_device_has_old_firmware(microbit):
    """
    If the device has some unknown old firmware, force flash it.
    """
    version_info = {
        "sysname": "microbit",
        "nodename": "microbit",
        "release": "1.0",
        "version": ("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="/path/microbit"), 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):
        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.copy_main = mock.MagicMock()
        mm.set_buttons = mock.MagicMock()
        mm.flash_and_send = mock.MagicMock(side_effect=mm.flash_and_send)
        mm.flash()
        assert mm.flash_thread == mock_flasher
        assert editor.show_status_message.call_count == 1
        mm.set_buttons.assert_called_once_with(flash=False,
                                               repl=False,
                                               files=False,
                                               plotter=False)
        mm.flash_and_send.assert_called_once_with(b"foo", "/path/microbit")
        mock_flasher_class.assert_called_once_with("/path/microbit",
                                                   path_to_runtime=None,
                                                   python_script=None)
        mock_flasher.finished.connect.assert_called_once_with(
            mm.flash_finished)
        mock_flasher.on_flash_fail.connect.assert_called_once_with(
            mm.flash_failed)
        mock_flasher.start.assert_called_once_with()
示例#11
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
示例#12
0
def test_flash_with_attached_device_has_old_firmware():
    """
    If the device has some unknown old firmware, force flash it.
    """
    version_info = {
        "sysname": "microbit",
        "nodename": "microbit",
        "release": "1.0",
        "version": ("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 = ""
        mm = MicrobitMode(editor, view)
        mm.find_device = mock.MagicMock(return_value=("bar", "990112345"))
        mm.copy_main = mock.MagicMock()
        mm.set_buttons = mock.MagicMock()
        mm.flash()
        assert mm.flash_thread == mock_flasher
        assert editor.show_status_message.call_count == 1
        mm.set_buttons.assert_called_once_with(flash=False)
        mock_flasher_class.assert_called_once_with(["bar"], b"", None)
        mock_flasher.finished.connect.assert_called_once_with(
            mm.flash_finished
        )
        mock_flasher.on_flash_fail.connect.assert_called_once_with(
            mm.flash_failed
        )
        mock_flasher.start.assert_called_once_with()
示例#13
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()
    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
    mm.set_buttons.assert_called_once_with(flash=True)
    assert mm.flash_thread is None
    assert mm.flash_timer is None
    mock_timer.stop.assert_called_once_with()
示例#14
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)
示例#15
0
def test_flash_minify_no_minify():
    view = mock.MagicMock()
    view.current_tab.label = 'foo'
    view.show_message = mock.MagicMock()
    script = '#' + ('x' * 8193) + '\n'
    view.current_tab.text = mock.MagicMock(return_value=script)
    editor = mock.MagicMock()
    editor.minify = True
    mm = MicrobitMode(editor, view)
    mm.set_buttons = mock.MagicMock()
    with mock.patch('mu.modes.microbit.can_minify', False):
        with mock.patch('nudatus.mangle', return_value='') as m:
            mm.flash()
            assert m.call_count == 0
            view.show_message.assert_called_once_with(
                'Unable to flash "foo"', 'Your script is too long'
                ' and the minifier '
                'isn\'t available', 'Warning')
示例#16
0
def test_flash_with_attached_device_has_old_firmware():
    """
    If the device has some unknown old firmware, force flash it.
    """
    version_info = {
        'sysname': 'microbit',
        'nodename': 'microbit',
        'release': '1.0',
        'version': ("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.copy_main = mock.MagicMock()
        mm.set_buttons = mock.MagicMock()
        mm.flash()
        assert mm.flash_thread == mock_flasher
        assert editor.show_status_message.call_count == 1
        mm.set_buttons.assert_called_once_with(flash=False)
        mock_flasher_class.assert_called_once_with([
            'bar',
        ], b'', None)
        mock_flasher.finished.connect.\
            assert_called_once_with(mm.flash_finished)
        mock_flasher.on_flash_fail.connect.\
            assert_called_once_with(mm.flash_failed)
        mock_flasher.start.assert_called_once_with()
示例#17
0
def test_flash_finished_copy_main():
    """
    Ensure state is set back as expected when the flashing thread is finished.
    """
    view = mock.MagicMock()
    editor = mock.MagicMock()
    mm = MicrobitMode(editor, view)
    mm.python_script = b"foo"
    mm.copy_main = mock.MagicMock()
    mm.set_buttons = mock.MagicMock()
    mm.flash_thread = mock.MagicMock()
    mm.flash_finished()
    mm.set_buttons.assert_called_once_with(flash=True,
                                           repl=True,
                                           files=True,
                                           plotter=True)
    editor.show_status_message.assert_called_once_with("Finished flashing.")
    assert mm.flash_thread is None
    mm.copy_main.assert_called_once_with(b"foo")
示例#18
0
def test_toggle_repl():
    """
    Ensure the REPL is able to toggle on if there's no file system pane.
    """
    view = mock.MagicMock()
    view.show_message = mock.MagicMock()
    editor = mock.MagicMock()
    mm = MicrobitMode(editor, view)
    mm.set_buttons = mock.MagicMock()

    def side_effect(*args, **kwargs):
        mm.repl = True

    with mock.patch('mu.modes.microbit.MicroPythonMode.toggle_repl',
                    side_effect=side_effect) as tr:
        mm.repl = None
        mm.toggle_repl(None)
        tr.assert_called_once_with(None)
        mm.set_buttons.assert_called_once_with(files=False)
示例#19
0
def test_toggle_plotter():
    """
    Ensure the plotter is toggled on if the file system pane is absent.
    """
    view = mock.MagicMock()
    view.show_message = mock.MagicMock()
    editor = mock.MagicMock()
    mm = MicrobitMode(editor, view)
    mm.set_buttons = mock.MagicMock()

    def side_effect(*args, **kwargs):
        mm.plotter = True

    with mock.patch('mu.modes.microbit.MicroPythonMode.toggle_plotter',
                    side_effect=side_effect) as tp:
        mm.plotter = None
        mm.toggle_plotter(None)
        tp.assert_called_once_with(None)
        mm.set_buttons.assert_called_once_with(files=False)
示例#20
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)
示例#21
0
def test_flash_minify():
    view = mock.MagicMock()
    script = '#' + ('x' * 8193) + '\n'
    view.current_tab.text = mock.MagicMock(return_value=script)
    view.show_message = mock.MagicMock()
    editor = mock.MagicMock()
    editor.minify = True
    mm = MicrobitMode(editor, view)
    mm.set_buttons = mock.MagicMock()
    with mock.patch('mu.modes.microbit.DeviceFlasher'):
        with mock.patch('nudatus.mangle', return_value='') as m:
            mm.flash()
            m.assert_called_once_with(script)

    ex = TokenError('Bad', (1, 0))
    with mock.patch('nudatus.mangle', side_effect=ex) as m:
        mm.flash()
        view.show_message.assert_called_once_with('Problem with script',
                                                  'Bad [1:0]', 'Warning')
示例#22
0
def test_flash_force_with_no_micropython(microbit):
    """
    Ensure the expected calls are made to DeviceFlasher and a helpful status
    message is enacted if there is no MicroPython firmware on the device.
    """
    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",
                        side_effect=ValueError("bang")), mock.patch(
                            "mu.modes.microbit.os.path.isfile",
                            return_value=True), mock.patch(
                                "mu.modes.microbit.os.path.exists",
                                return_value=True), mock.patch(
                                    "mu.modes.microbit.DeviceFlasher",
                                    mock_flasher_class):
        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 = "/foo/bar"
        editor.current_device = microbit
        mm = MicrobitMode(editor, view)
        mm.set_buttons = mock.MagicMock()
        mm.flash_and_send = mock.MagicMock(side_effect=mm.flash_and_send)
        mm.flash()
        assert mm.flash_thread == mock_flasher
        assert editor.show_status_message.call_count == 1
        mm.set_buttons.assert_called_once_with(flash=False,
                                               repl=False,
                                               files=False,
                                               plotter=False)
        mm.flash_and_send.assert_called_once_with(b"foo", "bar", "/foo/bar")
        mock_flasher_class.assert_called_once_with("bar",
                                                   python_script=None,
                                                   path_to_runtime="/foo/bar")
        mock_flasher.finished.connect.assert_called_once_with(
            mm.flash_finished)
        mock_flasher.on_flash_fail.connect.assert_called_once_with(
            mm.flash_failed)
        mock_flasher.start.assert_called_once_with()
示例#23
0
def test_flash_minify():
    view = mock.MagicMock()
    script = "#" + ("x" * 8193) + "\n"
    view.current_tab.text = mock.MagicMock(return_value=script)
    view.show_message = mock.MagicMock()
    editor = mock.MagicMock()
    editor.minify = True
    mm = MicrobitMode(editor, view)
    mm.set_buttons = mock.MagicMock()
    with mock.patch("mu.modes.microbit.DeviceFlasher"):
        with mock.patch("nudatus.mangle", return_value="") as m:
            mm.flash()
            m.assert_called_once_with(script)

    ex = TokenError("Bad", (1, 0))
    with mock.patch("nudatus.mangle", side_effect=ex) as m:
        mm.flash()
        view.show_message.assert_called_once_with(
            "Problem with script", "Bad [1:0]", "Warning"
        )
示例#24
0
def test_flash_with_attached_device_has_latest_firmware(microbit):
    """
    There's NO need to use the DeviceFlasher if the board already has the
    latest firmware. In which case, just call copy_main.
    """
    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.copy_main = mock.MagicMock()
        mm.set_buttons = mock.MagicMock()
        mm.flash()
        assert mock_flasher_class.call_count == 0
        mm.copy_main.assert_called_once_with()
示例#25
0
def test_flash_with_attached_device_has_latest_firmware():
    """
    There's NO need to use the DeviceFlasher if the board already has the
    latest firmware. In which case, just call copy_main.
    """
    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.copy_main = mock.MagicMock()
        mm.set_buttons = mock.MagicMock()
        mm.flash()
        assert mock_flasher_class.call_count == 0
        mm.copy_main.assert_called_once_with()
示例#26
0
def test_toggle_plotter_no_repl_or_plotter():
    """
    Ensure the file system button is enabled if the plotter toggles off and the
    repl isn't active.
    """
    view = mock.MagicMock()
    view.show_message = mock.MagicMock()
    editor = mock.MagicMock()
    mm = MicrobitMode(editor, view)
    mm.set_buttons = mock.MagicMock()

    def side_effect(*args, **kwargs):
        mm.plotter = False
        mm.repl = False

    with mock.patch('mu.modes.microbit.MicroPythonMode.toggle_plotter',
                    side_effect=side_effect) as tp:
        mm.plotter = None
        mm.toggle_plotter(None)
        tp.assert_called_once_with(None)
        mm.set_buttons.assert_called_once_with(files=True)
示例#27
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)
示例#28
0
def test_flash_minify_no_minify():
    view = mock.MagicMock()
    view.current_tab.label = "foo"
    view.show_message = mock.MagicMock()
    script = "#" + ("x" * 8193) + "\n"
    view.current_tab.text = mock.MagicMock(return_value=script)
    editor = mock.MagicMock()
    editor.minify = True
    mm = MicrobitMode(editor, view)
    mm.set_buttons = mock.MagicMock()
    with mock.patch("mu.modes.microbit.can_minify", False):
        with mock.patch("nudatus.mangle", return_value="") as m:
            mm.flash()
            assert m.call_count == 0
            view.show_message.assert_called_once_with(
                'Unable to flash "foo"',
                "Your script is too long"
                " and the minifier "
                "isn't available",
                "Warning",
            )
示例#29
0
def test_flash_with_attached_device_as_not_windows():
    """
    Ensure the expected calls are made to DeviceFlasher and a helpful status
    message is enacted as if not on Windows.
    """
    mock_timer = mock.MagicMock()
    mock_timer_class = mock.MagicMock(return_value=mock_timer)
    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.os.path.exists', return_value=True),\
            mock.patch('mu.modes.microbit.DeviceFlasher',
                       mock_flasher_class), \
            mock.patch('mu.modes.microbit.sys.platform', 'linux'), \
            mock.patch('mu.modes.microbit.QTimer', mock_timer_class):
        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.set_buttons = mock.MagicMock()
        mm.flash()
        assert mm.flash_timer == mock_timer
        assert editor.show_status_message.call_count == 1
        mm.set_buttons.assert_called_once_with(flash=False)
        mock_flasher_class.assert_called_once_with([
            'bar',
        ], b'foo', None)
        assert mock_flasher.finished.connect.call_count == 0
        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)
        mock_flasher.on_flash_fail.connect.\
            assert_called_once_with(mm.flash_failed)
        mock_flasher.start.assert_called_once_with()
示例#30
0
def test_flash_force_with_no_micropython():
    """
    Ensure the expected calls are made to DeviceFlasher and a helpful status
    message is enacted if there is no MicroPython firmware on the device.
    """
    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',
                       side_effect=ValueError('bang')),\
            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 = '/foo/bar'
        mm = MicrobitMode(editor, view)
        mm.set_buttons = mock.MagicMock()
        mm.flash()
        assert mm.flash_thread == mock_flasher
        assert editor.show_status_message.call_count == 1
        mm.set_buttons.assert_called_once_with(flash=False)
        mock_flasher_class.assert_called_once_with([
            'bar',
        ], b'', '/foo/bar')
        mock_flasher.finished.connect.\
            assert_called_once_with(mm.flash_finished)
        mock_flasher.on_flash_fail.connect.\
            assert_called_once_with(mm.flash_failed)
        mock_flasher.start.assert_called_once_with()