def test_force_flash_no_serial_connection(): """ If Mu cannot establish a serial connection to the micro:bit, BUT the path to the micro:bit on the filesystem is known, then fall back to old-school flashing of hex with script appended to the end. """ mock_flasher = mock.MagicMock() mock_flasher_class = mock.MagicMock(return_value=mock_flasher) with mock.patch("mu.contrib.uflash.find_microbit", return_value="/path/microbit"), mock.patch( "mu.contrib.microfs.get_serial"), mock.patch( "mu.contrib.microfs.version", side_effect=IOError("bang")), mock.patch( "mu.logic.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 = None mm = MicrobitMode(editor, view) mm.flash_attached = mock.MagicMock(side_effect=mm.flash_attached) mm.flash() mm.flash_attached.assert_called_once_with(b"foo", "/path/microbit") mock_flasher_class.assert_called_once_with("/path/microbit", b"foo") mock_flasher.finished.connect.assert_called_once_with( mm.flash_finished)
def test_flash_device_has_latest_firmware_encounters_serial_problem( microbit, ): """ If copy_main encounters an IOError (likely on Windows), revert to old-school flashing. """ version_info = { "sysname": "microbit", "nodename": "microbit", "release": uflash.MICROPYTHON_V1_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="/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.flash_failed = mock.MagicMock() error = IOError("bang") mm.copy_main = mock.MagicMock(side_effect=error) mm.set_buttons = mock.MagicMock() mm.flash_attached = mock.MagicMock(side_effect=mm.flash_attached) mm.flash() mm.copy_main.assert_called_once_with(b"foo") mm.flash_attached.assert_called_once_with(b"foo", "/path/microbit") mock_flasher_class.assert_called_once_with("/path/microbit", b"foo") 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()
def test_force_flash_user_specified_device_path(): """ Ensure that if a micro:bit is not automatically found by uflash then it prompts the user to locate the device and, assuming a path was given, saves the hex in the expected location. """ version_info = { "sysname": "microbit", "nodename": "microbit", "release": uflash.MICROPYTHON_V1_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.contrib.uflash.find_microbit", return_value=None), mock.patch( "mu.contrib.microfs.get_serial"), mock.patch( "mu.contrib.microfs.version", return_value=version_info), mock.patch( "mu.logic.os.path.exists", return_value=True), mock.patch( "mu.modes.microbit.DeviceFlasher", mock_flasher_class): view = mock.MagicMock() view.get_microbit_path = mock.MagicMock(return_value="/path/microbit") 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 = None mm = MicrobitMode(editor, view) mm.flash_attached = mock.MagicMock(side_effect=mm.flash_attached) mm.flash() home = HOME_DIRECTORY view.get_microbit_path.assert_called_once_with(home) mm.flash_attached.assert_called_once_with(b"foo", "/path/microbit") mock_flasher_class.assert_called_once_with("/path/microbit", b"foo") mock_flasher.finished.connect.assert_called_once_with( mm.flash_finished)