-
Notifications
You must be signed in to change notification settings - Fork 451
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
Showing
5 changed files
with
46 additions
and
30 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
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
59 changes: 36 additions & 23 deletions
59
src/tribler/core/components/watch_folder/tests/test_watch_folder.py
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,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() |
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