Skip to content

Commit

Permalink
fix(config): wrap snap config handler in snap check (#499)
Browse files Browse the repository at this point in the history
  • Loading branch information
lengau authored Oct 4, 2024
1 parent 940d8ac commit 52eb37c
Show file tree
Hide file tree
Showing 2 changed files with 12 additions and 0 deletions.
2 changes: 2 additions & 0 deletions craft_application/services/config.py
Original file line number Diff line number Diff line change
Expand Up @@ -91,6 +91,8 @@ class SnapConfigHandler(ConfigHandler):

def __init__(self, app: application.AppMetadata) -> None:
super().__init__(app)
if not snaphelpers.is_snap():
raise OSError("Not running as a snap.")
try:
self._snap = snaphelpers.SnapConfig()
except KeyError:
Expand Down
10 changes: 10 additions & 0 deletions tests/unit/services/test_config.py
Original file line number Diff line number Diff line change
Expand Up @@ -149,6 +149,7 @@ def test_craft_environment_handler_error(
],
)
def test_snap_config_handler_create_error(mocker, default_app_metadata, error):
mocker.patch("snaphelpers.is_snap", return_value=True)
mock_snap_config = mocker.patch(
"snaphelpers.SnapConfig",
side_effect=error,
Expand All @@ -159,6 +160,15 @@ def test_snap_config_handler_create_error(mocker, default_app_metadata, error):
mock_snap_config.assert_called_once_with()


def test_snap_config_handler_not_snap(mocker, default_app_metadata):
mock_is_snap = mocker.patch("snaphelpers.is_snap", return_value=False)

with pytest.raises(OSError, match="Not running as a snap."):
config.SnapConfigHandler(default_app_metadata)

mock_is_snap.asssert_called_once_with()


@given(
item=strategies.text(alphabet=string.ascii_letters + "_", min_size=1),
content=strategies.text(
Expand Down

0 comments on commit 52eb37c

Please sign in to comment.