diff --git a/qbt_migrate/methods.py b/qbt_migrate/methods.py index df07106..4ed581f 100644 --- a/qbt_migrate/methods.py +++ b/qbt_migrate/methods.py @@ -34,14 +34,14 @@ def convert_slashes(path: str, target_os: str): return path.replace('\\', '/') -def backup_folder(folder_path: Union[str, os.PathLike], archive_path: Union[str, os.PathLike]): +def backup_folder(folder_path: Union[str, os.PathLike], archive_path: Union[str, os.PathLike], + include_torrents: bool = True): logger.info(f'Creating Archive {archive_path} ...') folder_path = Path(folder_path) archive_path = Path(archive_path) with zipfile.ZipFile(archive_path, 'w') as archive: for file in folder_path.iterdir(): - if file == archive_path: - continue - logger.debug(f'Archiving {file} into {archive_path}...') - archive.write(file) + if file.name.endswith('.fastresume') or (include_torrents and file.name.endswith('.torrent')): + logger.debug(f'Archiving {file} into {archive_path}...') + archive.write(file) logger.info('Done!') diff --git a/setup.py b/setup.py index b21e884..8ae5731 100644 --- a/setup.py +++ b/setup.py @@ -11,7 +11,7 @@ setup( name='qbt_migrate', - version='2.1.3', + version='2.1.4', packages=find_packages(), install_requires=dependencies, description='Migrate qBittorrent FastResume files.', diff --git a/tests/test_methods.py b/tests/test_methods.py index 879f457..cf6c700 100644 --- a/tests/test_methods.py +++ b/tests/test_methods.py @@ -8,23 +8,50 @@ def test_backup_folder(): temp_dir = Path(tempfile.mkdtemp()) # Get a tmp dir - file_count = 20 # Number of test files to build + fastresume_file_count = 20 # Number of test fastresume files to build + torrent_file_count = 20 # Number of test torrent files to build + invalid_file_count = 20 # Number of invalid files to build # Build test files - for x in range(file_count): - with open(temp_dir / f'test_{x}.txt', 'w') as f: + for x in range(fastresume_file_count): + with open(temp_dir / f'test_{x}.fastresume', 'w') as f: f.write(f'This is file {x}') + for x in range(torrent_file_count): + with open(temp_dir / f'test_{x}.torrent', 'w') as f: + f.write(f'This is file {x}') + + for x in range(invalid_file_count): + with open(temp_dir / f'test_{x}.invalid', 'w') as f: + f.write(f'This is invalid file {x}') + # 'test.zip' will fail the filename check in archive if it gets archived archive_path = temp_dir / 'test.zip' - backup_folder(temp_dir, archive_path) # Call backup_folder + backup_folder(temp_dir, archive_path) # Call backup_folder with torrent files included # Validate zip file with zipfile.ZipFile(archive_path, 'r') as archive: assert(archive.testzip() is None) # Check no errors - assert(len(archive.infolist()) == file_count) # Assert that it has the correct file count + # Assert that it has the correct file count + assert(len(archive.infolist()) == fastresume_file_count + torrent_file_count) for file in archive.infolist(): - filename = re.findall(r'test_(\d+).txt', file.filename) # Get the integer of the file + # Get the integer of the file + filename = re.findall(r'test_(\d+).(?:fastresume)?(?:torrent)?', file.filename) assert(len(filename) != 0) # Ensure we got the integer filename = filename[0] assert(archive.read(file.filename) == bytes(f'This is file {filename}', 'utf-8')) # Integrity Check + + archive_path = temp_dir / 'test2.zip' + backup_folder(temp_dir, archive_path, False) + + # Validate zip file + with zipfile.ZipFile(archive_path, 'r') as archive: + assert (archive.testzip() is None) # Check no errors + # Assert that it has the correct file count + assert (len(archive.infolist()) == fastresume_file_count) + for file in archive.infolist(): + # Get the integer of the file + filename = re.findall(r'test_(\d+).fastresume', file.filename) + assert (len(filename) != 0) # Ensure we got the integer + filename = filename[0] + assert (archive.read(file.filename) == bytes(f'This is file {filename}', 'utf-8')) # Integrity Check