19
19
import re
20
20
21
21
import threading
22
+ import shutil
23
+ import tempfile
22
24
from builtins import super
23
25
from copy import copy
24
26
from mbed_os_tools .test import init_host_test_cli_params
25
27
from mbed_os_tools .test .host_tests_runner .host_test_default import DefaultTestSelector
26
28
from mock import patch , MagicMock
27
- from pyfakefs import fake_filesystem_unittest
28
29
29
30
30
31
class MockThread (threading .Thread ):
31
32
def __init__ (self , target = None , args = None ):
32
33
super ().__init__ (target = target , args = args )
33
- print ('Mock Thread constr' )
34
34
self ._terminates = 0
35
35
self .exitcode = 0 # TODO maybe this needs to be setable? Mock sys.exit
36
36
@@ -44,8 +44,8 @@ def __init__(self, *args, **kwargs):
44
44
self ._kwargs = kwargs
45
45
self ._open = True
46
46
self ._rx_counter = 0
47
- self ._tx_buffer = ""
48
- self ._rx_buffer = ""
47
+ self ._tx_buffer = b ""
48
+ self ._rx_buffer = b ""
49
49
self ._upstream_write_cb = None
50
50
51
51
def read (self , count ):
@@ -63,12 +63,15 @@ def close(self):
63
63
self ._open = False
64
64
65
65
def downstream_write (self , data ):
66
+ self ._rx_buffer += data .encode ("utf-8" )
67
+
68
+ def downstream_write_bytes (self , data ):
66
69
self ._rx_buffer += data
67
70
68
71
def on_upstream_write (self , func ):
69
72
self ._upstream_write_cb = func
70
73
71
- kv_regex = re .compile (r "\{\{([\w\d_-]+);([^\}]+)\}\}" )
74
+ kv_regex = re .compile ("\{\{([\w\d_-]+);([^\}]+)\}\}" )
72
75
73
76
class MockMbedDevice (object ):
74
77
def __init__ (self , serial ):
@@ -90,7 +93,7 @@ def send_kv(self, key, value):
90
93
self ._serial .downstream_write ("{{{{{};{}}}}}\r \n " .format (key , value ))
91
94
92
95
def on_write (self , data ):
93
- kvs = kv_regex .findall (data )
96
+ kvs = kv_regex .findall (data . decode ( "utf-8" ) )
94
97
95
98
for key , value in kvs :
96
99
self .handle_kv (key , value )
@@ -112,8 +115,12 @@ class MockTestEnvironment(object):
112
115
113
116
def __init__ (self , test_case , platform_info , image_path ):
114
117
self ._test_case = test_case
115
- self ._platform_info = platform_info
116
- self ._image_path = image_path
118
+ self ._tempdir = tempfile .mkdtemp ()
119
+ self ._platform_info = copy (platform_info )
120
+ self ._platform_info ['mount_point' ] = os .path .join (self ._tempdir , self ._platform_info ['mount_point' ])
121
+ self ._platform_info ['serial_port' ] = os .path .join (self ._tempdir , self ._platform_info ['serial_port' ])
122
+ self ._image_path = os .path .join (self ._tempdir , image_path )
123
+
117
124
self ._patch_definitions = []
118
125
self .patches = {}
119
126
@@ -161,8 +168,11 @@ def patch(self, path, **kwargs):
161
168
self ._patch_definitions .append ((path , patch (path , ** kwargs )))
162
169
163
170
def __enter__ (self ):
164
- self ._test_case .fs .create_file (self ._image_path )
165
- self ._test_case .fs .create_dir (self ._platform_info ['mount_point' ])
171
+ os .makedirs (os .path .dirname (self ._image_path ))
172
+ with open (self ._image_path , 'w' ) as _ :
173
+ pass
174
+
175
+ os .makedirs (self ._platform_info ['mount_point' ])
166
176
167
177
for path , patcher in self ._patch_definitions :
168
178
self .patches [path ] = patcher .start ()
@@ -171,6 +181,8 @@ def __exit__(self, type, value, traceback):
171
181
for _ , patcher in self ._patch_definitions :
172
182
patcher .stop ()
173
183
184
+ shutil .rmtree (self ._tempdir )
185
+
174
186
class MockTestEnvironmentPosix (MockTestEnvironment ):
175
187
176
188
def __init__ (self , test_case , platform_info , image_path ):
@@ -280,20 +292,14 @@ def __exit__(self, type, value, traceback):
280
292
mock_platform_info = {
281
293
"platform_name" : "K64F" ,
282
294
"target_id" : "0240000031754e45000c0018948500156461000097969900" ,
283
- "mount_point" : os .path .normpath ("/ mnt/DAPLINK" ),
284
- "serial_port" : os .path .normpath ("/ dev/ttyACM0" ),
295
+ "mount_point" : os .path .normpath ("mnt/DAPLINK" ),
296
+ "serial_port" : os .path .normpath ("dev/ttyACM0" ),
285
297
}
286
298
mock_image_path = os .path .normpath (
287
299
"BUILD/tests/K64F/GCC_ARM/TESTS/network/interface/interface.bin"
288
300
)
289
301
290
- class BlackBoxHostTestTestCase (fake_filesystem_unittest .TestCase ):
291
-
292
- def setUp (self ):
293
- self .setUpPyfakefs ()
294
-
295
- def tearDown (self ):
296
- pass
302
+ class BlackBoxHostTestTestCase (unittest .TestCase ):
297
303
298
304
def test_host_test_linux (self ):
299
305
with MockTestEnvironmentLinux (self , mock_platform_info , mock_image_path ) as _env :
0 commit comments