ON Semiconductor / mbed-os

Dependents:   mbed-TFT-example-NCS36510 mbed-Accelerometer-example-NCS36510 mbed-Accelerometer-example-NCS36510

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 
00024 """
00025 Tests for build_api.py
00026 """
00027 
00028 class BuildApiTests (unittest.TestCase):
00029     """
00030     Test cases for Build Api
00031     """
00032 
00033     def setUp (self):
00034         """
00035         Called before each test case
00036 
00037         :return:
00038         """
00039         self.target  = "K64F"
00040         self.src_paths  = ['.']
00041         self.toolchain_name  = "ARM"
00042         self.build_path  = "build_path"
00043 
00044     def tearDown (self):
00045         """
00046         Called after each test case
00047 
00048         :return:
00049         """
00050         pass
00051 
00052     @patch('tools.toolchains.arm.ARM_STD.parse_dependencies',
00053            return_value=["foo"])
00054     @patch('tools.toolchains.mbedToolchain.need_update',
00055            side_effect=[i % 2 for i in range(3000)])
00056     @patch('os.mkdir')
00057     @patch('tools.toolchains.exists', return_value=True)
00058     @patch('tools.utils.run_cmd', return_value=("", "", 0))
00059     def test_always_complete_build(self, *_):
00060         with MagicMock() as notify:
00061             toolchain = prepare_toolchain(self.src_paths , self.target ,
00062                                           self.toolchain_name , notify=notify)
00063 
00064             res = scan_resources(self.src_paths , toolchain)
00065 
00066             toolchain.RESPONSE_FILES=False
00067             toolchain.config_processed = True
00068             toolchain.config_file = "junk"
00069             toolchain.compile_sources(res, self.build_path )
00070 
00071             assert any('percent' in msg[0] and msg[0]['percent'] == 100.0
00072                        for _, msg, _ in notify.mock_calls if msg)
00073 
00074 
00075     @patch('tools.build_api.Config')
00076     def test_prepare_toolchain_app_config (self, mock_config_init):
00077         """
00078         Test that prepare_toolchain uses app_config correctly
00079 
00080         :param mock_config_init: mock of Config __init__
00081         :return:
00082         """
00083         app_config = "app_config"
00084         mock_config_init.return_value = namedtuple("Config", "target")(
00085             namedtuple("Target",
00086                        "init_hooks name features core")(lambda _, __ : None,
00087                                                         "Junk", [], "Cortex-M3"))
00088 
00089         prepare_toolchain(self.src_paths , self.target , self.toolchain_name ,
00090                           app_config=app_config)
00091 
00092         mock_config_init.assert_called_once_with(self.target , self.src_paths ,
00093                                                  app_config=app_config)
00094 
00095     @patch('tools.build_api.Config')
00096     def test_prepare_toolchain_no_app_config (self, mock_config_init):
00097         """
00098         Test that prepare_toolchain correctly deals with no app_config
00099 
00100         :param mock_config_init: mock of Config __init__
00101         :return:
00102         """
00103         mock_config_init.return_value = namedtuple("Config", "target")(
00104             namedtuple("Target",
00105                        "init_hooks name features core")(lambda _, __ : None,
00106                                                         "Junk", [], "Cortex-M3"))
00107 
00108         prepare_toolchain(self.src_paths , self.target , self.toolchain_name )
00109 
00110         mock_config_init.assert_called_once_with(self.target , self.src_paths ,
00111                                                  app_config=None)
00112 
00113     @patch('tools.build_api.scan_resources')
00114     @patch('tools.build_api.mkdir')
00115     @patch('os.path.exists')
00116     @patch('tools.build_api.prepare_toolchain')
00117     def test_build_project_app_config (self, mock_prepare_toolchain, mock_exists, _, __):
00118         """
00119         Test that build_project uses app_config correctly
00120 
00121         :param mock_prepare_toolchain: mock of function prepare_toolchain
00122         :param mock_exists: mock of function os.path.exists
00123         :param _: mock of function mkdir (not tested)
00124         :param __: mock of function scan_resources (not tested)
00125         :return:
00126         """
00127         app_config = "app_config"
00128         mock_exists.return_value = False
00129         mock_prepare_toolchain().link_program.return_value = 1, 2
00130 
00131         build_project(self.src_paths , self.build_path , self.target ,
00132                       self.toolchain_name , app_config=app_config)
00133 
00134         args = mock_prepare_toolchain.call_args
00135         self.assertTrue('app_config' in args[1],
00136                         "prepare_toolchain was not called with app_config")
00137         self.assertEqual(args[1]['app_config'], app_config,
00138                          "prepare_toolchain was called with an incorrect app_config")
00139 
00140     @patch('tools.build_api.scan_resources')
00141     @patch('tools.build_api.mkdir')
00142     @patch('os.path.exists')
00143     @patch('tools.build_api.prepare_toolchain')
00144     def test_build_project_no_app_config (self, mock_prepare_toolchain, mock_exists, _, __):
00145         """
00146         Test that build_project correctly deals with no app_config
00147 
00148         :param mock_prepare_toolchain: mock of function prepare_toolchain
00149         :param mock_exists: mock of function os.path.exists
00150         :param _: mock of function mkdir (not tested)
00151         :param __: mock of function scan_resources (not tested)
00152         :return:
00153         """
00154         mock_exists.return_value = False
00155         # Needed for the unpacking of the returned value
00156         mock_prepare_toolchain().link_program.return_value = 1, 2
00157 
00158         build_project(self.src_paths , self.build_path , self.target ,
00159                       self.toolchain_name )
00160 
00161         args = mock_prepare_toolchain.call_args
00162         self.assertTrue('app_config' in args[1],
00163                         "prepare_toolchain was not called with app_config")
00164         self.assertEqual(args[1]['app_config'], None,
00165                          "prepare_toolchain was called with an incorrect app_config")
00166 
00167     @patch('tools.build_api.scan_resources')
00168     @patch('tools.build_api.mkdir')
00169     @patch('os.path.exists')
00170     @patch('tools.build_api.prepare_toolchain')
00171     def test_build_library_app_config (self, mock_prepare_toolchain, mock_exists, _, __):
00172         """
00173         Test that build_library uses app_config correctly
00174 
00175         :param mock_prepare_toolchain: mock of function prepare_toolchain
00176         :param mock_exists: mock of function os.path.exists
00177         :param _: mock of function mkdir (not tested)
00178         :param __: mock of function scan_resources (not tested)
00179         :return:
00180         """
00181         app_config = "app_config"
00182         mock_exists.return_value = False
00183 
00184         build_library(self.src_paths , self.build_path , self.target ,
00185                       self.toolchain_name , app_config=app_config)
00186 
00187         args = mock_prepare_toolchain.call_args
00188         self.assertTrue('app_config' in args[1],
00189                         "prepare_toolchain was not called with app_config")
00190         self.assertEqual(args[1]['app_config'], app_config,
00191                          "prepare_toolchain was called with an incorrect app_config")
00192 
00193     @patch('tools.build_api.scan_resources')
00194     @patch('tools.build_api.mkdir')
00195     @patch('os.path.exists')
00196     @patch('tools.build_api.prepare_toolchain')
00197     def test_build_library_no_app_config (self, mock_prepare_toolchain, mock_exists, _, __):
00198         """
00199         Test that build_library correctly deals with no app_config
00200 
00201         :param mock_prepare_toolchain: mock of function prepare_toolchain
00202         :param mock_exists: mock of function os.path.exists
00203         :param _: mock of function mkdir (not tested)
00204         :param __: mock of function scan_resources (not tested)
00205         :return:
00206         """
00207         mock_exists.return_value = False
00208 
00209         build_library(self.src_paths , self.build_path , self.target ,
00210                       self.toolchain_name )
00211 
00212         args = mock_prepare_toolchain.call_args
00213         self.assertTrue('app_config' in args[1],
00214                         "prepare_toolchain was not called with app_config")
00215         self.assertEqual(args[1]['app_config'], None,
00216                          "prepare_toolchain was called with an incorrect app_config")
00217 
00218 if __name__ == '__main__':
00219     unittest.main()