diff --git a/tests/unit/test_snap.py b/tests/unit/test_snap.py index dc3d94b6..a6cebc2d 100644 --- a/tests/unit/test_snap.py +++ b/tests/unit/test_snap.py @@ -7,7 +7,7 @@ import json import unittest from subprocess import CalledProcessError -from typing import Iterable, Optional +from typing import Any, Dict, Iterable, Optional from unittest.mock import MagicMock, mock_open, patch import fake_snapd as fake_snapd @@ -842,7 +842,26 @@ def __getitem__(self, name: str) -> snap.Snap: self.assertIn("Failed to install or refresh snap(s): nothere", ctx.exception.message) def test_snap_get(self): + """Test the multiple different ways of calling the Snap.get function. + + Valid ways: + ("key", typed=False) -> returns a string + ("key", typed=True) -> returns value parsed from json + (None, typed=True) -> returns parsed json for all keys + ("", typed=True) -> returns parsed json for all keys + + An invalid key will raise an error if typed=False, but return None if typed=True. + """ def fake_snap(command: str, optargs: Optional[Iterable[str]] = None) -> str: + """Snap._snap would normally call subprocess.check_output(["snap", ...], ...) + + Here we only handle the "get" commands generated by Snap.get: + ["snap", "get", "-d"] -- equivalent to (None, typed=True) + ["snap", "get", "key"] -- equivalent to ("key", typed=False) + ["snap", "get", "-d" "key"] -- equivalent to ("key", typed=True) + + Values are returned from the local keys_and_values dict instead of calling out to snap. + """ assert command == "get" assert optargs is not None optargs = list(optargs) @@ -862,7 +881,7 @@ def fake_snap(command: str, optargs: Optional[Iterable[str]] = None) -> str: foo = snap.Snap("foo", snap.SnapState.Latest, "stable", "1", "classic") foo._snap = MagicMock(side_effect=fake_snap) - keys_and_values = { + keys_and_values: Dict[str, Any] = { "key_w_string_value": "string", "key_w_float_value": 4.2, "key_w_int_value": 13,