Skip to content

Commit

Permalink
Fix "ValueError: relative path can't be expressed as a file URI"
Browse files Browse the repository at this point in the history
This commit adds a new test case to the `test_tribler_window.py` file. The test checks that the `on_add_torrent_browse_file` method in the `TriblerWindow` class works correctly. It mocks certain methods and uses patching to simulate user interaction with a file dialog. The test verifies that the appropriate methods are called and asserts their expected behavior.

The code changes in `tribler_window.py` modify the implementation of the `on_add_torrent_browse_file` method. It now handles cases where no filenames are selected from the file dialog, preventing unnecessary processing. Additionally, it resolves each filename to its absolute URI path before appending it to the list of pending URI requests.

These changes improve the functionality and reliability of adding torrent files through browsing in TriblerWindow.
  • Loading branch information
drew2a committed Dec 8, 2023
1 parent 0d9a839 commit 1492aae
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 1492aae

Please sign in to comment.