Important changes to repositories hosted on mbed.com
Mbed hosted mercurial repositories are deprecated and are due to be permanently deleted in July 2026.
To keep a copy of this software download the repository Zip archive or clone locally using Mercurial.
It is also possible to export all your personal repositories from the account settings page.
Fork of mbed-sdk-tools by
test/build_api/build_api_test.py@31:182518299918, 2017-01-04 (annotated)
- Committer:
- The Other Jimmy
- Date:
- Wed Jan 04 11:58:24 2017 -0600
- Revision:
- 31:182518299918
Update tools to follow mbed-os tools release 5.3.1
Who changed what in which revision?
User | Revision | Line number | New contents of line |
---|---|---|---|
The Other Jimmy |
31:182518299918 | 1 | """ |
The Other Jimmy |
31:182518299918 | 2 | mbed SDK |
The Other Jimmy |
31:182518299918 | 3 | Copyright (c) 2016 ARM Limited |
The Other Jimmy |
31:182518299918 | 4 | |
The Other Jimmy |
31:182518299918 | 5 | Licensed under the Apache License, Version 2.0 (the "License"); |
The Other Jimmy |
31:182518299918 | 6 | you may not use this file except in compliance with the License. |
The Other Jimmy |
31:182518299918 | 7 | You may obtain a copy of the License at |
The Other Jimmy |
31:182518299918 | 8 | |
The Other Jimmy |
31:182518299918 | 9 | http://www.apache.org/licenses/LICENSE-2.0 |
The Other Jimmy |
31:182518299918 | 10 | |
The Other Jimmy |
31:182518299918 | 11 | Unless required by applicable law or agreed to in writing, software |
The Other Jimmy |
31:182518299918 | 12 | distributed under the License is distributed on an "AS IS" BASIS, |
The Other Jimmy |
31:182518299918 | 13 | WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
The Other Jimmy |
31:182518299918 | 14 | See the License for the specific language governing permissions and |
The Other Jimmy |
31:182518299918 | 15 | limitations under the License. |
The Other Jimmy |
31:182518299918 | 16 | """ |
The Other Jimmy |
31:182518299918 | 17 | |
The Other Jimmy |
31:182518299918 | 18 | import unittest |
The Other Jimmy |
31:182518299918 | 19 | from collections import namedtuple |
The Other Jimmy |
31:182518299918 | 20 | from mock import patch, MagicMock |
The Other Jimmy |
31:182518299918 | 21 | from tools.build_api import prepare_toolchain, build_project, build_library,\ |
The Other Jimmy |
31:182518299918 | 22 | scan_resources |
The Other Jimmy |
31:182518299918 | 23 | |
The Other Jimmy |
31:182518299918 | 24 | """ |
The Other Jimmy |
31:182518299918 | 25 | Tests for build_api.py |
The Other Jimmy |
31:182518299918 | 26 | """ |
The Other Jimmy |
31:182518299918 | 27 | |
The Other Jimmy |
31:182518299918 | 28 | class BuildApiTests(unittest.TestCase): |
The Other Jimmy |
31:182518299918 | 29 | """ |
The Other Jimmy |
31:182518299918 | 30 | Test cases for Build Api |
The Other Jimmy |
31:182518299918 | 31 | """ |
The Other Jimmy |
31:182518299918 | 32 | |
The Other Jimmy |
31:182518299918 | 33 | def setUp(self): |
The Other Jimmy |
31:182518299918 | 34 | """ |
The Other Jimmy |
31:182518299918 | 35 | Called before each test case |
The Other Jimmy |
31:182518299918 | 36 | |
The Other Jimmy |
31:182518299918 | 37 | :return: |
The Other Jimmy |
31:182518299918 | 38 | """ |
The Other Jimmy |
31:182518299918 | 39 | self.target = "K64F" |
The Other Jimmy |
31:182518299918 | 40 | self.src_paths = ['.'] |
The Other Jimmy |
31:182518299918 | 41 | self.toolchain_name = "ARM" |
The Other Jimmy |
31:182518299918 | 42 | self.build_path = "build_path" |
The Other Jimmy |
31:182518299918 | 43 | |
The Other Jimmy |
31:182518299918 | 44 | def tearDown(self): |
The Other Jimmy |
31:182518299918 | 45 | """ |
The Other Jimmy |
31:182518299918 | 46 | Called after each test case |
The Other Jimmy |
31:182518299918 | 47 | |
The Other Jimmy |
31:182518299918 | 48 | :return: |
The Other Jimmy |
31:182518299918 | 49 | """ |
The Other Jimmy |
31:182518299918 | 50 | pass |
The Other Jimmy |
31:182518299918 | 51 | |
The Other Jimmy |
31:182518299918 | 52 | @patch('tools.toolchains.arm.ARM_STD.parse_dependencies', |
The Other Jimmy |
31:182518299918 | 53 | return_value=["foo"]) |
The Other Jimmy |
31:182518299918 | 54 | @patch('tools.toolchains.mbedToolchain.need_update', |
The Other Jimmy |
31:182518299918 | 55 | side_effect=[i % 2 for i in range(3000)]) |
The Other Jimmy |
31:182518299918 | 56 | @patch('os.mkdir') |
The Other Jimmy |
31:182518299918 | 57 | @patch('tools.toolchains.exists', return_value=True) |
The Other Jimmy |
31:182518299918 | 58 | @patch('tools.utils.run_cmd', return_value=("", "", 0)) |
The Other Jimmy |
31:182518299918 | 59 | def test_always_complete_build(self, *_): |
The Other Jimmy |
31:182518299918 | 60 | with MagicMock() as notify: |
The Other Jimmy |
31:182518299918 | 61 | toolchain = prepare_toolchain(self.src_paths, self.target, |
The Other Jimmy |
31:182518299918 | 62 | self.toolchain_name, notify=notify) |
The Other Jimmy |
31:182518299918 | 63 | |
The Other Jimmy |
31:182518299918 | 64 | res = scan_resources(self.src_paths, toolchain) |
The Other Jimmy |
31:182518299918 | 65 | |
The Other Jimmy |
31:182518299918 | 66 | toolchain.RESPONSE_FILES=False |
The Other Jimmy |
31:182518299918 | 67 | toolchain.config_processed = True |
The Other Jimmy |
31:182518299918 | 68 | toolchain.config_file = "junk" |
The Other Jimmy |
31:182518299918 | 69 | toolchain.compile_sources(res, self.build_path) |
The Other Jimmy |
31:182518299918 | 70 | |
The Other Jimmy |
31:182518299918 | 71 | assert any('percent' in msg[0] and msg[0]['percent'] == 100.0 |
The Other Jimmy |
31:182518299918 | 72 | for _, msg, _ in notify.mock_calls if msg) |
The Other Jimmy |
31:182518299918 | 73 | |
The Other Jimmy |
31:182518299918 | 74 | |
The Other Jimmy |
31:182518299918 | 75 | @patch('tools.build_api.Config') |
The Other Jimmy |
31:182518299918 | 76 | def test_prepare_toolchain_app_config(self, mock_config_init): |
The Other Jimmy |
31:182518299918 | 77 | """ |
The Other Jimmy |
31:182518299918 | 78 | Test that prepare_toolchain uses app_config correctly |
The Other Jimmy |
31:182518299918 | 79 | |
The Other Jimmy |
31:182518299918 | 80 | :param mock_config_init: mock of Config __init__ |
The Other Jimmy |
31:182518299918 | 81 | :return: |
The Other Jimmy |
31:182518299918 | 82 | """ |
The Other Jimmy |
31:182518299918 | 83 | app_config = "app_config" |
The Other Jimmy |
31:182518299918 | 84 | mock_config_init.return_value = namedtuple("Config", "target")( |
The Other Jimmy |
31:182518299918 | 85 | namedtuple("Target", |
The Other Jimmy |
31:182518299918 | 86 | "init_hooks name features core")(lambda _, __ : None, |
The Other Jimmy |
31:182518299918 | 87 | "Junk", [], "Cortex-M3")) |
The Other Jimmy |
31:182518299918 | 88 | |
The Other Jimmy |
31:182518299918 | 89 | prepare_toolchain(self.src_paths, self.target, self.toolchain_name, |
The Other Jimmy |
31:182518299918 | 90 | app_config=app_config) |
The Other Jimmy |
31:182518299918 | 91 | |
The Other Jimmy |
31:182518299918 | 92 | mock_config_init.assert_called_once_with(self.target, self.src_paths, |
The Other Jimmy |
31:182518299918 | 93 | app_config=app_config) |
The Other Jimmy |
31:182518299918 | 94 | |
The Other Jimmy |
31:182518299918 | 95 | @patch('tools.build_api.Config') |
The Other Jimmy |
31:182518299918 | 96 | def test_prepare_toolchain_no_app_config(self, mock_config_init): |
The Other Jimmy |
31:182518299918 | 97 | """ |
The Other Jimmy |
31:182518299918 | 98 | Test that prepare_toolchain correctly deals with no app_config |
The Other Jimmy |
31:182518299918 | 99 | |
The Other Jimmy |
31:182518299918 | 100 | :param mock_config_init: mock of Config __init__ |
The Other Jimmy |
31:182518299918 | 101 | :return: |
The Other Jimmy |
31:182518299918 | 102 | """ |
The Other Jimmy |
31:182518299918 | 103 | mock_config_init.return_value = namedtuple("Config", "target")( |
The Other Jimmy |
31:182518299918 | 104 | namedtuple("Target", |
The Other Jimmy |
31:182518299918 | 105 | "init_hooks name features core")(lambda _, __ : None, |
The Other Jimmy |
31:182518299918 | 106 | "Junk", [], "Cortex-M3")) |
The Other Jimmy |
31:182518299918 | 107 | |
The Other Jimmy |
31:182518299918 | 108 | prepare_toolchain(self.src_paths, self.target, self.toolchain_name) |
The Other Jimmy |
31:182518299918 | 109 | |
The Other Jimmy |
31:182518299918 | 110 | mock_config_init.assert_called_once_with(self.target, self.src_paths, |
The Other Jimmy |
31:182518299918 | 111 | app_config=None) |
The Other Jimmy |
31:182518299918 | 112 | |
The Other Jimmy |
31:182518299918 | 113 | @patch('tools.build_api.scan_resources') |
The Other Jimmy |
31:182518299918 | 114 | @patch('tools.build_api.mkdir') |
The Other Jimmy |
31:182518299918 | 115 | @patch('os.path.exists') |
The Other Jimmy |
31:182518299918 | 116 | @patch('tools.build_api.prepare_toolchain') |
The Other Jimmy |
31:182518299918 | 117 | def test_build_project_app_config(self, mock_prepare_toolchain, mock_exists, _, __): |
The Other Jimmy |
31:182518299918 | 118 | """ |
The Other Jimmy |
31:182518299918 | 119 | Test that build_project uses app_config correctly |
The Other Jimmy |
31:182518299918 | 120 | |
The Other Jimmy |
31:182518299918 | 121 | :param mock_prepare_toolchain: mock of function prepare_toolchain |
The Other Jimmy |
31:182518299918 | 122 | :param mock_exists: mock of function os.path.exists |
The Other Jimmy |
31:182518299918 | 123 | :param _: mock of function mkdir (not tested) |
The Other Jimmy |
31:182518299918 | 124 | :param __: mock of function scan_resources (not tested) |
The Other Jimmy |
31:182518299918 | 125 | :return: |
The Other Jimmy |
31:182518299918 | 126 | """ |
The Other Jimmy |
31:182518299918 | 127 | app_config = "app_config" |
The Other Jimmy |
31:182518299918 | 128 | mock_exists.return_value = False |
The Other Jimmy |
31:182518299918 | 129 | mock_prepare_toolchain().link_program.return_value = 1, 2 |
The Other Jimmy |
31:182518299918 | 130 | |
The Other Jimmy |
31:182518299918 | 131 | build_project(self.src_paths, self.build_path, self.target, |
The Other Jimmy |
31:182518299918 | 132 | self.toolchain_name, app_config=app_config) |
The Other Jimmy |
31:182518299918 | 133 | |
The Other Jimmy |
31:182518299918 | 134 | args = mock_prepare_toolchain.call_args |
The Other Jimmy |
31:182518299918 | 135 | self.assertTrue('app_config' in args[1], |
The Other Jimmy |
31:182518299918 | 136 | "prepare_toolchain was not called with app_config") |
The Other Jimmy |
31:182518299918 | 137 | self.assertEqual(args[1]['app_config'], app_config, |
The Other Jimmy |
31:182518299918 | 138 | "prepare_toolchain was called with an incorrect app_config") |
The Other Jimmy |
31:182518299918 | 139 | |
The Other Jimmy |
31:182518299918 | 140 | @patch('tools.build_api.scan_resources') |
The Other Jimmy |
31:182518299918 | 141 | @patch('tools.build_api.mkdir') |
The Other Jimmy |
31:182518299918 | 142 | @patch('os.path.exists') |
The Other Jimmy |
31:182518299918 | 143 | @patch('tools.build_api.prepare_toolchain') |
The Other Jimmy |
31:182518299918 | 144 | def test_build_project_no_app_config(self, mock_prepare_toolchain, mock_exists, _, __): |
The Other Jimmy |
31:182518299918 | 145 | """ |
The Other Jimmy |
31:182518299918 | 146 | Test that build_project correctly deals with no app_config |
The Other Jimmy |
31:182518299918 | 147 | |
The Other Jimmy |
31:182518299918 | 148 | :param mock_prepare_toolchain: mock of function prepare_toolchain |
The Other Jimmy |
31:182518299918 | 149 | :param mock_exists: mock of function os.path.exists |
The Other Jimmy |
31:182518299918 | 150 | :param _: mock of function mkdir (not tested) |
The Other Jimmy |
31:182518299918 | 151 | :param __: mock of function scan_resources (not tested) |
The Other Jimmy |
31:182518299918 | 152 | :return: |
The Other Jimmy |
31:182518299918 | 153 | """ |
The Other Jimmy |
31:182518299918 | 154 | mock_exists.return_value = False |
The Other Jimmy |
31:182518299918 | 155 | # Needed for the unpacking of the returned value |
The Other Jimmy |
31:182518299918 | 156 | mock_prepare_toolchain().link_program.return_value = 1, 2 |
The Other Jimmy |
31:182518299918 | 157 | |
The Other Jimmy |
31:182518299918 | 158 | build_project(self.src_paths, self.build_path, self.target, |
The Other Jimmy |
31:182518299918 | 159 | self.toolchain_name) |
The Other Jimmy |
31:182518299918 | 160 | |
The Other Jimmy |
31:182518299918 | 161 | args = mock_prepare_toolchain.call_args |
The Other Jimmy |
31:182518299918 | 162 | self.assertTrue('app_config' in args[1], |
The Other Jimmy |
31:182518299918 | 163 | "prepare_toolchain was not called with app_config") |
The Other Jimmy |
31:182518299918 | 164 | self.assertEqual(args[1]['app_config'], None, |
The Other Jimmy |
31:182518299918 | 165 | "prepare_toolchain was called with an incorrect app_config") |
The Other Jimmy |
31:182518299918 | 166 | |
The Other Jimmy |
31:182518299918 | 167 | @patch('tools.build_api.scan_resources') |
The Other Jimmy |
31:182518299918 | 168 | @patch('tools.build_api.mkdir') |
The Other Jimmy |
31:182518299918 | 169 | @patch('os.path.exists') |
The Other Jimmy |
31:182518299918 | 170 | @patch('tools.build_api.prepare_toolchain') |
The Other Jimmy |
31:182518299918 | 171 | def test_build_library_app_config(self, mock_prepare_toolchain, mock_exists, _, __): |
The Other Jimmy |
31:182518299918 | 172 | """ |
The Other Jimmy |
31:182518299918 | 173 | Test that build_library uses app_config correctly |
The Other Jimmy |
31:182518299918 | 174 | |
The Other Jimmy |
31:182518299918 | 175 | :param mock_prepare_toolchain: mock of function prepare_toolchain |
The Other Jimmy |
31:182518299918 | 176 | :param mock_exists: mock of function os.path.exists |
The Other Jimmy |
31:182518299918 | 177 | :param _: mock of function mkdir (not tested) |
The Other Jimmy |
31:182518299918 | 178 | :param __: mock of function scan_resources (not tested) |
The Other Jimmy |
31:182518299918 | 179 | :return: |
The Other Jimmy |
31:182518299918 | 180 | """ |
The Other Jimmy |
31:182518299918 | 181 | app_config = "app_config" |
The Other Jimmy |
31:182518299918 | 182 | mock_exists.return_value = False |
The Other Jimmy |
31:182518299918 | 183 | |
The Other Jimmy |
31:182518299918 | 184 | build_library(self.src_paths, self.build_path, self.target, |
The Other Jimmy |
31:182518299918 | 185 | self.toolchain_name, app_config=app_config) |
The Other Jimmy |
31:182518299918 | 186 | |
The Other Jimmy |
31:182518299918 | 187 | args = mock_prepare_toolchain.call_args |
The Other Jimmy |
31:182518299918 | 188 | self.assertTrue('app_config' in args[1], |
The Other Jimmy |
31:182518299918 | 189 | "prepare_toolchain was not called with app_config") |
The Other Jimmy |
31:182518299918 | 190 | self.assertEqual(args[1]['app_config'], app_config, |
The Other Jimmy |
31:182518299918 | 191 | "prepare_toolchain was called with an incorrect app_config") |
The Other Jimmy |
31:182518299918 | 192 | |
The Other Jimmy |
31:182518299918 | 193 | @patch('tools.build_api.scan_resources') |
The Other Jimmy |
31:182518299918 | 194 | @patch('tools.build_api.mkdir') |
The Other Jimmy |
31:182518299918 | 195 | @patch('os.path.exists') |
The Other Jimmy |
31:182518299918 | 196 | @patch('tools.build_api.prepare_toolchain') |
The Other Jimmy |
31:182518299918 | 197 | def test_build_library_no_app_config(self, mock_prepare_toolchain, mock_exists, _, __): |
The Other Jimmy |
31:182518299918 | 198 | """ |
The Other Jimmy |
31:182518299918 | 199 | Test that build_library correctly deals with no app_config |
The Other Jimmy |
31:182518299918 | 200 | |
The Other Jimmy |
31:182518299918 | 201 | :param mock_prepare_toolchain: mock of function prepare_toolchain |
The Other Jimmy |
31:182518299918 | 202 | :param mock_exists: mock of function os.path.exists |
The Other Jimmy |
31:182518299918 | 203 | :param _: mock of function mkdir (not tested) |
The Other Jimmy |
31:182518299918 | 204 | :param __: mock of function scan_resources (not tested) |
The Other Jimmy |
31:182518299918 | 205 | :return: |
The Other Jimmy |
31:182518299918 | 206 | """ |
The Other Jimmy |
31:182518299918 | 207 | mock_exists.return_value = False |
The Other Jimmy |
31:182518299918 | 208 | |
The Other Jimmy |
31:182518299918 | 209 | build_library(self.src_paths, self.build_path, self.target, |
The Other Jimmy |
31:182518299918 | 210 | self.toolchain_name) |
The Other Jimmy |
31:182518299918 | 211 | |
The Other Jimmy |
31:182518299918 | 212 | args = mock_prepare_toolchain.call_args |
The Other Jimmy |
31:182518299918 | 213 | self.assertTrue('app_config' in args[1], |
The Other Jimmy |
31:182518299918 | 214 | "prepare_toolchain was not called with app_config") |
The Other Jimmy |
31:182518299918 | 215 | self.assertEqual(args[1]['app_config'], None, |
The Other Jimmy |
31:182518299918 | 216 | "prepare_toolchain was called with an incorrect app_config") |
The Other Jimmy |
31:182518299918 | 217 | |
The Other Jimmy |
31:182518299918 | 218 | if __name__ == '__main__': |
The Other Jimmy |
31:182518299918 | 219 | unittest.main() |