mbed-os

Dependents:   cobaLCDJoyMotor_Thread odometry_omni_3roda_v3 odometry_omni_3roda_v1 odometry_omni_3roda_v2 ... more

Committer:
be_bryan
Date:
Mon Dec 11 17:54:04 2017 +0000
Revision:
0:b74591d5ab33
motor ++

Who changed what in which revision?

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