Skip to content

Commit 0b77c60

Browse files
committed
Cleaning up paths
1 parent 1894a27 commit 0b77c60

File tree

4 files changed

+35
-32
lines changed

4 files changed

+35
-32
lines changed

test/test/host_test_black_box.py

Lines changed: 16 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -15,13 +15,9 @@
1515
# limitations under the License.
1616

1717
import unittest
18-
import os
19-
import re
20-
from builtins import super
2118
from copy import copy
2219
from mbed_os_tools.test import init_host_test_cli_params
2320
from mbed_os_tools.test.host_tests_runner.host_test_default import DefaultTestSelector
24-
from mock import patch, MagicMock
2521

2622
from .mocks.environment.linux import MockTestEnvironmentLinux
2723
from .mocks.environment.darwin import MockTestEnvironmentDarwin
@@ -30,41 +26,38 @@
3026
mock_platform_info = {
3127
"platform_name": "K64F",
3228
"target_id": "0240000031754e45000c0018948500156461000097969900",
33-
"mount_point": os.path.normpath("mnt/DAPLINK"),
34-
"serial_port": os.path.normpath("dev/ttyACM0"),
29+
"mount_point": "/mnt/DAPLINK",
30+
"serial_port": "/dev/ttyACM0",
3531
}
36-
mock_image_path = os.path.normpath(
37-
"BUILD/tests/K64F/GCC_ARM/TESTS/network/interface/interface.bin"
38-
)
32+
mock_image_path = "BUILD/tests/K64F/GCC_ARM/TESTS/network/interface/interface.bin"
3933

4034
class BlackBoxHostTestTestCase(unittest.TestCase):
4135

42-
def test_host_test_linux(self):
43-
with MockTestEnvironmentLinux(self, mock_platform_info, mock_image_path) as _env:
36+
def _run_host_test(self, environment):
37+
with environment as _env:
4438
test_selector = DefaultTestSelector(init_host_test_cli_params())
4539
result = test_selector.execute()
4640
test_selector.finish()
4741

4842
self.assertEqual(result, 0)
4943

50-
def test_host_test_darwin(self):
51-
with MockTestEnvironmentDarwin(self, mock_platform_info, mock_image_path) as _env:
52-
test_selector = DefaultTestSelector(init_host_test_cli_params())
53-
result = test_selector.execute()
54-
test_selector.finish()
44+
def test_host_test_linux(self):
45+
self._run_host_test(
46+
MockTestEnvironmentLinux(self, mock_platform_info, mock_image_path)
47+
)
5548

56-
self.assertEqual(result, 0)
49+
def test_host_test_darwin(self):
50+
self._run_host_test(
51+
MockTestEnvironmentDarwin(self, mock_platform_info, mock_image_path)
52+
)
5753

5854
def test_host_test_windows(self):
5955
win_mock_platform_info = copy(mock_platform_info)
60-
win_mock_platform_info["mount_point"] = "D:"
6156
win_mock_platform_info["serial_port"] = "COM5"
62-
with MockTestEnvironmentWindows(self, mock_platform_info, mock_image_path) as _env:
63-
test_selector = DefaultTestSelector(init_host_test_cli_params())
64-
result = test_selector.execute()
65-
test_selector.finish()
6657

67-
self.assertEqual(result, 0)
58+
self._run_host_test(
59+
MockTestEnvironmentWindows(self, win_mock_platform_info, mock_image_path)
60+
)
6861

6962
if __name__ == '__main__':
7063
unittest.main()

test/test/mocks/environment/__init__.py

Lines changed: 18 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -13,13 +13,20 @@ class MockTestEnvironment(object):
1313
def __init__(self, test_case, platform_info, image_path):
1414
self._test_case = test_case
1515
self._tempdir = tempfile.mkdtemp()
16-
self._platform_info = copy(platform_info)
17-
self._platform_info['mount_point'] = os.path.splitdrive(os.path.join(self._tempdir, self._platform_info['mount_point']))[1]
18-
self._platform_info['serial_port'] = os.path.splitdrive(os.path.join(self._tempdir, self._platform_info['serial_port']))[1]
19-
self._image_path = os.path.splitdrive(os.path.join(self._tempdir, image_path))[1]
20-
2116
self._patch_definitions = []
2217
self.patches = {}
18+
self._platform_info = copy(platform_info)
19+
20+
# Clean and retarget path to tempdir
21+
self._image_path = self._clean_path(image_path)
22+
self._platform_info['mount_point'] = self._clean_path(
23+
self._platform_info['mount_point']
24+
)
25+
26+
# Need to remove the drive letter in this case
27+
self._platform_info['serial_port'] = os.path.splitdrive(
28+
self._clean_path(self._platform_info['serial_port'])
29+
)[1]
2330

2431
args = (
2532
'mbedhtrun -m {} -p {}:9600 -f '
@@ -42,7 +49,7 @@ def __init__(self, test_case, platform_info, image_path):
4249
]
4350
self.patch('mbed_os_tools.detect.create', new=detect_mock)
4451

45-
# Mock process
52+
# Mock process calls and move them to threads to preserve mocks
4653
self.patch(
4754
'mbed_os_tools.test.host_tests_runner.host_test_default.Process',
4855
new=MagicMock(side_effect=self._process_side_effect)
@@ -52,14 +59,18 @@ def __init__(self, test_case, platform_info, image_path):
5259
new=MagicMock(return_value=0)
5360
)
5461

55-
# Mock serial
5662
mock_serial = MockSerial()
5763
mock_device = MockMbedDevice(mock_serial)
5864
self.patch(
5965
'mbed_os_tools.test.host_tests_conn_proxy.conn_primitive_serial.Serial',
6066
new=MagicMock(return_value=mock_serial)
6167
)
6268

69+
def _clean_path(self, path):
70+
# Remove the drive letter and ensure separators are consistent
71+
path = os.path.splitdrive(os.path.normpath(path))[1]
72+
return os.path.join(self._tempdir, path.lstrip(os.sep))
73+
6374
@staticmethod
6475
def _process_side_effect(target=None, args=None):
6576
return MockThread(target=target, args=args)

test/test/mocks/serial.py

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,6 @@ def read(self, count):
1919
def write(self, data):
2020
self._tx_buffer += data
2121
if self._upstream_write_cb:
22-
# TODO this may not work...
2322
self._upstream_write_cb(data)
2423

2524
def close(self):

test/test/mocks/thread.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@ class MockThread(Thread):
55
def __init__(self, target=None, args=None):
66
super().__init__(target=target, args=args)
77
self._terminates = 0
8-
self.exitcode = 0 # TODO maybe this needs to be setable? Mock sys.exit
8+
self.exitcode = 0
99

1010
def terminate(self):
1111
self._terminates += 1

0 commit comments

Comments
 (0)