Skip to content

Commit 111f086

Browse files
committed
Create Mock notifier for use in tests
1 parent dc006d6 commit 111f086

File tree

3 files changed

+71
-33
lines changed

3 files changed

+71
-33
lines changed

tools/notifier/mock.py

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
# mbed SDK
2+
# Copyright (c) 2011-2013 ARM Limited
3+
#
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+
#
8+
# http://www.apache.org/licenses/LICENSE-2.0
9+
#
10+
# Unless required by applicable law or agreed to in writing, software
11+
# distributed under the License is distributed on an "AS IS" BASIS,
12+
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
# See the License for the specific language governing permissions and
14+
# limitations under the License.
15+
16+
from __future__ import print_function, division, absolute_import
17+
18+
from . import Notifier
19+
20+
class MockNotifier(Notifier):
21+
"""Collect notifications
22+
"""
23+
def __init__(self):
24+
self.messages = []
25+
26+
def notify(self, message):
27+
self.messages.append(message)

tools/test/build_api/build_api_test.py

Lines changed: 20 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -21,13 +21,15 @@
2121
from tools.build_api import prepare_toolchain, build_project, build_library,\
2222
scan_resources
2323
from tools.toolchains import TOOLCHAINS
24+
from tools.notifier.mock import MockNotifier
2425

2526
"""
2627
Tests for build_api.py
2728
"""
2829
make_mock_target = namedtuple(
2930
"Target", "init_hooks name features core supported_toolchains")
3031

32+
3133
class BuildApiTests(unittest.TestCase):
3234
"""
3335
Test cases for Build Api
@@ -61,19 +63,19 @@ def tearDown(self):
6163
@patch('tools.toolchains.mbedToolchain.dump_build_profile')
6264
@patch('tools.utils.run_cmd', return_value=(b'', b'', 0))
6365
def test_always_complete_build(self, *_):
64-
with MagicMock() as notify:
65-
toolchain = prepare_toolchain(self.src_paths, self.build_path, self.target,
66-
self.toolchain_name, notify=notify)
66+
notify = MockNotifier()
67+
toolchain = prepare_toolchain(self.src_paths, self.build_path, self.target,
68+
self.toolchain_name, notify=notify)
6769

68-
res = scan_resources(self.src_paths, toolchain)
70+
res = scan_resources(self.src_paths, toolchain)
6971

70-
toolchain.RESPONSE_FILES=False
71-
toolchain.config_processed = True
72-
toolchain.config_file = "junk"
73-
toolchain.compile_sources(res)
72+
toolchain.RESPONSE_FILES=False
73+
toolchain.config_processed = True
74+
toolchain.config_file = "junk"
75+
toolchain.compile_sources(res)
7476

75-
assert any('percent' in msg[0] and msg[0]['percent'] == 100.0
76-
for _, msg, _ in notify.mock_calls if msg)
77+
assert any('percent' in msg and msg['percent'] == 100.0
78+
for msg in notify.messages if msg)
7779

7880

7981
@patch('tools.build_api.Config')
@@ -128,14 +130,15 @@ def test_build_project_app_config(self, mock_prepare_toolchain, mock_exists, _,
128130
:param __: mock of function scan_resources (not tested)
129131
:return:
130132
"""
133+
notify = MockNotifier()
131134
app_config = "app_config"
132135
mock_exists.return_value = False
133136
mock_prepare_toolchain().link_program.return_value = 1, 2
134137
mock_prepare_toolchain().config = namedtuple(
135138
"Config", "has_regions name lib_config_data")(None, None, {})
136139

137140
build_project(self.src_paths, self.build_path, self.target,
138-
self.toolchain_name, app_config=app_config)
141+
self.toolchain_name, app_config=app_config, notify=notify)
139142

140143
args = mock_prepare_toolchain.call_args
141144
self.assertTrue('app_config' in args[1],
@@ -157,14 +160,15 @@ def test_build_project_no_app_config(self, mock_prepare_toolchain, mock_exists,
157160
:param __: mock of function scan_resources (not tested)
158161
:return:
159162
"""
163+
notify = MockNotifier()
160164
mock_exists.return_value = False
161165
# Needed for the unpacking of the returned value
162166
mock_prepare_toolchain().link_program.return_value = 1, 2
163167
mock_prepare_toolchain().config = namedtuple(
164168
"Config", "has_regions name lib_config_data")(None, None, {})
165169

166170
build_project(self.src_paths, self.build_path, self.target,
167-
self.toolchain_name)
171+
self.toolchain_name, notify=notify)
168172

169173
args = mock_prepare_toolchain.call_args
170174
self.assertTrue('app_config' in args[1],
@@ -186,11 +190,12 @@ def test_build_library_app_config(self, mock_prepare_toolchain, mock_exists, _,
186190
:param __: mock of function scan_resources (not tested)
187191
:return:
188192
"""
193+
notify = MockNotifier()
189194
app_config = "app_config"
190195
mock_exists.return_value = False
191196

192197
build_library(self.src_paths, self.build_path, self.target,
193-
self.toolchain_name, app_config=app_config)
198+
self.toolchain_name, app_config=app_config, notify=notify)
194199

195200
args = mock_prepare_toolchain.call_args
196201
self.assertTrue('app_config' in args[1],
@@ -212,10 +217,11 @@ def test_build_library_no_app_config(self, mock_prepare_toolchain, mock_exists,
212217
:param __: mock of function scan_resources (not tested)
213218
:return:
214219
"""
220+
notify = MockNotifier()
215221
mock_exists.return_value = False
216222

217223
build_library(self.src_paths, self.build_path, self.target,
218-
self.toolchain_name)
224+
self.toolchain_name, notify=notify)
219225

220226
args = mock_prepare_toolchain.call_args
221227
self.assertTrue('app_config' in args[1],

tools/test/toolchains/api_test.py

Lines changed: 24 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@
1414
from tools.toolchains import TOOLCHAIN_CLASSES, LEGACY_TOOLCHAIN_NAMES,\
1515
Resources, TOOLCHAIN_PATHS, mbedToolchain
1616
from tools.targets import TARGET_MAP
17+
from tools.notifier.mock import MockNotifier
1718

1819
ALPHABET = [char for char in printable if char not in [u'.', u'/']]
1920

@@ -32,7 +33,8 @@ def test_toolchain_profile_c(profile, source_file):
3233
to_compile = os.path.join(*filename)
3334
with patch('os.mkdir') as _mkdir:
3435
for _, tc_class in TOOLCHAIN_CLASSES.items():
35-
toolchain = tc_class(TARGET_MAP["K64F"], build_profile=profile)
36+
toolchain = tc_class(TARGET_MAP["K64F"], build_profile=profile,
37+
notify=MockNotifier())
3638
toolchain.inc_md5 = ""
3739
toolchain.build_dir = ""
3840
toolchain.config = MagicMock(app_config_location=None)
@@ -62,7 +64,8 @@ def test_toolchain_profile_cpp(profile, source_file):
6264
to_compile = os.path.join(*filename)
6365
with patch('os.mkdir') as _mkdir:
6466
for _, tc_class in TOOLCHAIN_CLASSES.items():
65-
toolchain = tc_class(TARGET_MAP["K64F"], build_profile=profile)
67+
toolchain = tc_class(TARGET_MAP["K64F"], build_profile=profile,
68+
notify=MockNotifier())
6669
toolchain.inc_md5 = ""
6770
toolchain.build_dir = ""
6871
toolchain.config = MagicMock(app_config_location=None)
@@ -92,7 +95,8 @@ def test_toolchain_profile_asm(profile, source_file):
9295
to_compile = os.path.join(*filename)
9396
with patch('os.mkdir') as _mkdir:
9497
for _, tc_class in TOOLCHAIN_CLASSES.items():
95-
toolchain = tc_class(TARGET_MAP["K64F"], build_profile=profile)
98+
toolchain = tc_class(TARGET_MAP["K64F"], build_profile=profile,
99+
notify=MockNotifier)
96100
toolchain.inc_md5 = ""
97101
toolchain.build_dir = ""
98102
for parameter in profile['asm']:
@@ -109,7 +113,7 @@ def test_toolchain_profile_asm(profile, source_file):
109113
parameter)
110114

111115
for name, Class in TOOLCHAIN_CLASSES.items():
112-
CLS = Class(TARGET_MAP["K64F"])
116+
CLS = Class(TARGET_MAP["K64F"], notify=MockNotifier())
113117
assert name == CLS.name or name == LEGACY_TOOLCHAIN_NAMES[CLS.name]
114118

115119
@given(fixed_dictionaries({
@@ -128,7 +132,8 @@ def test_toolchain_profile_ld(profile, source_file):
128132
with patch('os.mkdir') as _mkdir,\
129133
patch('tools.toolchains.mbedToolchain.default_cmd') as _dflt_cmd:
130134
for _, tc_class in TOOLCHAIN_CLASSES.items():
131-
toolchain = tc_class(TARGET_MAP["K64F"], build_profile=profile)
135+
toolchain = tc_class(TARGET_MAP["K64F"], build_profile=profile,
136+
notify=MockNotifier())
132137
toolchain.RESPONSE_FILES = False
133138
toolchain.inc_md5 = ""
134139
toolchain.build_dir = ""
@@ -146,7 +151,7 @@ def test_toolchain_profile_ld(profile, source_file):
146151
parameter)
147152

148153
for name, Class in TOOLCHAIN_CLASSES.items():
149-
CLS = Class(TARGET_MAP["K64F"])
154+
CLS = Class(TARGET_MAP["K64F"], notify=MockNotifier())
150155
assert name == CLS.name or name == LEGACY_TOOLCHAIN_NAMES[CLS.name]
151156

152157

@@ -155,20 +160,20 @@ def test_detect_duplicates(filenames):
155160
c_sources = [os.path.join(name, "dupe.c") for name in filenames]
156161
s_sources = [os.path.join(name, "dupe.s") for name in filenames]
157162
cpp_sources = [os.path.join(name, "dupe.cpp") for name in filenames]
158-
with MagicMock() as notify:
159-
toolchain = TOOLCHAIN_CLASSES["ARM"](TARGET_MAP["K64F"], notify=notify)
160-
res = Resources()
161-
res.c_sources = c_sources
162-
res.s_sources = s_sources
163-
res.cpp_sources = cpp_sources
164-
assert res.detect_duplicates(toolchain) == 1,\
165-
"Not Enough duplicates found"
163+
notify = MockNotifier()
164+
toolchain = TOOLCHAIN_CLASSES["ARM"](TARGET_MAP["K64F"], notify=notify)
165+
res = Resources()
166+
res.c_sources = c_sources
167+
res.s_sources = s_sources
168+
res.cpp_sources = cpp_sources
169+
assert res.detect_duplicates(toolchain) == 1,\
170+
"Not Enough duplicates found"
166171

167-
_, (notification, _), _ = notify.mock_calls[1]
168-
assert "dupe.o" in notification["message"]
169-
assert "dupe.s" in notification["message"]
170-
assert "dupe.c" in notification["message"]
171-
assert "dupe.cpp" in notification["message"]
172+
notification = notify.messages[0]
173+
assert "dupe.o" in notification["message"]
174+
assert "dupe.s" in notification["message"]
175+
assert "dupe.c" in notification["message"]
176+
assert "dupe.cpp" in notification["message"]
172177

173178
@given(text(alphabet=ALPHABET + ["/"], min_size=1))
174179
@given(booleans())

0 commit comments

Comments
 (0)