def test_builds_config_from_sources(self): source_a = SourceFactory(config={ "bool": True, "string": "foo" }, macros=["MACRO_A=A"]) source_b = SourceFactory(config={"number": 1}, overrides={"bool": False}, macros=["MACRO_B=B"]) config = Config.from_sources([source_a, source_b]) self.assertEqual( config.options, { "bool": Option.build("bool", True, source_a).set_value( False, source_b), "number": Option.build("number", 1, source_b), "string": Option.build("string", "foo", source_a), }, ) self.assertEqual( config.macros, { "MACRO_A": Macro.build("MACRO_A=A", source_a), "MACRO_B": Macro.build("MACRO_B=B", source_b) }, )
def test_sanitizes_booleans_into_ints(self): option = Option.build(key="foo", data="some-data", source=SourceFactory()) option.set_value(False, SourceFactory()) self.assertEqual(option.value, 0)
def test_assembles_metadata_from_sources(self): for field in fields(CumulativeData): with self.subTest(f"Assemble {field.name}"): source_a = SourceFactory(overrides={f"target.{field.name}": ["FOO"]}) source_b = SourceFactory(overrides={f"target.{field.name}_add": ["BAR", "BAZ"]}) source_c = SourceFactory(overrides={f"target.{field.name}_remove": ["BAR"]}) config_cumulative_data = CumulativeData.from_sources([source_a, source_b, source_c]) self.assertEqual(getattr(config_cumulative_data, field.name), {"FOO", "BAZ"})
def test_keeps_old_option_data(self): source_a = SourceFactory( config={"bool": { "help": "A simple bool", "value": True }}) source_b = SourceFactory(overrides={"bool": False}) config = Config.from_sources([source_a, source_b]) self.assertEqual(config.options["bool"].help_text, "A simple bool")
def test_builds_macros_without_value(self): source = SourceFactory() macro = Macro.build("FOO", source) self.assertEqual( macro, Macro(name="FOO", value=None, set_by=source.human_name), )
def test_sanitizes_booleans_into_ints(self): source = SourceFactory() option = Option.build(key="target.is-cool", data={"value": True}, source=source) self.assertEqual(option.value, 1) option = Option.build(key="target.is-cool", data=False, source=source) self.assertEqual(option.value, 0)
def test_assembles_metadata_from_sources(self): for field in fields(BootloaderOverrides): with self.subTest(f"Assemble {field.name}"): source = SourceFactory(overrides={f"target.{field.name}": ["FOO"]}) bootloader_overrides = BootloaderOverrides.from_sources([source]) self.assertEqual( getattr(bootloader_overrides, field.name), BootloaderOverride(set_by=source.human_name, name=field.name, value=["FOO"]), )
def test_assembles_config_using_all_relevant_files(self): target_source = SourceFactory(config={"target.foo": "foo"}, overrides={"target.labels": ["A"]}) mbed_lib_files = [ { "path": Path("TARGET_A", "mbed_lib.json"), "json_contents": { "name": "a", "config": {"number": 123}, "target_overrides": {"*": {"target.features_add": ["RED"]}}, }, }, { "path": Path("subdir", "FEATURE_RED", "mbed_lib.json"), "json_contents": { "name": "red", "config": {"bool": False}, "target_overrides": { "A": {"bool": True, "target.features_add": ["BLUE"], "target.components_add": ["LEG"]} }, "macros": ["RED_MACRO"], }, }, { "path": Path("COMPONENT_LEG", "mbed_lib.json"), "json_contents": {"name": "leg", "config": {"number-of-fingers": 5}, "macros": ["LEG_MACRO"]}, }, ] unused_mbed_lib_file = { "path": Path("subdir", "FEATURE_BROWN", "mbed_lib.json"), "json_contents": { "name": "brown", "target_overrides": {"*": {"red.bool": "DON'T USE ME"}}, "macros": ["DONT_USE_THIS_MACRO"], }, } mbed_app_file = { "path": Path("mbed_app.json"), "json_contents": {"target_overrides": {"*": {"target.foo": "bar"}}}, } with TemporaryDirectory() as directory: created_mbed_lib_files = create_files(directory, mbed_lib_files) created_mbed_app_file = create_files(directory, [mbed_app_file])[0] create_files(directory, [unused_mbed_lib_file]) subject = _assemble_config_from_sources_and_lib_files( target_source, find_files("mbed_lib.json", Path(directory)), created_mbed_app_file ) mbed_lib_sources = [Source.from_mbed_lib(Path(directory, file), ["A"]) for file in created_mbed_lib_files] mbed_app_source = Source.from_mbed_app(created_mbed_app_file, ["A"]) expected_config = Config.from_sources([target_source] + mbed_lib_sources + [mbed_app_source]) self.assertEqual(subject, expected_config)
def test_generates_macro_name_if_not_in_data(self): source = SourceFactory() data = { "value": 123, "help": "some help text", } option = Option.build(key="update-client.storage-size", data=data, source=source) self.assertEqual(option.macro_name, "MBED_CONF_UPDATE_CLIENT_STORAGE_SIZE")
def test_builds_option_from_config_data(self): source = SourceFactory() data = { "value": 123, "help": "some help text", "macro_name": "FOO_MACRO", } option = Option.build(key="target.stack-size", data=data, source=source) self.assertEqual( option, Option( key="target.stack-size", value=data["value"], help_text=data["help"], macro_name=data["macro_name"], set_by=source.human_name, ), )
def test_does_not_explode_on_override_keys_used_by_other_parsers(self): for key in CUMULATIVE_OVERRIDE_KEYS_IN_SOURCE + BOOTLOADER_OVERRIDE_KEYS_IN_SOURCE: with self.subTest("Ignores override key '{key}'"): source_a = SourceFactory() source_b = SourceFactory(overrides={key: "boom?"}) Config.from_sources([source_a, source_b])
def test_raises_when_trying_to_override_existing_macro(self): source_a = SourceFactory(macros=["MACRO_A=X"]) source_b = SourceFactory(macros=["MACRO_A=Y"]) with self.assertRaises(ValueError): Config.from_sources([source_a, source_b])
def test_raises_when_trying_to_override_unset_option(self): source_a = SourceFactory(config={"bool": True}) source_b = SourceFactory(overrides={"string": "hello"}) with self.assertRaises(ValueError): Config.from_sources([source_a, source_b])