Skip to content

Commit ba99fc8

Browse files
committed
Updating tests with consolidated listdir behavior
1 parent 1a035a0 commit ba99fc8

File tree

1 file changed

+20
-37
lines changed

1 file changed

+20
-37
lines changed

test/mbedls_toolsbase.py

Lines changed: 20 additions & 37 deletions
Original file line numberDiff line numberDiff line change
@@ -59,13 +59,13 @@ def test_list_mbeds_valid_platform(self):
5959
with patch("mbed_lstools.lstools_base.MbedLsToolsBase._read_htm_ids") as _read_htm,\
6060
patch("mbed_lstools.lstools_base.MbedLsToolsBase.mount_point_ready") as _mpr,\
6161
patch("mbed_lstools.lstools_base.PlatformDatabase.get") as _get,\
62-
patch("mbed_lstools.lstools_base.MbedLsToolsBase._detect_device_type") as _d_type:
62+
patch('os.listdir') as _listdir:
6363
_mpr.return_value = True
6464
_read_htm.return_value = (u'0241BEEFDEAD', {})
6565
_get.return_value = {
6666
'platform_name': 'foo_target'
6767
}
68-
_d_type.return_value = 'daplink'
68+
_listdir.return_value = []
6969
to_check = self.base.list_mbeds()
7070
_read_htm.assert_called_once_with('dummy_mount_point')
7171
_get.assert_called_once_with('0241', device_type='daplink', verbose_data=True)
@@ -83,13 +83,13 @@ def test_list_mbeds_invalid_tid(self):
8383
with patch("mbed_lstools.lstools_base.MbedLsToolsBase._read_htm_ids") as _read_htm,\
8484
patch("mbed_lstools.lstools_base.MbedLsToolsBase.mount_point_ready") as _mpr,\
8585
patch("mbed_lstools.lstools_base.PlatformDatabase.get") as _get,\
86-
patch("mbed_lstools.lstools_base.MbedLsToolsBase._detect_device_type") as _d_type:
86+
patch('os.listdir') as _listdir:
8787
_mpr.return_value = True
8888
_read_htm.side_effect = [(u'0241BEEFDEAD', {}), (None, {})]
8989
_get.return_value = {
9090
'platform_name': 'foo_target'
9191
}
92-
_d_type.return_value = 'daplink'
92+
_listdir.return_value = []
9393
to_check = self.base.list_mbeds()
9494
_get.assert_called_once_with('0241', device_type='daplink', verbose_data=True)
9595
self.assertEqual(len(to_check), 2)
@@ -106,11 +106,11 @@ def test_list_mbeds_invalid_platform(self):
106106
with patch("mbed_lstools.lstools_base.MbedLsToolsBase._read_htm_ids") as _read_htm,\
107107
patch("mbed_lstools.lstools_base.MbedLsToolsBase.mount_point_ready") as _mpr,\
108108
patch("mbed_lstools.lstools_base.PlatformDatabase.get") as _get,\
109-
patch("mbed_lstools.lstools_base.MbedLsToolsBase._detect_device_type") as _d_type:
109+
patch('os.listdir') as _listdir:
110110
_mpr.return_value = True
111111
_read_htm.return_value = (u'not_in_target_db', {})
112112
_get.return_value = None
113-
_d_type.return_value = 'daplink'
113+
_listdir.return_value = []
114114
to_check = self.base.list_mbeds()
115115
_read_htm.assert_called_once_with('dummy_mount_point')
116116
_get.assert_called_once_with('not_', device_type='daplink', verbose_data=True)
@@ -165,19 +165,11 @@ def test_update_device_from_fs_unknown(self):
165165
self.assertEqual(device['device_type'], 'unknown')
166166

167167
def test_detect_device_test(self):
168-
dummy_mount_point = 'dummy'
169-
with patch('os.listdir') as _listdir:
170-
_listdir.return_value = ['Segger.html']
171-
device_type = self.base._detect_device_type(dummy_mount_point)
172-
self.assertEqual(device_type, 'jlink')
173-
_listdir.assert_called_once_with(dummy_mount_point)
174-
175-
_listdir.reset_mock()
176-
_listdir.return_value = ['MBED.HTM', 'DETAILS.TXT']
168+
device_type = self.base._detect_device_type(['Segger.html'])
169+
self.assertEqual(device_type, 'jlink')
177170

178-
device_type = self.base._detect_device_type(dummy_mount_point)
179-
self.assertEqual(device_type, 'daplink')
180-
_listdir.assert_called_once_with(dummy_mount_point)
171+
device_type = self.base._detect_device_type(['MBED.HTM', 'DETAILS.TXT'])
172+
self.assertEqual(device_type, 'daplink')
181173

182174
def test_update_device_details_jlink(self):
183175
jlink_html_contents = ('<html><head><meta http-equiv="refresh" '
@@ -189,35 +181,26 @@ def test_update_device_details_jlink(self):
189181
'mount_point': dummy_mount_point
190182
}
191183

192-
with patch('os.listdir') as _listdir,\
193-
patch('mbed_lstools.lstools_base.open', _open):
194-
_listdir.return_value = ['Board.html', 'User Guide.html']
184+
with patch('mbed_lstools.lstools_base.open', _open):
195185
device = deepcopy(base_device)
196-
self.base._update_device_details_jlink(device, False)
186+
self.base._update_device_details_jlink(device, False, ['Board.html', 'User Guide.html'])
197187
self.assertEqual(device['url'], 'http://www.nxp.com/FRDM-KL27Z')
198188
self.assertEqual(device['platform_name'], 'KL27Z')
199-
_listdir.assert_called_once_with(dummy_mount_point)
200189
_open.assert_called_once_with(os.path.join(dummy_mount_point, 'Board.html'), 'r')
201190

202191
_open.reset_mock()
203-
_listdir.reset_mock()
204-
_listdir.return_value = ['User Guide.html']
205192

206193
device = deepcopy(base_device)
207-
self.base._update_device_details_jlink(device, False)
194+
self.base._update_device_details_jlink(device, False, ['User Guide.html'])
208195
self.assertEqual(device['url'], 'http://www.nxp.com/FRDM-KL27Z')
209196
self.assertEqual(device['platform_name'], 'KL27Z')
210-
_listdir.assert_called_once_with(dummy_mount_point)
211197
_open.assert_called_once_with(os.path.join(dummy_mount_point, 'User Guide.html'), 'r')
212198

213199
_open.reset_mock()
214-
_listdir.reset_mock()
215-
_listdir.return_value = ['unhelpful_file.html']
216200

217201
device = deepcopy(base_device)
218-
self.base._update_device_details_jlink(device, False)
202+
self.base._update_device_details_jlink(device, False, ['unhelpful_file.html'])
219203
self.assertEqual(device, base_device)
220-
_listdir.assert_called_once_with(dummy_mount_point)
221204
_open.assert_not_called()
222205

223206
def test_fs_never(self):
@@ -259,10 +242,10 @@ def test_fs_after(self):
259242
}
260243
with patch("mbed_lstools.lstools_base.MbedLsToolsBase._read_htm_ids") as _read_htm,\
261244
patch("mbed_lstools.lstools_base.MbedLsToolsBase._details_txt") as _up_details,\
262-
patch("mbed_lstools.lstools_base.MbedLsToolsBase._detect_device_type") as _d_type:
245+
patch('os.listdir') as _listdir:
263246
new_device_id = "00017531642046"
264247
_read_htm.return_value = (new_device_id, {})
265-
_d_type.return_value = 'daplink'
248+
_listdir.return_value = []
266249
_up_details.return_value = {
267250
'automation_allowed': '0'
268251
}
@@ -319,10 +302,10 @@ def test_fs_before(self):
319302
}
320303
with patch("mbed_lstools.lstools_base.MbedLsToolsBase._read_htm_ids") as _read_htm,\
321304
patch("mbed_lstools.lstools_base.MbedLsToolsBase._details_txt") as _up_details,\
322-
patch("mbed_lstools.lstools_base.MbedLsToolsBase._detect_device_type") as _d_type:
305+
patch('os.listdir') as _listdir:
323306
new_device_id = u'00017575430420'
324307
_read_htm.return_value = (new_device_id, {})
325-
_d_type.return_value = 'daplink'
308+
_listdir.return_value = []
326309
_up_details.return_value = {
327310
'automation_allowed': '0'
328311
}
@@ -402,13 +385,13 @@ def test_list_mbeds_valid_platform(self):
402385
with patch('mbed_lstools.lstools_base.MbedLsToolsBase._read_htm_ids') as _read_htm,\
403386
patch('mbed_lstools.lstools_base.MbedLsToolsBase.mount_point_ready') as _mpr,\
404387
patch('mbed_lstools.lstools_base.PlatformDatabase.get') as _get,\
405-
patch("mbed_lstools.lstools_base.MbedLsToolsBase._detect_device_type") as _d_type:
388+
patch('os.listdir') as _listdir:
406389
_mpr.return_value = True
407390
_read_htm.return_value = (u'0240DEADBEEF', {})
408391
_get.return_value = {
409392
'platform_name': 'foo_target'
410393
}
411-
_d_type.return_value = 'daplink'
394+
_listdir.return_value = []
412395
to_check = self.base.list_mbeds()
413396
self.assertEqual(len(to_check), 1)
414397
self.assertEqual(to_check[0]['serial_port'], 'valid')

0 commit comments

Comments
 (0)