diff --git a/tools/test/build_api/build_api_test.py b/tools/test/build_api/build_api_test.py new file mode 100644 index 0000000000..97c2f1cf21 --- /dev/null +++ b/tools/test/build_api/build_api_test.py @@ -0,0 +1,188 @@ +""" +mbed SDK +Copyright (c) 2016 ARM Limited + +Licensed under the Apache License, Version 2.0 (the "License"); +you may not use this file except in compliance with the License. +You may obtain a copy of the License at + +http://www.apache.org/licenses/LICENSE-2.0 + +Unless required by applicable law or agreed to in writing, software +distributed under the License is distributed on an "AS IS" BASIS, +WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +See the License for the specific language governing permissions and +limitations under the License. +""" + +import unittest +from mock import patch +from tools.build_api import prepare_toolchain, build_project, build_library + +""" +Tests for build_api.py +""" + +class BuildApiTests(unittest.TestCase): + """ + Test cases for Build Api + """ + + def setUp(self): + """ + Called before each test case + + :return: + """ + self.target = "K64F" + self.src_paths = ['.'] + self.toolchain_name = "ARM" + self.build_path = "build_path" + + def tearDown(self): + """ + Called after each test case + + :return: + """ + pass + + @patch('tools.config.Config.__init__') + def test_prepare_toolchain_app_config(self, mock_config_init): + """ + Test that prepare_toolchain uses app_config correctly + + :param mock_config_init: mock of Config __init__ + :return: + """ + app_config = "app_config" + mock_config_init.return_value = None + + prepare_toolchain(self.src_paths, self.target, self.toolchain_name, + app_config=app_config) + + mock_config_init.assert_called_with(self.target, self.src_paths, + app_config=app_config) + + @patch('tools.config.Config.__init__') + def test_prepare_toolchain_no_app_config(self, mock_config_init): + """ + Test that prepare_toolchain correctly deals with no app_config + + :param mock_config_init: mock of Config __init__ + :return: + """ + mock_config_init.return_value = None + + prepare_toolchain(self.src_paths, self.target, self.toolchain_name) + + mock_config_init.assert_called_with(self.target, self.src_paths, + app_config=None) + + @patch('tools.build_api.scan_resources') + @patch('tools.build_api.mkdir') + @patch('os.path.exists') + @patch('tools.build_api.prepare_toolchain') + def test_build_project_app_config(self, mock_prepare_toolchain, mock_exists, _, __): + """ + Test that build_project uses app_config correctly + + :param mock_prepare_toolchain: mock of function prepare_toolchain + :param mock_exists: mock of function os.path.exists + :param _: mock of function mkdir (not tested) + :param __: mock of function scan_resources (not tested) + :return: + """ + app_config = "app_config" + mock_exists.return_value = False + mock_prepare_toolchain().link_program.return_value = 1, 2 + + build_project(self.src_paths, self.build_path, self.target, + self.toolchain_name, app_config=app_config) + + args = mock_prepare_toolchain.call_args + self.assertTrue('app_config' in args[1], + "prepare_toolchain was not called with app_config") + self.assertEqual(args[1]['app_config'], app_config, + "prepare_toolchain was called with an incorrect app_config") + + @patch('tools.build_api.scan_resources') + @patch('tools.build_api.mkdir') + @patch('os.path.exists') + @patch('tools.build_api.prepare_toolchain') + def test_build_project_no_app_config(self, mock_prepare_toolchain, mock_exists, _, __): + """ + Test that build_project correctly deals with no app_config + + :param mock_prepare_toolchain: mock of function prepare_toolchain + :param mock_exists: mock of function os.path.exists + :param _: mock of function mkdir (not tested) + :param __: mock of function scan_resources (not tested) + :return: + """ + mock_exists.return_value = False + # Needed for the unpacking of the returned value + mock_prepare_toolchain().link_program.return_value = 1, 2 + + build_project(self.src_paths, self.build_path, self.target, + self.toolchain_name) + + args = mock_prepare_toolchain.call_args + self.assertTrue('app_config' in args[1], + "prepare_toolchain was not called with app_config") + self.assertEqual(args[1]['app_config'], None, + "prepare_toolchain was called with an incorrect app_config") + + @patch('tools.build_api.scan_resources') + @patch('tools.build_api.mkdir') + @patch('os.path.exists') + @patch('tools.build_api.prepare_toolchain') + def test_build_library_app_config(self, mock_prepare_toolchain, mock_exists, _, __): + """ + Test that build_library uses app_config correctly + + :param mock_prepare_toolchain: mock of function prepare_toolchain + :param mock_exists: mock of function os.path.exists + :param _: mock of function mkdir (not tested) + :param __: mock of function scan_resources (not tested) + :return: + """ + app_config = "app_config" + mock_exists.return_value = False + + build_library(self.src_paths, self.build_path, self.target, + self.toolchain_name, app_config=app_config) + + args = mock_prepare_toolchain.call_args + self.assertTrue('app_config' in args[1], + "prepare_toolchain was not called with app_config") + self.assertEqual(args[1]['app_config'], app_config, + "prepare_toolchain was called with an incorrect app_config") + + @patch('tools.build_api.scan_resources') + @patch('tools.build_api.mkdir') + @patch('os.path.exists') + @patch('tools.build_api.prepare_toolchain') + def test_build_library_no_app_config(self, mock_prepare_toolchain, mock_exists, _, __): + """ + Test that build_library correctly deals with no app_config + + :param mock_prepare_toolchain: mock of function prepare_toolchain + :param mock_exists: mock of function os.path.exists + :param _: mock of function mkdir (not tested) + :param __: mock of function scan_resources (not tested) + :return: + """ + mock_exists.return_value = False + + build_library(self.src_paths, self.build_path, self.target, + self.toolchain_name) + + args = mock_prepare_toolchain.call_args + self.assertTrue('app_config' in args[1], + "prepare_toolchain was not called with app_config") + self.assertEqual(args[1]['app_config'], None, + "prepare_toolchain was called with an incorrect app_config") + +if __name__ == '__main__': + unittest.main() diff --git a/tools/test/config/config_test.py b/tools/test/config/config_test.py new file mode 100644 index 0000000000..114f088c28 --- /dev/null +++ b/tools/test/config/config_test.py @@ -0,0 +1,132 @@ +""" +mbed SDK +Copyright (c) 2016 ARM Limited + +Licensed under the Apache License, Version 2.0 (the "License"); +you may not use this file except in compliance with the License. +You may obtain a copy of the License at + +http://www.apache.org/licenses/LICENSE-2.0 + +Unless required by applicable law or agreed to in writing, software +distributed under the License is distributed on an "AS IS" BASIS, +WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +See the License for the specific language governing permissions and +limitations under the License. +""" + +import os.path +import unittest +from mock import patch +from tools.config import Config + +""" +Tests for config.py +""" + +class ConfigTests(unittest.TestCase): + """ + Test cases for Config class + """ + + def setUp(self): + """ + Called before each test case + + :return: + """ + self.target = "K64F" + + def tearDown(self): + """ + Called after each test case + + :return: + """ + pass + + @patch.object(Config, '_process_config_and_overrides') + @patch('tools.config.json_file_to_dict') + def test_init_app_config(self, mock_json_file_to_dict, _): + """ + Test that the initialisation correctly uses app_config + + :param mock_json_file_to_dict: mock of function json_file_to_dict + :param _: mock of function _process_config_and_overrides (not tested) + :return: + """ + app_config = "app_config" + mock_return = {'config': 'test'} + mock_json_file_to_dict.return_value = mock_return + + config = Config(self.target, app_config=app_config) + + mock_json_file_to_dict.assert_called_with(app_config) + self.assertEqual(config.app_config_data, mock_return, + "app_config_data should be set to the returned value") + + @patch.object(Config, '_process_config_and_overrides') + @patch('tools.config.json_file_to_dict') + def test_init_no_app_config(self, mock_json_file_to_dict, _): + """ + Test that the initialisation works without app config + + :param mock_json_file_to_dict: mock of function json_file_to_dict + :param _: patch of function _process_config_and_overrides (not tested) + :return: + """ + config = Config(self.target) + + mock_json_file_to_dict.assert_not_called() + self.assertEqual(config.app_config_data, {}, + "app_config_data should be set an empty dictionary") + + @patch.object(Config, '_process_config_and_overrides') + @patch('os.path.isfile') + @patch('tools.config.json_file_to_dict') + def test_init_no_app_config_with_dir(self, mock_json_file_to_dict, mock_isfile, _): + """ + Test that the initialisation works without app config and with a + specified top level directory + + :param mock_json_file_to_dict: mock of function json_file_to_dict + :param _: patch of function _process_config_and_overrides (not tested) + :return: + """ + directory = '.' + path = os.path.join('.', 'mbed_app.json') + mock_return = {'config': 'test'} + mock_json_file_to_dict.return_value = mock_return + mock_isfile.return_value = True + + config = Config(self.target, [directory]) + + mock_isfile.assert_called_with(path) + mock_json_file_to_dict.assert_called_once_with(path) + self.assertEqual(config.app_config_data, mock_return, + "app_config_data should be set to the returned value") + + @patch.object(Config, '_process_config_and_overrides') + @patch('tools.config.json_file_to_dict') + def test_init_override_app_config(self, mock_json_file_to_dict, _): + """ + Test that the initialisation uses app_config instead of top_level_dir + when both are specified + + :param mock_json_file_to_dict: mock of function json_file_to_dict + :param _: patch of function _process_config_and_overrides (not tested) + :return: + """ + app_config = "app_config" + directory = '.' + mock_return = {'config': 'test'} + mock_json_file_to_dict.return_value = mock_return + + config = Config(self.target, [directory], app_config=app_config) + + mock_json_file_to_dict.assert_called_once_with(app_config) + self.assertEqual(config.app_config_data, mock_return, + "app_config_data should be set to the returned value") + +if __name__ == '__main__': + unittest.main() diff --git a/tools/test/test_api/test_api_test.py b/tools/test/test_api/test_api_test.py new file mode 100644 index 0000000000..9f16941b04 --- /dev/null +++ b/tools/test/test_api/test_api_test.py @@ -0,0 +1,141 @@ +""" +mbed SDK +Copyright (c) 2016 ARM Limited + +Licensed under the Apache License, Version 2.0 (the "License"); +you may not use this file except in compliance with the License. +You may obtain a copy of the License at + +http://www.apache.org/licenses/LICENSE-2.0 + +Unless required by applicable law or agreed to in writing, software +distributed under the License is distributed on an "AS IS" BASIS, +WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +See the License for the specific language governing permissions and +limitations under the License. +""" + +import unittest +from mock import patch +from tools.test_api import find_tests, build_tests + +""" +Tests for test_api.py +""" + +class TestApiTests(unittest.TestCase): + """ + Test cases for Test Api + """ + + def setUp(self): + """ + Called before each test case + + :return: + """ + self.base_dir = 'base_dir' + self.target = "K64F" + self.toolchain_name = "ARM" + + def tearDown(self): + """ + Called after each test case + + :return: + """ + pass + + @patch('tools.test_api.scan_resources') + @patch('tools.test_api.prepare_toolchain') + def test_find_tests_app_config(self, mock_prepare_toolchain, mock_scan_resources): + """ + Test find_tests for correct use of app_config + + :param mock_prepare_toolchain: mock of function prepare_toolchain + :param mock_scan_resources: mock of function scan_resources + :return: + """ + app_config = "app_config" + mock_scan_resources().inc_dirs.return_value = [] + + find_tests(self.base_dir, self.target, self.toolchain_name, app_config=app_config) + + args = mock_prepare_toolchain.call_args + self.assertTrue('app_config' in args[1], + "prepare_toolchain was not called with app_config") + self.assertEqual(args[1]['app_config'], app_config, + "prepare_toolchain was called with an incorrect app_config") + + @patch('tools.test_api.scan_resources') + @patch('tools.test_api.prepare_toolchain') + def test_find_tests_no_app_config(self, mock_prepare_toolchain, mock_scan_resources): + """ + Test find_tests correctly deals with no app_config + + :param mock_prepare_toolchain: mock of function prepare_toolchain + :param mock_scan_resources: mock of function scan_resources + :return: + """ + mock_scan_resources().inc_dirs.return_value = [] + + find_tests(self.base_dir, self.target, self.toolchain_name) + + args = mock_prepare_toolchain.call_args + self.assertTrue('app_config' in args[1], + "prepare_toolchain was not called with app_config") + self.assertEqual(args[1]['app_config'], None, + "prepare_toolchain was called with an incorrect app_config") + + @patch('tools.test_api.scan_resources') + @patch('tools.test_api.build_project') + def test_build_tests_app_config(self, mock_build_project, mock_scan_resources): + """ + Test build_tests for correct use of app_config + + :param mock_prepare_toolchain: mock of function prepare_toolchain + :param mock_scan_resources: mock of function scan_resources + :return: + """ + tests = {'test1': 'test1_path','test2': 'test2_path'} + src_paths = ['.'] + build_path = "build_path" + app_config = "app_config" + mock_build_project.return_value = "build_project" + + build_tests(tests, src_paths, build_path, self.target, self.toolchain_name, + app_config=app_config) + + arg_list = mock_build_project.call_args_list + for args in arg_list: + self.assertTrue('app_config' in args[1], + "build_tests was not called with app_config") + self.assertEqual(args[1]['app_config'], app_config, + "build_tests was called with an incorrect app_config") + + @patch('tools.test_api.scan_resources') + @patch('tools.test_api.build_project') + def test_build_tests_no_app_config(self, mock_build_project, mock_scan_resources): + """ + Test build_tests correctly deals with no app_config + + :param mock_prepare_toolchain: mock of function prepare_toolchain + :param mock_scan_resources: mock of function scan_resources + :return: + """ + tests = {'test1': 'test1_path', 'test2': 'test2_path'} + src_paths = ['.'] + build_path = "build_path" + mock_build_project.return_value = "build_project" + + build_tests(tests, src_paths, build_path, self.target, self.toolchain_name) + + arg_list = mock_build_project.call_args_list + for args in arg_list: + self.assertTrue('app_config' in args[1], + "build_tests was not called with app_config") + self.assertEqual(args[1]['app_config'], None, + "build_tests was called with an incorrect app_config") + +if __name__ == '__main__': + unittest.main()