takashi kadono / Mbed OS Nucleo_446

Dependencies:   ssd1331

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 from tools.resources import 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 = Resources(MockNotifier()).scan_with_toolchain(
00071             self.src_paths , toolchain)
00072 
00073         toolchain.RESPONSE_FILES=False
00074         toolchain.config_processed = True
00075         toolchain.config_file = "junk"
00076         toolchain.compile_sources(res)
00077 
00078         assert any('percent' in msg and msg['percent'] == 100.0
00079                    for msg in notify.messages if msg)
00080 
00081 
00082     @patch('tools.build_api.Config')
00083     def test_prepare_toolchain_app_config (self, mock_config_init):
00084         """
00085         Test that prepare_toolchain uses app_config correctly
00086 
00087         :param mock_config_init: mock of Config __init__
00088         :return:
00089         """
00090         app_config = "app_config"
00091         mock_target = make_mock_target(lambda _, __ : None,
00092                                        "Junk", [], "Cortex-M3", TOOLCHAINS)
00093         mock_config_init.return_value = namedtuple(
00094             "Config", "target has_regions name")(mock_target, False, None)
00095 
00096         prepare_toolchain(self.src_paths , None, self.target , self.toolchain_name ,
00097                           app_config=app_config)
00098 
00099         mock_config_init.assert_called_once_with(self.target , self.src_paths ,
00100                                                  app_config=app_config)
00101 
00102     @patch('tools.build_api.Config')
00103     def test_prepare_toolchain_no_app_config (self, mock_config_init):
00104         """
00105         Test that prepare_toolchain correctly deals with no app_config
00106 
00107         :param mock_config_init: mock of Config __init__
00108         :return:
00109         """
00110         mock_target = make_mock_target(lambda _, __ : None,
00111                                        "Junk", [], "Cortex-M3", TOOLCHAINS)
00112         mock_config_init.return_value = namedtuple(
00113             "Config", "target has_regions name")(mock_target, False, None)
00114 
00115         prepare_toolchain(self.src_paths , None, self.target , self.toolchain_name )
00116 
00117         mock_config_init.assert_called_once_with(self.target , self.src_paths ,
00118                                                  app_config=None)
00119 
00120     @patch('tools.build_api.Resources')
00121     @patch('tools.build_api.mkdir')
00122     @patch('os.path.exists')
00123     @patch('tools.build_api.prepare_toolchain')
00124     def test_build_project_app_config (self, mock_prepare_toolchain, mock_exists, _, __):
00125         """
00126         Test that build_project uses app_config correctly
00127 
00128         :param mock_prepare_toolchain: mock of function prepare_toolchain
00129         :param mock_exists: mock of function os.path.exists
00130         :param _: mock of function mkdir (not tested)
00131         :param __: mock of class Resources (not tested)
00132         :return:
00133         """
00134         notify = MockNotifier()
00135         app_config = "app_config"
00136         mock_exists.return_value = False
00137         mock_prepare_toolchain().link_program.return_value = 1, 2
00138         mock_prepare_toolchain().config = namedtuple(
00139             "Config", "has_regions name lib_config_data")(None, None, {})
00140 
00141         build_project(self.src_paths , self.build_path , self.target ,
00142                       self.toolchain_name , app_config=app_config, notify=notify)
00143 
00144         args = mock_prepare_toolchain.call_args
00145         self.assertTrue('app_config' in args[1],
00146                         "prepare_toolchain was not called with app_config")
00147         self.assertEqual(args[1]['app_config'], app_config,
00148                          "prepare_toolchain was called with an incorrect app_config")
00149 
00150     @patch('tools.build_api.Resources')
00151     @patch('tools.build_api.mkdir')
00152     @patch('os.path.exists')
00153     @patch('tools.build_api.prepare_toolchain')
00154     def test_build_project_no_app_config (self, mock_prepare_toolchain, mock_exists, _, __):
00155         """
00156         Test that build_project correctly deals with no app_config
00157 
00158         :param mock_prepare_toolchain: mock of function prepare_toolchain
00159         :param mock_exists: mock of function os.path.exists
00160         :param _: mock of function mkdir (not tested)
00161         :param __: mock of class Resources (not tested)
00162         :return:
00163         """
00164         notify = MockNotifier()
00165         mock_exists.return_value = False
00166         # Needed for the unpacking of the returned value
00167         mock_prepare_toolchain().link_program.return_value = 1, 2
00168         mock_prepare_toolchain().config = namedtuple(
00169             "Config", "has_regions name lib_config_data")(None, None, {})
00170 
00171         build_project(self.src_paths , self.build_path , self.target ,
00172                       self.toolchain_name , notify=notify)
00173 
00174         args = mock_prepare_toolchain.call_args
00175         self.assertTrue('app_config' in args[1],
00176                         "prepare_toolchain was not called with app_config")
00177         self.assertEqual(args[1]['app_config'], None,
00178                          "prepare_toolchain was called with an incorrect app_config")
00179 
00180     @patch('tools.build_api.Resources')
00181     @patch('tools.build_api.mkdir')
00182     @patch('os.path.exists')
00183     @patch('tools.build_api.prepare_toolchain')
00184     def test_build_library_app_config (self, mock_prepare_toolchain, mock_exists, _, __):
00185         """
00186         Test that build_library uses app_config correctly
00187 
00188         :param mock_prepare_toolchain: mock of function prepare_toolchain
00189         :param mock_exists: mock of function os.path.exists
00190         :param _: mock of function mkdir (not tested)
00191         :param __: mock of class Resources (not tested)
00192         :return:
00193         """
00194         notify = MockNotifier()
00195         app_config = "app_config"
00196         mock_exists.return_value = False
00197 
00198         build_library(self.src_paths , self.build_path , self.target ,
00199                       self.toolchain_name , app_config=app_config, notify=notify)
00200 
00201         args = mock_prepare_toolchain.call_args
00202         self.assertTrue('app_config' in args[1],
00203                         "prepare_toolchain was not called with app_config")
00204         self.assertEqual(args[1]['app_config'], app_config,
00205                          "prepare_toolchain was called with an incorrect app_config")
00206 
00207     @patch('tools.build_api.Resources')
00208     @patch('tools.build_api.mkdir')
00209     @patch('os.path.exists')
00210     @patch('tools.build_api.prepare_toolchain')
00211     def test_build_library_no_app_config (self, mock_prepare_toolchain, mock_exists, _, __):
00212         """
00213         Test that build_library correctly deals with no app_config
00214 
00215         :param mock_prepare_toolchain: mock of function prepare_toolchain
00216         :param mock_exists: mock of function os.path.exists
00217         :param _: mock of function mkdir (not tested)
00218         :param __: mock of class Resources (not tested)
00219         :return:
00220         """
00221         notify = MockNotifier()
00222         mock_exists.return_value = False
00223 
00224         build_library(self.src_paths , self.build_path , self.target ,
00225                       self.toolchain_name , notify=notify)
00226 
00227         args = mock_prepare_toolchain.call_args
00228         self.assertTrue('app_config' in args[1],
00229                         "prepare_toolchain was not called with app_config")
00230         self.assertEqual(args[1]['app_config'], None,
00231                          "prepare_toolchain was called with an incorrect app_config")
00232 
00233 if __name__ == '__main__':
00234     unittest.main()