Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
11 changes: 8 additions & 3 deletions snap_http/api/snaps.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
from typing import Dict, List, Literal, Optional, Union
from typing import Dict, List, Literal, Optional, Union, Iterable

from .. import http
from ..types import FileUpload, FormData, SnapdResponse
Expand Down Expand Up @@ -244,12 +244,17 @@ def unhold_all(names: List[str]) -> SnapdResponse:
return http.post("/snaps", {"action": "unhold", "snaps": names})


def list() -> SnapdResponse:
def list(*, snaps: Optional[Iterable[str]] = None) -> SnapdResponse:
"""GETs a list of installed snaps.

This stomps on builtins.list, so please import it namespaced.

:param snaps: An optional iterable of snap names by which to filter.
"""
return http.get("/snaps")
query_params = {}
if snaps is not None:
query_params["snaps"] = ",".join(snaps)
return http.get("/snaps", query_params=query_params)


def list_all() -> SnapdResponse:
Expand Down
21 changes: 21 additions & 0 deletions tests/integration/test_snaps.py
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
import pytest

import snap_http

from tests.utils import get_snap_details, is_snap_installed, remove_assertion, wait_for
Expand All @@ -9,6 +11,25 @@ def test_list_snaps():
assert "snapd" in installed_snaps


@pytest.mark.parametrize(
"snaps",
[
{"snapd"},
{"snapd", "core24"},
]
)
def test_list_snaps_with_names(snaps):
"""Test listing snaps, filtering by name."""
listed_snaps = {snap["name"] for snap in snap_http.list(snaps=snaps).result}
assert listed_snaps == snaps


def test_list_snaps_with_names_no_match():
"""Test listing snaps, filtering by name."""
listed_snaps = {snap["name"] for snap in snap_http.list(snaps=["othsnaushoeatnlxbuehceaoemtapgi0ceuobhtkerobhgcio"]).result}
assert listed_snaps == set()


def test_list_all_snaps():
"""Test listing snaps."""
installed_snaps = {snap["name"] for snap in snap_http.list_all().result}
Expand Down
15 changes: 12 additions & 3 deletions tests/unit/api/test_snaps.py
Original file line number Diff line number Diff line change
Expand Up @@ -877,7 +877,14 @@ def mock_post(path, body):
_ = api.unhold_all(["placeholder1", "placeholder2"])


def test_list(monkeypatch):
@pytest.mark.parametrize(
("snaps", "expected_params"),
[
(None, {}),
(["snapd", "core24"], {"snaps": "snapd,core24"}),
]
)
def test_list(monkeypatch, snaps, expected_params):
"""`api.list` returns a `types.SnapdResponse`."""
mock_response = types.SnapdResponse(
type="sync",
Expand All @@ -886,14 +893,16 @@ def test_list(monkeypatch):
result=[{"title": "placeholder1"}, {"title": "placeholder2"}],
)

def mock_get(path):
def mock_get(path, query_params):
assert path == "/snaps"

assert query_params == expected_params

return mock_response

monkeypatch.setattr(http, "get", mock_get)

result = api.list()
result = api.list(snaps=snaps)

assert result == mock_response

Expand Down