Skip to content

Commit

Permalink
Merge pull request #528 from Alexandero89/Alexandero89-optimize-chrom…
Browse files Browse the repository at this point in the history
…e-wrapper-test

Optimize chrome wrapper test and describe functionality
  • Loading branch information
codders authored Feb 5, 2024
2 parents 540712c + 923a1b2 commit 3c3647b
Show file tree
Hide file tree
Showing 2 changed files with 39 additions and 18 deletions.
5 changes: 3 additions & 2 deletions flathunter/chrome_wrapper.py
Original file line number Diff line number Diff line change
Expand Up @@ -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"""
Expand All @@ -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', 'chromium-browser',
'/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:
Expand Down
52 changes: 36 additions & 16 deletions test/test_chrome_wrapper.py
Original file line number Diff line number Diff line change
Expand Up @@ -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]:
Expand Down

0 comments on commit 3c3647b

Please sign in to comment.