Knight KE / Mbed OS Game_Master
Embed: (wiki syntax)

« Back to documentation index

Show/hide line numbers build_api_test.py Source File

build_api_test.py

00001 """
00002 mbed SDK
00003 Copyright (c) 2016 ARM Limited
00004 
00005 Licensed under the Apache License, Version 2.0 (the "License");
00006 you may not use this file except in compliance with the License.
00007 You may obtain a copy of the License at
00008 
00009 http://www.apache.org/licenses/LICENSE-2.0
00010 
00011 Unless required by applicable law or agreed to in writing, software
00012 distributed under the License is distributed on an "AS IS" BASIS,
00013 WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
00014 See the License for the specific language governing permissions and
00015 limitations under the License.
00016 """
00017 
00018 import unittest
00019 from collections import namedtuple
00020 from mock import patch, MagicMock
00021 from tools.build_api import prepare_toolchain, build_project, build_library,\
00022     scan_resources
00023 from tools.toolchains import TOOLCHAINS
00024 from tools.notifier.mock import MockNotifier
00025 
00026 """
00027 Tests for build_api.py
00028 """
00029 make_mock_target = namedtuple(
00030     "Target", "init_hooks name features core supported_toolchains")
00031 
00032 
00033 class BuildApiTests (unittest.TestCase):
00034     """
00035     Test cases for Build Api
00036     """
00037 
00038     def setUp (self):
00039         """
00040         Called before each test case
00041 
00042         :return:
00043         """
00044         self.target  = "K64F"
00045         self.src_paths  = ['.']
00046         self.toolchain_name  = "ARM"
00047         self.build_path  = "build_path"
00048 
00049     def tearDown (self):
00050         """
00051         Called after each test case
00052 
00053         :return:
00054         """
00055         pass
00056 
00057     @patch('tools.toolchains.arm.ARM_STD.parse_dependencies',
00058            return_value=["foo"])
00059     @patch('tools.toolchains.mbedToolchain.need_update',
00060            side_effect=[i % 2 for i in range(3000)])
00061     @patch('os.mkdir')
00062     @patch('tools.toolchains.exists', return_value=True)
00063     @patch('tools.toolchains.mbedToolchain.dump_build_profile')
00064     @patch('tools.utils.run_cmd', return_value=(b'', b'', 0))
00065     def test_always_complete_build(self, *_):
00066         notify = MockNotifier()
00067         toolchain = prepare_toolchain(self.src_paths , self.build_path , self.target ,
00068                                         self.toolchain_name , notify=notify)
00069 
00070         res = scan_resources(self.src_paths , toolchain)
00071 
00072         toolchain.RESPONSE_FILES=False
00073         toolchain.config_processed = True
00074         toolchain.config_file = "junk"
00075         toolchain.compile_sources(res)
00076 
00077         assert any('percent' in msg and msg['percent'] == 100.0
00078                    for msg in notify.messages if msg)
00079 
00080 
00081     @patch('tools.build_api.Config')
00082     def test_prepare_toolchain_app_config (self, mock_config_init):
00083         """
00084         Test that prepare_toolchain uses app_config correctly
00085 
00086         :param mock_config_init: mock of Config __init__
00087         :return:
00088         """
00089         app_config = "app_config"
00090         mock_target = make_mock_target(lambda _, __ : None,
00091                                        "Junk", [], "Cortex-M3", TOOLCHAINS)
00092         mock_config_init.return_value = namedtuple(
00093             "Config", "target has_regions name")(mock_target, False, None)
00094 
00095         prepare_toolchain(self.src_paths , None, self.target , self.toolchain_name ,
00096                           app_config=app_config)
00097 
00098         mock_config_init.assert_called_once_with(self.target , self.src_paths ,
00099                                                  app_config=app_config)
00100 
00101     @patch('tools.build_api.Config')
00102     def test_prepare_toolchain_no_app_config (self, mock_config_init):
00103         """
00104         Test that prepare_toolchain correctly deals with no app_config
00105 
00106         :param mock_config_init: mock of Config __init__
00107         :return:
00108         """
00109         mock_target = make_mock_target(lambda _, __ : None,
00110                                        "Junk", [], "Cortex-M3", TOOLCHAINS)
00111         mock_config_init.return_value = namedtuple(
00112             "Config", "target has_regions name")(mock_target, False, None)
00113 
00114         prepare_toolchain(self.src_paths , None, self.target , self.toolchain_name )
00115 
00116         mock_config_init.assert_called_once_with(self.target , self.src_paths ,
00117                                                  app_config=None)
00118 
00119     @patch('tools.build_api.scan_resources')
00120     @patch('tools.build_api.mkdir')
00121     @patch('os.path.exists')
00122     @patch('tools.build_api.prepare_toolchain')
00123     def test_build_project_app_config (self, mock_prepare_toolchain, mock_exists, _, __):
00124         """
00125         Test that build_project uses app_config correctly
00126 
00127         :param mock_prepare_toolchain: mock of function prepare_toolchain
00128         :param mock_exists: mock of function os.path.exists
00129         :param _: mock of function mkdir (not tested)
00130         :param __: mock of function scan_resources (not tested)
00131         :return:
00132         """
00133         notify = MockNotifier()
00134         app_config = "app_config"
00135         mock_exists.return_value = False
00136         mock_prepare_toolchain().link_program.return_value = 1, 2
00137         mock_prepare_toolchain().config = namedtuple(
00138             "Config", "has_regions name lib_config_data")(None, None, {})
00139 
00140         build_project(self.src_paths , self.build_path , self.target ,
00141                       self.toolchain_name , app_config=app_config, notify=notify)
00142 
00143         args = mock_prepare_toolchain.call_args
00144         self.assertTrue('app_config' in args[1],
00145                         "prepare_toolchain was not called with app_config")
00146         self.assertEqual(args[1]['app_config'], app_config,
00147                          "prepare_toolchain was called with an incorrect app_config")
00148 
00149     @patch('tools.build_api.scan_resources')
00150     @patch('tools.build_api.mkdir')
00151     @patch('os.path.exists')
00152     @patch('tools.build_api.prepare_toolchain')
00153     def test_build_project_no_app_config (self, mock_prepare_toolchain, mock_exists, _, __):
00154         """
00155         Test that build_project correctly deals with no app_config
00156 
00157         :param mock_prepare_toolchain: mock of function prepare_toolchain
00158         :param mock_exists: mock of function os.path.exists
00159         :param _: mock of function mkdir (not tested)
00160         :param __: mock of function scan_resources (not tested)
00161         :return:
00162         """
00163         notify = MockNotifier()
00164         mock_exists.return_value = False
00165         # Needed for the unpacking of the returned value
00166         mock_prepare_toolchain().link_program.return_value = 1, 2
00167         mock_prepare_toolchain().config = namedtuple(
00168             "Config", "has_regions name lib_config_data")(None, None, {})
00169 
00170         build_project(self.src_paths , self.build_path , self.target ,
00171                       self.toolchain_name , notify=notify)
00172 
00173         args = mock_prepare_toolchain.call_args
00174         self.assertTrue('app_config' in args[1],
00175                         "prepare_toolchain was not called with app_config")
00176         self.assertEqual(args[1]['app_config'], None,
00177                          "prepare_toolchain was called with an incorrect app_config")
00178 
00179     @patch('tools.build_api.scan_resources')
00180     @patch('tools.build_api.mkdir')
00181     @patch('os.path.exists')
00182     @patch('tools.build_api.prepare_toolchain')
00183     def test_build_library_app_config (self, mock_prepare_toolchain, mock_exists, _, __):
00184         """
00185         Test that build_library uses app_config correctly
00186 
00187         :param mock_prepare_toolchain: mock of function prepare_toolchain
00188         :param mock_exists: mock of function os.path.exists
00189         :param _: mock of function mkdir (not tested)
00190         :param __: mock of function scan_resources (not tested)
00191         :return:
00192         """
00193         notify = MockNotifier()
00194         app_config = "app_config"
00195         mock_exists.return_value = False
00196 
00197         build_library(self.src_paths , self.build_path , self.target ,
00198                       self.toolchain_name , app_config=app_config, notify=notify)
00199 
00200         args = mock_prepare_toolchain.call_args
00201         self.assertTrue('app_config' in args[1],
00202                         "prepare_toolchain was not called with app_config")
00203         self.assertEqual(args[1]['app_config'], app_config,
00204                          "prepare_toolchain was called with an incorrect app_config")
00205 
00206     @patch('tools.build_api.scan_resources')
00207     @patch('tools.build_api.mkdir')
00208     @patch('os.path.exists')
00209     @patch('tools.build_api.prepare_toolchain')
00210     def test_build_library_no_app_config (self, mock_prepare_toolchain, mock_exists, _, __):
00211         """
00212         Test that build_library correctly deals with no app_config
00213 
00214         :param mock_prepare_toolchain: mock of function prepare_toolchain
00215         :param mock_exists: mock of function os.path.exists
00216         :param _: mock of function mkdir (not tested)
00217         :param __: mock of function scan_resources (not tested)
00218         :return:
00219         """
00220         notify = MockNotifier()
00221         mock_exists.return_value = False
00222 
00223         build_library(self.src_paths , self.build_path , self.target ,
00224                       self.toolchain_name , notify=notify)
00225 
00226         args = mock_prepare_toolchain.call_args
00227         self.assertTrue('app_config' in args[1],
00228                         "prepare_toolchain was not called with app_config")
00229         self.assertEqual(args[1]['app_config'], None,
00230                          "prepare_toolchain was called with an incorrect app_config")
00231 
00232 if __name__ == '__main__':
00233     unittest.main()