def test_raw_on(): """ Check the expected commands are sent to the device to put MicroPython into raw mode. """ mock_serial = mock.MagicMock() mock_serial.inWaiting.side_effect = [5, 3, 2, 1, 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>', ] mock_serial.read_until.side_effect = data microfs.raw_on(mock_serial) assert mock_serial.inWaiting.call_count == 5 assert mock_serial.write.call_count == 6 assert mock_serial.write.call_args_list[0][0][0] == b'\x02' assert mock_serial.write.call_args_list[1][0][0] == b'\r\x03' assert mock_serial.write.call_args_list[2][0][0] == b'\r\x03' assert mock_serial.write.call_args_list[3][0][0] == b'\r\x03' assert mock_serial.write.call_args_list[4][0][0] == b'\r\x01' assert mock_serial.write.call_args_list[5][0][0] == b'\x04' assert mock_serial.read_until.call_count == 3 assert mock_serial.read_until.call_args_list[0][0][0] == data[0] assert mock_serial.read_until.call_args_list[1][0][0] == data[1] assert mock_serial.read_until.call_args_list[2][0][0] == data[2]
def test_raw_on(): """ Check the expected commands are sent to the device to put MicroPython into raw mode. """ mock_serial = mock.MagicMock() mock_serial.read_until = mock.MagicMock(side_effect=[b'\r\n>', b'\r\n>OK']) microfs.raw_on(mock_serial) assert mock_serial.write.call_count == 2 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.read_until.call_count == 2 assert mock_serial.read_until.call_args_list[0][0][0] == b'\n>' assert mock_serial.read_until.call_args_list[1][0][0] == b'\r\n>OK'
def test_raw_on(): """ Check the expected commands are sent to the device to put MicroPython into raw mode. """ 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>", ] mock_serial.read_until.side_effect = data microfs.raw_on(mock_serial) assert mock_serial.inWaiting.call_count == 2 assert mock_serial.write.call_count == 6 assert mock_serial.write.call_args_list[0][0][0] == b"\x02" assert mock_serial.write.call_args_list[1][0][0] == b"\r\x03" assert mock_serial.write.call_args_list[2][0][0] == b"\r\x03" assert mock_serial.write.call_args_list[3][0][0] == b"\r\x03" assert mock_serial.write.call_args_list[4][0][0] == b"\r\x01" assert mock_serial.write.call_args_list[5][0][0] == b"\x04" assert mock_serial.read_until.call_count == 3 assert mock_serial.read_until.call_args_list[0][0][0] == data[0] assert mock_serial.read_until.call_args_list[1][0][0] == data[1] assert mock_serial.read_until.call_args_list[2][0][0] == data[2] mock_serial.reset_mock() data = [ b"raw REPL; CTRL-B to exit\r\n>", b"soft reboot\r\n", b"foo\r\n", b"raw REPL; CTRL-B to exit\r\n>", ] mock_serial.read_until.side_effect = data microfs.raw_on(mock_serial) assert mock_serial.inWaiting.call_count == 2 assert mock_serial.write.call_count == 7 assert mock_serial.write.call_args_list[0][0][0] == b"\x02" assert mock_serial.write.call_args_list[1][0][0] == b"\r\x03" assert mock_serial.write.call_args_list[2][0][0] == b"\r\x03" assert mock_serial.write.call_args_list[3][0][0] == b"\r\x03" assert mock_serial.write.call_args_list[4][0][0] == b"\r\x01" assert mock_serial.write.call_args_list[5][0][0] == b"\x04" assert mock_serial.write.call_args_list[6][0][0] == b"\r\x01" assert mock_serial.read_until.call_count == 4 assert mock_serial.read_until.call_args_list[0][0][0] == data[0] assert mock_serial.read_until.call_args_list[1][0][0] == data[1] assert mock_serial.read_until.call_args_list[2][0][0] == data[3] assert mock_serial.read_until.call_args_list[3][0][0] == data[3]
def test_raw_on_failures(): """ Check problem data results in an IO error. """ mock_serial = mock.MagicMock() mock_serial.inWaiting.side_effect = [5, 3, 2, 1, 0] data = [ b'raw REPL; CTRL-B to exit\r\n> foo', ] mock_serial.read_until.side_effect = data with pytest.raises(IOError) as ex: microfs.raw_on(mock_serial) assert ex.value.args[0] == 'Could not enter raw REPL.' data = [ b'raw REPL; CTRL-B to exit\r\n>', b'soft reboot\r\n foo', ] mock_serial.read_until.side_effect = data mock_serial.inWaiting.side_effect = [5, 3, 2, 1, 0] with pytest.raises(IOError) as ex: microfs.raw_on(mock_serial) assert ex.value.args[0] == 'Could not enter raw REPL.' data = [ b'raw REPL; CTRL-B to exit\r\n>', b'soft reboot\r\n', b'foo', b'foo', ] mock_serial.read_until.side_effect = data mock_serial.inWaiting.side_effect = None mock_serial.inWaiting.return_value = 0 with pytest.raises(IOError) as ex: microfs.raw_on(mock_serial) assert ex.value.args[0] == 'Could not enter raw REPL.'
def test_raw_on_failures_command_line_flag_on(): """ If the COMMAND_LINE_FLAG is True, ensure the last data received is output via the print statemen for debugging purposes. """ with mock.patch('builtins.print') as mock_print, \ mock.patch('microfs.COMMAND_LINE_FLAG', True): mock_serial = mock.MagicMock() mock_serial.inWaiting.side_effect = [5, 3, 2, 1, 0] data = [ b'raw REPL; CTRL-B to exit\r\n> foo', ] mock_serial.read_until.side_effect = data with pytest.raises(IOError) as ex: microfs.raw_on(mock_serial) mock_print.assert_called_once_with(data[0]) assert ex.value.args[0] == 'Could not enter raw REPL.' mock_print.reset_mock() data = [ b'raw REPL; CTRL-B to exit\r\n>', b'soft reboot\r\n foo', ] mock_serial.read_until.side_effect = data mock_serial.inWaiting.side_effect = [5, 3, 2, 1, 0] with pytest.raises(IOError) as ex: microfs.raw_on(mock_serial) mock_print.assert_called_once_with(data[1]) assert ex.value.args[0] == 'Could not enter raw REPL.' mock_print.reset_mock() data = [ b'raw REPL; CTRL-B to exit\r\n>', b'soft reboot\r\n', b'foo', b'foo', ] mock_serial.read_until.side_effect = data mock_serial.inWaiting.side_effect = None mock_serial.inWaiting.return_value = 0 with pytest.raises(IOError) as ex: microfs.raw_on(mock_serial) mock_print.assert_called_once_with(data[3]) assert ex.value.args[0] == 'Could not enter raw REPL.'