Пример #1
0
    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)
            },
        )
Пример #2
0
def _assemble_config_from_sources_and_lib_files(
        target_attributes: dict,
        mbed_lib_files: Iterable[Path],
        mbed_app_file: Optional[Path] = None) -> Config:
    previous_cumulative_data = None
    target_source = Source.from_target(target_attributes)
    current_cumulative_data = CumulativeData.from_sources([target_source])
    while previous_cumulative_data != current_cumulative_data:
        current_labels = current_cumulative_data.labels | current_cumulative_data.extra_labels
        filtered_files = _filter_files(mbed_lib_files, current_labels,
                                       current_cumulative_data.features,
                                       current_cumulative_data.components)
        mbed_lib_sources = [
            Source.from_mbed_lib(file, current_labels)
            for file in filtered_files
        ]
        all_sources = [target_source] + mbed_lib_sources
        if mbed_app_file:
            all_sources = all_sources + [
                Source.from_mbed_app(mbed_app_file, current_labels)
            ]

        previous_cumulative_data = current_cumulative_data
        current_cumulative_data = CumulativeData.from_sources(all_sources)

    _update_target_attributes(target_attributes, current_cumulative_data)

    return Config.from_sources(all_sources)
Пример #3
0
    def test_ignores_present_option(self):
        source = SourceFactory(config={
            "mbed_component.present": {
                "help": "Mbed Component",
                "value": True
            }
        })

        config = Config.from_sources([source])

        self.assertFalse(config.options)
Пример #4
0
    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")
Пример #5
0
def _assemble_config_from_sources_and_lib_files(
        target_source: Source,
        mbed_lib_files: Iterable[Path],
        mbed_app_file: Optional[Path] = None) -> Config:
    previous_cumulative_data = None
    current_cumulative_data = CumulativeData.from_sources([target_source])
    while previous_cumulative_data != current_cumulative_data:
        filtered_files = _filter_files(mbed_lib_files, current_cumulative_data)
        mbed_lib_sources = [
            Source.from_mbed_lib(file, current_cumulative_data.labels)
            for file in filtered_files
        ]
        all_sources = [target_source] + mbed_lib_sources
        if mbed_app_file:
            all_sources = all_sources + [
                Source.from_mbed_app(mbed_app_file,
                                     current_cumulative_data.labels)
            ]
        previous_cumulative_data = current_cumulative_data
        current_cumulative_data = CumulativeData.from_sources(all_sources)

    return Config.from_sources(all_sources)
Пример #6
0
 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])
Пример #7
0
    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])
Пример #8
0
    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])
Пример #9
0
    def test_assembles_config_using_all_relevant_files(self):
        target = {
            "config": {
                "foo": None
            },
            "macros": {},
            "labels": set("A"),
            "extra_labels": set(),
            "features": set("RED"),
            "components": set(),
            "c_lib": "std",
            "printf_lib": "minimal-printf",
        }
        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, 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(
                [Source.from_target(target)] + mbed_lib_sources +
                [mbed_app_source])

            self.assertEqual(subject, expected_config)