def test_modify_existing_target(): """Set default targets file, then override base Target definition""" initial_target_json = """ { "Target": { "core": null, "default_toolchain": "ARM", "supported_toolchains": null, "extra_labels": [], "is_disk_virtual": false, "macros": [], "device_has": [], "features": [], "detect_code": [], "public": false, "default_lib": "std", "bootloader_supported": false }, "Test_Target": { "inherits": ["Target"], "core": "Cortex-M4", "supported_toolchains": ["ARM"] } }""" test_target_json = """ { "Target": { "core": "Cortex-M0", "default_toolchain": "GCC_ARM", "supported_toolchains": null, "extra_labels": [], "is_disk_virtual": false, "macros": [], "device_has": [], "features": [], "detect_code": [], "public": false, "default_lib": "std", "bootloader_supported": true } } """ with temp_target_file(initial_target_json, json_filename="targets.json") as targets_dir: Target.set_targets_json_location(os.path.join(targets_dir, "targets.json")) update_target_data() assert TARGET_MAP["Test_Target"].core == "Cortex-M4" assert TARGET_MAP["Test_Target"].default_toolchain == 'ARM' assert TARGET_MAP["Test_Target"].bootloader_supported == False with temp_target_file(test_target_json) as source_dir: Target.add_extra_targets(source_dir=source_dir) update_target_data() assert TARGET_MAP["Test_Target"].core == "Cortex-M4" # The existing target should not be modified by custom targets assert TARGET_MAP["Test_Target"].default_toolchain != 'GCC_ARM' assert TARGET_MAP["Test_Target"].bootloader_supported != True
def temp_target_file(self, extra_target, json_filename='custom_targets.json'): """Create an extra targets temp file in a context manager""" tempdir = tempfile.mkdtemp() try: targetfile = os.path.join(tempdir, json_filename) with open(targetfile, 'w') as f: f.write(extra_target) yield tempdir finally: # Reset extra targets Target.set_targets_json_location() # Delete temp files shutil.rmtree(tempdir)
def temp_target_file(extra_target, json_filename='custom_targets.json'): """Create an extra targets temp file in a context manager :param extra_target: the contents of the extra targets temp file """ tempdir = tempfile.mkdtemp() try: targetfile = os.path.join(tempdir, json_filename) with open(targetfile, 'w') as f: f.write(extra_target) yield tempdir finally: # Reset extra targets Target.set_targets_json_location() # Delete temp files shutil.rmtree(tempdir)