Skip to content

Commit

Permalink
Cover both failure paths when running snap commands
Browse files Browse the repository at this point in the history
  • Loading branch information
james-garner-canonical committed Sep 6, 2024
1 parent ec806bd commit cd21ab0
Showing 1 changed file with 17 additions and 1 deletion.
18 changes: 17 additions & 1 deletion tests/unit/test_snap.py
Original file line number Diff line number Diff line change
Expand Up @@ -810,7 +810,7 @@ def test_can_ensure_states(self, mock_subprocess):
self.assertTrue(baz.present)

@patch("charms.operator_libs_linux.v2.snap.subprocess.check_output")
def test_raises_snap_not_found_error(self, mock_subprocess):
def test_raises_snap_error_on_failed_subprocess(self, mock_subprocess: MagicMock):
def raise_error(cmd, **kwargs):
# If we can't find the snap, we should raise a CalledProcessError.
#
Expand All @@ -824,6 +824,22 @@ def raise_error(cmd, **kwargs):
self.assertEqual("<charms.operator_libs_linux.v2.snap.SnapError>", ctx.exception.name)
self.assertIn("Failed to install or refresh snap(s): nothere", ctx.exception.message)

def test_raises_snap_error_on_snap_not_found(self):
"""A cache failure will also ultimately result in a SnapError."""

class NotFoundCache:
cache = None

def __getitem__(self, name: str) -> snap.Snap:
raise snap.SnapNotFoundError()

with patch.object(snap, "_Cache", new=NotFoundCache()):
with self.assertRaises(snap.SnapError) as ctx:
snap.add("nothere")
repr(ctx.exception) # ensure custom __repr__ doesn't error
self.assertEqual("<charms.operator_libs_linux.v2.snap.SnapError>", ctx.exception.name)
self.assertIn("Failed to install or refresh snap(s): nothere", ctx.exception.message)

def test_snap_get(self):
def fake_snap(command: str, optargs: Optional[Iterable[str]] = None) -> str:
assert command == "get"
Expand Down

0 comments on commit cd21ab0

Please sign in to comment.