diff --git a/src/tribler/core/components/libtorrent/download_manager/download_manager.py b/src/tribler/core/components/libtorrent/download_manager/download_manager.py index 68fda1c2376..32e7bb08a17 100644 --- a/src/tribler/core/components/libtorrent/download_manager/download_manager.py +++ b/src/tribler/core/components/libtorrent/download_manager/download_manager.py @@ -540,7 +540,7 @@ def start_download(self, torrent_file=None, tdef=None, config: DownloadConfig = self._logger.debug(f'Starting download: filename: {torrent_file}, torrent def: {tdef}') if config is None: config = DownloadConfig.convert(self.download_defaults) - self._logger.debug(f'Use a default config.') + self._logger.debug('Use a default config.') # the priority of the parameters is: (1) tdef, (2) torrent_file. # so if we have tdef, and torrent_file will be ignored, and so on. diff --git a/src/tribler/core/components/libtorrent/restapi/tests/test_torrentinfo_endpoint.py b/src/tribler/core/components/libtorrent/restapi/tests/test_torrentinfo_endpoint.py index bbb5e84e020..683d7d3ceb8 100644 --- a/src/tribler/core/components/libtorrent/restapi/tests/test_torrentinfo_endpoint.py +++ b/src/tribler/core/components/libtorrent/restapi/tests/test_torrentinfo_endpoint.py @@ -10,7 +10,7 @@ from tribler.core import notifications from tribler.core.components.libtorrent.restapi.torrentinfo_endpoint import TorrentInfoEndpoint -from tribler.core.components.libtorrent.settings import LibtorrentSettings +from tribler.core.components.libtorrent.settings import DownloadDefaultsSettings, LibtorrentSettings from tribler.core.components.libtorrent.torrentdef import TorrentDef from tribler.core.components.metadata_store.db.orm_bindings.torrent_metadata import tdef_to_metadata_dict from tribler.core.components.restapi.rest.base_api_test import do_request @@ -29,6 +29,7 @@ def download_manager(state_dir): dlmgr = MagicMock() dlmgr.config = LibtorrentSettings() + dlmgr.download_defaults = DownloadDefaultsSettings() dlmgr.shutdown = lambda: succeed(None) checkpoints_dir = state_dir / 'dlcheckpoints' checkpoints_dir.mkdir() diff --git a/src/tribler/core/components/libtorrent/tests/test_download_api.py b/src/tribler/core/components/libtorrent/tests/test_download_api.py index 18a46f56a4a..4451c1c5958 100644 --- a/src/tribler/core/components/libtorrent/tests/test_download_api.py +++ b/src/tribler/core/components/libtorrent/tests/test_download_api.py @@ -2,6 +2,7 @@ import pytest +from tribler.core.components.libtorrent.download_manager.download_config import DownloadConfig from tribler.core.tests.tools.common import TORRENT_UBUNTU_FILE from tribler.core.utilities.rest_utils import path_to_url from tribler.core.utilities.simpledefs import DLSTATUS_DOWNLOADING @@ -11,14 +12,15 @@ async def test_download_torrent_from_url(tmp_path, file_server, download_manager): # Setup file server to serve torrent file shutil.copyfile(TORRENT_UBUNTU_FILE, tmp_path / "ubuntu.torrent") - download = await download_manager.start_download_from_uri(f'http://localhost:{file_server}/ubuntu.torrent') + download = await download_manager.start_download_from_uri(f'http://localhost:{file_server}/ubuntu.torrent', + config=DownloadConfig()) await download.wait_for_status(DLSTATUS_DOWNLOADING) @pytest.mark.asyncio async def test_download_torrent_from_file(download_manager): uri = path_to_url(TORRENT_UBUNTU_FILE) - d = await download_manager.start_download_from_uri(uri) + d = await download_manager.start_download_from_uri(uri, config=DownloadConfig()) await d.wait_for_status(DLSTATUS_DOWNLOADING) @@ -27,5 +29,5 @@ async def test_download_torrent_from_file_with_escaped_characters(download_manag destination = tmp_path / 'ubuntu%20%21 15.04.torrent' shutil.copyfile(TORRENT_UBUNTU_FILE, destination) uri = path_to_url(destination) - d = await download_manager.start_download_from_uri(uri) + d = await download_manager.start_download_from_uri(uri, config=DownloadConfig()) await d.wait_for_status(DLSTATUS_DOWNLOADING) diff --git a/src/tribler/core/components/watch_folder/tests/test_watch_folder.py b/src/tribler/core/components/watch_folder/tests/test_watch_folder.py index 35838fdd759..802ab221f01 100644 --- a/src/tribler/core/components/watch_folder/tests/test_watch_folder.py +++ b/src/tribler/core/components/watch_folder/tests/test_watch_folder.py @@ -1,55 +1,68 @@ import os import shutil -from asynctest import MagicMock - import pytest +from asynctest import MagicMock +from tribler.core.components.watch_folder.settings import WatchFolderSettings from tribler.core.components.watch_folder.watch_folder import WatchFolder from tribler.core.tests.tools.common import TESTS_DATA_DIR, TORRENT_UBUNTU_FILE +# pylint: disable=redefined-outer-name, protected-access + @pytest.fixture -async def watcher_fixture(tmp_path): - watch = WatchFolder(watch_folder_path=tmp_path, download_manager=MagicMock(), notifier=MagicMock()) +async def watch_folder(tmp_path): + watch = WatchFolder( + state_dir=tmp_path, + settings=WatchFolderSettings( + enabled=True, + directory='' + ), + download_manager=MagicMock(), + notifier=MagicMock() + ) yield watch await watch.stop() -def test_watchfolder_no_files(watcher_fixture): - watcher_fixture.check_watch_folder() - watcher_fixture.download_manager.start_download.assert_not_called() +def test_watchfolder_no_files(watch_folder): + watch_folder.check_watch_folder() + watch_folder.download_manager.start_download.assert_not_called() + +def test_watchfolder_no_torrent_file(watch_folder: WatchFolder): + directory = watch_folder.settings.get_path_as_absolute('directory', watch_folder.state_dir) -def test_watchfolder_no_torrent_file(watcher_fixture): - shutil.copyfile(TORRENT_UBUNTU_FILE, watcher_fixture.watch_folder / "test.txt") - watcher_fixture.check_watch_folder() - watcher_fixture.download_manager.start_download.assert_not_called() + shutil.copyfile(TORRENT_UBUNTU_FILE, directory / "test.txt") + watch_folder.check_watch_folder() + watch_folder.download_manager.start_download.assert_not_called() -def test_watchfolder_utf8_dir(watcher_fixture, tmp_path): +def test_watchfolder_utf8_dir(watch_folder, tmp_path): new_watch_dir = tmp_path / "\xe2\x82\xac" os.mkdir(new_watch_dir) shutil.copyfile(TORRENT_UBUNTU_FILE, new_watch_dir / "\xe2\x82\xac.torrent") - watcher_fixture.watch_folder = new_watch_dir - watcher_fixture.check_watch_folder() + watch_folder.watch_folder = new_watch_dir + watch_folder.check_watch_folder() -def test_watchfolder_torrent_file_one_corrupt(watcher_fixture): +def test_watchfolder_torrent_file_one_corrupt(watch_folder: WatchFolder): + directory = watch_folder.settings.get_path_as_absolute('directory', watch_folder.state_dir) def mock_start_download(*_, **__): mock_start_download.downloads_started += 1 mock_start_download.downloads_started = 0 - shutil.copyfile(TORRENT_UBUNTU_FILE, watcher_fixture.watch_folder / "test.torrent") - shutil.copyfile(TESTS_DATA_DIR / 'test_rss.xml', watcher_fixture.watch_folder / "test2.torrent") - watcher_fixture.download_manager.start_download = mock_start_download - watcher_fixture.download_manager.download_exists = lambda *_: False - watcher_fixture.check_watch_folder() + shutil.copyfile(TORRENT_UBUNTU_FILE, directory / "test.torrent") + shutil.copyfile(TESTS_DATA_DIR / 'test_rss.xml', directory / "test2.torrent") + watch_folder.download_manager.start_download = mock_start_download + watch_folder.download_manager.download_exists = lambda *_: False + watch_folder.check_watch_folder() assert mock_start_download.downloads_started == 1 - assert (watcher_fixture.watch_folder / "test2.torrent.corrupt").is_file() + assert (directory / "test2.torrent.corrupt").is_file() -def test_cleanup(watcher_fixture): - watcher_fixture.cleanup_torrent_file(TESTS_DATA_DIR, 'thisdoesnotexist123.bla') +def test_cleanup(watch_folder): + watch_folder.cleanup_torrent_file(TESTS_DATA_DIR, 'thisdoesnotexist123.bla') assert not (TESTS_DATA_DIR / 'thisdoesnotexist123.bla.corrupt').exists() diff --git a/src/tribler/core/components/watch_folder/watch_folder.py b/src/tribler/core/components/watch_folder/watch_folder.py index 7a700bb6039..6273ab7573e 100644 --- a/src/tribler/core/components/watch_folder/watch_folder.py +++ b/src/tribler/core/components/watch_folder/watch_folder.py @@ -48,7 +48,7 @@ def cleanup_torrent_file(self, root, name): self.notifier[notifications.watch_folder_corrupt_file](name) def check_watch_folder(self): - self._logger.debug(f'Checking watch folder...') + self._logger.debug('Checking watch folder...') if not self.settings.enabled or not self.state_dir: self._logger.debug(f'Cancelled. Enabled: {self.settings.enabled}. State dir: {self.state_dir}.') @@ -83,4 +83,4 @@ def check_watch_folder(self): if not self.download_manager.download_exists(infohash): self._logger.info("Starting download from torrent file %s", name) self.download_manager.start_download(torrent_file=root / name) - self._logger.debug(f'Checking watch folder completed.') + self._logger.debug('Checking watch folder completed.')