Skip to content

Commit 5453d02

Browse files
committed
Unit tests for port auto-detect and flashing
1 parent 4baf6d5 commit 5453d02

File tree

1 file changed

+68
-10
lines changed

1 file changed

+68
-10
lines changed
Lines changed: 68 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
from unittest.mock import patch
1+
from unittest import mock
22
from pathlib import Path
33

44
import pytest
@@ -7,32 +7,90 @@
77

88

99
class TestGenerateProject:
10-
"""A class with common parameters, `param1` and `param2`."""
10+
DEFAULT_OPTIONS = {"arduino_cli_cmd": "arduino-cli", "arduino_board": "nano33ble"}
1111

1212
def test_print_c_array(self):
1313
handler = microtvm_api_server.Handler()
1414
c_arr = handler._print_c_array([1, 32, 32, 3])
1515
assert c_arr == "{1, 32, 32, 3}"
1616

1717
def _set_pathlib_path_exists(self, value):
18-
with patch.object(Path, "exists") as mock_exists:
18+
with mock.patch.object(Path, "exists") as mock_exists:
1919
mock_exists.return_value = value
2020

21-
@patch("pathlib.Path")
22-
def test_find_modified_include_path(self, MockPath):
21+
@mock.patch("pathlib.Path")
22+
def test_find_modified_include_path(self, mock_pathlib_path):
2323
handler = microtvm_api_server.Handler()
2424

25-
project_dir = MockPath("/dummy/project")
26-
file_path = project_dir / "src/standalone_crt/src/runtime/crt/graph_executor/load_json.c"
25+
project_dir = mock_pathlib_path("/dummy/project")
26+
file_path = (
27+
project_dir
28+
/ "src"
29+
/ "standalone_crt"
30+
/ "src"
31+
/ "runtime"
32+
/ "crt"
33+
/ "graph_executor"
34+
/ "load_json.c"
35+
)
2736

2837
# Should return C standard libs unmodified
2938
clib_output = handler._find_modified_include_path(project_dir, file_path, "math.h")
3039
assert clib_output == "math.h"
3140

3241
# If import already works, should return unmodified
33-
valid_ardino_import = "../../../../include/tvm/runtime/crt/platform.h"
42+
valid_arduino_import = "../../../../include/tvm/runtime/crt/platform.h"
3443
self._set_pathlib_path_exists(True)
3544
valid_output = handler._find_modified_include_path(
36-
project_dir, file_path, valid_ardino_import
45+
project_dir, file_path, valid_arduino_import
3746
)
38-
assert valid_output == valid_ardino_import
47+
assert valid_output == valid_arduino_import
48+
49+
BOARD_CONNECTED_OUTPUT = bytes(
50+
"Port Type Board Name FQBN Core \n"
51+
"/dev/ttyACM0 Serial Port (USB) Arduino Nano 33 BLE arduino:mbed_nano:nano33ble arduino:mbed_nano\n"
52+
"/dev/ttyS4 Serial Port Unknown \n"
53+
"\n",
54+
"utf-8",
55+
)
56+
BOARD_DISCONNECTED_OUTPUT = bytes(
57+
"Port Type Board Name FQBN Core\n"
58+
"/dev/ttyS4 Serial Port Unknown \n"
59+
"\n",
60+
"utf-8",
61+
)
62+
63+
@mock.patch("subprocess.check_output")
64+
def test_auto_detect_port(self, mock_subprocess_check_output):
65+
process_mock = mock.Mock()
66+
handler = microtvm_api_server.Handler()
67+
68+
# Test it returns the correct port when a board is connected
69+
mock_subprocess_check_output.return_value = self.BOARD_CONNECTED_OUTPUT
70+
detected_port = handler._auto_detect_port(self.DEFAULT_OPTIONS)
71+
assert detected_port == "/dev/ttyACM0"
72+
73+
# Test it raises an exception when no board is connected
74+
mock_subprocess_check_output.return_value = self.BOARD_DISCONNECTED_OUTPUT
75+
with pytest.raises(microtvm_api_server.BoardAutodetectFailed):
76+
handler._auto_detect_port(self.DEFAULT_OPTIONS)
77+
78+
@mock.patch("subprocess.check_call")
79+
def test_flash(self, mock_subprocess_check_call):
80+
handler = microtvm_api_server.Handler()
81+
handler._port = "/dev/ttyACM0"
82+
83+
# Test no exception thrown when code 0 returned
84+
mock_subprocess_check_call.return_value = 0
85+
handler.flash(self.DEFAULT_OPTIONS)
86+
mock_subprocess_check_call.assert_called_once()
87+
88+
# Test InvalidPortException raised when port incorrect
89+
mock_subprocess_check_call.return_value = 2
90+
with pytest.raises(microtvm_api_server.InvalidPortException):
91+
handler.flash(self.DEFAULT_OPTIONS)
92+
93+
# Test SketchUploadException raised for other issues
94+
mock_subprocess_check_call.return_value = 1
95+
with pytest.raises(microtvm_api_server.SketchUploadException):
96+
handler.flash(self.DEFAULT_OPTIONS)

0 commit comments

Comments
 (0)