Maxim nexpaq / nexpaq_dev
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 mock import patch
00020 from tools.build_api import prepare_toolchain, build_project, build_library
00021 
00022 """
00023 Tests for build_api.py
00024 """
00025 
00026 class BuildApiTests (unittest.TestCase):
00027     """
00028     Test cases for Build Api
00029     """
00030 
00031     def setUp (self):
00032         """
00033         Called before each test case
00034 
00035         :return:
00036         """
00037         self.target  = "K64F"
00038         self.src_paths  = ['.']
00039         self.toolchain_name  = "ARM"
00040         self.build_path  = "build_path"
00041 
00042     def tearDown (self):
00043         """
00044         Called after each test case
00045 
00046         :return:
00047         """
00048         pass
00049 
00050     @patch('tools.config.Config.__init__')
00051     def test_prepare_toolchain_app_config (self, mock_config_init):
00052         """
00053         Test that prepare_toolchain uses app_config correctly
00054 
00055         :param mock_config_init: mock of Config __init__
00056         :return:
00057         """
00058         app_config = "app_config"
00059         mock_config_init.return_value = None
00060 
00061         prepare_toolchain(self.src_paths , self.target , self.toolchain_name ,
00062                           app_config=app_config)
00063 
00064         mock_config_init.assert_called_with(self.target , self.src_paths ,
00065                                             app_config=app_config)
00066 
00067     @patch('tools.config.Config.__init__')
00068     def test_prepare_toolchain_no_app_config (self, mock_config_init):
00069         """
00070         Test that prepare_toolchain correctly deals with no app_config
00071 
00072         :param mock_config_init: mock of Config __init__
00073         :return:
00074         """
00075         mock_config_init.return_value = None
00076 
00077         prepare_toolchain(self.src_paths , self.target , self.toolchain_name )
00078 
00079         mock_config_init.assert_called_with(self.target , self.src_paths ,
00080                                             app_config=None)
00081 
00082     @patch('tools.build_api.scan_resources')
00083     @patch('tools.build_api.mkdir')
00084     @patch('os.path.exists')
00085     @patch('tools.build_api.prepare_toolchain')
00086     def test_build_project_app_config (self, mock_prepare_toolchain, mock_exists, _, __):
00087         """
00088         Test that build_project uses app_config correctly
00089 
00090         :param mock_prepare_toolchain: mock of function prepare_toolchain
00091         :param mock_exists: mock of function os.path.exists
00092         :param _: mock of function mkdir (not tested)
00093         :param __: mock of function scan_resources (not tested)
00094         :return:
00095         """
00096         app_config = "app_config"
00097         mock_exists.return_value = False
00098         mock_prepare_toolchain().link_program.return_value = 1, 2
00099 
00100         build_project(self.src_paths , self.build_path , self.target ,
00101                       self.toolchain_name , app_config=app_config)
00102 
00103         args = mock_prepare_toolchain.call_args
00104         self.assertTrue('app_config' in args[1],
00105                         "prepare_toolchain was not called with app_config")
00106         self.assertEqual(args[1]['app_config'], app_config,
00107                          "prepare_toolchain was called with an incorrect app_config")
00108 
00109     @patch('tools.build_api.scan_resources')
00110     @patch('tools.build_api.mkdir')
00111     @patch('os.path.exists')
00112     @patch('tools.build_api.prepare_toolchain')
00113     def test_build_project_no_app_config (self, mock_prepare_toolchain, mock_exists, _, __):
00114         """
00115         Test that build_project correctly deals with no app_config
00116 
00117         :param mock_prepare_toolchain: mock of function prepare_toolchain
00118         :param mock_exists: mock of function os.path.exists
00119         :param _: mock of function mkdir (not tested)
00120         :param __: mock of function scan_resources (not tested)
00121         :return:
00122         """
00123         mock_exists.return_value = False
00124         # Needed for the unpacking of the returned value
00125         mock_prepare_toolchain().link_program.return_value = 1, 2
00126 
00127         build_project(self.src_paths , self.build_path , self.target ,
00128                       self.toolchain_name )
00129 
00130         args = mock_prepare_toolchain.call_args
00131         self.assertTrue('app_config' in args[1],
00132                         "prepare_toolchain was not called with app_config")
00133         self.assertEqual(args[1]['app_config'], None,
00134                          "prepare_toolchain was called with an incorrect app_config")
00135 
00136     @patch('tools.build_api.scan_resources')
00137     @patch('tools.build_api.mkdir')
00138     @patch('os.path.exists')
00139     @patch('tools.build_api.prepare_toolchain')
00140     def test_build_library_app_config (self, mock_prepare_toolchain, mock_exists, _, __):
00141         """
00142         Test that build_library uses app_config correctly
00143 
00144         :param mock_prepare_toolchain: mock of function prepare_toolchain
00145         :param mock_exists: mock of function os.path.exists
00146         :param _: mock of function mkdir (not tested)
00147         :param __: mock of function scan_resources (not tested)
00148         :return:
00149         """
00150         app_config = "app_config"
00151         mock_exists.return_value = False
00152 
00153         build_library(self.src_paths , self.build_path , self.target ,
00154                       self.toolchain_name , app_config=app_config)
00155 
00156         args = mock_prepare_toolchain.call_args
00157         self.assertTrue('app_config' in args[1],
00158                         "prepare_toolchain was not called with app_config")
00159         self.assertEqual(args[1]['app_config'], app_config,
00160                          "prepare_toolchain was called with an incorrect app_config")
00161 
00162     @patch('tools.build_api.scan_resources')
00163     @patch('tools.build_api.mkdir')
00164     @patch('os.path.exists')
00165     @patch('tools.build_api.prepare_toolchain')
00166     def test_build_library_no_app_config (self, mock_prepare_toolchain, mock_exists, _, __):
00167         """
00168         Test that build_library correctly deals with no app_config
00169 
00170         :param mock_prepare_toolchain: mock of function prepare_toolchain
00171         :param mock_exists: mock of function os.path.exists
00172         :param _: mock of function mkdir (not tested)
00173         :param __: mock of function scan_resources (not tested)
00174         :return:
00175         """
00176         mock_exists.return_value = False
00177 
00178         build_library(self.src_paths , self.build_path , self.target ,
00179                       self.toolchain_name )
00180 
00181         args = mock_prepare_toolchain.call_args
00182         self.assertTrue('app_config' in args[1],
00183                         "prepare_toolchain was not called with app_config")
00184         self.assertEqual(args[1]['app_config'], None,
00185                          "prepare_toolchain was called with an incorrect app_config")
00186 
00187 if __name__ == '__main__':
00188     unittest.main()