Skip to content

Commit

Permalink
Merge pull request #7764 from drew2a/fix/6345
Browse files Browse the repository at this point in the history
Fix "ValueError: relative path can't be expressed as a file URI"
  • Loading branch information
drew2a committed Jan 9, 2024
2 parents 183bc92 + 576a557 commit ef54a97
Show file tree
Hide file tree
Showing 2 changed files with 37 additions and 7 deletions.
29 changes: 29 additions & 0 deletions src/tribler/gui/tests/test_tribler_window.py
@@ -0,0 +1,29 @@
from unittest.mock import Mock, patch

import pytest
from PyQt5.QtWidgets import QFileDialog

from tribler.gui.tribler_window import TriblerWindow


# pylint: disable=redefined-outer-name
@pytest.fixture
def tribler_window():
""" Create mocked TriblerWindow instance"""
with patch('tribler.gui.tribler_window.TriblerWindow.__init__', Mock(return_value=None)):
window = TriblerWindow(Mock(), Mock(), Mock(), Mock())
window.pending_uri_requests = []
return window


def test_on_add_torrent_browse_file(tribler_window: TriblerWindow):
""" Test that the on_add_torrent_browse_file method works correctly"""
tribler_window.raise_window = Mock()
tribler_window.process_uri_request = Mock()

with patch.object(QFileDialog, 'getOpenFileNames', Mock(return_value=['.'])) as patched_getOpenFileNames:
tribler_window.on_add_torrent_browse_file()

assert tribler_window.raise_window.called
assert patched_getOpenFileNames.called
assert tribler_window.process_uri_request.called
15 changes: 8 additions & 7 deletions src/tribler/gui/tribler_window.py
Expand Up @@ -83,15 +83,13 @@
from tribler.gui.utilities import (
connect,
create_api_key,
disconnect,
format_api_key,
get_font_path,
get_gui_setting,
get_image_path,
get_ui_file_path,
is_dir_writable,
set_api_key,
show_message_box,
tr,
)
from tribler.gui.widgets.instanttooltipstyle import InstantTooltipStyle
Expand Down Expand Up @@ -681,13 +679,16 @@ def on_create_torrent_updates(self, update_dict):

def on_add_torrent_browse_file(self, *_):
self.raise_window() # For the case when the action is triggered by tray icon
filenames = QFileDialog.getOpenFileNames(
filenames, *_ = QFileDialog.getOpenFileNames(
self, tr("Please select the .torrent file"), QDir.homePath(), tr("Torrent files%s") % " (*.torrent)"
)
if len(filenames[0]) > 0:
for filename in filenames[0]:
self.pending_uri_requests.append(Path(filename).as_uri())
self.process_uri_request()
if not filenames:
return

for filename in filenames:
uri = Path(filename).resolve().as_uri()
self.pending_uri_requests.append(uri)
self.process_uri_request()

def start_download_from_uri(self, uri):
uri = uri.decode('utf-8') if isinstance(uri, bytes) else uri
Expand Down

0 comments on commit ef54a97

Please sign in to comment.