1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272
|
import os
import unittest
from unittest.mock import patch
import pytest
import confuse
from . import _root
class EnvSourceTest(unittest.TestCase):
def setUp(self):
self.env_patcher = patch.dict("os.environ", {})
self.env_patcher.start()
def tearDown(self):
self.env_patcher.stop()
def test_prefix(self):
os.environ["TEST_FOO"] = "a"
os.environ["BAR"] = "b"
config = _root(confuse.EnvSource("TEST_"))
assert config.get() == {"foo": "a"}
def test_number_type_conversion(self):
os.environ["TEST_FOO"] = "1"
os.environ["TEST_BAR"] = "2.0"
config = _root(confuse.EnvSource("TEST_"))
foo = config["foo"].get()
bar = config["bar"].get()
assert isinstance(foo, int)
assert foo == 1
assert isinstance(bar, float)
assert bar == 2.0
def test_bool_type_conversion(self):
os.environ["TEST_FOO"] = "true"
os.environ["TEST_BAR"] = "FALSE"
config = _root(confuse.EnvSource("TEST_"))
assert config["foo"].get() is True
assert config["bar"].get() is False
def test_null_type_conversion(self):
os.environ["TEST_FOO"] = "null"
os.environ["TEST_BAR"] = ""
config = _root(confuse.EnvSource("TEST_"))
assert config["foo"].get() is None
assert config["bar"].get() is None
def test_unset_lower_config(self):
os.environ["TEST_FOO"] = "null"
config = _root({"foo": "bar"})
assert config["foo"].get() == "bar"
config.set(confuse.EnvSource("TEST_"))
assert config["foo"].get() is None
def test_sep_default(self):
os.environ["TEST_FOO__BAR"] = "a"
os.environ["TEST_FOO_BAZ"] = "b"
config = _root(confuse.EnvSource("TEST_"))
assert config["foo"]["bar"].get() == "a"
assert config["foo_baz"].get() == "b"
def test_sep_single_underscore_adjacent_seperators(self):
os.environ["TEST_FOO__BAR"] = "a"
os.environ["TEST_FOO_BAZ"] = "b"
config = _root(confuse.EnvSource("TEST_", sep="_"))
assert config["foo"][""]["bar"].get() == "a"
assert config["foo"]["baz"].get() == "b"
def test_nested(self):
os.environ["TEST_FOO__BAR"] = "a"
os.environ["TEST_FOO__BAZ__QUX"] = "b"
config = _root(confuse.EnvSource("TEST_"))
assert config["foo"]["bar"].get() == "a"
assert config["foo"]["baz"]["qux"].get() == "b"
def test_nested_rev(self):
# Reverse to ensure order doesn't matter
os.environ["TEST_FOO__BAZ__QUX"] = "b"
os.environ["TEST_FOO__BAR"] = "a"
config = _root(confuse.EnvSource("TEST_"))
assert config["foo"]["bar"].get() == "a"
assert config["foo"]["baz"]["qux"].get() == "b"
def test_nested_clobber(self):
os.environ["TEST_FOO__BAR"] = "a"
os.environ["TEST_FOO__BAR__BAZ"] = "b"
config = _root(confuse.EnvSource("TEST_"))
# Clobbered
assert config["foo"]["bar"].get() == {"baz": "b"}
assert config["foo"]["bar"]["baz"].get() == "b"
def test_nested_clobber_rev(self):
# Reverse to ensure order doesn't matter
os.environ["TEST_FOO__BAR__BAZ"] = "b"
os.environ["TEST_FOO__BAR"] = "a"
config = _root(confuse.EnvSource("TEST_"))
# Clobbered
assert config["foo"]["bar"].get() == {"baz": "b"}
assert config["foo"]["bar"]["baz"].get() == "b"
def test_lower_applied_after_prefix_match(self):
os.environ["TEST_FOO"] = "a"
config = _root(confuse.EnvSource("test_", lower=True))
assert config.get() == {}
def test_lower_already_lowercase(self):
os.environ["TEST_foo"] = "a"
config = _root(confuse.EnvSource("TEST_", lower=True))
assert config.get() == {"foo": "a"}
def test_lower_does_not_alter_value(self):
os.environ["TEST_FOO"] = "UPPER"
config = _root(confuse.EnvSource("TEST_", lower=True))
assert config.get() == {"foo": "UPPER"}
def test_lower_false(self):
os.environ["TEST_FOO"] = "a"
config = _root(confuse.EnvSource("TEST_", lower=False))
assert config.get() == {"FOO": "a"}
def test_handle_lists_good_list(self):
os.environ["TEST_FOO__0"] = "a"
os.environ["TEST_FOO__1"] = "b"
os.environ["TEST_FOO__2"] = "c"
config = _root(confuse.EnvSource("TEST_", handle_lists=True))
assert config["foo"].get() == ["a", "b", "c"]
def test_handle_lists_good_list_rev(self):
# Reverse to ensure order doesn't matter
os.environ["TEST_FOO__2"] = "c"
os.environ["TEST_FOO__1"] = "b"
os.environ["TEST_FOO__0"] = "a"
config = _root(confuse.EnvSource("TEST_", handle_lists=True))
assert config["foo"].get() == ["a", "b", "c"]
def test_handle_lists_nested_lists(self):
os.environ["TEST_FOO__0__0"] = "a"
os.environ["TEST_FOO__0__1"] = "b"
os.environ["TEST_FOO__1__0"] = "c"
config = _root(confuse.EnvSource("TEST_", handle_lists=True))
assert config["foo"].get() == [["a", "b"], ["c"]]
def test_handle_lists_bad_list_missing_index(self):
os.environ["TEST_FOO__0"] = "a"
os.environ["TEST_FOO__2"] = "b"
os.environ["TEST_FOO__3"] = "c"
config = _root(confuse.EnvSource("TEST_", handle_lists=True))
assert config["foo"].get() == {"0": "a", "2": "b", "3": "c"}
def test_handle_lists_bad_list_non_zero_start(self):
os.environ["TEST_FOO__1"] = "a"
os.environ["TEST_FOO__2"] = "b"
os.environ["TEST_FOO__3"] = "c"
config = _root(confuse.EnvSource("TEST_", handle_lists=True))
assert config["foo"].get() == {"1": "a", "2": "b", "3": "c"}
def test_handle_lists_bad_list_non_numeric(self):
os.environ["TEST_FOO__0"] = "a"
os.environ["TEST_FOO__ONE"] = "b"
os.environ["TEST_FOO__2"] = "c"
config = _root(confuse.EnvSource("TEST_", handle_lists=True))
assert config["foo"].get() == {"0": "a", "one": "b", "2": "c"}
def test_handle_lists_top_level_always_dict(self):
os.environ["TEST_0"] = "a"
os.environ["TEST_1"] = "b"
os.environ["TEST_2"] = "c"
config = _root(confuse.EnvSource("TEST_", handle_lists=True))
assert config.get() == {"0": "a", "1": "b", "2": "c"}
def test_handle_lists_not_a_list(self):
os.environ["TEST_FOO__BAR"] = "a"
os.environ["TEST_FOO__BAZ"] = "b"
config = _root(confuse.EnvSource("TEST_", handle_lists=True))
assert config["foo"].get() == {"bar": "a", "baz": "b"}
def test_parse_yaml_docs_scalar(self):
os.environ["TEST_FOO"] = "a"
config = _root(confuse.EnvSource("TEST_", parse_yaml_docs=True))
assert config["foo"].get() == "a"
def test_parse_yaml_docs_list(self):
os.environ["TEST_FOO"] = "[a, b]"
config = _root(confuse.EnvSource("TEST_", parse_yaml_docs=True))
assert config["foo"].get() == ["a", "b"]
def test_parse_yaml_docs_dict(self):
os.environ["TEST_FOO"] = "{bar: a, baz: b}"
config = _root(confuse.EnvSource("TEST_", parse_yaml_docs=True))
assert config["foo"].get() == {"bar": "a", "baz": "b"}
def test_parse_yaml_docs_nested(self):
os.environ["TEST_FOO"] = "{bar: [a, b], baz: {qux: c}}"
config = _root(confuse.EnvSource("TEST_", parse_yaml_docs=True))
assert config["foo"]["bar"].get() == ["a", "b"]
assert config["foo"]["baz"].get() == {"qux": "c"}
def test_parse_yaml_docs_number_conversion(self):
os.environ["TEST_FOO"] = "{bar: 1, baz: 2.0}"
config = _root(confuse.EnvSource("TEST_", parse_yaml_docs=True))
bar = config["foo"]["bar"].get()
baz = config["foo"]["baz"].get()
assert isinstance(bar, int)
assert bar == 1
assert isinstance(baz, float)
assert baz == 2.0
def test_parse_yaml_docs_bool_conversion(self):
os.environ["TEST_FOO"] = "{bar: true, baz: FALSE}"
config = _root(confuse.EnvSource("TEST_", parse_yaml_docs=True))
assert config["foo"]["bar"].get() is True
assert config["foo"]["baz"].get() is False
def test_parse_yaml_docs_null_conversion(self):
os.environ["TEST_FOO"] = "{bar: null, baz: }"
config = _root(confuse.EnvSource("TEST_", parse_yaml_docs=True))
assert config["foo"]["bar"].get() is None
assert config["foo"]["baz"].get() is None
def test_parse_yaml_docs_syntax_error(self):
os.environ["TEST_FOO"] = "{:}"
with pytest.raises(confuse.ConfigError, match="TEST_FOO"):
_root(confuse.EnvSource("TEST_", parse_yaml_docs=True))
def test_parse_yaml_docs_false(self):
os.environ["TEST_FOO"] = "{bar: a, baz: b}"
config = _root(confuse.EnvSource("TEST_", parse_yaml_docs=False))
assert config["foo"].get() == "{bar: a, baz: b}"
with pytest.raises(confuse.ConfigError):
config["foo"]["bar"].get()
class ConfigEnvTest(unittest.TestCase):
def setUp(self):
self.env_patcher = patch.dict(
"os.environ",
{
"TESTAPP_FOO": "a",
"TESTAPP_BAR__NESTED": "b",
"TESTAPP_BAZ_SEP_NESTED": "c",
"MYAPP_QUX_SEP_NESTED": "d",
},
)
self.env_patcher.start()
self.config = confuse.Configuration("TestApp", read=False)
def test_defaults(self):
self.config.set_env()
assert self.config.get() == {
"foo": "a",
"bar": {"nested": "b"},
"baz_sep_nested": "c",
}
def test_with_prefix(self):
self.config.set_env(prefix="MYAPP_")
assert self.config.get() == {"qux_sep_nested": "d"}
def test_with_sep(self):
self.config.set_env(sep="_sep_")
assert self.config.get() == {
"foo": "a",
"bar__nested": "b",
"baz": {"nested": "c"},
}
def test_with_prefix_and_sep(self):
self.config.set_env(prefix="MYAPP_", sep="_sep_")
assert self.config.get() == {"qux": {"nested": "d"}}
|