mbed-os

Fork of mbed-os by erkin yucel

Committer:
elessair
Date:
Sun Oct 23 15:10:02 2016 +0000
Revision:
0:f269e3021894
Initial commit

Who changed what in which revision?

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