Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: inject aliased snaps into build environment #467

Merged
merged 2 commits into from
Sep 16, 2024
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
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
64 changes: 48 additions & 16 deletions tests/unit/services/test_provider.py
Original file line number Diff line number Diff line change
Expand Up @@ -84,41 +84,70 @@ 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",
mr-cal marked this conversation as resolved.
Show resolved Hide resolved
"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_1", channel=None, classic=True)],
id="inject-from-host-ignore-channel",
),
pytest.param(
# SNAP_INSTANCE_NAME may not exist if snapd < 2.43 or feature is disabled
{
"SNAP_NAME": "testcraft",
"SNAP": "/snap/testcraft/x1",
},
[Snap(name="testcraft", channel=None, classic=True)],
id="missing-snap-instance-name",
),
(False, {}, []),
(False, {"CRAFT_SNAP_CHANNEL": "something"}, []),
(
False,
pytest.param(
# SNAP_INSTANCE_NAME may not exist if snapd < 2.43 or feature is disabled
{
"SNAP_NAME": "testcraft",
"SNAP": "/snap/testcraft/x1",
# CRAFT_SNAP_CHANNEL should be ignored
"CRAFT_SNAP_CHANNEL": "something",
},
[],
[Snap(name="testcraft", channel=None, classic=True)],
id="missing-snap-instance-name-ignore-snap-channel",
),
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 +172,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
Loading