def test_filter_by_all_libraries(self): """ Assert something """ res = Resources(MockNotifier()) res._add_labels('TARGET', ['K64F', 'FRDM']) for name, loc in SRC_PATHS.items(): res.add_directory(loc, into_path=name) res.filter_by_libraries(res.get_file_refs(FileType.JSON)) assert ("main.cpp" in res.get_file_names(FileType.CPP_SRC))
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] notify = MockNotifier() res = Resources(notify) res.add_files_to_type(FileType.C_SRC, c_sources) res.add_files_to_type(FileType.ASM_SRC, s_sources) res.add_files_to_type(FileType.CPP_SRC, cpp_sources) assert res.detect_duplicates() == 1,\ "Not Enough duplicates found" 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"]
def test_find_secure_image(): mock_notifier = MockNotifier() mock_resources = Resources(mock_notifier) ns_image_path = os.path.join('BUILD', 'TARGET_NS', 'app.bin') ns_test_path = os.path.join('BUILD', 'TARGET_NS', 'test.bin') config_s_image_name = 'target_config.bin' default_bin = os.path.join('prebuilt', config_s_image_name) test_bin = os.path.join('prebuilt', 'test.bin') with pytest.raises( Exception, match='ns_image_path and configured_s_image_path are mandatory'): find_secure_image(mock_notifier, mock_resources, None, None, FileType.BIN) find_secure_image(mock_notifier, mock_resources, ns_image_path, None, FileType.BIN) find_secure_image(mock_notifier, mock_resources, None, config_s_image_name, FileType.BIN) with pytest.raises(Exception, match='image_type must be of type BIN or HEX'): find_secure_image(mock_notifier, mock_resources, ns_image_path, config_s_image_name, None) find_secure_image(mock_notifier, mock_resources, ns_image_path, config_s_image_name, FileType.C_SRC) with pytest.raises(Exception, match='No image files found for this target'): find_secure_image(mock_notifier, mock_resources, ns_image_path, config_s_image_name, FileType.BIN) dummy_bin = os.path.join('path', 'to', 'dummy.bin') mock_resources.add_file_ref(FileType.BIN, dummy_bin, dummy_bin) with pytest.raises(Exception, match='Required secure image not found'): find_secure_image(mock_notifier, mock_resources, ns_image_path, config_s_image_name, FileType.BIN) mock_resources.add_file_ref(FileType.BIN, default_bin, default_bin) mock_resources.add_file_ref(FileType.BIN, test_bin, test_bin) secure_image = find_secure_image(mock_notifier, mock_resources, ns_image_path, config_s_image_name, FileType.BIN) assert secure_image == default_bin secure_image = find_secure_image(mock_notifier, mock_resources, ns_test_path, config_s_image_name, FileType.BIN) assert secure_image == test_bin
def test_basic_scan(self): """ Verify that the ordering of Target info addition and directory addition does not matter, so long as all the Target info and all directories are added. """ first = Resources(MockNotifier()) first._add_labels('TARGET', ['K64F']) first._add_labels('TARGET', ['FRDM']) for name, loc in SRC_PATHS.items(): print(name, loc) first.add_directory(loc, into_path=name) assert ("main.cpp" in first.get_file_names(FileType.CPP_SRC))
def test_always_complete_build(self, *_): notify = MockNotifier() toolchain = prepare_toolchain(self.src_paths, self.build_path, self.target, self.toolchain_name, notify=notify) res = Resources(MockNotifier()).scan_with_toolchain( self.src_paths, toolchain) toolchain.RESPONSE_FILES=False toolchain.config_processed = True toolchain.config_file = "junk" toolchain.compile_sources(res) assert any('percent' in msg and msg['percent'] == 100.0 for msg in notify.messages if msg)
def test_only_one_linker_script(self): """ Verify that when multiple linker scripts are added to a resource object, only the last one added is used. """ resources = Resources(MockNotifier()) linker_scripts = [ "first_linker_script.sct", "second_linker_script.sct" ] for linker_script in linker_scripts: resources.add_file_ref(FileType.LD_SCRIPT, linker_script, linker_script) assert (len(resources.get_file_refs(FileType.LD_SCRIPT)) == 1) assert (resources.get_file_refs( FileType.LD_SCRIPT)[-1].name == linker_scripts[-1]) assert (resources.get_file_refs( FileType.LD_SCRIPT)[-1].path == linker_scripts[-1])
config_header_dir = os.path.join(generated_path, "config-headers") pathlib.Path(config_header_dir).mkdir(parents=True, exist_ok=True) # create dir if not exists notifier = TerminalNotifier(True, False) # create a different toolchain for each profile so that we can detect the flags needed in each configuration profile_toolchains = [] for profile_json_path in profile_jsons: with open(profile_json_path) as profile_file: print(">> Collecting data for config " + profile_json_path) profile_data = json.load(profile_file) profile_toolchain = build_api.prepare_toolchain(src_paths=[mbed_os_dir], build_dir=config_header_dir, target=target_name, toolchain_name=toolchain_name, build_profile=[profile_data]) # each toolchain must then scan the mbed dir to pick up more configs resources = Resources(notifier).scan_with_toolchain(src_paths=[mbed_os_dir], toolchain=profile_toolchain, exclude=True) profile_toolchain.RESPONSE_FILES=False profile_toolchains.append(profile_toolchain) # Profiles seem to only set flags, so for the remaining operations we can use any toolchain toolchain = profile_toolchains[0] print("Generated config header: " + toolchain.get_config_header()) print("Using settings from these JSON files:\n " + "\n ".join(resources.get_file_paths(FileType.JSON))) # Write target header
def test_detect_duplicates(self): """ Verify that detect_duplicates finds all of the duplicate object files in the scanned tree. """ notifier = MockNotifier() first = Resources(notifier) first._add_labels('TARGET', ['K64F']) for name, loc in SRC_PATHS.items(): first.add_directory(loc, into_path=name) notifier.messages = [] first.detect_duplicates() error_messages = "\n".join(m['message'] for m in notifier.messages if m['type'] == 'tool_error') assert (" eggs.o " in error_messages) first._add_labels('TARGET', ['FRDM']) first.detect_duplicates() error_messages = "\n".join(m['message'] for m in notifier.messages if m['type'] == 'tool_error') assert (" eggs.o " in error_messages) assert (" not-main.o " in error_messages) assert (" main.o " in error_messages)
def test_add_target_info(self): """ Verify that the ordering of Target info addition and directory addition does not matter, so long as all the Target info and all directories are added. """ first = Resources(MockNotifier()) middle = Resources(MockNotifier()) last = Resources(MockNotifier()) first._add_labels('TARGET', ['K64F']) first._add_labels('TARGET', ['FRDM']) middle._add_labels('TARGET', ['FRDM']) for name, loc in SRC_PATHS.items(): first.add_directory(loc, into_path=name) middle.add_directory(loc, into_path=name) last.add_directory(loc, into_path=name) middle._add_labels('TARGET', ['K64F']) last._add_labels('TARGET', ['K64F']) last._add_labels('TARGET', ['FRDM']) for ftype in Resources.ALL_FILE_TYPES: assert (set(first.get_file_refs(ftype)) == set( middle.get_file_refs(ftype))) assert (set(last.get_file_refs(ftype)) == set( middle.get_file_refs(ftype)))
def test_filter_by_bm_lib(self): res = Resources(MockNotifier()) res._add_labels('TARGET', ['K64F', 'FRDM']) for name, loc in SRC_PATHS.items(): res.add_directory(loc, into_path=name) filter_by = [ ref for ref in res.get_file_refs(FileType.JSON) if join("platform", "bm", "mbed_lib.json") in ref.name ] res.filter_by_libraries(filter_by) assert ("main.cpp" not in res.get_file_names(FileType.CPP_SRC)) assert (join("mbed-os", "platform", "bm", "bm.cpp") in res.get_file_names(FileType.CPP_SRC)) assert (join("mbed-os", "TARGET_FRDM", "not-main.cpp") in res.get_file_names(FileType.CPP_SRC))
def test_filter_by_bm_lib(self): res = Resources(MockNotifier(), collect_ignores=True) res._add_labels('TARGET', ['K64F', 'FRDM']) for name, loc in SRC_PATHS.items(): res.add_directory(loc, into_path=name) libs = [ ref for ref in res.get_file_refs(FileType.JSON) if basename(ref.name) == MBED_LIB_FILENAME ] filter_by = [ ref for ref in libs if join("platform", "bm", "mbed_lib.json") in ref.name ] res.filter_by_libraries(filter_by) assert ("main.cpp" not in res.get_file_names(FileType.CPP_SRC)) assert (join("mbed-os", "platform", "bm", "bm.cpp") in res.get_file_names(FileType.CPP_SRC)) assert (join("mbed-os", "TARGET_FRDM", "not-main.cpp") in res.get_file_names(FileType.CPP_SRC)) inc_names = [dirname(name) or "." for name, _ in filter_by] assert (not any(d in res.ignored_dirs for d in inc_names)) excluded_libs = set(libs) - set(filter_by) exc_names = [dirname(name) or "." for name, _ in excluded_libs] assert (all(e in res.ignored_dirs for e in exc_names))
def test_filter_by_all_libraries(self): """ Assert something """ res = Resources(MockNotifier(), collect_ignores=True) res._add_labels('TARGET', ['K64F', 'FRDM']) for name, loc in SRC_PATHS.items(): res.add_directory(loc, into_path=name) res.filter_by_libraries(res.get_file_refs(FileType.JSON)) assert ("main.cpp" in res.get_file_names(FileType.CPP_SRC)) lib_dirs = (dirname(name) or "." for name in res.get_file_names(FileType.JSON)) assert (not any(dir in res.ignored_dirs for dir in lib_dirs))
# create a different toolchain for each profile so that we can detect the flags needed in each configuration profile_toolchains = [] for profile_json_path in profile_jsons: with open(profile_json_path) as profile_file: print(">> Collecting data for config " + profile_json_path) profile_data = json.load(profile_file) profile_toolchain = build_api.prepare_toolchain( src_paths=[mbed_os_dir], build_dir=config_header_dir, target=target_name, toolchain_name=toolchain_name, build_profile=[profile_data]) # each toolchain must then scan the mbed dir to pick up more configs resources = Resources(notifier).scan_with_toolchain( src_paths=[mbed_os_dir], toolchain=profile_toolchain, exclude=True) profile_toolchain.RESPONSE_FILES = False profile_toolchains.append(profile_toolchain) # Profiles seem to only set flags, so for the remaining operations we can use any toolchain toolchain = profile_toolchains[0] print("Generated config header: " + toolchain.get_config_header()) print("Using settings from these JSON files:\n " + "\n ".join(resources.get_file_paths(FileType.JSON))) # Write target header # ------------------------------------------------------------------------- target_header_path = os.path.join(config_header_dir, "mbed_target_config.h")
with open(profile_json_path) as profile_file: print(">> Collecting data for config " + profile_json_path) profile_data = json.load(profile_file) profile_toolchain = build_api.prepare_toolchain( src_paths=[mbed_os_dir], build_dir=config_header_dir, target=target_name, toolchain_name=toolchain_name, build_profile=[profile_data], app_config=app_config_path) # each toolchain must then scan the mbed dir to pick up more configs resources = Resources(notifier).scan_with_toolchain( src_paths=[mbed_os_dir], toolchain=profile_toolchain, exclude=True, mbedignore_path=mbedignore_file) is_custom_target = os.path.dirname( Target.get_json_target_data()[target_name] ["_from_file"]) == custom_target_dir if is_custom_target: # Add the directory of the custom target resources.add_directory( os.path.join(custom_target_dir, "TARGET_" + target_name)) # Filter out duplicate files (if a target overrides e.g. system_clock.c) dupe_objects, dupe_headers = resources._collect_duplicates( dict(), dict()) duplicates = {**dupe_objects, **dupe_headers}