Skip to content

Commit cb03042

Browse files
Merge pull request #249 from bridadan/test_cleanup
Get tests passing on Windows (and other cleanup)
2 parents d4d7f53 + 0ee9341 commit cb03042

File tree

6 files changed

+43
-157
lines changed

6 files changed

+43
-157
lines changed

mbed_lstools/platform_database.py

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -267,7 +267,8 @@ def __init__(self, database_files, primary_database=None):
267267
self._keys = set()
268268
for db in database_files:
269269
try:
270-
new_db = json.load(open(db, encoding="utf-8"))
270+
with open(db, encoding="utf-8") as db_json_file:
271+
new_db = json.load(db_json_file)
271272
duplicates = set(self._keys).intersection(set(new_db.keys()))
272273
if duplicates:
273274
logger.warning(

test/basic.py

Lines changed: 0 additions & 40 deletions
This file was deleted.

test/listing.py

Lines changed: 0 additions & 97 deletions
This file was deleted.

test/mbedls_toolsbase.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -47,7 +47,7 @@ def setUp(self):
4747
def tearDown(self):
4848
pass
4949

50-
def test_list_mbeds(self):
50+
def test_list_mbeds_valid_platform(self):
5151
self.base.return_value = [{'mount_point': 'dummy_mount_point',
5252
'target_id_usb_id': u'0240DEADBEEF',
5353
'serial_port': "dummy_serial_port"},
@@ -67,7 +67,7 @@ def test_list_mbeds(self):
6767
self.assertEqual(to_check[0]['target_id'], "0241BEEFDEAD")
6868
self.assertEqual(to_check[0]['platform_name'], 'foo_target')
6969

70-
def test_list_mbeds(self):
70+
def test_list_mbeds_invalid_platform(self):
7171
self.base.return_value = [{'mount_point': 'dummy_mount_point',
7272
'target_id_usb_id': u'not_in_target_db',
7373
'serial_port': "dummy_serial_port"}]

test/os_linux_generic.py

Lines changed: 16 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -17,10 +17,11 @@
1717
'''
1818

1919
import unittest
20+
import sys
21+
import os
2022
from mock import patch
2123
from mbed_lstools.linux import MbedLsToolsLinuxGeneric
2224

23-
2425
class LinuxPortTestCase(unittest.TestCase):
2526
''' Basic test cases checking trivial asserts
2627
'''
@@ -79,18 +80,32 @@ def test_get_mount_point_ext(self):
7980
self.assertEqual('/mnt/DAPLINK__', mount_dict['/dev/sdi'])
8081

8182
def find_candidates_with_patch(self, mount_list, link_dict, listdir_dict):
83+
if not getattr(sys.modules['os'], 'readlink', None):
84+
sys.modules['os'].readlink = None
85+
8286
with patch('mbed_lstools.linux.MbedLsToolsLinuxGeneric._run_cli_process') as _cliproc,\
8387
patch('os.readlink') as _readlink,\
8488
patch('os.listdir') as _listdir,\
89+
patch('mbed_lstools.linux.abspath') as _abspath,\
8590
patch('mbed_lstools.linux.isdir') as _isdir:
8691
_isdir.return_value = True
8792
_cliproc.return_value = (b'\n'.join(mount_list), None, 0)
8893
def do_readlink(link):
94+
# Fix for testing on Windows
95+
link = link.replace('\\', '/')
8996
return link_dict[link]
9097
_readlink.side_effect = do_readlink
9198
def do_listdir(dir):
99+
# Fix for testing on Windows
100+
dir = dir.replace('\\', '/')
92101
return listdir_dict[dir]
93102
_listdir.side_effect = do_listdir
103+
def do_abspath(dir):
104+
_, path = os.path.splitdrive(
105+
os.path.normpath(os.path.join(os.getcwd(), dir)))
106+
path = path.replace('\\', '/')
107+
return path
108+
_abspath.side_effect = do_abspath
94109
ret_val = self.linux_generic.find_candidates()
95110
_cliproc.assert_called_once_with('mount')
96111
return ret_val

test/platform_database.py

Lines changed: 23 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -38,31 +38,32 @@ class EmptyPlatformDatabaseTests(unittest.TestCase):
3838
"""
3939

4040
def setUp(self):
41-
self.base_db = tempfile.NamedTemporaryFile(prefix='base')
41+
self.base_db_path = os.path.join(tempfile.mkdtemp(), 'base')
42+
self.base_db = open(self.base_db_path, 'w+b')
4243
self.base_db.write(b'{}')
4344
self.base_db.seek(0)
44-
self.pdb = PlatformDatabase([self.base_db.name])
45+
self.pdb = PlatformDatabase([self.base_db_path])
4546

4647
def tearDown(self):
47-
pass
48+
self.base_db.close()
4849

4950
def test_broken_database_io(self):
5051
"""Verify that the platform database still works without a
5152
working backing file
5253
"""
5354
with patch("mbed_lstools.platform_database.open") as _open:
5455
_open.side_effect = IOError("Bogus")
55-
self.pdb = PlatformDatabase([self.base_db.name])
56+
self.pdb = PlatformDatabase([self.base_db_path])
5657
self.pdb.add("1234", "MYTARGET")
5758
self.assertEqual(self.pdb.get("1234"), "MYTARGET")
5859

5960
def test_broken_database_bad_json(self):
6061
"""Verify that the platform database still works without a
6162
working backing file
6263
"""
63-
self.base_db.write(b'{{}')
64+
self.base_db.write(b'{}')
6465
self.base_db.seek(0)
65-
self.pdb = PlatformDatabase([self.base_db.name])
66+
self.pdb = PlatformDatabase([self.base_db_path])
6667
self.pdb.add("1234", "MYTARGET")
6768
self.assertEqual(self.pdb.get("1234"), "MYTARGET")
6869

@@ -119,20 +120,24 @@ class OverriddenPlatformDatabaseTests(unittest.TestCase):
119120
"""
120121

121122
def setUp(self):
122-
self.base_db = tempfile.NamedTemporaryFile(prefix='base')
123+
self.temp_dir = tempfile.mkdtemp()
124+
self.base_db_path = os.path.join(self.temp_dir, 'base')
125+
self.base_db = open(self.base_db_path, 'w+b')
123126
self.base_db.write(json.dumps(dict([('0123', 'Base_Platform')])).
124127
encode('utf-8'))
125128
self.base_db.seek(0)
126-
self.overriding_db = tempfile.NamedTemporaryFile(prefix='overriding')
129+
self.overriding_db_path = os.path.join(self.temp_dir, 'overriding')
130+
self.overriding_db = open(self.overriding_db_path, 'w+b')
127131
self.overriding_db.write(b'{}')
128132
self.overriding_db.seek(0)
129-
self.pdb = PlatformDatabase([self.overriding_db.name, self.base_db.name],
130-
primary_database=self.overriding_db.name)
133+
self.pdb = PlatformDatabase([self.overriding_db_path, self.base_db_path],
134+
primary_database=self.overriding_db_path)
131135
self.base_db.seek(0)
132136
self.overriding_db.seek(0)
133137

134138
def tearDown(self):
135-
pass
139+
self.base_db.close()
140+
self.overriding_db.close()
136141

137142
def assertBaseUnchanged(self):
138143
"""Assert that the base database has not changed
@@ -170,8 +175,8 @@ def test_load_override(self):
170175
self.overriding_db.write(json.dumps(dict([('0123', 'Overriding_Platform')])).
171176
encode('utf-8'))
172177
self.overriding_db.seek(0)
173-
self.pdb = PlatformDatabase([self.overriding_db.name, self.base_db.name],
174-
primary_database=self.overriding_db.name)
178+
self.pdb = PlatformDatabase([self.overriding_db_path, self.base_db_path],
179+
primary_database=self.overriding_db_path)
175180
self.assertIn(('0123', 'Overriding_Platform'), list(self.pdb.items()))
176181
self.assertEqual(set(self.pdb.all_ids()), set(['0123']))
177182
self.assertEqual(self.pdb.get('0123'), 'Overriding_Platform')
@@ -231,14 +236,16 @@ def setUp(self):
231236
self.mocked_lock = patch('mbed_lstools.platform_database.InterProcessLock', spec=True).start()
232237
self.acquire = self.mocked_lock.return_value.acquire
233238
self.release = self.mocked_lock.return_value.release
234-
self.base_db = tempfile.NamedTemporaryFile(prefix='base')
239+
240+
self.base_db_path = os.path.join(tempfile.mkdtemp(), 'base')
241+
self.base_db = open(self.base_db_path, 'w+b')
235242
self.base_db.write(b'{}')
236243
self.base_db.seek(0)
237-
self.pdb = PlatformDatabase([self.base_db.name])
244+
self.pdb = PlatformDatabase([self.base_db_path])
238245
self.addCleanup(patch.stopall)
239246

240247
def tearDown(self):
241-
pass
248+
self.base_db.close()
242249

243250
def test_no_update(self):
244251
"""Test that no locks are used when no modifications are specified

0 commit comments

Comments
 (0)