Skip to content

Commit

Permalink
WiP to fix for all platforms.
Browse files Browse the repository at this point in the history
  • Loading branch information
ntoll committed May 2, 2018
1 parent 05ea0f2 commit 2718ff3
Show file tree
Hide file tree
Showing 3 changed files with 91 additions and 32 deletions.
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -5,3 +5,4 @@ microfs.egg-info/*
.coverage
dist/*
build/*
.pytest_cache/*
37 changes: 27 additions & 10 deletions microfs.py
Original file line number Diff line number Diff line change
Expand Up @@ -56,23 +56,40 @@ def raw_on(serial):
"""
Puts the device into raw mode.
"""
attempts_left = 3
while attempts_left:
serial.write(b'\x03') # Send CTRL-C to break out of loop.
if serial.read_until(b'\n>>>').endswith(b'\n>>>'):
break
attempts_left -= 1
else:
raise IOError('Could not enter REPL mode.')
serial.write(b'\x01') # Go into raw mode.
serial.read_until(b'\r\n>') # Flush buffer until raw mode prompt.
# Send CTRL-B to end raw mode if required.
serial.write(b'\x02')
# Send CTRL-C twice to break out of loop.
serial.write(b'\r\x03\x03')
# Flush input (without relying on serial.flushInput())
n = serial.inWaiting()
while n > 0:
serial.read(n)
n = serial.inWaiting()
# Go into raw mode.
serial.write(b'\r\x01')
# Flush
data = serial.read_until(b'raw REPL; CTRL-B to exit\r\n>')
if not data.endswith(b'raw REPL; CTRL-B to exit\r\n>'):
print(data)
raise IOError('Could not enter raw REPL.')
# Soft Reset with CTRL-D
serial.write(b'\x04')
data = serial.read_until(b'soft reboot\r\n')
if not data.endswith(b'soft reboot\r\n'):
print(data)
raise IOError('Could not enter raw REPL.')
data = serial.read_until(b'raw REPL; CTRL-B to exit\r\n>')
if not data.endswith(b'raw REPL; CTRL-B to exit\r\n>'):
print(data)

This comment has been minimized.

Copy link
@carlosperate

carlosperate May 3, 2018

Contributor

Should these 3 print statements still be here?

This comment has been minimized.

Copy link
@ntoll

ntoll May 3, 2018

Author Owner

Yes. I'm just copying what pyboard.py does and it'll only show up for people running this from the command line... so it's useful debug information for those who I presume are "advanced" users.

This comment has been minimized.

Copy link
@carlosperate

carlosperate May 3, 2018

Contributor

It would still be shown on the terminal when running Mu, so it'd be directed there instead of something more useful for us like the log.
I do like the idea of having that displayed/captured, perhaps we can add a "set pipe" function, so that by default it prints to std_out, but could still allow Mu to pipe it into the logger?

raise IOError('Could not enter raw REPL.')


def raw_off(serial):
"""
Takes the device out of raw mode.
"""
serial.write(b'\x02') # Send CTRL-B to get out of raw mode.
serial.write(b'\x04') # Send CTRL-D to restart.


def get_serial():
Expand Down
85 changes: 63 additions & 22 deletions tests/test_microfs.py
Original file line number Diff line number Diff line change
Expand Up @@ -55,29 +55,58 @@ def test_raw_on():
raw mode.
"""
mock_serial = mock.MagicMock()
mock_serial.read_until = mock.MagicMock(side_effect=[b'\r\n>>>',
b'\r\n>'])
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.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>'
assert mock_serial.inWaiting.call_count == 5
assert mock_serial.write.call_count == 4
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\x03'
assert mock_serial.write.call_args_list[2][0][0] == b'\r\x01'
assert mock_serial.write.call_args_list[3][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_attempts():
def test_raw_on_failures():
"""
Ensure that if the expect serial data is not encountered three times it
will raise an exception.
Check problem data results in an IO error.
"""
mock_serial = mock.MagicMock()
mock_serial.read_until = mock.MagicMock(side_effect=[b'Unexpected data',
b'Nothing good here',
b'Last chance'])
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 REPL mode.'
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'raw REPL; CTRL-B to exit\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.'


def test_raw_off():
Expand All @@ -87,7 +116,9 @@ def test_raw_off():
"""
mock_serial = mock.MagicMock()
microfs.raw_off(mock_serial)
mock_serial.write.assert_called_once_with(b'\x02')
assert mock_serial.write.call_count == 2
assert mock_serial.write.call_args_list[0][0][0] == b'\x02'
assert mock_serial.write.call_args_list[1][0][0] == b'\x04'


def test_get_serial():
Expand Down Expand Up @@ -149,9 +180,14 @@ def test_execute_err_result():
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>',
b'OK\x04Error\x04>'])
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>',
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)
Expand All @@ -166,9 +202,14 @@ def test_execute_no_serial():
attempts to get_serial().
"""
mock_serial = mock.MagicMock()
mock_serial.read_until = mock.MagicMock(side_effect=[b'\r\n>>>',
b'\r\n>',
b'OK\x04Error\x04>'])
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>',
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) as p:
out, err = microfs.execute(command)
Expand Down

0 comments on commit 2718ff3

Please sign in to comment.