Skip to content

Commit

Permalink
Merge pull request #80 from canonical/fix-falsy-config-defaults
Browse files Browse the repository at this point in the history
fix falsy config defaults
  • Loading branch information
PietroPasotti authored Nov 16, 2023
2 parents 70bb022 + 1c0ff40 commit f5ac9c8
Show file tree
Hide file tree
Showing 3 changed files with 10 additions and 3 deletions.
2 changes: 1 addition & 1 deletion pyproject.toml
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ build-backend = "setuptools.build_meta"
[project]
name = "ops-scenario"

version = "5.6"
version = "5.6.1"

authors = [
{ name = "Pietro Pasotti", email = "[email protected]" }
Expand Down
9 changes: 7 additions & 2 deletions scenario/mocking.py
Original file line number Diff line number Diff line change
Expand Up @@ -72,6 +72,9 @@ def send_signal(self, sig: Union[int, str]): # noqa: U100
raise NotImplementedError()


_NOT_GIVEN = object() # non-None default value sentinel


class _MockModelBackend(_ModelBackend):
def __init__(
self,
Expand Down Expand Up @@ -221,8 +224,10 @@ def config_get(self):

for key, value in charm_config["options"].items():
# if it has a default, and it's not overwritten from State, use it:
if key not in state_config and (default_value := value.get("default")):
state_config[key] = default_value
if key not in state_config:
default_value = value.get("default", _NOT_GIVEN)
if default_value is not _NOT_GIVEN: # accept False as default value
state_config[key] = default_value

return state_config # full config

Expand Down
2 changes: 2 additions & 0 deletions tests/test_e2e/test_config.py
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,7 @@ def test_config_get_default_from_meta(mycharm):
def check_cfg(charm: CharmBase):
assert charm.config["foo"] == "bar"
assert charm.config["baz"] == 2
assert charm.config["qux"] is False

trigger(
State(
Expand All @@ -53,6 +54,7 @@ def check_cfg(charm: CharmBase):
"options": {
"foo": {"type": "string"},
"baz": {"type": "integer", "default": 2},
"qux": {"type": "boolean", "default": False},
},
},
post_event=check_cfg,
Expand Down

0 comments on commit f5ac9c8

Please sign in to comment.