Exemple #1
0
def test_int_from_environ():
    c = Config(env_prefix='PREFIX_')
    c.init('NUM', int, 666)
    with env(PREFIX_NUM='42'):
        c.load()

    assert c['NUM'] == 42
Exemple #2
0
def test_prefixed_variables_override():
    c = Config(env_prefix='PREFIX_')
    c.init('HOST', str, '0.0.0.0')
    with env(PREFIX_HOST='1.2.3.4'):
        c.load()

    assert c['HOST'] == '1.2.3.4'
Exemple #3
0
def test_bool_like_true(value):
    c = Config(env_prefix='')
    c.init('VALUE', bool_like)
    with env(VALUE=value):
        c.load()

    assert c['VALUE'] is True
Exemple #4
0
def test_nonprefixed_variables_dont_override_nonempty_prefix():
    c = Config(env_prefix='PREFIX_')
    c.init('HOST', str, '0.0.0.0')
    with env(HOST='1.2.3.4'):
        c.load()

    assert c['HOST'] == '0.0.0.0'
Exemple #5
0
def test_env_update():
    try:
        os.environ["FOO"] = "BAR"
        with env(FOO="BARBAR"):
            assert os.environ["FOO"] == "BARBAR"
        assert os.environ["FOO"] == "BAR"
    finally:
        os.environ.pop("FOO", None)
Exemple #6
0
def test_env_remove():
    try:
        os.environ["FOO"] = "BAR"
        with env(FOO=None):
            assert "FOO" not in os.environ
        assert os.environ["FOO"] == "BAR"
    finally:
        os.environ.pop("FOO", None)
Exemple #7
0
def test_env_remove():
    try:
        os.environ["FOO"] = "BAR"
        with env(FOO=None):
            assert "FOO" not in os.environ
        assert os.environ["FOO"] == "BAR"
    finally:
        os.environ.pop("FOO", None)
Exemple #8
0
def test_python_bool_type(value):
    c = Config(env_prefix='')
    with pytest.warns(UserWarning):
        c.init('VALUE', bool)
    with env(VALUE=value):
        c.load()

    assert c['VALUE'] is True
Exemple #9
0
def test_env_update():
    try:
        os.environ["FOO"] = "BAR"
        with env(FOO="BARBAR"):
            assert os.environ["FOO"] == "BARBAR"
        assert os.environ["FOO"] == "BAR"
    finally:
        os.environ.pop("FOO", None)
Exemple #10
0
def test_json():
    c = Config(env_prefix='')
    c.init('VALUE1', json)
    c.init('VALUE2', json)
    with env(VALUE1='{"aha": [1, 1.2, false]}', VALUE2='1'):
        c.load()

    assert c['VALUE1'] == {'aha': [1, 1.2, False]}
    assert c['VALUE2'] == 1
Exemple #11
0
def test_python_numeric_types_converters(_type):
    c = Config(env_prefix='')
    c.init('VALUE', _type)
    with env(VALUE='1'):
        c.load()

    assert c['VALUE'] == _type('1')
    assert c['VALUE'] == 1
    assert type(c['VALUE']) == _type
Exemple #12
0
def test_custom_converter():
    def custom(value):
        return ...

    c = Config(env_prefix='')
    c.init('VALUE', custom)
    with env(VALUE='42'):
        c.load()
    assert c['VALUE'] is ...
Exemple #13
0
def test_get_app_dir_env_set():
    path = "/app"
    with env(KNOWHOW_HOME=path):
        assert conf.get_app_dir() == path
Exemple #14
0
def test_get_app_dir_windows():
    appdata = r"C:\\Users\\Foo\\AppData\\Roaming"
    expected = os.path.join(appdata, "knowhow")
    with env(APPDATA=appdata):
        assert conf.get_app_dir(platform="win32") == expected
Exemple #15
0
def test_get_app_dir_other():
    for platform in ["linux", "cygwin", "darwin"]:
        home = "/home/foo"
        expected = os.path.join(home, ".knowhow")
        with env(HOME=home):
            assert conf.get_app_dir(platform=platform) == expected
Exemple #16
0
def test_get_config_using_env(conf_path):
    with env(KNOWHOW_CONF=conf_path):
        c = conf.get_config()
        assert c.get("main", "data") == "/app/data"
Exemple #17
0
def test_env_add():
    assert "FOO" not in os.environ
    with env(FOO="BAR"):
        assert os.environ["FOO"] == "BAR"
    assert "FOO" not in os.environ
Exemple #18
0
def test_get_data_dir_env_unset(conf_path):
    conf_dir = os.path.dirname(conf_path)
    with env(KNOWHOW_DATA=None, KNOWHOW_HOME=conf_dir):
        assert conf.get_data_dir() == "/app/data"
        assert conf.get_data_dir(app_dir=conf_dir) == "/app/data"
Exemple #19
0
def test_get_data_dir_env_set():
    path = "/data"
    with env(KNOWHOW_DATA=path):
        assert conf.get_data_dir() == path
Exemple #20
0
def test_custom_anonymous_converter():
    c = Config(env_prefix='')
    c.init('VALUE', lambda x:...)
    with env(VALUE='42'):
        c.load()
    assert c['VALUE'] is ...
Exemple #21
0
def test_env_add():
    assert "FOO" not in os.environ
    with env(FOO="BAR"):
        assert os.environ["FOO"] == "BAR"
    assert "FOO" not in os.environ