From 3bcfc29beb7e4b4823d008127dd486c2717e1ccf Mon Sep 17 00:00:00 2001 From: drew2a Date: Tue, 12 Dec 2023 15:31:15 +0100 Subject: [PATCH 1/2] Add tests for `fix_win_long_file` function on Windows and Linux - Added import statements for `sys` and `patch` - Added two new test functions: `test_fix_win_long_file_win()` and `test_fix_win_long_file_linux()` - Each test function checks if the `fix_win_long_file` function returns the expected result on Windows and Linux respectively --- .../core/utilities/tests/test_path_utils.py | 17 +++++++++++++++++ 1 file changed, 17 insertions(+) diff --git a/src/tribler/core/utilities/tests/test_path_utils.py b/src/tribler/core/utilities/tests/test_path_utils.py index 093b61b26a..3d9cd3fcf5 100644 --- a/src/tribler/core/utilities/tests/test_path_utils.py +++ b/src/tribler/core/utilities/tests/test_path_utils.py @@ -1,3 +1,6 @@ +import sys +from unittest.mock import patch + import pytest from tribler.core.utilities.path_util import Path, tail @@ -101,3 +104,17 @@ def test_size_folder(tribler_tmp_path: Path): assert tribler_tmp_path.size(include_dir_sizes=False) == 300 assert tribler_tmp_path.size() >= 300 + + +@patch.object(sys, 'platform', 'win32') +def test_fix_win_long_file_win(): + """ Test that fix_win_long_file works correct on Windows""" + path = Path(r'C:\Users\User\AppData\Roaming\.Tribler\7.7') + assert Path.fix_win_long_file(path) == r'\\?\C:\Users\User\AppData\Roaming\.Tribler\7.7' + + +@patch.object(sys, 'platform', 'linux') +def test_fix_win_long_file_linux(): + """ Test that fix_win_long_file works correct on Linux""" + path = Path('/home/user/.Tribler/7.7') + assert Path.fix_win_long_file(path) == '/home/user/.Tribler/7.7' From fba5966598589e1c7cb076d08eec70eb49ec0ed3 Mon Sep 17 00:00:00 2001 From: drew2a Date: Tue, 12 Dec 2023 15:57:09 +0100 Subject: [PATCH 2/2] Refactor download configuration write method to accept a Path object - Refactored the `write` method in `DownloadConfig` class to accept a `filename` parameter of type `Path`. - Updated the code in `download.py` to pass the correct argument type when calling the `write` method. This commit improves code readability and ensures proper handling of file paths. --- .../core/components/libtorrent/download_manager/download.py | 2 +- .../components/libtorrent/download_manager/download_config.py | 2 +- 2 files changed, 2 insertions(+), 2 deletions(-) diff --git a/src/tribler/core/components/libtorrent/download_manager/download.py b/src/tribler/core/components/libtorrent/download_manager/download.py index 6467ede03e..4be439fc01 100644 --- a/src/tribler/core/components/libtorrent/download_manager/download.py +++ b/src/tribler/core/components/libtorrent/download_manager/download.py @@ -322,7 +322,7 @@ def on_save_resume_data_alert(self, alert: lt.save_resume_data_alert): filename = self.download_manager.get_checkpoint_dir() / basename self.config.config['download_defaults']['name'] = self.tdef.get_name_as_unicode() # store name (for debugging) try: - self.config.write(str(filename)) + self.config.write(filename) except OSError as e: self._logger.warning(f'{e.__class__.__name__}: {e}') else: diff --git a/src/tribler/core/components/libtorrent/download_manager/download_config.py b/src/tribler/core/components/libtorrent/download_manager/download_config.py index 87810e7849..6acfa51b6f 100644 --- a/src/tribler/core/components/libtorrent/download_manager/download_config.py +++ b/src/tribler/core/components/libtorrent/download_manager/download_config.py @@ -68,7 +68,7 @@ def copy(self): return DownloadConfig(ConfigObj(self.config, configspec=str(CONFIG_SPEC_PATH), default_encoding='utf-8'), state_dir=self.state_dir) - def write(self, filename): + def write(self, filename: Path): self.config.filename = Path.fix_win_long_file(filename) self.config.write()