From b51a2177c4117eacc3fdec745db2e1c45f4f3d89 Mon Sep 17 00:00:00 2001 From: Alexandero89 Date: Mon, 5 Feb 2024 17:29:10 +0100 Subject: [PATCH 1/2] Update and comment test_chrome_wrapper --- test/test_chrome_wrapper.py | 52 +++++++++++++++++++++++++------------ 1 file changed, 36 insertions(+), 16 deletions(-) diff --git a/test/test_chrome_wrapper.py b/test/test_chrome_wrapper.py index 939434e1..258354b3 100644 --- a/test/test_chrome_wrapper.py +++ b/test/test_chrome_wrapper.py @@ -2,25 +2,45 @@ import unittest from unittest.mock import patch -from flathunter.chrome_wrapper import get_chrome_version +from flathunter.chrome_wrapper import get_chrome_version, CHROME_BINARY_NAMES from flathunter.exceptions import ChromeNotFound -CHROME_VERSION_RESULTS = [ - [], [], [], - [], ['Chromium 107.0.5304.87 built on Debian bookworm/sid, running on Debian bookworm/sid'], - ['Google Chrome 107.0.5304.110'], - ['Chromium 107.0.5304.87 built on Debian 11.5, running on Debian 11.5'], - [], [], [], - ] + +def calc_linux_binary_names(): + """ + Creates a list containing empty lists for each name in CHROME_BINARY_NAMES that does not start with a forward slash. + """ + return [[] for name in CHROME_BINARY_NAMES if not name.startswith('/')] + + +""" +The list of mock commands get_command_output should return as an output. + +The first returns should all be empty [] so the get_chrome_version function at flathunter/chrome_wrapper.py:31 +thinks no linux chrome is installed and then checks for windows registry entry at flathunter/chrome_wrapper.py:46 +Therefore prepending calc_linux_binary_names(). +Append the same amount empty returns to the end so flathunter/chrome_wrapper.py:31 is forced to check for windows +again and self.assertEqual(get_chrome_version(), 116) works out +""" +CHROME_VERSION_RESULTS = calc_linux_binary_names() + [ + ['Chromium 107.0.5304.87 built on Debian bookworm/sid, running on Debian bookworm/sid'], + ['Google Chrome 107.0.5304.110'], + ['Chromium 107.0.5304.87 built on Debian 11.5, running on Debian 11.5'], +] + calc_linux_binary_names() + +""" +The first return should be empty ([]) so the system thinks no chrome installed at all and +self.assertEqual(get_chrome_version(), None) works out correctly +""" REG_VERSION_RESULTS = [ - [], - [ - '', - r'HKEY_CURRENT_USER\Software\Google\Chrome\BLBeacon', - ' version REG_SZ 116.0.5845.141', - '', - ] - ] + [], + [ + '', + r'HKEY_CURRENT_USER\Software\Google\Chrome\BLBeacon', + ' version REG_SZ 116.0.5845.141', + '', + ] +] def my_subprocess_mock(args, static={ 'chrome_calls': 0, 'reg_calls': 0 }): if 'chrom' in args[0]: From a55f7a17f3291937deadad69e5f27f1cdb41c3a9 Mon Sep 17 00:00:00 2001 From: Alexandero89 Date: Mon, 5 Feb 2024 17:30:50 +0100 Subject: [PATCH 2/2] Make CHROME_BINARY_NAMES globally available --- flathunter/chrome_wrapper.py | 5 +++-- 1 file changed, 3 insertions(+), 2 deletions(-) diff --git a/flathunter/chrome_wrapper.py b/flathunter/chrome_wrapper.py index 2d5b3bca..a7f0a6a0 100644 --- a/flathunter/chrome_wrapper.py +++ b/flathunter/chrome_wrapper.py @@ -13,6 +13,8 @@ CHROME_VERSION_REGEXP = re.compile(r'.* (\d+\.\d+\.\d+\.\d+)( .*)?') WINDOWS_CHROME_REG_PATH = r'HKEY_CURRENT_USER\Software\Google\Chrome\BLBeacon' WINDOWS_CHROME_REG_REGEXP = re.compile(r'\s*version\s*REG_SZ\s*(\d+)\..*') +CHROME_BINARY_NAMES = ['google-chrome', 'chromium', 'chrome', 'chromium-browser', + '/Applications/Google Chrome.app/Contents/MacOS/Google Chrome'] def get_command_output(args) -> List[str]: """Run a command and return stdout""" @@ -28,8 +30,7 @@ def get_command_output(args) -> List[str]: def get_chrome_version() -> int: """Determine the correct name for the chrome binary""" - for binary_name in ['google-chrome', 'chromium', 'chrome', - '/Applications/Google Chrome.app/Contents/MacOS/Google Chrome']: + for binary_name in CHROME_BINARY_NAMES: try: version_output = get_command_output([binary_name, '--version']) if not version_output: