Skip to content

Commit

Permalink
snap.py: fix snap.get optional key (#125)
Browse files Browse the repository at this point in the history
Fix snap.get method to handle the case where the key is falsy. Treat it
as no key provided, which results in the expected behavior to get all
values from snap configs.
  • Loading branch information
gboutry authored May 30, 2024
1 parent 522d54e commit 74ddb08
Show file tree
Hide file tree
Showing 2 changed files with 21 additions and 2 deletions.
7 changes: 5 additions & 2 deletions lib/charms/operator_libs_linux/v2/snap.py
Original file line number Diff line number Diff line change
Expand Up @@ -83,7 +83,7 @@

# Increment this PATCH version before using `charmcraft publish-lib` or reset
# to 0 if you are raising the major API version
LIBPATCH = 6
LIBPATCH = 7


# Regex to locate 7-bit C1 ANSI sequences
Expand Down Expand Up @@ -319,7 +319,10 @@ def get(self, key: Optional[str], *, typed: bool = False) -> Any:
Default is to return a string.
"""
if typed:
config = json.loads(self._snap("get", ["-d", key]))
args = ["-d"]
if key:
args.append(key)
config = json.loads(self._snap("get", args))
if key:
return config.get(key)
return config
Expand Down
16 changes: 16 additions & 0 deletions tests/integration/test_snap.py
Original file line number Diff line number Diff line change
Expand Up @@ -116,6 +116,22 @@ def test_snap_set_and_get_with_typed():

assert lxd.get("criu.enable", typed=True) == "true"
assert lxd.get("ceph.external", typed=True) == "false"
assert lxd.get(None, typed=True) == {
"true": True,
"false": False,
"integer": 1,
"float": 2.0,
"list": [1, 2.0, True, False, None],
"dict": {
"true": True,
"false": False,
"integer": 1,
"float": 2.0,
"list": [1, 2.0, True, False, None],
},
"criu": {"enable": "true"},
"ceph": {"external": "false"},
}


def test_snap_set_and_get_untyped():
Expand Down

0 comments on commit 74ddb08

Please sign in to comment.