Create Mock notifier for use in tests

pull/6781/head
Jimmy Brisson 2018-05-01 09:10:59 -05:00
parent dc006d6aa3
commit 111f086796
3 changed files with 71 additions and 33 deletions

27
tools/notifier/mock.py Normal file
View File

@ -0,0 +1,27 @@
# mbed SDK
# Copyright (c) 2011-2013 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.
from __future__ import print_function, division, absolute_import
from . import Notifier
class MockNotifier(Notifier):
"""Collect notifications
"""
def __init__(self):
self.messages = []
def notify(self, message):
self.messages.append(message)

View File

@ -21,6 +21,7 @@ from mock import patch, MagicMock
from tools.build_api import prepare_toolchain, build_project, build_library,\
scan_resources
from tools.toolchains import TOOLCHAINS
from tools.notifier.mock import MockNotifier
"""
Tests for build_api.py
@ -28,6 +29,7 @@ Tests for build_api.py
make_mock_target = namedtuple(
"Target", "init_hooks name features core supported_toolchains")
class BuildApiTests(unittest.TestCase):
"""
Test cases for Build Api
@ -61,19 +63,19 @@ class BuildApiTests(unittest.TestCase):
@patch('tools.toolchains.mbedToolchain.dump_build_profile')
@patch('tools.utils.run_cmd', return_value=(b'', b'', 0))
def test_always_complete_build(self, *_):
with MagicMock() as notify:
toolchain = prepare_toolchain(self.src_paths, self.build_path, self.target,
self.toolchain_name, notify=notify)
notify = MockNotifier()
toolchain = prepare_toolchain(self.src_paths, self.build_path, self.target,
self.toolchain_name, notify=notify)
res = scan_resources(self.src_paths, toolchain)
res = scan_resources(self.src_paths, toolchain)
toolchain.RESPONSE_FILES=False
toolchain.config_processed = True
toolchain.config_file = "junk"
toolchain.compile_sources(res)
toolchain.RESPONSE_FILES=False
toolchain.config_processed = True
toolchain.config_file = "junk"
toolchain.compile_sources(res)
assert any('percent' in msg[0] and msg[0]['percent'] == 100.0
for _, msg, _ in notify.mock_calls if msg)
assert any('percent' in msg and msg['percent'] == 100.0
for msg in notify.messages if msg)
@patch('tools.build_api.Config')
@ -128,6 +130,7 @@ class BuildApiTests(unittest.TestCase):
:param __: mock of function scan_resources (not tested)
:return:
"""
notify = MockNotifier()
app_config = "app_config"
mock_exists.return_value = False
mock_prepare_toolchain().link_program.return_value = 1, 2
@ -135,7 +138,7 @@ class BuildApiTests(unittest.TestCase):
"Config", "has_regions name lib_config_data")(None, None, {})
build_project(self.src_paths, self.build_path, self.target,
self.toolchain_name, app_config=app_config)
self.toolchain_name, app_config=app_config, notify=notify)
args = mock_prepare_toolchain.call_args
self.assertTrue('app_config' in args[1],
@ -157,6 +160,7 @@ class BuildApiTests(unittest.TestCase):
:param __: mock of function scan_resources (not tested)
:return:
"""
notify = MockNotifier()
mock_exists.return_value = False
# Needed for the unpacking of the returned value
mock_prepare_toolchain().link_program.return_value = 1, 2
@ -164,7 +168,7 @@ class BuildApiTests(unittest.TestCase):
"Config", "has_regions name lib_config_data")(None, None, {})
build_project(self.src_paths, self.build_path, self.target,
self.toolchain_name)
self.toolchain_name, notify=notify)
args = mock_prepare_toolchain.call_args
self.assertTrue('app_config' in args[1],
@ -186,11 +190,12 @@ class BuildApiTests(unittest.TestCase):
:param __: mock of function scan_resources (not tested)
:return:
"""
notify = MockNotifier()
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)
self.toolchain_name, app_config=app_config, notify=notify)
args = mock_prepare_toolchain.call_args
self.assertTrue('app_config' in args[1],
@ -212,10 +217,11 @@ class BuildApiTests(unittest.TestCase):
:param __: mock of function scan_resources (not tested)
:return:
"""
notify = MockNotifier()
mock_exists.return_value = False
build_library(self.src_paths, self.build_path, self.target,
self.toolchain_name)
self.toolchain_name, notify=notify)
args = mock_prepare_toolchain.call_args
self.assertTrue('app_config' in args[1],

View File

@ -14,6 +14,7 @@ sys.path.insert(0, ROOT)
from tools.toolchains import TOOLCHAIN_CLASSES, LEGACY_TOOLCHAIN_NAMES,\
Resources, TOOLCHAIN_PATHS, mbedToolchain
from tools.targets import TARGET_MAP
from tools.notifier.mock import MockNotifier
ALPHABET = [char for char in printable if char not in [u'.', u'/']]
@ -32,7 +33,8 @@ def test_toolchain_profile_c(profile, source_file):
to_compile = os.path.join(*filename)
with patch('os.mkdir') as _mkdir:
for _, tc_class in TOOLCHAIN_CLASSES.items():
toolchain = tc_class(TARGET_MAP["K64F"], build_profile=profile)
toolchain = tc_class(TARGET_MAP["K64F"], build_profile=profile,
notify=MockNotifier())
toolchain.inc_md5 = ""
toolchain.build_dir = ""
toolchain.config = MagicMock(app_config_location=None)
@ -62,7 +64,8 @@ def test_toolchain_profile_cpp(profile, source_file):
to_compile = os.path.join(*filename)
with patch('os.mkdir') as _mkdir:
for _, tc_class in TOOLCHAIN_CLASSES.items():
toolchain = tc_class(TARGET_MAP["K64F"], build_profile=profile)
toolchain = tc_class(TARGET_MAP["K64F"], build_profile=profile,
notify=MockNotifier())
toolchain.inc_md5 = ""
toolchain.build_dir = ""
toolchain.config = MagicMock(app_config_location=None)
@ -92,7 +95,8 @@ def test_toolchain_profile_asm(profile, source_file):
to_compile = os.path.join(*filename)
with patch('os.mkdir') as _mkdir:
for _, tc_class in TOOLCHAIN_CLASSES.items():
toolchain = tc_class(TARGET_MAP["K64F"], build_profile=profile)
toolchain = tc_class(TARGET_MAP["K64F"], build_profile=profile,
notify=MockNotifier)
toolchain.inc_md5 = ""
toolchain.build_dir = ""
for parameter in profile['asm']:
@ -109,7 +113,7 @@ def test_toolchain_profile_asm(profile, source_file):
parameter)
for name, Class in TOOLCHAIN_CLASSES.items():
CLS = Class(TARGET_MAP["K64F"])
CLS = Class(TARGET_MAP["K64F"], notify=MockNotifier())
assert name == CLS.name or name == LEGACY_TOOLCHAIN_NAMES[CLS.name]
@given(fixed_dictionaries({
@ -128,7 +132,8 @@ def test_toolchain_profile_ld(profile, source_file):
with patch('os.mkdir') as _mkdir,\
patch('tools.toolchains.mbedToolchain.default_cmd') as _dflt_cmd:
for _, tc_class in TOOLCHAIN_CLASSES.items():
toolchain = tc_class(TARGET_MAP["K64F"], build_profile=profile)
toolchain = tc_class(TARGET_MAP["K64F"], build_profile=profile,
notify=MockNotifier())
toolchain.RESPONSE_FILES = False
toolchain.inc_md5 = ""
toolchain.build_dir = ""
@ -146,7 +151,7 @@ def test_toolchain_profile_ld(profile, source_file):
parameter)
for name, Class in TOOLCHAIN_CLASSES.items():
CLS = Class(TARGET_MAP["K64F"])
CLS = Class(TARGET_MAP["K64F"], notify=MockNotifier())
assert name == CLS.name or name == LEGACY_TOOLCHAIN_NAMES[CLS.name]
@ -155,20 +160,20 @@ def test_detect_duplicates(filenames):
c_sources = [os.path.join(name, "dupe.c") for name in filenames]
s_sources = [os.path.join(name, "dupe.s") for name in filenames]
cpp_sources = [os.path.join(name, "dupe.cpp") for name in filenames]
with MagicMock() as notify:
toolchain = TOOLCHAIN_CLASSES["ARM"](TARGET_MAP["K64F"], notify=notify)
res = Resources()
res.c_sources = c_sources
res.s_sources = s_sources
res.cpp_sources = cpp_sources
assert res.detect_duplicates(toolchain) == 1,\
"Not Enough duplicates found"
notify = MockNotifier()
toolchain = TOOLCHAIN_CLASSES["ARM"](TARGET_MAP["K64F"], notify=notify)
res = Resources()
res.c_sources = c_sources
res.s_sources = s_sources
res.cpp_sources = cpp_sources
assert res.detect_duplicates(toolchain) == 1,\
"Not Enough duplicates found"
_, (notification, _), _ = notify.mock_calls[1]
assert "dupe.o" in notification["message"]
assert "dupe.s" in notification["message"]
assert "dupe.c" in notification["message"]
assert "dupe.cpp" in notification["message"]
notification = notify.messages[0]
assert "dupe.o" in notification["message"]
assert "dupe.s" in notification["message"]
assert "dupe.c" in notification["message"]
assert "dupe.cpp" in notification["message"]
@given(text(alphabet=ALPHABET + ["/"], min_size=1))
@given(booleans())