diff --git a/snapcraft/internal/meta/_desktop.py b/snapcraft/internal/meta/_desktop.py index d5c1cc547d..71ca384c52 100644 --- a/snapcraft/internal/meta/_desktop.py +++ b/snapcraft/internal/meta/_desktop.py @@ -68,7 +68,11 @@ def parse_and_reformat(self, extracted_metadata: _metadata.ExtractedMetadata): icon = metadata_icon icon = icon.lstrip("/") - if os.path.exists(os.path.join(self._prime_dir, icon)): + if icon.startswith("${SNAP}/") and os.path.exists( + os.path.join(self._prime_dir, icon[8:]) + ): + self._parser[section]["Icon"] = icon + elif os.path.exists(os.path.join(self._prime_dir, icon)): self._parser[section]["Icon"] = "${{SNAP}}/{}".format(icon) else: logger.warning( diff --git a/tests/unit/test_meta.py b/tests/unit/test_meta.py index 8e0ace62ea..b1b12d70e7 100644 --- a/tests/unit/test_meta.py +++ b/tests/unit/test_meta.py @@ -398,6 +398,9 @@ def test_create_meta_with_app(self): self.assertThat(y, Equals(expected)) def test_create_meta_with_app_desktop_key(self): + fake_logger = fixtures.FakeLogger(level=logging.INFO) + self.useFixture(fake_logger) + os.mkdir(self.prime_dir) _create_file(os.path.join(self.prime_dir, "app.sh")) _create_file( @@ -411,9 +414,10 @@ def test_create_meta_with_app_desktop_key(self): os.path.join(self.prime_dir, "app2.desktop"), content="[Desktop Entry]\nExec=app2.exe\nIcon=/usr/share/app2.png", ) + _create_file(os.path.join(icon_dir, "app3.png")) _create_file( os.path.join(self.prime_dir, "app3.desktop"), - content="[Desktop Entry]\nExec=app3.exe\nIcon=app3.png", + content="[Desktop Entry]\nExec=app3.exe\nIcon=${SNAP}/usr/share/app3.png", ) self.config_data["apps"] = { "app1": {"command": "app.sh", "desktop": "app1.desktop"}, @@ -450,6 +454,20 @@ def test_create_meta_with_app_desktop_key(self): section = "Desktop Entry" self.assertTrue(section in contents) self.assertThat(contents[section].get("Exec"), Equals("my-package %U")) + self.assertThat( + contents[section].get("Icon"), Equals("${SNAP}/usr/share/app3.png") + ) + self.assertThat( + fake_logger.output, + Not( + Contains( + "Icon {} specified in desktop file {} not found " + "in prime directory".format( + "${SNAP}/usr/share/app3.png", "app3.desktop" + ) + ) + ), + ) snap_yaml = os.path.join("prime", "meta", "snap.yaml") self.assertThat(snap_yaml, Not(FileContains("desktop: app1.desktop")))