From ea40e4f0b73dc4f92cfff92567b9c5d5f2e213cf Mon Sep 17 00:00:00 2001 From: Vinicius Moreira Date: Mon, 24 Oct 2022 17:32:58 -0300 Subject: [PATCH] [gems.appimage] improvement: better TryExec field cleaning + tests fixed --- bauh/gems/appimage/util.py | 4 +-- tests/common/test_util.py | 1 - tests/gems/appimage/test_util.py | 49 +++++++++++++++++++++++++++----- 3 files changed, 44 insertions(+), 10 deletions(-) diff --git a/bauh/gems/appimage/util.py b/bauh/gems/appimage/util.py index ba5b387d..2057d92c 100644 --- a/bauh/gems/appimage/util.py +++ b/bauh/gems/appimage/util.py @@ -2,7 +2,7 @@ import os import re from typing import Optional -RE_DESKTOP_EXEC = re.compile(r'(\w*Exec\s*=(.+)(\n?))') +RE_DESKTOP_EXEC = re.compile(r'(\n?\s*\w*Exec\s*=(.+))') RE_MANY_SPACES = re.compile(r'\s+') @@ -25,7 +25,7 @@ def replace_desktop_entry_exec_command(desktop_entry: str, appname: str, file_pa for exec_groups in execs: full_match = exec_groups[0] - if full_match.startswith("TryExec"): # TryExec cause issues in some DE to display the app icon + if full_match.strip().startswith("TryExec"): # TryExec cause issues in some DE to display the app icon final_entry = final_entry.replace(full_match, "") continue diff --git a/tests/common/test_util.py b/tests/common/test_util.py index 22086bd0..f883bb28 100644 --- a/tests/common/test_util.py +++ b/tests/common/test_util.py @@ -1,4 +1,3 @@ -import time from unittest import TestCase from bauh.commons.util import size_to_byte, sanitize_command_input diff --git a/tests/gems/appimage/test_util.py b/tests/gems/appimage/test_util.py index fa445e0b..3a5b894d 100644 --- a/tests/gems/appimage/test_util.py +++ b/tests/gems/appimage/test_util.py @@ -79,6 +79,45 @@ class TestUtil(TestCase): self.assertEqual(expected, res) + def test_replace_desktop_entry_exec_command__exec_as_the_first_field(self): + desktop_entry = """ + Exec=myapp %f + Name=MyApp + Icon=MyApp + """ + + res = replace_desktop_entry_exec_command(desktop_entry=desktop_entry, + appname='myapp', + file_path='/path/to/myapp.appimage') + + expected = """ + Exec="/path/to/myapp.appimage" %f + Name=MyApp + Icon=MyApp + """ + + self.assertEqual(expected, res) + + def test_replace_desktop_entry_exec_command__try_exec_as_the_first_field(self): + desktop_entry = """ + TryExec=MyApp + Exec=myapp %f + Name=MyApp + Icon=MyApp + """ + + res = replace_desktop_entry_exec_command(desktop_entry=desktop_entry, + appname='myapp', + file_path='/path/to/myapp.appimage') + + expected = """ + Exec="/path/to/myapp.appimage" %f + Name=MyApp + Icon=MyApp + """ + + self.assertEqual(expected, res) + def test_replace_desktop_entry_exec_command__only_one_exec_field_with_spaces_and_params(self): desktop_entry = """ Name=MyApp @@ -136,7 +175,7 @@ class TestUtil(TestCase): self.assertEqual(expected, res) - def test_replace_desktop_entry_exec_command__only_one_tryexec_field_with_spaces_and_params(self): + def test_replace_desktop_entry_exec_command__must_remove_try_exec_field(self): desktop_entry = """ Name=MyApp Icon=MyApp @@ -150,12 +189,11 @@ class TestUtil(TestCase): expected = """ Name=MyApp Icon=MyApp - TryExec ="/path/to/myapp.appimage" %f --a """ self.assertEqual(expected, res) - def test_replace_desktop_entry_exec_command__exec_and_tryexec_fields(self): + def test_replace_desktop_entry_exec_command__must_replace_exec_and_remove_tryexec_fields(self): desktop_entry = """ Name=MyApp Icon=MyApp @@ -171,14 +209,13 @@ class TestUtil(TestCase): expected = """ Name=MyApp Icon=MyApp - TryExec ="/path/to/myapp.appimage" %f Exec="/path/to/myapp.appimage" --a Terminal=false """ self.assertEqual(expected, res) - def test_replace_desktop_entry_exec_command__exec_and_tryexec_fields_with_envvars_and_params(self): + def test_replace_desktop_entry_exec_command__exec_field_with_envvars_and_params(self): desktop_entry = """ Name=MyApp Icon=MyApp @@ -194,7 +231,6 @@ class TestUtil(TestCase): expected = """ Name=MyApp Icon=MyApp - TryExec=__MY_VAR=1 "/path/to/myapp.appimage" %f Exec=NEW_VAR=abc "/path/to/myapp.appimage" --a Terminal=false """ @@ -228,7 +264,6 @@ Name=RPCS3 GenericName=PlayStation 3 Emulator Comment=An open-source PlayStation 3 emulator/debugger written in C++. Icon=rpcs3 -TryExec="/path/to/rpcs3.appimage" Exec="/path/to/rpcs3.appimage" %f Terminal=false Categories=Game;Emulator;