Ejemplo n.º 1
0
def test_cast_from_conf_file():
    def to_string(x):
        return str(x)

    to_string.cast_from_config = True

    assert easyconf.Config(EXAMPLE_CONF).BANANAS(cast=to_string) == "1"
    assert easyconf.Config(EXAMPLE_CONF).BANANAS(cast=str) == 1
Ejemplo n.º 2
0
def test_initial():
    with tempfile.TemporaryDirectory() as temp_dir:
        path = str(Path(temp_dir) / "example.yml")
        config = easyconf.Config(path)
        assert config.CHOCOLATE(initial="yum") == "yum"
        with open(path) as f:
            assert f.read() == "CHOCOLATE: yum\n"
Ejemplo n.º 3
0
def test_default_with_help():
    with tempfile.TemporaryDirectory() as temp_dir:
        path = str(Path(temp_dir) / "example.yml")
        config = easyconf.Config(path)
        config.CHOCOLATE(default="yum", help="Chocolate preference")
        with open(path) as f:
            assert f.read() == "# Chocolate preference\n# CHOCOLATE: yum\n{}\n"
Ejemplo n.º 4
0
def test_invalid_frame(mocker):
    with tempfile.TemporaryDirectory() as temp_dir:
        mocked_inspect = mocker.patch("easyconf.generators.gitignore.inspect")
        mocked_inspect.currentframe().f_back.f_back = None
        path = Path(temp_dir)
        easyconf.Config(str(path / "example.yml"))
        assert not (path / ".gitignore").exists()
Ejemplo n.º 5
0
def test_multiple():
    with tempfile.TemporaryDirectory() as temp_dir:
        path = str(Path(temp_dir) / "example.yml")
        config = easyconf.Config(path)
        assert config.FIRST(default="1") == "1"
        assert config.SECOND(initial="2", help="Second") == "2"
        assert config.THIRD(default="3", help="third") == "3"
        assert config.FOURTH(initial=lambda: "four") == "four"
        assert config.FIFTH(default="5") == "5"
        assert config.LAST(default="end") == "end"
        with open(path) as f:
            assert (f.read() == """\
# FIRST: '1'

# Second
SECOND: '2'

# third
# THIRD: '3'

FOURTH: four

# FIFTH: '5'

# LAST: end
""")
Ejemplo n.º 6
0
def init_settings(_default_config_params):
    p = globals().get("__package__")
    local_config_dir = next(
        iter(
            [
                d / "config"
                for d in list(Path(os.path.abspath(__file__)).parents)[:2]
                if (d / "config").exists()
            ]
        ),
        None,
    )
    if local_config_dir and local_config_dir.exists():
        logger.info(f"found local config directory in {local_config_dir}")
        base_config_dir = local_config_dir
    else:
        base_config_dir = user_config_dir(p)
        logger.info(
            f"no local config directory, creating settings folder {base_config_dir}"
        )
    conf_path = Path(base_config_dir) / Path(f"{p}.yaml")
    logger.info(f"using config {conf_path}")
    if not conf_path.exists():
        conf_path.parent.mkdir(parents=True, exist_ok=True)
        logger.info(f"creating {conf_path}")
    conf_obj = easyconf.Config(str(conf_path))
    conf = {}
    for param, initials in _default_config_params.items():
        if initials.get("default", None) is None:
            initials["default"] = initials["initial"]
        conf[param] = functools.partial(getattr(conf_obj, param), **initials)()
    logger.info(f"configuration loaded {conf}")
    # check for new settings
    old_settings = conf_obj._config
    new_temp = Path(base_config_dir) / Path(
        f"{next(tempfile._get_candidate_names())}.yaml"
    )
    new_settings = easyconf.Config(str(new_temp))
    for k, v in conf.items():
        getattr(new_settings, k)(initial=v)
        if k not in old_settings:
            logger.warning(f"new setting added to config {k}")
    del conf_obj
    del new_settings
    new_temp.replace(conf_path)
    return conf
Ejemplo n.º 7
0
def test_no_git(mocker):
    with tempfile.TemporaryDirectory() as temp_dir:
        mocked_inspect = mocker.patch("easyconf.generators.gitignore.inspect")
        mocked_inspect.currentframe().f_back.f_back.f_code.co_filename = str(
            Path(temp_dir) / "settings.py")
        path = Path(temp_dir)
        easyconf.Config(str(path / "example.yml"))
        assert not (path / ".gitignore").exists()
Ejemplo n.º 8
0
def test_make_first():
    with tempfile.TemporaryDirectory() as temp_dir:
        bad = str(Path(temp_dir) / "bad" / "example.yml")
        good = str(Path(temp_dir) / "example.yml")
        following = str(Path(temp_dir) / "example2.yml")
        config = easyconf.Config([bad, good])
        config.CHOCOLATE(initial="yum")
        assert os.path.exists(good)
        assert not os.path.exists(bad)
        assert not os.path.exists(following)
Ejemplo n.º 9
0
def test_existing_no_newline(mocker):
    with tempfile.TemporaryDirectory() as temp_dir:
        mocked_inspect = mocker.patch("easyconf.generators.gitignore.inspect")
        mocked_inspect.currentframe().f_back.f_back.f_code.co_filename = str(
            Path(temp_dir) / "settings.py")
        git.Repo.init(temp_dir)
        path = Path(temp_dir)
        gitignore = path / ".gitignore"
        with open(gitignore, "w") as f:
            f.write("test")
        easyconf.Config(str(path / "example.yml"))
        with open(gitignore, "r") as f:
            assert f.read() == "test\nexample.yml\n"
Ejemplo n.º 10
0
def test_default():
    with tempfile.TemporaryDirectory() as temp_dir:
        path = str(Path(temp_dir) / "example.yml")
        config = easyconf.Config(path)
        assert config.CHOCOLATE(default="yum") == "yum"
        assert config.APPLES(default=0) == 0
        with open(path) as f:
            assert (f.read() == """\
# CHOCOLATE: yum

# APPLES: 0
{}
""")
Ejemplo n.º 11
0
def test_cast():
    with tempfile.TemporaryDirectory() as temp_dir:
        path = str(Path(temp_dir) / "example.yml")
        config = easyconf.Config(path)
        assert config.CASTED(default="1",
                             cast=lambda x: {"inner": int(x)}) == {
                                 "inner": 1
                             }
        with open(path) as f:
            assert (f.read() == """\
# CASTED:
#   inner: 1
{}
""")
Ejemplo n.º 12
0
def test_cant_make(caplog):
    logging.getLogger().info("test")
    with tempfile.TemporaryDirectory() as temp_dir:
        bad = str(Path(temp_dir) / "bad" / "example.yml")
        config = easyconf.Config(bad)
        assert config.TEST(default=1) == 1

        logged = [record[1:] for record in caplog.record_tuples]
        assert logged == [
            (
                logging.WARNING,
                "No configuration files found, attempting to create one...",
            ),
            (logging.WARNING, "Could not create a configuration file"),
        ]
Ejemplo n.º 13
0
def test_no_git_executable(mocker):
    import easyconf.generators.gitignore

    try:
        original_git = easyconf.generators.gitignore.git
        easyconf.generators.gitignore.git = None

        mocked_inspect = mocker.patch("easyconf.generators.gitignore.inspect")
        with tempfile.TemporaryDirectory() as temp_dir:
            git.Repo.init(temp_dir)
            path = Path(temp_dir)
            easyconf.Config(str(path / "example.yml"))
            assert not mocked_inspect.called
    finally:
        easyconf.generators.gitignore.git = original_git
Ejemplo n.º 14
0
def test_environment(monkeypatch):
    monkeypatch.setenv("AGE", "21")
    monkeypatch.setenv("HEIGHT", "160")
    with tempfile.TemporaryDirectory() as temp_dir:
        path = str(Path(temp_dir) / "example.yml")
        config = easyconf.Config(path)
        assert config.AGE(default=35) == 21
        assert config.HEIGHT(default=150, help="Height in CMs") == 160
        with open(path) as f:
            assert (f.read() == """\
# Set to initial environment variable value
AGE: 21

# Height in CMs
# (set to initial environment variable value)
HEIGHT: 160
""")
Ejemplo n.º 15
0
def test_conf_file(monkeypatch):
    config = easyconf.Config(EXAMPLE_CONF)
    assert config.BANANAS() == 1
    assert config.APPLES() is None
    monkeypatch.setenv("APPLES", "ten")
    assert config.APPLES() == "ten"
Ejemplo n.º 16
0
def test_required_var():
    with tempfile.TemporaryDirectory() as temp_dir:
        path = str(Path(temp_dir) / "example.yml")
        config = easyconf.Config(path)
        with pytest.raises(RequiredConfigVarMissing):
            config.CHOCOLATE()
Ejemplo n.º 17
0
def test_conf_file_from_json(monkeypatch):
    config = easyconf.Config(EXAMPLE_JSON_CONF)
    assert config.BANANAS() == 2
    assert config.APPLES() == "three"
    monkeypatch.setenv("APPLES", "ten")
    assert config.APPLES() == "ten"
Ejemplo n.º 18
0
def test_cast(monkeypatch):
    monkeypatch.setenv("APPLES", "1")
    assert easyconf.Config().APPLES(cast=int) == 1
Ejemplo n.º 19
0
def test_env_list(monkeypatch):
    monkeypatch.setenv("FRUIT", "Apples,Bananas")
    assert easyconf.Config().FRUIT(cast=list) == ["Apples", "Bananas"]
Ejemplo n.º 20
0
def test_find_any_existing():
    with tempfile.TemporaryDirectory() as temp_dir:
        nonexistant = str(Path(temp_dir) / "example.yml")
        config = easyconf.Config([nonexistant, EXAMPLE_CONF])
        assert config.BANANAS() == 1
Ejemplo n.º 21
0
def test_disabled():
    with tempfile.TemporaryDirectory() as temp_dir:
        nonexistant = str(Path(temp_dir) / "example.yml")
        config = easyconf.Config(nonexistant, generate=False)
        assert config.APPLES(default=1) == 1
        assert not os.path.exists(nonexistant)
Ejemplo n.º 22
0
def test_env_cast_from_default(monkeypatch):
    monkeypatch.setenv("APPLES", "2")
    assert easyconf.Config().APPLES(default=1) == 2
Ejemplo n.º 23
0
def test_required_var():
    config = easyconf.Config(EXAMPLE_CONF)
    with pytest.raises(RequiredConfigVarMissing):
        config.CHOCOLATE()
Ejemplo n.º 24
0
def test_default_var():
    config = easyconf.Config(EXAMPLE_CONF)
    assert config.BANANAS(default=2) == 1
    assert config.CHOCOLATE(default="yum") == "yum"
Ejemplo n.º 25
0
def test_cast_default():
    assert easyconf.Config().APPLES(default="1", cast=int) == 1
Ejemplo n.º 26
0
def test_env_bool(monkeypatch):
    monkeypatch.setenv("TEST", "False")
    assert easyconf.Config().TEST(cast=bool) is False
Ejemplo n.º 27
0
def test_conf_file_from_env(monkeypatch):
    monkeypatch.setenv("CONF_FILE", EXAMPLE_CONF)
    config = easyconf.Config(file_env_var="CONF_FILE")
    assert config.BANANAS() == 1
Ejemplo n.º 28
0
def test_env(monkeypatch):
    monkeypatch.setenv("TEST", "True")
    assert easyconf.Config().TEST() == "True"
Ejemplo n.º 29
0
def test_initial_var():
    config = easyconf.Config(EXAMPLE_CONF)
    assert config.BANANAS(initial=2) == 1