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.
test/config/config_test.py@31:8ea194f6145b, 2017-01-04 (annotated)
- Committer:
- The Other Jimmy
- Date:
- Wed Jan 04 11:58:24 2017 -0600
- Revision:
- 31:8ea194f6145b
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:8ea194f6145b | 1 | """ |
| The Other Jimmy |
31:8ea194f6145b | 2 | mbed SDK |
| The Other Jimmy |
31:8ea194f6145b | 3 | Copyright (c) 2016 ARM Limited |
| The Other Jimmy |
31:8ea194f6145b | 4 | |
| The Other Jimmy |
31:8ea194f6145b | 5 | Licensed under the Apache License, Version 2.0 (the "License"); |
| The Other Jimmy |
31:8ea194f6145b | 6 | you may not use this file except in compliance with the License. |
| The Other Jimmy |
31:8ea194f6145b | 7 | You may obtain a copy of the License at |
| The Other Jimmy |
31:8ea194f6145b | 8 | |
| The Other Jimmy |
31:8ea194f6145b | 9 | http://www.apache.org/licenses/LICENSE-2.0 |
| The Other Jimmy |
31:8ea194f6145b | 10 | |
| The Other Jimmy |
31:8ea194f6145b | 11 | Unless required by applicable law or agreed to in writing, software |
| The Other Jimmy |
31:8ea194f6145b | 12 | distributed under the License is distributed on an "AS IS" BASIS, |
| The Other Jimmy |
31:8ea194f6145b | 13 | WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| The Other Jimmy |
31:8ea194f6145b | 14 | See the License for the specific language governing permissions and |
| The Other Jimmy |
31:8ea194f6145b | 15 | limitations under the License. |
| The Other Jimmy |
31:8ea194f6145b | 16 | """ |
| The Other Jimmy |
31:8ea194f6145b | 17 | |
| The Other Jimmy |
31:8ea194f6145b | 18 | import os.path |
| The Other Jimmy |
31:8ea194f6145b | 19 | import unittest |
| The Other Jimmy |
31:8ea194f6145b | 20 | from mock import patch |
| The Other Jimmy |
31:8ea194f6145b | 21 | from tools.config import Config |
| The Other Jimmy |
31:8ea194f6145b | 22 | |
| The Other Jimmy |
31:8ea194f6145b | 23 | """ |
| The Other Jimmy |
31:8ea194f6145b | 24 | Tests for config.py |
| The Other Jimmy |
31:8ea194f6145b | 25 | """ |
| The Other Jimmy |
31:8ea194f6145b | 26 | |
| The Other Jimmy |
31:8ea194f6145b | 27 | class ConfigTests(unittest.TestCase): |
| The Other Jimmy |
31:8ea194f6145b | 28 | """ |
| The Other Jimmy |
31:8ea194f6145b | 29 | Test cases for Config class |
| The Other Jimmy |
31:8ea194f6145b | 30 | """ |
| The Other Jimmy |
31:8ea194f6145b | 31 | |
| The Other Jimmy |
31:8ea194f6145b | 32 | def setUp(self): |
| The Other Jimmy |
31:8ea194f6145b | 33 | """ |
| The Other Jimmy |
31:8ea194f6145b | 34 | Called before each test case |
| The Other Jimmy |
31:8ea194f6145b | 35 | |
| The Other Jimmy |
31:8ea194f6145b | 36 | :return: |
| The Other Jimmy |
31:8ea194f6145b | 37 | """ |
| The Other Jimmy |
31:8ea194f6145b | 38 | self.target = "K64F" |
| The Other Jimmy |
31:8ea194f6145b | 39 | |
| The Other Jimmy |
31:8ea194f6145b | 40 | def tearDown(self): |
| The Other Jimmy |
31:8ea194f6145b | 41 | """ |
| The Other Jimmy |
31:8ea194f6145b | 42 | Called after each test case |
| The Other Jimmy |
31:8ea194f6145b | 43 | |
| The Other Jimmy |
31:8ea194f6145b | 44 | :return: |
| The Other Jimmy |
31:8ea194f6145b | 45 | """ |
| The Other Jimmy |
31:8ea194f6145b | 46 | pass |
| The Other Jimmy |
31:8ea194f6145b | 47 | |
| The Other Jimmy |
31:8ea194f6145b | 48 | @patch.object(Config, '_process_config_and_overrides') |
| The Other Jimmy |
31:8ea194f6145b | 49 | @patch('tools.config.json_file_to_dict') |
| The Other Jimmy |
31:8ea194f6145b | 50 | def test_init_app_config(self, mock_json_file_to_dict, _): |
| The Other Jimmy |
31:8ea194f6145b | 51 | """ |
| The Other Jimmy |
31:8ea194f6145b | 52 | Test that the initialisation correctly uses app_config |
| The Other Jimmy |
31:8ea194f6145b | 53 | |
| The Other Jimmy |
31:8ea194f6145b | 54 | :param mock_json_file_to_dict: mock of function json_file_to_dict |
| The Other Jimmy |
31:8ea194f6145b | 55 | :param _: mock of function _process_config_and_overrides (not tested) |
| The Other Jimmy |
31:8ea194f6145b | 56 | :return: |
| The Other Jimmy |
31:8ea194f6145b | 57 | """ |
| The Other Jimmy |
31:8ea194f6145b | 58 | app_config = "app_config" |
| The Other Jimmy |
31:8ea194f6145b | 59 | mock_return = {'config': 'test'} |
| The Other Jimmy |
31:8ea194f6145b | 60 | mock_json_file_to_dict.return_value = mock_return |
| The Other Jimmy |
31:8ea194f6145b | 61 | |
| The Other Jimmy |
31:8ea194f6145b | 62 | config = Config(self.target, app_config=app_config) |
| The Other Jimmy |
31:8ea194f6145b | 63 | |
| The Other Jimmy |
31:8ea194f6145b | 64 | mock_json_file_to_dict.assert_called_with(app_config) |
| The Other Jimmy |
31:8ea194f6145b | 65 | self.assertEqual(config.app_config_data, mock_return, |
| The Other Jimmy |
31:8ea194f6145b | 66 | "app_config_data should be set to the returned value") |
| The Other Jimmy |
31:8ea194f6145b | 67 | |
| The Other Jimmy |
31:8ea194f6145b | 68 | @patch.object(Config, '_process_config_and_overrides') |
| The Other Jimmy |
31:8ea194f6145b | 69 | @patch('tools.config.json_file_to_dict') |
| The Other Jimmy |
31:8ea194f6145b | 70 | def test_init_no_app_config(self, mock_json_file_to_dict, _): |
| The Other Jimmy |
31:8ea194f6145b | 71 | """ |
| The Other Jimmy |
31:8ea194f6145b | 72 | Test that the initialisation works without app config |
| The Other Jimmy |
31:8ea194f6145b | 73 | |
| The Other Jimmy |
31:8ea194f6145b | 74 | :param mock_json_file_to_dict: mock of function json_file_to_dict |
| The Other Jimmy |
31:8ea194f6145b | 75 | :param _: patch of function _process_config_and_overrides (not tested) |
| The Other Jimmy |
31:8ea194f6145b | 76 | :return: |
| The Other Jimmy |
31:8ea194f6145b | 77 | """ |
| The Other Jimmy |
31:8ea194f6145b | 78 | config = Config(self.target) |
| The Other Jimmy |
31:8ea194f6145b | 79 | |
| The Other Jimmy |
31:8ea194f6145b | 80 | mock_json_file_to_dict.assert_not_called() |
| The Other Jimmy |
31:8ea194f6145b | 81 | self.assertEqual(config.app_config_data, {}, |
| The Other Jimmy |
31:8ea194f6145b | 82 | "app_config_data should be set an empty dictionary") |
| The Other Jimmy |
31:8ea194f6145b | 83 | |
| The Other Jimmy |
31:8ea194f6145b | 84 | @patch.object(Config, '_process_config_and_overrides') |
| The Other Jimmy |
31:8ea194f6145b | 85 | @patch('os.path.isfile') |
| The Other Jimmy |
31:8ea194f6145b | 86 | @patch('tools.config.json_file_to_dict') |
| The Other Jimmy |
31:8ea194f6145b | 87 | def test_init_no_app_config_with_dir(self, mock_json_file_to_dict, mock_isfile, _): |
| The Other Jimmy |
31:8ea194f6145b | 88 | """ |
| The Other Jimmy |
31:8ea194f6145b | 89 | Test that the initialisation works without app config and with a |
| The Other Jimmy |
31:8ea194f6145b | 90 | specified top level directory |
| The Other Jimmy |
31:8ea194f6145b | 91 | |
| The Other Jimmy |
31:8ea194f6145b | 92 | :param mock_json_file_to_dict: mock of function json_file_to_dict |
| The Other Jimmy |
31:8ea194f6145b | 93 | :param _: patch of function _process_config_and_overrides (not tested) |
| The Other Jimmy |
31:8ea194f6145b | 94 | :return: |
| The Other Jimmy |
31:8ea194f6145b | 95 | """ |
| The Other Jimmy |
31:8ea194f6145b | 96 | directory = '.' |
| The Other Jimmy |
31:8ea194f6145b | 97 | path = os.path.join('.', 'mbed_app.json') |
| The Other Jimmy |
31:8ea194f6145b | 98 | mock_return = {'config': 'test'} |
| The Other Jimmy |
31:8ea194f6145b | 99 | mock_json_file_to_dict.return_value = mock_return |
| The Other Jimmy |
31:8ea194f6145b | 100 | mock_isfile.return_value = True |
| The Other Jimmy |
31:8ea194f6145b | 101 | |
| The Other Jimmy |
31:8ea194f6145b | 102 | config = Config(self.target, [directory]) |
| The Other Jimmy |
31:8ea194f6145b | 103 | |
| The Other Jimmy |
31:8ea194f6145b | 104 | mock_isfile.assert_called_with(path) |
| The Other Jimmy |
31:8ea194f6145b | 105 | mock_json_file_to_dict.assert_called_once_with(path) |
| The Other Jimmy |
31:8ea194f6145b | 106 | self.assertEqual(config.app_config_data, mock_return, |
| The Other Jimmy |
31:8ea194f6145b | 107 | "app_config_data should be set to the returned value") |
| The Other Jimmy |
31:8ea194f6145b | 108 | |
| The Other Jimmy |
31:8ea194f6145b | 109 | @patch.object(Config, '_process_config_and_overrides') |
| The Other Jimmy |
31:8ea194f6145b | 110 | @patch('tools.config.json_file_to_dict') |
| The Other Jimmy |
31:8ea194f6145b | 111 | def test_init_override_app_config(self, mock_json_file_to_dict, _): |
| The Other Jimmy |
31:8ea194f6145b | 112 | """ |
| The Other Jimmy |
31:8ea194f6145b | 113 | Test that the initialisation uses app_config instead of top_level_dir |
| The Other Jimmy |
31:8ea194f6145b | 114 | when both are specified |
| The Other Jimmy |
31:8ea194f6145b | 115 | |
| The Other Jimmy |
31:8ea194f6145b | 116 | :param mock_json_file_to_dict: mock of function json_file_to_dict |
| The Other Jimmy |
31:8ea194f6145b | 117 | :param _: patch of function _process_config_and_overrides (not tested) |
| The Other Jimmy |
31:8ea194f6145b | 118 | :return: |
| The Other Jimmy |
31:8ea194f6145b | 119 | """ |
| The Other Jimmy |
31:8ea194f6145b | 120 | app_config = "app_config" |
| The Other Jimmy |
31:8ea194f6145b | 121 | directory = '.' |
| The Other Jimmy |
31:8ea194f6145b | 122 | mock_return = {'config': 'test'} |
| The Other Jimmy |
31:8ea194f6145b | 123 | mock_json_file_to_dict.return_value = mock_return |
| The Other Jimmy |
31:8ea194f6145b | 124 | |
| The Other Jimmy |
31:8ea194f6145b | 125 | config = Config(self.target, [directory], app_config=app_config) |
| The Other Jimmy |
31:8ea194f6145b | 126 | |
| The Other Jimmy |
31:8ea194f6145b | 127 | mock_json_file_to_dict.assert_called_once_with(app_config) |
| The Other Jimmy |
31:8ea194f6145b | 128 | self.assertEqual(config.app_config_data, mock_return, |
| The Other Jimmy |
31:8ea194f6145b | 129 | "app_config_data should be set to the returned value") |
| The Other Jimmy |
31:8ea194f6145b | 130 | |
| The Other Jimmy |
31:8ea194f6145b | 131 | if __name__ == '__main__': |
| The Other Jimmy |
31:8ea194f6145b | 132 | unittest.main() |