Secure your code as it's written. Use Snyk Code to scan source code in minutes - no build needed - and fix issues immediately.
def test_default_must_be_string():
config = ConfigManager([])
with pytest.raises(ConfigurationError):
assert config("DOESNOTEXIST", default=True)
def test_with_options():
"""Verify .with_options() restricts configuration"""
config = ConfigManager(
[ConfigDictEnv({"FOO_BAR": "a", "FOO_BAZ": "b", "BAR": "c", "BAZ": "d"})]
)
class SomeComponent(RequiredConfigMixin):
required_config = ConfigOptions()
required_config.add_option("baz", default="", doc="some help here", parser=str)
def __init__(self, config):
self.config = config.with_options(self)
# Create the component with regular config
comp = SomeComponent(config)
assert comp.config("baz") == "d"
with pytest.raises(ConfigurationError):
# This is not a valid option for this component
comp.config("bar")
def test_invalidvalueerror():
config = ConfigManager.from_dict({"foo_bar": "bat"})
with pytest.raises(InvalidValueError) as excinfo:
config("bar", namespace="foo", parser=bool)
assert excinfo.value.namespace == "foo"
assert excinfo.value.key == "bar"
assert excinfo.value.parser == bool
def test_alternate_keys_with_namespace(key, alternate_keys, expected):
config = ConfigManager.from_dict(
{
"COMMON_FOO": "common_foo_abc",
"FOO": "foo_abc",
"FOO_BAR": "foo_bar_abc",
"FOO_BAR_BAZ": "foo_bar_baz_abc",
}
)
config = config.with_namespace("FOO")
assert config(key, alternate_keys=alternate_keys) == expected
def test_bound_config(self):
config = ConfigManager.from_dict({"foo": 12345})
class ComponentA(RequiredConfigMixin):
required_config = ConfigOptions()
required_config.add_option("foo", parser=int)
required_config.add_option("bar", parser=int, default="1")
def __init__(self, config):
self.config = config.with_options(self)
comp = ComponentA(config)
assert list(comp.get_runtime_config()) == [
([], "foo", "12345", Option(key="foo", parser=int)),
([], "bar", "1", Option(key="bar", parser=int, default="1")),
]
def test_config_from_dict():
config = ConfigManager.from_dict({})
assert config("FOO", raise_error=False) is NO_VALUE
config = ConfigManager.from_dict({"FOO": "bar"})
assert config("FOO", raise_error=False) == "bar"
def test_doc():
config = ConfigManager.from_dict({"FOO_BAR": "bat"})
class SomeComponent(RequiredConfigMixin):
required_config = ConfigOptions()
required_config.add_option("foo_bar", parser=int, doc="omg!")
def __init__(self, config):
self.config = config.with_options(self)
comp = SomeComponent(config)
try:
# This throws an exception becase "bat" is not an int
comp.config("foo_bar")
except Exception as exc:
# We're going to lazily assert that omg! is in exc msg because if it
# is, it came from the option and that's what we want to know.
def test_get_namespace():
config = ConfigManager.from_dict(
{"FOO": "abc", "FOO_BAR": "abc", "FOO_BAR_BAZ": "abc"}
)
assert config.get_namespace() == []
ns_foo_config = config.with_namespace("foo")
assert ns_foo_config.get_namespace() == ["foo"]
ns_foo_bar_config = ns_foo_config.with_namespace("bar")
assert ns_foo_bar_config.get_namespace() == ["foo", "bar"]
def test_nested_options():
"""Verify nested BoundOptions works."""
config = ConfigManager.from_dict({})
class Foo(RequiredConfigMixin):
required_config = ConfigOptions()
required_config.add_option("option1", default="opt1default", parser=str)
class Bar(RequiredConfigMixin):
required_config = ConfigOptions()
required_config.add_option("option2", default="opt2default", parser=str)
config = ConfigManager.basic_config()
config = config.with_options(Foo)
config = config.with_options(Bar)
assert config("option2") == "opt2default"
with pytest.raises(ConfigurationError):
config("option1")
.. autocomponent:: test_sphinxext.ComponentOptionDoc
"""
)
assert parse(tmpdir, rst) == dedent(
"""\
component test_sphinxext.ComponentOptionDoc
Options:
**user** (*str*) -- ou812
"""
)
class ComponentOptionDocDefault(RequiredConfigMixin):
required_config = ConfigOptions()
required_config.add_option("user", doc="This is some docs.", default="ou812")
def test_option_doc_default(tmpdir):
rst = dedent(
"""\
.. autocomponent:: test_sphinxext.ComponentOptionDocDefault
"""
)
assert parse(tmpdir, rst) == dedent(
"""\
component test_sphinxext.ComponentOptionDocDefault
Options:
**user** (*str*) --