Knight KE / Mbed OS Game_Master
Embed: (wiki syntax)

« Back to documentation index

Show/hide line numbers api_test.py Source File

api_test.py

00001 """Tests for the toolchain sub-system"""
00002 import sys
00003 import os
00004 from string import printable
00005 from copy import deepcopy
00006 from mock import MagicMock, patch
00007 from hypothesis import given, settings
00008 from hypothesis.strategies import text, lists, fixed_dictionaries, booleans
00009 
00010 ROOT = os.path.abspath(os.path.join(os.path.dirname(__file__), "..", "..",
00011                                     ".."))
00012 sys.path.insert(0, ROOT)
00013 
00014 from tools.toolchains import TOOLCHAIN_CLASSES, LEGACY_TOOLCHAIN_NAMES,\
00015     Resources, TOOLCHAIN_PATHS, mbedToolchain
00016 from tools.targets import TARGET_MAP
00017 from tools.notifier.mock import MockNotifier
00018 
00019 ALPHABET = [char for char in printable if char not in [u'.', u'/', u'\\']]
00020 
00021 @given(fixed_dictionaries({
00022     'common': lists(text()),
00023     'c': lists(text()),
00024     'cxx': lists(text()),
00025     'asm': lists(text()),
00026     'ld': lists(text())}),
00027        lists(text(min_size=1, alphabet=ALPHABET), min_size=1))
00028 def test_toolchain_profile_c (profile, source_file):
00029     """Test that the appropriate profile parameters are passed to the
00030     C compiler"""
00031     filename = deepcopy(source_file)
00032     filename[-1] += ".c"
00033     to_compile = os.path.join(*filename)
00034     with patch('os.mkdir') as _mkdir:
00035         for _, tc_class in TOOLCHAIN_CLASSES.items():
00036             toolchain = tc_class(TARGET_MAP["K64F"], build_profile=profile,
00037                                  notify=MockNotifier())
00038             toolchain.inc_md5 = ""
00039             toolchain.build_dir = ""
00040             toolchain.config = MagicMock(app_config_location=None)
00041             for parameter in profile['c'] + profile['common']:
00042                 assert any(parameter in cmd for cmd in toolchain.cc), \
00043                     "Toolchain %s did not propagate arg %s" % (toolchain.name,
00044                                                             parameter)
00045             compile_command = toolchain.compile_command(to_compile,
00046                                                         to_compile + ".o", [])
00047             for parameter in profile['c'] + profile['common']:
00048                 assert any(parameter in cmd for cmd in compile_command), \
00049                     "Toolchain %s did not propagate arg %s" % (toolchain.name,
00050                                                             parameter)
00051 
00052 @given(fixed_dictionaries({
00053     'common': lists(text()),
00054     'c': lists(text()),
00055     'cxx': lists(text()),
00056     'asm': lists(text()),
00057     'ld': lists(text())}),
00058        lists(text(min_size=1, alphabet=ALPHABET), min_size=1))
00059 def test_toolchain_profile_cpp (profile, source_file):
00060     """Test that the appropriate profile parameters are passed to the
00061     C++ compiler"""
00062     filename = deepcopy(source_file)
00063     filename[-1] += ".cpp"
00064     to_compile = os.path.join(*filename)
00065     with patch('os.mkdir') as _mkdir:
00066         for _, tc_class in TOOLCHAIN_CLASSES.items():
00067             toolchain = tc_class(TARGET_MAP["K64F"], build_profile=profile,
00068                                  notify=MockNotifier())
00069             toolchain.inc_md5 = ""
00070             toolchain.build_dir = ""
00071             toolchain.config = MagicMock(app_config_location=None)
00072             for parameter in profile['cxx'] + profile['common']:
00073                 assert any(parameter in cmd for cmd in toolchain.cppc), \
00074                     "Toolchain %s did not propagate arg %s" % (toolchain.name,
00075                                                             parameter)
00076             compile_command = toolchain.compile_command(to_compile,
00077                                                         to_compile + ".o", [])
00078             for parameter in profile['cxx'] + profile['common']:
00079                 assert any(parameter in cmd for cmd in compile_command), \
00080                     "Toolchain %s did not propagate arg %s" % (toolchain.name,
00081                                                             parameter)
00082 
00083 @given(fixed_dictionaries({
00084     'common': lists(text()),
00085     'c': lists(text()),
00086     'cxx': lists(text()),
00087     'asm': lists(text()),
00088     'ld': lists(text())}),
00089        lists(text(min_size=1, alphabet=ALPHABET), min_size=1))
00090 def test_toolchain_profile_asm (profile, source_file):
00091     """Test that the appropriate profile parameters are passed to the
00092     Assembler"""
00093     filename = deepcopy(source_file)
00094     filename[-1] += ".s"
00095     to_compile = os.path.join(*filename)
00096     with patch('os.mkdir') as _mkdir:
00097         for _, tc_class in TOOLCHAIN_CLASSES.items():
00098             toolchain = tc_class(TARGET_MAP["K64F"], build_profile=profile,
00099                                  notify=MockNotifier)
00100             toolchain.inc_md5 = ""
00101             toolchain.build_dir = ""
00102             for parameter in profile['asm']:
00103                 assert any(parameter in cmd for cmd in toolchain.asm), \
00104                     "Toolchain %s did not propagate arg %s" % (toolchain.name,
00105                                                                parameter)
00106             compile_command = toolchain.compile_command(to_compile,
00107                                                         to_compile + ".o", [])
00108             if not compile_command:
00109                 assert compile_command, to_compile
00110             for parameter in profile['asm']:
00111                 assert any(parameter in cmd for cmd in compile_command), \
00112                     "Toolchain %s did not propagate arg %s" % (toolchain.name,
00113                                                                parameter)
00114 
00115     for name, Class in  TOOLCHAIN_CLASSES.items():
00116         CLS = Class(TARGET_MAP["K64F"], notify=MockNotifier())
00117         assert name == CLS.name or name ==  LEGACY_TOOLCHAIN_NAMES[CLS.name]
00118 
00119 @given(fixed_dictionaries({
00120     'common': lists(text()),
00121     'c': lists(text()),
00122     'cxx': lists(text()),
00123     'asm': lists(text()),
00124     'ld': lists(text(min_size=1))}),
00125        lists(text(min_size=1, alphabet=ALPHABET), min_size=1))
00126 def test_toolchain_profile_ld (profile, source_file):
00127     """Test that the appropriate profile parameters are passed to the
00128     Linker"""
00129     filename = deepcopy(source_file)
00130     filename[-1] += ".o"
00131     to_compile = os.path.join(*filename)
00132     with patch('os.mkdir') as _mkdir,\
00133          patch('tools.toolchains.mbedToolchain.default_cmd') as _dflt_cmd:
00134         for _, tc_class in TOOLCHAIN_CLASSES.items():
00135             toolchain = tc_class(TARGET_MAP["K64F"], build_profile=profile,
00136                                  notify=MockNotifier())
00137             toolchain.RESPONSE_FILES = False
00138             toolchain.inc_md5 = ""
00139             toolchain.build_dir = ""
00140             for parameter in profile['ld']:
00141                 assert any(parameter in cmd for cmd in toolchain.ld), \
00142                     "Toolchain %s did not propagate arg %s" % (toolchain.name,
00143                                                                parameter)
00144             toolchain.link(to_compile + ".elf", [to_compile], [], [], None)
00145             compile_cmd = _dflt_cmd.call_args_list
00146             if not compile_cmd:
00147                 assert compile_cmd, to_compile
00148             for parameter in profile['ld']:
00149                 assert any(parameter in cmd[0][0] for cmd in compile_cmd), \
00150                     "Toolchain %s did not propagate arg %s" % (toolchain.name,
00151                                                                parameter)
00152 
00153     for name, Class in  TOOLCHAIN_CLASSES.items():
00154         CLS = Class(TARGET_MAP["K64F"], notify=MockNotifier())
00155         assert name == CLS.name or name ==  LEGACY_TOOLCHAIN_NAMES[CLS.name]
00156 
00157 
00158 @given(lists(text(alphabet=ALPHABET, min_size=1), min_size=1))
00159 def test_detect_duplicates(filenames):
00160     c_sources = [os.path.join(name, "dupe.c") for name in filenames]
00161     s_sources = [os.path.join(name, "dupe.s") for name in filenames]
00162     cpp_sources = [os.path.join(name, "dupe.cpp") for name in filenames]
00163     notify = MockNotifier()
00164     toolchain = TOOLCHAIN_CLASSES["ARM"](TARGET_MAP["K64F"], notify=notify)
00165     res = Resources()
00166     res.c_sources = c_sources
00167     res.s_sources = s_sources
00168     res.cpp_sources = cpp_sources
00169     assert res.detect_duplicates(toolchain) == 1,\
00170         "Not Enough duplicates found"
00171 
00172     notification = notify.messages[0]
00173     assert "dupe.o" in notification["message"]
00174     assert "dupe.s" in notification["message"]
00175     assert "dupe.c" in notification["message"]
00176     assert "dupe.cpp" in notification["message"]
00177 
00178 @given(text(alphabet=ALPHABET + [os.sep], min_size=1))
00179 @given(booleans())
00180 @given(booleans())
00181 @settings(max_examples=20)
00182 def test_path_specified_gcc(gcc_loc, exists_at_loc, exists_in_path):
00183     with patch('tools.toolchains.gcc.exists') as _exists:
00184         with patch('tools.toolchains.gcc.find_executable') as _find:
00185             _exists.return_value = exists_at_loc
00186             _find.return_value = exists_in_path
00187             TOOLCHAIN_PATHS['GCC_ARM'] = gcc_loc
00188             toolchain_class = TOOLCHAIN_CLASSES["GCC_ARM"]
00189             found_p = toolchain_class.check_executable()
00190             assert found_p == (exists_at_loc or exists_in_path)
00191             if exists_at_loc:
00192                 assert TOOLCHAIN_PATHS['GCC_ARM'] == gcc_loc
00193             elif exists_in_path:
00194                 assert TOOLCHAIN_PATHS['GCC_ARM'] == ''