forked from johnny-moonbeam/galaxy-origin
-
Notifications
You must be signed in to change notification settings - Fork 14
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
93c021d
commit 5e490f0
Showing
4 changed files
with
79 additions
and
21 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,42 +1,42 @@ | ||
import platform | ||
|
||
|
||
if platform.system().lower() == "windows": | ||
|
||
import winreg | ||
import shlex | ||
import os | ||
|
||
def is_uri_handler_installed(protocol): | ||
try: | ||
key = winreg.OpenKey(winreg.HKEY_CLASSES_ROOT, r"{}\shell\open\command".format(protocol)) | ||
except OSError: | ||
return False | ||
def _get_path_from_cmd_template(cmd_template) -> str: | ||
return cmd_template.replace("\"", "").partition("%")[0].strip() | ||
|
||
def is_uri_handler_installed(protocol) -> bool: | ||
try: | ||
executable_template = winreg.QueryValue(key, None) | ||
splitted_exec = shlex.split(executable_template) | ||
if not splitted_exec: | ||
return False | ||
return os.path.exists(splitted_exec[0]) | ||
except ValueError: | ||
with winreg.OpenKey( | ||
winreg.HKEY_CLASSES_ROOT, r"{}\shell\open\command".format(protocol) | ||
) as key: | ||
executable_template = winreg.QueryValue(key, None) | ||
path = _get_path_from_cmd_template(executable_template) | ||
return os.path.exists(path) | ||
except OSError: | ||
return False | ||
finally: | ||
winreg.CloseKey(key) | ||
|
||
return True | ||
|
||
elif platform.system().lower() == "darwin": | ||
|
||
from CoreServices.LaunchServices import LSCopyDefaultHandlerForURLScheme | ||
from AppKit import NSWorkspace | ||
|
||
def is_uri_handler_installed(protocol): | ||
def is_uri_handler_installed(protocol) -> bool: | ||
bundle_id = LSCopyDefaultHandlerForURLScheme(protocol) | ||
if not bundle_id: | ||
return False | ||
return NSWorkspace.sharedWorkspace().absolutePathForAppBundleWithIdentifier_(bundle_id) is not None | ||
return ( | ||
NSWorkspace.sharedWorkspace().absolutePathForAppBundleWithIdentifier_(bundle_id) | ||
is not None | ||
) | ||
|
||
|
||
else: | ||
|
||
def is_uri_handler_installed(protocol): | ||
def is_uri_handler_installed(protocol) -> bool: | ||
return False |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,50 @@ | ||
import platform | ||
|
||
import pytest | ||
|
||
from uri_scheme_handler import is_uri_handler_installed | ||
|
||
|
||
@pytest.mark.skipif(platform.system().lower() != "windows", reason="windows registry") | ||
@pytest.mark.parametrize( | ||
"reg_value_template", | ||
[ | ||
pytest.param('{} "%1"', id="Origin case"), | ||
pytest.param('"{}" "%1"', id="EA Desktop case"), | ||
pytest.param('{} "%1" "%2"', id="more params (hypotetical)"), | ||
pytest.param("{} %1", id="no quotes around param (hypotetical)"), | ||
pytest.param("{}", id="just exe without params (hypotetical)"), | ||
], | ||
) | ||
def test_win_reg_uri_handler_installed(mocker, reg_value_template): | ||
launcher_path = r"C:\Program Files\EA Desktop Example Path\EALauncher.exe" | ||
reg_value = reg_value_template.format(launcher_path) | ||
|
||
mocker.patch("winreg.OpenKey") | ||
mocker.patch("winreg.QueryValue", return_value=reg_value) | ||
mocker.patch("os.path.exists", side_effect=lambda x: x == launcher_path) | ||
|
||
assert is_uri_handler_installed(mocker.Mock()) == True | ||
|
||
|
||
@pytest.mark.skipif(platform.system().lower() != "windows", reason="windows registry") | ||
@pytest.mark.parametrize( | ||
"problem_patch_config", | ||
[ | ||
{"target": "winreg.OpenKey", "side_effect": OSError}, | ||
{"target": "winreg.OpenKey", "side_effect": FileNotFoundError}, | ||
{"target": "winreg.QueryValue", "side_effect": PermissionError}, | ||
{"target": "os.path.exists", "return_value": False}, | ||
], | ||
) | ||
def test_win_reg_uri_hanlder_not_installed(mocker, problem_patch_config): | ||
# "installed" case patches | ||
mocker.patch("winreg.OpenKey") | ||
mocker.patch("winreg.QueryValue", return_value="path"), | ||
mocker.patch("os.path.exists", return_value=True) | ||
assert is_uri_handler_installed(mocker.Mock()) == True, "test preconfiguration failed" | ||
|
||
# problem patch | ||
mocker.patch(**problem_patch_config) | ||
|
||
assert is_uri_handler_installed(mocker.Mock()) == False |