Skip to content

Commit

Permalink
fix: inject aliased snaps into build environment
Browse files Browse the repository at this point in the history
Signed-off-by: Callahan Kovacs <callahankovacs@gmail.com>
  • Loading branch information
mr-cal committed Sep 13, 2024
1 parent 2bda257 commit e68695e
Show file tree
Hide file tree
Showing 2 changed files with 49 additions and 23 deletions.
16 changes: 10 additions & 6 deletions craft_application/services/provider.py
Original file line number Diff line number Diff line change
Expand Up @@ -98,12 +98,16 @@ def setup(self) -> None:
self.environment[f"{scheme.upper()}_PROXY"] = value

if self._install_snap:
channel = (
None
if util.is_running_from_snap(self._app.name)
else os.getenv("CRAFT_SNAP_CHANNEL", "latest/stable")
)
self.snaps.append(Snap(name=self._app.name, channel=channel, classic=True))
if util.is_running_from_snap(self._app.name):
# use the aliased name of the snap when injecting
name = os.getenv("SNAP_INSTANCE_NAME", self._app.name)
channel = None
else:
# use the snap name when installing from the store
name = self._app.name
channel = os.getenv("CRAFT_SNAP_CHANNEL", "latest/stable")

self.snaps.append(Snap(name=name, channel=channel, classic=True))

@contextlib.contextmanager
def instance(
Expand Down
56 changes: 39 additions & 17 deletions tests/unit/services/test_provider.py
Original file line number Diff line number Diff line change
Expand Up @@ -84,41 +84,60 @@ def test_setup_proxy_environment(


@pytest.mark.parametrize(
("install_snap", "environment", "snaps"),
("environment", "snaps"),
[
(True, {}, [Snap(name="testcraft", channel="latest/stable", classic=True)]),
(
True,
pytest.param(
{},
[Snap(name="testcraft", channel="latest/stable", classic=True)],
id="install-from-store-default-channel",
),
pytest.param(
{"CRAFT_SNAP_CHANNEL": "something"},
[Snap(name="testcraft", channel="something", classic=True)],
id="install-from-store-with-channel",
),
(
True,
{"SNAP_NAME": "testcraft", "SNAP": "/snap/testcraft/x1"},
[Snap(name="testcraft", channel=None, classic=True)],
pytest.param(
{
"SNAP_NAME": "testcraft",
"SNAP_INSTANCE_NAME": "testcraft_1",
"SNAP": "/snap/testcraft/x1",
},
[Snap(name="testcraft_1", channel=None, classic=True)],
id="inject-from-host",
),
(
True,
pytest.param(
{
"SNAP_NAME": "testcraft",
"SNAP_INSTANCE_NAME": "testcraft_1",
"SNAP": "/snap/testcraft/x1",
"CRAFT_SNAP_CHANNEL": "something",
},
[Snap(name="testcraft", channel=None, classic=True)],
[Snap(name="testcraft_1", channel=None, classic=True)],
id="inject-from-host-ignore-channel"
),
(False, {}, []),
(False, {"CRAFT_SNAP_CHANNEL": "something"}, []),
(
False,
pytest.param(
# SNAP_INSTANCE_NAME may not exist if snapd < 2.43
{
"SNAP_NAME": "testcraft",
"SNAP": "/snap/testcraft/x1",
"CRAFT_SNAP_CHANNEL": "something",
},
[],
[Snap(name="testcraft", channel=None, classic=True)],
id="missing-snap-instance-name",
),
pytest.param(
# this can happen when running testcraft from a venv in a snapped terminal
{
"SNAP_NAME": "kitty",
"SNAP_INSTANCE_NAME": "kitty",
"SNAP": "/snap/kitty/x1",
},
[Snap(name="testcraft", channel="latest/stable", classic=True)],
id="running-inside-another-snap"
)
],
)
@pytest.mark.parametrize("install_snap", [True, False])
def test_install_snap(
monkeypatch,
app_metadata,
Expand All @@ -143,7 +162,10 @@ def test_install_snap(
)
service.setup()

assert service.snaps == snaps
if install_snap:
assert service.snaps == snaps
else:
assert service.snaps == []


@pytest.mark.parametrize(
Expand Down

0 comments on commit e68695e

Please sign in to comment.