|
| 1 | +""" |
| 2 | +mbed SDK |
| 3 | +Copyright (c) 2016 ARM Limited |
| 4 | +
|
| 5 | +Licensed under the Apache License, Version 2.0 (the "License"); |
| 6 | +you may not use this file except in compliance with the License. |
| 7 | +You may obtain a copy of the License at |
| 8 | +
|
| 9 | +http://www.apache.org/licenses/LICENSE-2.0 |
| 10 | +
|
| 11 | +Unless required by applicable law or agreed to in writing, software |
| 12 | +distributed under the License is distributed on an "AS IS" BASIS, |
| 13 | +WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 14 | +See the License for the specific language governing permissions and |
| 15 | +limitations under the License. |
| 16 | +""" |
| 17 | + |
| 18 | +import unittest |
| 19 | +from mock import patch |
| 20 | +from tools.build_api import prepare_toolchain, build_project, build_library |
| 21 | + |
| 22 | +""" |
| 23 | +Tests for build_api.py |
| 24 | +""" |
| 25 | + |
| 26 | +class BuildApiTests(unittest.TestCase): |
| 27 | + """ |
| 28 | + Test cases for Build Api |
| 29 | + """ |
| 30 | + |
| 31 | + def setUp(self): |
| 32 | + """ |
| 33 | + Called before each test case |
| 34 | +
|
| 35 | + :return: |
| 36 | + """ |
| 37 | + self.target = "K64F" |
| 38 | + self.src_paths = ['.'] |
| 39 | + self.toolchain_name = "ARM" |
| 40 | + self.build_path = "build_path" |
| 41 | + |
| 42 | + def tearDown(self): |
| 43 | + """ |
| 44 | + Called after each test case |
| 45 | +
|
| 46 | + :return: |
| 47 | + """ |
| 48 | + pass |
| 49 | + |
| 50 | + @patch('tools.config.Config.__init__') |
| 51 | + def test_prepare_toolchain_app_config(self, mock_config_init): |
| 52 | + """ |
| 53 | + Test that prepare_toolchain uses app_config correctly |
| 54 | +
|
| 55 | + :param mock_config_init: mock of Config __init__ |
| 56 | + :return: |
| 57 | + """ |
| 58 | + app_config = "app_config" |
| 59 | + mock_config_init.return_value = None |
| 60 | + |
| 61 | + prepare_toolchain(self.src_paths, self.target, self.toolchain_name, |
| 62 | + app_config=app_config) |
| 63 | + |
| 64 | + mock_config_init.assert_called_with(self.target, self.src_paths, |
| 65 | + app_config=app_config) |
| 66 | + |
| 67 | + @patch('tools.config.Config.__init__') |
| 68 | + def test_prepare_toolchain_no_app_config(self, mock_config_init): |
| 69 | + """ |
| 70 | + Test that prepare_toolchain correctly deals with no app_config |
| 71 | +
|
| 72 | + :param mock_config_init: mock of Config __init__ |
| 73 | + :return: |
| 74 | + """ |
| 75 | + mock_config_init.return_value = None |
| 76 | + |
| 77 | + prepare_toolchain(self.src_paths, self.target, self.toolchain_name) |
| 78 | + |
| 79 | + mock_config_init.assert_called_with(self.target, self.src_paths, |
| 80 | + app_config=None) |
| 81 | + |
| 82 | + @patch('tools.build_api.scan_resources') |
| 83 | + @patch('tools.build_api.mkdir') |
| 84 | + @patch('os.path.exists') |
| 85 | + @patch('tools.build_api.prepare_toolchain') |
| 86 | + def test_build_project_app_config(self, mock_prepare_toolchain, mock_exists, _, __): |
| 87 | + """ |
| 88 | + Test that build_project uses app_config correctly |
| 89 | +
|
| 90 | + :param mock_prepare_toolchain: mock of function prepare_toolchain |
| 91 | + :param mock_exists: mock of function os.path.exists |
| 92 | + :param _: mock of function mkdir (not tested) |
| 93 | + :param __: mock of function scan_resources (not tested) |
| 94 | + :return: |
| 95 | + """ |
| 96 | + app_config = "app_config" |
| 97 | + mock_exists.return_value = False |
| 98 | + mock_prepare_toolchain().link_program.return_value = 1, 2 |
| 99 | + |
| 100 | + build_project(self.src_paths, self.build_path, self.target, |
| 101 | + self.toolchain_name, app_config=app_config) |
| 102 | + |
| 103 | + args = mock_prepare_toolchain.call_args |
| 104 | + self.assertTrue('app_config' in args[1], |
| 105 | + "prepare_toolchain was not called with app_config") |
| 106 | + self.assertEqual(args[1]['app_config'], app_config, |
| 107 | + "prepare_toolchain was called with an incorrect app_config") |
| 108 | + |
| 109 | + @patch('tools.build_api.scan_resources') |
| 110 | + @patch('tools.build_api.mkdir') |
| 111 | + @patch('os.path.exists') |
| 112 | + @patch('tools.build_api.prepare_toolchain') |
| 113 | + def test_build_project_no_app_config(self, mock_prepare_toolchain, mock_exists, _, __): |
| 114 | + """ |
| 115 | + Test that build_project correctly deals with no app_config |
| 116 | +
|
| 117 | + :param mock_prepare_toolchain: mock of function prepare_toolchain |
| 118 | + :param mock_exists: mock of function os.path.exists |
| 119 | + :param _: mock of function mkdir (not tested) |
| 120 | + :param __: mock of function scan_resources (not tested) |
| 121 | + :return: |
| 122 | + """ |
| 123 | + mock_exists.return_value = False |
| 124 | + # Needed for the unpacking of the returned value |
| 125 | + mock_prepare_toolchain().link_program.return_value = 1, 2 |
| 126 | + |
| 127 | + build_project(self.src_paths, self.build_path, self.target, |
| 128 | + self.toolchain_name) |
| 129 | + |
| 130 | + args = mock_prepare_toolchain.call_args |
| 131 | + self.assertTrue('app_config' in args[1], |
| 132 | + "prepare_toolchain was not called with app_config") |
| 133 | + self.assertEqual(args[1]['app_config'], None, |
| 134 | + "prepare_toolchain was called with an incorrect app_config") |
| 135 | + |
| 136 | + @patch('tools.build_api.scan_resources') |
| 137 | + @patch('tools.build_api.mkdir') |
| 138 | + @patch('os.path.exists') |
| 139 | + @patch('tools.build_api.prepare_toolchain') |
| 140 | + def test_build_library_app_config(self, mock_prepare_toolchain, mock_exists, _, __): |
| 141 | + """ |
| 142 | + Test that build_library uses app_config correctly |
| 143 | +
|
| 144 | + :param mock_prepare_toolchain: mock of function prepare_toolchain |
| 145 | + :param mock_exists: mock of function os.path.exists |
| 146 | + :param _: mock of function mkdir (not tested) |
| 147 | + :param __: mock of function scan_resources (not tested) |
| 148 | + :return: |
| 149 | + """ |
| 150 | + app_config = "app_config" |
| 151 | + mock_exists.return_value = False |
| 152 | + |
| 153 | + build_library(self.src_paths, self.build_path, self.target, |
| 154 | + self.toolchain_name, app_config=app_config) |
| 155 | + |
| 156 | + args = mock_prepare_toolchain.call_args |
| 157 | + self.assertTrue('app_config' in args[1], |
| 158 | + "prepare_toolchain was not called with app_config") |
| 159 | + self.assertEqual(args[1]['app_config'], app_config, |
| 160 | + "prepare_toolchain was called with an incorrect app_config") |
| 161 | + |
| 162 | + @patch('tools.build_api.scan_resources') |
| 163 | + @patch('tools.build_api.mkdir') |
| 164 | + @patch('os.path.exists') |
| 165 | + @patch('tools.build_api.prepare_toolchain') |
| 166 | + def test_build_library_no_app_config(self, mock_prepare_toolchain, mock_exists, _, __): |
| 167 | + """ |
| 168 | + Test that build_library correctly deals with no app_config |
| 169 | +
|
| 170 | + :param mock_prepare_toolchain: mock of function prepare_toolchain |
| 171 | + :param mock_exists: mock of function os.path.exists |
| 172 | + :param _: mock of function mkdir (not tested) |
| 173 | + :param __: mock of function scan_resources (not tested) |
| 174 | + :return: |
| 175 | + """ |
| 176 | + mock_exists.return_value = False |
| 177 | + |
| 178 | + build_library(self.src_paths, self.build_path, self.target, |
| 179 | + self.toolchain_name) |
| 180 | + |
| 181 | + args = mock_prepare_toolchain.call_args |
| 182 | + self.assertTrue('app_config' in args[1], |
| 183 | + "prepare_toolchain was not called with app_config") |
| 184 | + self.assertEqual(args[1]['app_config'], None, |
| 185 | + "prepare_toolchain was called with an incorrect app_config") |
| 186 | + |
| 187 | +if __name__ == '__main__': |
| 188 | + unittest.main() |
0 commit comments