From d43a8476676134856c58e6e063a90bf90c16dcaf Mon Sep 17 00:00:00 2001 From: David Walters Date: Mon, 11 Sep 2017 16:54:32 +0100 Subject: [PATCH 01/11] Added additional DAPLink version info for `mbed detect` command` --- tools/detect_targets.py | 7 +++++-- tools/test_api.py | 6 ++++++ 2 files changed, 11 insertions(+), 2 deletions(-) diff --git a/tools/detect_targets.py b/tools/detect_targets.py index 5f32801db8..1ef57e764b 100644 --- a/tools/detect_targets.py +++ b/tools/detect_targets.py @@ -31,6 +31,7 @@ check_required_modules(['prettytable']) # Imports related to mbed build api from tools.build_api import mcu_toolchain_matrix from tools.test_api import get_autodetected_MUTS_list +from tools.test_api import get_mounted_details_txt from argparse import ArgumentParser @@ -75,9 +76,11 @@ def main(): count = 0 for mut in muts.values(): if re.match(mcu_filter, mut['mcu']): + # Grab additional target details about the mut + details_txt = get_mounted_details_txt(mut['disk']) print "" - print "[mbed] Detected %s, port %s, mounted %s" % \ - (mut['mcu'], mut['port'], mut['disk']) + print "[mbed] Detected %s, port %s, mounted %s, DAPLink version %s" % \ + (mut['mcu'], mut['port'], mut['disk'], details_txt['Interface Version']) print "[mbed] Supported toolchains for %s" % mut['mcu'] print mcu_toolchain_matrix(platform_filter=mut['mcu']) count += 1 diff --git a/tools/test_api.py b/tools/test_api.py index 985c318e85..13fac4cbe3 100644 --- a/tools/test_api.py +++ b/tools/test_api.py @@ -1647,6 +1647,12 @@ def get_module_avail(module_name): """ return module_name in sys.modules.keys() +def get_mounted_details_txt(mount_point): + """ Function returns object containing details.txt information from the specified mount point + @param mount_point Name of disk where platform is connected to host machine. + """ + mbeds = mbed_lstools.create() + return mbeds.get_details_txt(mount_point) def get_autodetected_MUTS_list(platform_name_filter=None): oldError = None From 7ff57c4671db8e948b9958d32ab87cacdfeee892 Mon Sep 17 00:00:00 2001 From: David Walters Date: Tue, 12 Sep 2017 10:46:07 +0100 Subject: [PATCH 02/11] Changed DAPLink version to interface version. --- tools/detect_targets.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/tools/detect_targets.py b/tools/detect_targets.py index 1ef57e764b..bf70c1c024 100644 --- a/tools/detect_targets.py +++ b/tools/detect_targets.py @@ -79,7 +79,7 @@ def main(): # Grab additional target details about the mut details_txt = get_mounted_details_txt(mut['disk']) print "" - print "[mbed] Detected %s, port %s, mounted %s, DAPLink version %s" % \ + print "[mbed] Detected %s, port %s, mounted %s, interface version %s" % \ (mut['mcu'], mut['port'], mut['disk'], details_txt['Interface Version']) print "[mbed] Supported toolchains for %s" % mut['mcu'] print mcu_toolchain_matrix(platform_filter=mut['mcu']) From e4b55dd6a0ea8fef84bb5e45d5a8ea1b55da878f Mon Sep 17 00:00:00 2001 From: David Walters Date: Fri, 15 Sep 2017 10:15:24 +0100 Subject: [PATCH 03/11] Removed get_mounted_details_txt from test_api.py Fixed minor typos in test_api.py Added get_interface_version to detect_targets --- tools/detect_targets.py | 33 +++++++++++++++++++++++++++------ tools/test_api.py | 9 +-------- 2 files changed, 28 insertions(+), 14 deletions(-) diff --git a/tools/detect_targets.py b/tools/detect_targets.py index bf70c1c024..c3474a8e1c 100644 --- a/tools/detect_targets.py +++ b/tools/detect_targets.py @@ -31,9 +31,13 @@ check_required_modules(['prettytable']) # Imports related to mbed build api from tools.build_api import mcu_toolchain_matrix from tools.test_api import get_autodetected_MUTS_list -from tools.test_api import get_mounted_details_txt +from tools.test_api import get_module_avail from argparse import ArgumentParser +try: + import mbed_lstools +except: + pass def main(): """Entry Point""" @@ -76,17 +80,17 @@ def main(): count = 0 for mut in muts.values(): if re.match(mcu_filter, mut['mcu']): - # Grab additional target details about the mut - details_txt = get_mounted_details_txt(mut['disk']) + interface_version = get_interface_version(mut['disk']) print "" - print "[mbed] Detected %s, port %s, mounted %s, interface version %s" % \ - (mut['mcu'], mut['port'], mut['disk'], details_txt['Interface Version']) + print "[mbed] Detected %s, port %s, mounted %s, interface version %s:" % \ + (mut['mcu'], mut['port'], mut['disk'], interface_version) + print "[mbed] Supported toolchains for %s" % mut['mcu'] print mcu_toolchain_matrix(platform_filter=mut['mcu']) count += 1 if count == 0: - print "[mbed] No mbed targets where detected on your system." + print "[mbed] No mbed targets were detected on your system." except KeyboardInterrupt: print "\n[CTRL+c] exit" @@ -95,6 +99,23 @@ def main(): traceback.print_exc(file=sys.stdout) print "[ERROR] %s" % str(exc) sys.exit(1) + +def get_interface_version(mount_point): + """ Function returns interface version from the target mounted on the specified mount point + mount_point = mut['port'] + @param mount_point Name of disk where platform is connected to host machine. + """ + if get_module_avail('mbed_lstools'): + mbeds = mbed_lstools.create() + details_txt = mbeds.get_details_txt(mount_point) + + if 'Interface Version' in details_txt: + return details_txt['Interface Version'] + + elif 'Version' in details_txt: + return details_txt['Version'] + + return 'unknown' if __name__ == '__main__': main() diff --git a/tools/test_api.py b/tools/test_api.py index 13fac4cbe3..c97e499dc5 100644 --- a/tools/test_api.py +++ b/tools/test_api.py @@ -1643,17 +1643,10 @@ def detect_database_verbose(db_url): def get_module_avail(module_name): - """ This function returns True if module_name is already impored module + """ This function returns True if module_name is already imported module """ return module_name in sys.modules.keys() -def get_mounted_details_txt(mount_point): - """ Function returns object containing details.txt information from the specified mount point - @param mount_point Name of disk where platform is connected to host machine. - """ - mbeds = mbed_lstools.create() - return mbeds.get_details_txt(mount_point) - def get_autodetected_MUTS_list(platform_name_filter=None): oldError = None if os.name == 'nt': From 256ab6114a627e87cfe9711d428161b09c6a4747 Mon Sep 17 00:00:00 2001 From: David Walters Date: Fri, 15 Sep 2017 15:30:34 +0100 Subject: [PATCH 04/11] Fixed mount_point example usage --- tools/detect_targets.py | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/tools/detect_targets.py b/tools/detect_targets.py index c3474a8e1c..bdccab1f3f 100644 --- a/tools/detect_targets.py +++ b/tools/detect_targets.py @@ -102,7 +102,8 @@ def main(): def get_interface_version(mount_point): """ Function returns interface version from the target mounted on the specified mount point - mount_point = mut['port'] + Example of mount_point: + mount_point = mut['disk'] @param mount_point Name of disk where platform is connected to host machine. """ if get_module_avail('mbed_lstools'): From aafe61414e0fbc98bd3f0251cfb169984d370993 Mon Sep 17 00:00:00 2001 From: David Walters Date: Fri, 15 Sep 2017 15:49:08 +0100 Subject: [PATCH 05/11] Edited get_interface_version info for clarity --- tools/detect_targets.py | 8 ++++++-- 1 file changed, 6 insertions(+), 2 deletions(-) diff --git a/tools/detect_targets.py b/tools/detect_targets.py index bdccab1f3f..ad90f5b078 100644 --- a/tools/detect_targets.py +++ b/tools/detect_targets.py @@ -102,8 +102,12 @@ def main(): def get_interface_version(mount_point): """ Function returns interface version from the target mounted on the specified mount point - Example of mount_point: - mount_point = mut['disk'] + + mount_point can be acquired via the following: + muts = get_autodetected_MUTS_list() + for mut in muts.values(): + mount_point = mut['disk'] + @param mount_point Name of disk where platform is connected to host machine. """ if get_module_avail('mbed_lstools'): From 7b4959125f29dd26c69ffcb7b3f519db6b07919c Mon Sep 17 00:00:00 2001 From: David Walters Date: Fri, 15 Sep 2017 16:00:20 +0100 Subject: [PATCH 06/11] Added in safety check if details_txt is None --- tools/detect_targets.py | 12 ++++++++---- 1 file changed, 8 insertions(+), 4 deletions(-) diff --git a/tools/detect_targets.py b/tools/detect_targets.py index ad90f5b078..ec1d90cd59 100644 --- a/tools/detect_targets.py +++ b/tools/detect_targets.py @@ -103,10 +103,10 @@ def main(): def get_interface_version(mount_point): """ Function returns interface version from the target mounted on the specified mount point - mount_point can be acquired via the following: - muts = get_autodetected_MUTS_list() - for mut in muts.values(): - mount_point = mut['disk'] + mount_point can be acquired via the following: + muts = get_autodetected_MUTS_list() + for mut in muts.values(): + mount_point = mut['disk'] @param mount_point Name of disk where platform is connected to host machine. """ @@ -114,6 +114,10 @@ def get_interface_version(mount_point): mbeds = mbed_lstools.create() details_txt = mbeds.get_details_txt(mount_point) + if (details_txt is None): + details_txt = {} + + if 'Interface Version' in details_txt: return details_txt['Interface Version'] From b887bb7de64a207fadfb24bca4ac2a3c9f2bf1bc Mon Sep 17 00:00:00 2001 From: David Walters Date: Mon, 25 Sep 2017 16:53:41 +0100 Subject: [PATCH 07/11] Added safety check, added test converage for new function --- tools/detect_targets.py | 22 ++++---- tools/test/detect_targets_test.py | 85 +++++++++++++++++++++++++++++++ 2 files changed, 96 insertions(+), 11 deletions(-) create mode 100644 tools/test/detect_targets_test.py diff --git a/tools/detect_targets.py b/tools/detect_targets.py index ec1d90cd59..81a7b80e20 100644 --- a/tools/detect_targets.py +++ b/tools/detect_targets.py @@ -111,18 +111,18 @@ def get_interface_version(mount_point): @param mount_point Name of disk where platform is connected to host machine. """ if get_module_avail('mbed_lstools'): - mbeds = mbed_lstools.create() - details_txt = mbeds.get_details_txt(mount_point) - - if (details_txt is None): - details_txt = {} + try : + mbeds = mbed_lstools.create() + details_txt = mbeds.get_details_txt(mount_point) - - if 'Interface Version' in details_txt: - return details_txt['Interface Version'] - - elif 'Version' in details_txt: - return details_txt['Version'] + if 'Interface Version' in details_txt: + return details_txt['Interface Version'] + + elif 'Version' in details_txt: + return details_txt['Version'] + + except : + return 'unknown' return 'unknown' diff --git a/tools/test/detect_targets_test.py b/tools/test/detect_targets_test.py new file mode 100644 index 0000000000..c7a996ccbf --- /dev/null +++ b/tools/test/detect_targets_test.py @@ -0,0 +1,85 @@ +""" +mbed SDK +Copyright (c) 2016 ARM Limited + +Licensed under the Apache License, Version 2.0 (the "License"); +you may not use this file except in compliance with the License. +You may obtain a copy of the License at + +http://www.apache.org/licenses/LICENSE-2.0 + +Unless required by applicable law or agreed to in writing, software +distributed under the License is distributed on an "AS IS" BASIS, +WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +See the License for the specific language governing permissions and +limitations under the License. +""" + +import unittest +from collections import namedtuple +from mock import patch, MagicMock +from tools.detect_targets import get_interface_version +from tools.test_api import get_autodetected_MUTS_list + +""" +Tests for detect_targets.py +""" + +class DetectTargetsTests(unittest.TestCase): + """ + Test cases for Detect Target functionality + """ + + def setUp(self): + """ + Called before each test case + + :return: + """ + # Gather a valid mount point from the host machine + muts = get_autodetected_MUTS_list() + + count = 0 + + for mut in muts.values(): + + count += 1 + self.valid_mount_point = mut['disk'] + break + + # If no targets are found, there is no way to determine Host OS mount point. + # Function should handle failure gracefully regardless of a mount point being present. + # Therefore it is safe to assume a valid mount point. + if count is 0: + self.valid_mount_point = "D:" + + self.invalid_mount_point = "23z/e\n" + self.missing_mount_point = None + + def tearDown(self): + """ + Nothing to tear down. + Called after each test case + + :return: + """ + pass + + def test_interface_version_valid_mount_point(self): + + interface_version = get_interface_version(self.valid_mount_point) + assert len(interface_version) > 0 + + def test_interface_version_invalid_mount_point(self): + + interface_version = get_interface_version(self.invalid_mount_point) + assert interface_version == 'unknown' + + def test_interface_version_missing_mount_point(self): + + interface_version = get_interface_version(self.missing_mount_point) + assert interface_version == 'unknown' + + +if __name__ == '__main__': + unittest.main() From 4c2d17eee0abea27a48a50048a08507fd74d3802 Mon Sep 17 00:00:00 2001 From: David Walters Date: Mon, 25 Sep 2017 16:58:52 +0100 Subject: [PATCH 08/11] Removing unused imports/update copywright` --- tools/test/detect_targets_test.py | 4 +--- 1 file changed, 1 insertion(+), 3 deletions(-) diff --git a/tools/test/detect_targets_test.py b/tools/test/detect_targets_test.py index c7a996ccbf..33e790f020 100644 --- a/tools/test/detect_targets_test.py +++ b/tools/test/detect_targets_test.py @@ -1,6 +1,6 @@ """ mbed SDK -Copyright (c) 2016 ARM Limited +Copyright (c) 2017 ARM Limited Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except in compliance with the License. @@ -16,8 +16,6 @@ limitations under the License. """ import unittest -from collections import namedtuple -from mock import patch, MagicMock from tools.detect_targets import get_interface_version from tools.test_api import get_autodetected_MUTS_list From a047f6276d64a531ee31191d302f503890b8408a Mon Sep 17 00:00:00 2001 From: David Walters Date: Fri, 29 Sep 2017 09:56:49 +0100 Subject: [PATCH 09/11] Increased test coverage / mocking of external modules --- tools/detect_targets.py | 2 +- tools/test/detect_targets_test.py | 131 +++++++++++++++++++++++------- 2 files changed, 103 insertions(+), 30 deletions(-) diff --git a/tools/detect_targets.py b/tools/detect_targets.py index 81a7b80e20..b9aa80df1c 100644 --- a/tools/detect_targets.py +++ b/tools/detect_targets.py @@ -113,7 +113,7 @@ def get_interface_version(mount_point): if get_module_avail('mbed_lstools'): try : mbeds = mbed_lstools.create() - details_txt = mbeds.get_details_txt(mount_point) + details_txt = mbeds.get_details_txt(mount_point) if 'Interface Version' in details_txt: return details_txt['Interface Version'] diff --git a/tools/test/detect_targets_test.py b/tools/test/detect_targets_test.py index 33e790f020..f1c7994529 100644 --- a/tools/test/detect_targets_test.py +++ b/tools/test/detect_targets_test.py @@ -16,14 +16,59 @@ limitations under the License. """ import unittest +from mock import patch from tools.detect_targets import get_interface_version -from tools.test_api import get_autodetected_MUTS_list + + +class MbedLsToolsMock(): + + def __init__(self, type): + self.interface_test_type = type + + def get_details_txt(self, mount_point): + return self.details_txt_types[self.interface_test_type]; + + # Static details.txt types. + details_txt_types = { + 'details_valid_interface_version' : { + 'Unique ID': '0226000029164e45002f0012706e0006f301000097969900', + 'HIF ID': '97969900', + 'Auto Reset': '0', + 'Automation allowed': '0', + 'Daplink Mode': 'Interface', + 'Interface Version': '0240', + 'Git SHA': 'c765cbb590f57598756683254ca38b211693ae5e', + 'Local Mods': '0', + 'USB Interfaces': 'MSD, CDC, HID', + 'Interface CRC': '0x26764ebf' + }, + 'details_valid_version' : { + 'Version': '0226', + 'Build': 'Aug 24 2015 17:06:30', + 'Git Commit SHA': '27a236b9fe39c674a703c5c89655fbd26b8e27e1', + 'Git Local mods': 'Yes' + }, + 'details_missing_interface_version' : { + 'Unique ID': '0226000033514e450044500585d4001de981000097969900', + 'HIC ID': '97969900', + 'Auto Reset': '0', + 'Automation allowed': '0', + 'Overflow detection': '0', + 'Daplink Mode': 'Interface', + 'Git SHA': 'b403a07e3696cee1e116d44cbdd64446e056ce38', + 'Local Mods': '0', + 'USB Interfaces': 'MSD, CDC, HID', + 'Interface CRC': '0x4d98bf7e', + 'Remount count': '0' + }, + 'details_invalid_none' : None + } """ Tests for detect_targets.py """ -class DetectTargetsTests(unittest.TestCase): +class DetectTargetsTest(unittest.TestCase): """ Test cases for Detect Target functionality """ @@ -33,26 +78,9 @@ class DetectTargetsTests(unittest.TestCase): Called before each test case :return: - """ - # Gather a valid mount point from the host machine - muts = get_autodetected_MUTS_list() - - count = 0 - - for mut in muts.values(): - - count += 1 - self.valid_mount_point = mut['disk'] - break - - # If no targets are found, there is no way to determine Host OS mount point. - # Function should handle failure gracefully regardless of a mount point being present. - # Therefore it is safe to assume a valid mount point. - if count is 0: - self.valid_mount_point = "D:" - - self.invalid_mount_point = "23z/e\n" + """ self.missing_mount_point = None + self.mount_point = "D:" def tearDown(self): """ @@ -62,22 +90,67 @@ class DetectTargetsTests(unittest.TestCase): :return: """ pass - - def test_interface_version_valid_mount_point(self): + + @patch("mbed_lstools.create", return_value=MbedLsToolsMock('details_valid_interface_version')) + def test_interface_version_valid(self, mbed_lstools_mock): + """ + Test that checks function returns correctly when given a valid Interface Version - interface_version = get_interface_version(self.valid_mount_point) - assert len(interface_version) > 0 + :param mbed_lstools_mock: Mocks Mbed LS tools with MbedLsToolsMock + :return + """ - def test_interface_version_invalid_mount_point(self): + interface_version = get_interface_version(self.mount_point) + assert interface_version == '0240' - interface_version = get_interface_version(self.invalid_mount_point) + @patch("mbed_lstools.create", return_value=MbedLsToolsMock('details_valid_version')) + def test_version_valid(self, mbed_lstools_mock): + """ + Test that checks function returns correctly when given a valid Version + + :param mbed_lstools_mock: Mocks Mbed LS tools with MbedLsToolsMock + :return + """ + + interface_version = get_interface_version(self.mount_point) + assert interface_version == '0226' + + @patch("mbed_lstools.create", return_value=MbedLsToolsMock('details_missing_interface_version')) + def test_interface_version_missing_interface_version(self, mbed_lstools_mock): + """ + Test that checks function returns correctly when DETAILS.txt is present + but an interface version is not listed. + + :param mbed_lstools_mock: Mocks Mbed LS tools with MbedLsToolsMock + :return + """ + + interface_version = get_interface_version(self.mount_point) assert interface_version == 'unknown' - def test_interface_version_missing_mount_point(self): + @patch("mbed_lstools.create", return_value=MbedLsToolsMock('details_invalid_none')) + def test_version_none(self, mbed_lstools_mock): + """ + Test that checks function returns correctly when a valid mount point is supplied + but DETAILS.txt is not present. + :param mbed_lstools_mock: Mocks Mbed LS tools with MbedLsToolsMock + :return + """ + + interface_version = get_interface_version(self.mount_point) + assert interface_version == 'unknown' + + @patch("mbed_lstools.create", return_value=MbedLsToolsMock('details_invalid_none')) + def test_interface_version_missing_mount_point(self, mbed_lstools_mock): + """ + Test that checks function returns correctly when no moint point is supplied. + + :param mbed_lstools_mock: Mocks Mbed LS tools with MbedLsToolsMock + :return + """ interface_version = get_interface_version(self.missing_mount_point) assert interface_version == 'unknown' - if __name__ == '__main__': unittest.main() From bb79f6991b110ed8fc1a09ed8f8d73c19f20f308 Mon Sep 17 00:00:00 2001 From: David Walters Date: Fri, 29 Sep 2017 10:12:02 +0100 Subject: [PATCH 10/11] Minor copy edits, added comment --- tools/test/detect_targets_test.py | 6 +++++- 1 file changed, 5 insertions(+), 1 deletion(-) diff --git a/tools/test/detect_targets_test.py b/tools/test/detect_targets_test.py index f1c7994529..6bf107381b 100644 --- a/tools/test/detect_targets_test.py +++ b/tools/test/detect_targets_test.py @@ -21,6 +21,9 @@ from tools.detect_targets import get_interface_version class MbedLsToolsMock(): + """ + Mock of mbedls tools + """ def __init__(self, type): self.interface_test_type = type @@ -144,11 +147,12 @@ class DetectTargetsTest(unittest.TestCase): @patch("mbed_lstools.create", return_value=MbedLsToolsMock('details_invalid_none')) def test_interface_version_missing_mount_point(self, mbed_lstools_mock): """ - Test that checks function returns correctly when no moint point is supplied. + Test that checks function returns correctly when no mount point is supplied. :param mbed_lstools_mock: Mocks Mbed LS tools with MbedLsToolsMock :return """ + interface_version = get_interface_version(self.missing_mount_point) assert interface_version == 'unknown' From 61f46b809dfc7abfc510180ce3abcaf99a1efd3f Mon Sep 17 00:00:00 2001 From: David Walters Date: Fri, 29 Sep 2017 15:34:32 +0100 Subject: [PATCH 11/11] Changed type => test_type --- tools/test/detect_targets_test.py | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/tools/test/detect_targets_test.py b/tools/test/detect_targets_test.py index 6bf107381b..be21a32956 100644 --- a/tools/test/detect_targets_test.py +++ b/tools/test/detect_targets_test.py @@ -25,8 +25,8 @@ class MbedLsToolsMock(): Mock of mbedls tools """ - def __init__(self, type): - self.interface_test_type = type + def __init__(self, test_type): + self.interface_test_type = test_type def get_details_txt(self, mount_point): return self.details_txt_types[self.interface_test_type];