def test_execute_no_microbit(): """ An IOError should be raised if no micro:bit is found. """ with mock.patch('microfs.find_microbit', return_value=None): with pytest.raises(IOError) as ex: microfs.execute('foo') assert ex.value.args[0] == 'Could not find micro:bit.'
def test_execute(): """ Ensure that the expected communication happens via the serial connection with the connected micro:bit to facilitate the execution of the passed in command. """ mock_serial = mock.MagicMock() mock_serial.read_until = mock.MagicMock( side_effect=[b'OK\x04\x04>', b'OK[]\x04\x04>']) commands = [ 'import os', 'os.listdir()', ] with mock.patch('microfs.get_serial', return_value=mock_serial), \ mock.patch('microfs.raw_on', return_value=None) as raw_mon, \ mock.patch('microfs.raw_off', return_value=None) as raw_moff: out, err = microfs.execute(commands, mock_serial) # Check the result is correctly parsed. assert out == b'[]' assert err == b'' # Check raw_on and raw_off were called. raw_mon.assert_called_once_with(mock_serial) raw_moff.assert_called_once_with(mock_serial) # Check the writes are of the right number and sort (to ensure the # device is put into the correct states). assert mock_serial.write.call_count == 4 encoded0 = commands[0].encode('utf-8') encoded1 = commands[1].encode('utf-8') assert mock_serial.write.call_args_list[0][0][0] == encoded0 assert mock_serial.write.call_args_list[1][0][0] == b'\x04' assert mock_serial.write.call_args_list[2][0][0] == encoded1 assert mock_serial.write.call_args_list[3][0][0] == b'\x04'
def test_execute(): """ Ensure that the expected communication happens via the serial connection with the connected micro:bit to facilitate the execution of the passed in command. """ mock_serial = mock.MagicMock() mock_serial.read_until = mock.MagicMock(side_effect=[b'\r\n>', b'\r\n>OK']) mock_serial.read_all = mock.MagicMock(return_value=b'OK[]\r\n\x04\x04>') mock_class = mock.MagicMock() mock_class.__enter__ = mock.MagicMock(return_value=mock_serial) command = 'import os; os.listdir()' with mock.patch('microfs.find_microbit', return_value='/dev/ttyACM3'): with mock.patch('microfs.Serial', return_value=mock_class): out, err = microfs.execute(command) # Check the result is correctly parsed. assert out == b'[]\r\n' assert err == b'' # Check the writes are of the right number and sort (to ensure the # device is put into the correct states). expected = command.encode('utf-8') + b'\x04' assert mock_serial.write.call_count == 4 assert mock_serial.write.call_args_list[0][0][0] == b'\x03' assert mock_serial.write.call_args_list[1][0][0] == b'\x01' assert mock_serial.write.call_args_list[2][0][0] == expected assert mock_serial.write.call_args_list[3][0][0] == b'\x02'
def test_execute_no_serial(): """ Ensure that if there's no serial object passed into the execute method, it attempts to get_serial(). """ mock_serial = mock.MagicMock() mock_serial.read_until = mock.MagicMock(side_effect=[b'\r\n>', b'\r\n>OK']) mock_serial.read_all = mock.MagicMock(return_value=b'OK\x04Error\x04>') command = 'import os; os.listdir()' with mock.patch('microfs.get_serial', return_value=mock_serial) as p: out, err = microfs.execute(command) p.assert_called_once_with()
def test_execute_err_result(): """ Ensure that if there's a problem reported via stderr on the Microbit, it's returned as such by the execute function. """ mock_serial = mock.MagicMock() mock_serial.read_until = mock.MagicMock(side_effect=[b'\r\n>', b'\r\n>OK']) mock_serial.read_all = mock.MagicMock(return_value=b'OK\x04Error\x04>') command = 'import os; os.listdir()' with mock.patch('microfs.get_serial', return_value=mock_serial): out, err = microfs.execute(command, mock_serial) # Check the result is correctly parsed. assert out == b'' assert err == b'Error'
def test_execute_no_serial(): """ Ensure that if there's no serial object passed into the execute method, it attempts to get_serial(). """ mock_serial = mock.MagicMock() mock_serial.read_until = mock.MagicMock( side_effect=[b'OK\x04\x04>', b'OK[]\x04\x04>']) commands = [ 'import os', 'os.listdir()', ] with mock.patch('microfs.get_serial', return_value=mock_serial) as p, \ mock.patch('microfs.raw_on', return_value=None), \ mock.patch('microfs.raw_off', return_value=None): out, err = microfs.execute(commands) p.assert_called_once_with() mock_serial.close.assert_called_once_with()
def test_execute_err_result(): """ Ensure that if there's a problem reported via stderr on the Microbit, it's returned as such by the execute function. """ mock_serial = mock.MagicMock() mock_serial.read_until = mock.MagicMock(side_effect=[b'\r\n>', b'\r\n>OK']) mock_serial.read_all = mock.MagicMock(return_value=b'OK\x04Error\x04>') mock_class = mock.MagicMock() mock_class.__enter__ = mock.MagicMock(return_value=mock_serial) command = 'import os; os.listdir()' with mock.patch('microfs.find_microbit', return_value='/dev/ttyACM3'): with mock.patch('microfs.Serial', return_value=mock_class): out, err = microfs.execute(command) # Check the result is correctly parsed. assert out == b'' assert err == b'Error'
def test_execute_err_result(): """ Ensure that if there's a problem reported via stderr on the Microbit, it's returned as such by the execute function. """ mock_serial = mock.MagicMock() mock_serial.inWaiting.return_value = 0 data = [ b'raw REPL; CTRL-B to exit\r\n>', b'soft reboot\r\n', b'raw REPL; CTRL-B to exit\r\n>', b'OK\x04Error\x04>', ] mock_serial.read_until.side_effect = data command = 'import os; os.listdir()' with mock.patch('microfs.get_serial', return_value=mock_serial): out, err = microfs.execute(command, mock_serial) # Check the result is correctly parsed. assert out == b'' assert err == b'Error'
import os, uflash, microfs, sys # 1. uflash empty python environment try: uflash.flash() except Exception as e: sys.stderr.write('\n%s: %s' % (type(e).__name__, str(e))) sys.exit(1) # 2. find & put required files dxk_folder = os.path.abspath( os.path.join(os.path.dirname(__file__), '..', 'dxk_ext')) commands = [] for file in os.listdir(dxk_folder): if file.endswith('.py'): commands.append("fd = open('{}', 'wb')".format(file)) commands.append("f = fd.write") with open(os.path.join(dxk_folder, file), 'rb') as local: content = local.read() while content: line = content[:64] if microfs.PY2: commands.append('f(b' + repr(line) + ')') else: commands.append('f(' + repr(line) + ')') content = content[64:] commands.append('fd.close()') print('Flashing modules...') microfs.execute(commands) print('Done.')