PageRenderTime 42ms CodeModel.GetById 17ms RepoModel.GetById 0ms app.codeStats 0ms

/external/mbed-os/tools/test/build_api/build_api_test.py

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