Example #1
0
def test_basic_build_with_two_mapping_sources_with_object() -> None:
    """Check that a second source overlays a value."""
    from valiant.config import ConfigBuilder
    from valiant.config.source import MappingSource

    builder = ConfigBuilder()
    builder.add_source(
        MappingSource({
            "name": "Fred",
            "pets": {
                "fido": "dog",
                "mittens": "cat"
            }
        }))
    builder.add_source(
        MappingSource({
            "name": "Jane",
            "pets": {
                "flipper": "fish"
            }
        }))
    builder.add_source(MappingSource({"name": "Dewi"}))
    c = builder.build()
    assert c
    assert c["name"] == "Dewi"
    assert c["pets"]["flipper"] == "fish"
    assert "fido" not in c["pets"]
    assert "mittens" not in c["pets"]
    # The dictionary read is recorded only once
    assert c["_builder_metadata"]["build_results"]["dictionary"] == "Read"
Example #2
0
def test_basic_build_with_two_mapping_sources() -> None:
    """Check that a second source overlays a value."""
    from valiant.config import ConfigBuilder
    from valiant.config.source import MappingSource

    builder = ConfigBuilder()
    builder.add_source(MappingSource({"name": "Fred"}))
    builder.add_source(MappingSource({"name": "Jane"}))
    c = builder.build()
    assert c
    assert c["name"] == "Jane"
    # The dictionary read is recorded only once
    assert c["_builder_metadata"]["build_results"]["dictionary"] == "Read"
Example #3
0
def test_basic_build_with_two_mapping_sources_that_are_filtered_out() -> None:
    """Test handling of a filter that throws everything away."""
    from valiant.config import ConfigBuilder
    from valiant.config.source import MappingSource

    builder = ConfigBuilder(filter=lambda d: {})
    assert builder

    builder.add_source(MappingSource({"name": "Fred"}))
    builder.add_source(MappingSource({"name": "Jane"}))
    c = builder.build()

    assert c
    assert "name" not in c
    assert c["_builder_metadata"]["build_results"]["dictionary"] == "Read"
Example #4
0
def test_basic_build_with_single_mapping_source() -> None:
    """Basic test that a source config is reflected in a build."""
    from valiant.config import ConfigBuilder
    from valiant.config.source import MappingSource

    builder = ConfigBuilder()
    builder.add_source(MappingSource({"name": "Fred"}))
    c = builder.build()
    assert c
    assert c["name"] == "Fred"
    assert c["_builder_metadata"]["build_results"]["dictionary"] == "Read"
Example #5
0
def test_no_repo_config(config_default_builder: ConfigBuilder) -> None:
    """Expect failure when there are no repository_configurations."""
    from valiant.config.source import MappingSource

    config_default_builder.add_source(
        MappingSource({"tool": {
            "valiant": {
                "repository_configurations": {}
            }
        }}))
    with pytest.raises(ValueError):
        get_config_instance(config_default_builder)
Example #6
0
def test_no_default_repo(
        config_default_builder: ConfigBuilder) -> None:  # noqa:ANN001
    """Expect failure when the default_repo isn't listed in repository_configurations."""
    from valiant.config.source import MappingSource

    config_default_builder.add_source(
        MappingSource(
            {"tool": {
                "valiant": {
                    "default_repository": "random_repo"
                }
            }}))
    with pytest.raises(ValueError):
        get_config_instance(config_default_builder)
Example #7
0
def test_basic_overlay_mapping(config_default_builder: ConfigBuilder,
                               datafiles: py.path) -> None:
    """Manually adds a config dict with a logging_configuration_file."""
    from valiant.config.source import MappingSource

    config_default_builder.add_source(
        MappingSource({
            "tool": {
                "valiant": {
                    "logging_configuration_file": Path(FIXTURE_DIR,
                                                       "logging.conf")
                }
            }
        }))
    c = get_config_instance(config_default_builder)
    assert c.logging_configuration_file == Path(FIXTURE_DIR, "logging.conf")
Example #8
0
    def __init__(
        self,
        initial_data: Optional[ConfigMap] = None,
        vendor: str = "unknown",
        app: str = "unknown",
        version: str = "0",
        include_pyproject: bool = True,
        include_user_config: bool = True,
        include_site_config: bool = True,
        filter: Callable[[ConfigMap], ConfigMap] = lambda d: d,
    ) -> None:
        """Initialiser.

        Args:
            initial_data: A map of config settings that forms a baseline.
            vendor: The application vendor.
                This is generally used by filesystem-based config for directory layout
            app: The application name.
                This is generally used by filesystem-based config for directory layout
            version: The application version.
                This is generally used by filesystem-based config for directory layout
            include_pyproject: True if the local pyproject.toml file is to be read
            include_user_config: True if the user's config directory is to be used
            include_site_config: True if the system's config directory is to be used
            filter: A callable (e.g. function) that accepts a dictionary as input
                and returns the filtered dictionary as output
        """
        from pathlib import Path
        from appdirs import AppDirs
        from valiant.config.source import TomlSource, MappingSource

        super().__init__(filter)
        if initial_data:
            self.add_source(MappingSource(initial_data))

        dirs = AppDirs(appname=app, appauthor=vendor, version=version)
        if include_site_config:
            self.add_source(
                TomlSource(Path(dirs.site_config_dir, "config.toml")))

        if include_user_config:
            self.add_source(
                TomlSource(Path(dirs.user_config_dir, "config.toml")))

        if include_pyproject:
            self.add_source(TomlSource(Path.cwd() / "pyproject.toml"))