Skip to content

Commit ac51eb6

Browse files
ConorPKeeganConor Keegan
authored and
Conor Keegan
committed
Add unit tests for app_config
1 parent aafcf55 commit ac51eb6

File tree

3 files changed

+461
-0
lines changed

3 files changed

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

tools/test/config/config_test.py

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

0 commit comments

Comments
 (0)