def test_expanduser_config_cake_processing(sample_configs):
    # We don't want to write in the test-running-user's home directory
    # so we just check that the expansion has happened.
    home_dir = path.expanduser("~")
    with pytest.raises(FileNotFoundError) as e:
        load_cake(sample_configs, "Expanduser Nonexistant File")
    assert home_dir in str(e.value)
def test_custom_config_parser(sample_configs):
    """Test that we can pass in our own config parser."""
    arbitrary_cake_name = "L1"

    # Make sure we are able to load this cake normally first (test self-check)
    load_cake(sample_configs, arbitrary_cake_name)

    # We are not testing ConfigParser itself, just that load_cake will use
    # the object we give it, so we give it something simple that we know
    # will break; we don't care exactly how it breaks, just that it does.
    with pytest.raises(Exception):
        load_cake(sample_configs, arbitrary_cake_name, into_config=object())
def test_env_var_can_define_new_section(sample_configs, monkeypatch):
    new_section = str(uuid4())
    new_key = str(uuid4())

    # First test that the section does not exist:
    with pytest.raises(NoSectionError):
        config = load_cake(sample_configs, "L1")
        config.get(new_section, new_key)

    new_value = "from environment creation"
    env_var_name = "CONFIG+{}+{}".format(new_section, new_key)
    monkeypatch.setenv(env_var_name, new_value)
    # Now check that it was created:
    config = load_cake(sample_configs, "L1")
    assert config.get(new_section, new_key) == new_value
def test_env_var_override_section_can_be_defined_in_a_layer(
    sample_configs, monkeypatch
):
    master = path.join(
        path.dirname(sample_configs), "master_without_env_override_section.config"
    )
    new_value = "key1 from environment override"
    monkeypatch.setenv("FROM_LAYER~section_1~key1", new_value)
    config = load_cake(master, "L3")
    assert config.get("section_1", "key1") == new_value
def test_config_ordering(sample_configs, config_name):
    """Test that  config files were loaded in the expected order."""

    config = load_cake(sample_configs, config_name)

    key1 = "layer2" if "L2" in config_name else "basic"
    target = "key1 from {} configuration".format(key1)
    assert target == config.get("Foundation", "key1")

    new_key = "layer2" if "L2" in config_name else "layer1"
    target = "new key from {} configuration".format(new_key)
    assert target == config.get("New Stuff", "new key")
def test_irrelevant_master_config_sections_are_not_in_config(sample_configs, cake_name):
    config = load_cake(sample_configs, cake_name)
    for cake_name in VALID_CAKE_NAMES:
        assert not config.has_section(cake_name)
def test_load_cake_includes_master_cake_keys_from_cake_section(
    sample_configs, config_name
):
    config = load_cake(sample_configs, config_name)
    assert config_name in config.defaults()["name"]
def test_munchifier(sample_configs):
    """Test conversion from config parser to munch."""
    # Munch has it's own tests, we're just spot checking our conversion function.
    config_by_dots = munchify_config(load_cake(sample_configs, "L1"))
    assert config_by_dots.Foundation.key1 == "key1 from basic configuration"
    assert config_by_dots.DEFAULT.key == "with default section value"
def test_broken_config(sample_configs):
    """Ensure expected error when a cake references a non-existant file."""
    with pytest.raises(FileNotFoundError):
        load_cake(sample_configs, "Nonexistant File")
def test_env_var_can_override_config_value(sample_configs, monkeypatch):
    new_value = "key1 from environment override"
    monkeypatch.setenv("CONFIG+Foundation+key1", new_value)
    config = load_cake(sample_configs, "L1")
    assert config.get("Foundation", "key1") == new_value
def test_load_config_relative_filenames(sample_configs, config_name):
    # This is so simple because sample_configs is already creating the configs
    # in a temporary directory, not the current working directory.
    config = load_cake(sample_configs, config_name)
    assert config is not None
    assert config.sections(), "Config data should have been loaded"
def test_fail_to_load_non_existant_config(sample_configs):
    with pytest.raises(NoSectionError):
        load_cake(sample_configs, str(uuid4()))