From dffe5e666466ca6636b7d72721c5d1ff621b0bc3 Mon Sep 17 00:00:00 2001 From: Soumyadeep Ghosh Date: Fri, 22 Nov 2024 22:28:05 +0530 Subject: [PATCH] desktop: stop renaming desktop file With snapd 2.66, it supports custom desktop file names. This patch allows it to happen from snapcraft side. --- snapcraft/parts/desktop_file.py | 14 +++++++++---- tests/conftest.py | 10 +++++++++ tests/unit/parts/test_desktop_file.py | 29 +++++++++++++++------------ 3 files changed, 36 insertions(+), 17 deletions(-) diff --git a/snapcraft/parts/desktop_file.py b/snapcraft/parts/desktop_file.py index 7db5bd085a7..afa7ba3c336 100644 --- a/snapcraft/parts/desktop_file.py +++ b/snapcraft/parts/desktop_file.py @@ -32,7 +32,7 @@ class DesktopFile: :param snap_name: The snap package name. :param app_name: The name of the app using the desktop file. - :param filename: The desktop file name. + :param filename: The desktop file path. :param prime_dir: The prime directory path. :raises DesktopFileError: If the desktop file does not exist. @@ -116,9 +116,15 @@ def write(self, *, gui_dir: Path, icon_path: Optional[str] = None) -> None: gui_dir.mkdir(parents=True, exist_ok=True) - # Rename the desktop file to match the app name. This will help - # unity8 associate them (https://launchpad.net/bugs/1659330). - target = gui_dir / f"{self._app_name}.desktop" + desktop_filename = os.path.basename(self._filename) + + # Stop renaming the desktop file. From snapd 2.66 onwards, + # users can declare custom desktop file names which snapd will not rename + # in the format of {SNAP_NAME}_{APP_NAME}.desktop + # https://snapcraft.io/docs/desktop-interface + target = gui_dir / desktop_filename + if not (desktop_filename.endswith(".desktop")): + target = gui_dir / f"{desktop_filename}.desktop" if target.exists(): # Unlikely. A desktop file in meta/gui/ already existed for diff --git a/tests/conftest.py b/tests/conftest.py index a09e205e6f2..2755e539eb0 100644 --- a/tests/conftest.py +++ b/tests/conftest.py @@ -15,6 +15,7 @@ # along with this program. If not, see . import os +from pathlib import Path import keyring import pytest @@ -52,6 +53,15 @@ def new_dir(tmp_path): os.chdir(cwd) +@pytest.fixture +def prime_dir(new_dir): + """Create a subdirectory structure 'new_dir/meta/gui'.""" + + prime_dir = Path(f"{new_dir}/meta/gui") + + yield prime_dir + + @pytest.fixture def memory_keyring(): """In memory keyring backend for testing.""" diff --git a/tests/unit/parts/test_desktop_file.py b/tests/unit/parts/test_desktop_file.py index 78470c069d1..3a7a5c60394 100644 --- a/tests/unit/parts/test_desktop_file.py +++ b/tests/unit/parts/test_desktop_file.py @@ -14,7 +14,6 @@ # You should have received a copy of the GNU General Public License # along with this program. If not, see . -from pathlib import Path from textwrap import dedent import pytest @@ -39,7 +38,9 @@ class TestDesktopExec: ("bar", "--arg", "foo.bar --arg"), ], ) - def test_generate_desktop_file(self, new_dir, app_name, app_args, expected_exec): + def test_generate_desktop_file( + self, new_dir, prime_dir, app_name, app_args, expected_exec + ): snap_name = "foo" desktop_file_path = new_dir / "app.desktop" @@ -53,9 +54,9 @@ def test_generate_desktop_file(self, new_dir, app_name, app_args, expected_exec) filename=desktop_file_path, prime_dir=new_dir.as_posix(), ) - d.write(gui_dir=Path()) + d.write(gui_dir=prime_dir) - expected_desktop_file = new_dir / f"{app_name}.desktop" + expected_desktop_file = prime_dir / "app.desktop" assert expected_desktop_file.exists() with expected_desktop_file.open() as desktop_file: assert desktop_file.read() == dedent( @@ -83,7 +84,9 @@ class TestDesktopIcon: ("foo", None, "foo"), ], ) - def test_generate_desktop_file(self, new_dir, icon, icon_path, expected_icon): + def test_generate_desktop_file( + self, new_dir, prime_dir, icon, icon_path, expected_icon + ): snap_name = app_name = "foo" desktop_file_path = new_dir / "app.desktop" @@ -101,14 +104,14 @@ def test_generate_desktop_file(self, new_dir, icon, icon_path, expected_icon): filename=desktop_file_path, prime_dir=new_dir, ) - d.write(gui_dir=Path()) + d.write(gui_dir=prime_dir) if icon_path is not None: - d.write(icon_path=icon_path, gui_dir=Path()) + d.write(icon_path=icon_path, gui_dir=prime_dir) else: - d.write(gui_dir=Path()) + d.write(gui_dir=prime_dir) - expected_desktop_file = new_dir / f"{app_name}.desktop" + expected_desktop_file = prime_dir / "app.desktop" assert expected_desktop_file.exists() with expected_desktop_file.open() as desktop_file: assert ( @@ -137,7 +140,7 @@ def test_generate_desktop_file(self, new_dir, icon, icon_path, expected_icon): ], ) def test_generate_desktop_file_multisection( - self, new_dir, icon, icon_path, expected_icon + self, new_dir, prime_dir, icon, icon_path, expected_icon ): snap_name = app_name = "foo" @@ -161,11 +164,11 @@ def test_generate_desktop_file_multisection( ) if icon_path is not None: - d.write(icon_path=icon_path, gui_dir=Path()) + d.write(icon_path=icon_path, gui_dir=prime_dir) else: - d.write(gui_dir=Path()) + d.write(gui_dir=prime_dir) - expected_desktop_file = new_dir / f"{app_name}.desktop" + expected_desktop_file = prime_dir / "app.desktop" assert expected_desktop_file.exists() with expected_desktop_file.open() as desktop_file: assert desktop_file.read() == dedent(