Skip to content

Commit b887bb7

Browse files
committed
Added safety check, added test converage for new function
1 parent 7b49591 commit b887bb7

File tree

2 files changed

+96
-11
lines changed

2 files changed

+96
-11
lines changed

tools/detect_targets.py

Lines changed: 11 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -111,18 +111,18 @@ def get_interface_version(mount_point):
111111
@param mount_point Name of disk where platform is connected to host machine.
112112
"""
113113
if get_module_avail('mbed_lstools'):
114-
mbeds = mbed_lstools.create()
115-
details_txt = mbeds.get_details_txt(mount_point)
116-
117-
if (details_txt is None):
118-
details_txt = {}
114+
try :
115+
mbeds = mbed_lstools.create()
116+
details_txt = mbeds.get_details_txt(mount_point)
119117

120-
121-
if 'Interface Version' in details_txt:
122-
return details_txt['Interface Version']
123-
124-
elif 'Version' in details_txt:
125-
return details_txt['Version']
118+
if 'Interface Version' in details_txt:
119+
return details_txt['Interface Version']
120+
121+
elif 'Version' in details_txt:
122+
return details_txt['Version']
123+
124+
except :
125+
return 'unknown'
126126

127127
return 'unknown'
128128

tools/test/detect_targets_test.py

Lines changed: 85 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,85 @@
1+
"""
2+
mbed SDK
3+
Copyright (c) 2016 ARM Limited
4+
5+
Licensed under the Apache License, Version 2.0 (the "License");
6+
you may not use this file except in compliance with the License.
7+
You may obtain a copy of the License at
8+
9+
http://www.apache.org/licenses/LICENSE-2.0
10+
11+
Unless required by applicable law or agreed to in writing, software
12+
distributed under the License is distributed on an "AS IS" BASIS,
13+
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14+
See the License for the specific language governing permissions and
15+
limitations under the License.
16+
"""
17+
18+
import unittest
19+
from collections import namedtuple
20+
from mock import patch, MagicMock
21+
from tools.detect_targets import get_interface_version
22+
from tools.test_api import get_autodetected_MUTS_list
23+
24+
"""
25+
Tests for detect_targets.py
26+
"""
27+
28+
class DetectTargetsTests(unittest.TestCase):
29+
"""
30+
Test cases for Detect Target functionality
31+
"""
32+
33+
def setUp(self):
34+
"""
35+
Called before each test case
36+
37+
:return:
38+
"""
39+
# Gather a valid mount point from the host machine
40+
muts = get_autodetected_MUTS_list()
41+
42+
count = 0
43+
44+
for mut in muts.values():
45+
46+
count += 1
47+
self.valid_mount_point = mut['disk']
48+
break
49+
50+
# If no targets are found, there is no way to determine Host OS mount point.
51+
# Function should handle failure gracefully regardless of a mount point being present.
52+
# Therefore it is safe to assume a valid mount point.
53+
if count is 0:
54+
self.valid_mount_point = "D:"
55+
56+
self.invalid_mount_point = "23z/e\n"
57+
self.missing_mount_point = None
58+
59+
def tearDown(self):
60+
"""
61+
Nothing to tear down.
62+
Called after each test case
63+
64+
:return:
65+
"""
66+
pass
67+
68+
def test_interface_version_valid_mount_point(self):
69+
70+
interface_version = get_interface_version(self.valid_mount_point)
71+
assert len(interface_version) > 0
72+
73+
def test_interface_version_invalid_mount_point(self):
74+
75+
interface_version = get_interface_version(self.invalid_mount_point)
76+
assert interface_version == 'unknown'
77+
78+
def test_interface_version_missing_mount_point(self):
79+
80+
interface_version = get_interface_version(self.missing_mount_point)
81+
assert interface_version == 'unknown'
82+
83+
84+
if __name__ == '__main__':
85+
unittest.main()

0 commit comments

Comments
 (0)