def test_copy_main_with_python_script(): """ If copy_main is called and there's something in self.python_script, then use microfs to write it to the device's on-board filesystem, followed by a soft-reboot. """ view = mock.MagicMock() editor = mock.MagicMock() mm = MicrobitMode(editor, view) mm.python_script = 'import love' with mock.patch('mu.modes.microbit.microfs') as mock_microfs: mock_microfs.execute.return_value = ('', '') mm.copy_main() serial = mock_microfs.get_serial() expected = [ "fd = open('main.py', 'wb')", "f = fd.write", "f('import love')", "fd.close()", ] mock_microfs.execute.assert_called_once_with(expected, serial) serial.write.call_count == 2 assert serial.write.call_args_list[0][0][0] == b'import microbit\r\n' assert serial.write.call_args_list[1][0][0] == b'microbit.reset()\r\n' # The script is re-set to empty. assert mm.python_script == ''
def test_copy_main_with_python_script_encounters_device_error(): """ If the device returns an error, then copy_main should raise an IOError. """ view = mock.MagicMock() editor = mock.MagicMock() mm = MicrobitMode(editor, view) mm.python_script = 'import love' with mock.patch('mu.modes.microbit.microfs') as mock_microfs: mock_microfs.execute.return_value = ('', 'BANG!') with pytest.raises(IOError): mm.copy_main()
def test_copy_main_no_python_script(): """ If copy_main is called and there's nothing in self.python_script, then don't do anything. """ view = mock.MagicMock() editor = mock.MagicMock() mm = MicrobitMode(editor, view) mm.python_script = '' with mock.patch('mu.modes.microbit.microfs') as mock_microfs: mm.copy_main() assert mock_microfs.execute.call_count == 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 = 'foo' mm.copy_main = mock.MagicMock() 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()
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)