From 7020f0530ad0d889fdf25e6484acb4e8b168bb17 Mon Sep 17 00:00:00 2001 From: Vandal <86826719+VandalByte@users.noreply.github.com> Date: Fri, 28 Mar 2025 22:22:19 +0530 Subject: [PATCH] update existing tests --- borgmatic/borg/recreate.py | 13 ++- borgmatic/commands/arguments.py | 1 + tests/unit/borg/test_recreate.py | 195 ++++++++++++++++--------------- 3 files changed, 111 insertions(+), 98 deletions(-) diff --git a/borgmatic/borg/recreate.py b/borgmatic/borg/recreate.py index ea2a2081..81d5a6ed 100644 --- a/borgmatic/borg/recreate.py +++ b/borgmatic/borg/recreate.py @@ -1,6 +1,5 @@ import logging import shlex -from datetime import datetime import borgmatic.borg.environment import borgmatic.config.paths @@ -76,10 +75,14 @@ def recreate_archive( + (('--timestamp', recreate_arguments.timestamp) if recreate_arguments.timestamp else ()) + (('--compression', compression) if compression else ()) + (('--chunker-params', chunker_params) if chunker_params else ()) - + flags.make_match_archives_flags( - recreate_arguments.match_archives or archive or config.get('match_archives'), - config.get('archive_name_format'), - local_borg_version, + + ( + flags.make_match_archives_flags( + recreate_arguments.match_archives or archive or config.get('match_archives'), + config.get('archive_name_format'), + local_borg_version, + ) + if recreate_arguments.match_archives + else () ) + (('--recompress', recreate_arguments.recompress) if recreate_arguments.recompress else ()) + exclude_flags diff --git a/borgmatic/commands/arguments.py b/borgmatic/commands/arguments.py index 689959ce..8e468e8a 100644 --- a/borgmatic/commands/arguments.py +++ b/borgmatic/commands/arguments.py @@ -1591,6 +1591,7 @@ def make_parsers(): '-a', '--match-archives', '--glob-archives', + dest='match_archives', metavar='PATTERN', help='Only consider archive names, hashes, or series matching this pattern', ) diff --git a/tests/unit/borg/test_recreate.py b/tests/unit/borg/test_recreate.py index 4bab00d9..aaa3c3fd 100644 --- a/tests/unit/borg/test_recreate.py +++ b/tests/unit/borg/test_recreate.py @@ -28,7 +28,17 @@ def test_recreate_archive_dry_run_skips_execution(): ).and_return(('repo::archive',)) flexmock(module.borgmatic.execute).should_receive('execute_command').never() - recreate_arguments = flexmock(repository=flexmock(), list=None, path=None) + recreate_arguments = flexmock( + repository=flexmock(), + list=None, + target=None, + comment=None, + timestamp=None, + compression=None, + chunker_params=None, + match_archives=None, + recompress=None, + ) result = module.recreate_archive( repository='repo', @@ -44,11 +54,6 @@ def test_recreate_archive_dry_run_skips_execution(): def test_recreate_calls_borg_with_required_flags(): - flexmock(module.borgmatic.borg.flags).should_receive('make_repository_flags').and_return( - ('repo::archive',) - ) - flexmock(module).should_receive('write_patterns_file').and_return(None) - flexmock(module).should_receive('make_list_filter_flags').and_return(None) insert_execute_command_mock(('borg', 'recreate', 'repo::archive')) module.recreate_archive( @@ -56,7 +61,16 @@ def test_recreate_calls_borg_with_required_flags(): archive='archive', config={}, local_borg_version='1.2.3', - recreate_arguments=flexmock(path=None, list=None), + recreate_arguments=flexmock( + list=None, + target=None, + comment=None, + timestamp=None, + compression=None, + chunker_params=None, + match_archives=None, + recompress=None, + ), global_arguments=flexmock(dry_run=False, log_json=False), local_path='borg', remote_path=None, @@ -64,30 +78,7 @@ def test_recreate_calls_borg_with_required_flags(): ) -def test_recreate_calls_borg_without_archive(): - logger_mock = flexmock(module.logger) - logger_mock.should_receive('error').with_args('Please provide a valid archive name.').once() - - flexmock(module.borgmatic.execute).should_receive('execute_command').never() - - module.recreate_archive( - repository='repo', - archive=None, - config={}, - local_borg_version='1.2.3', - recreate_arguments=flexmock(path=None, list=None), - global_arguments=flexmock(dry_run=False, log_json=False), - local_path='borg', - patterns=None, - ) - - def test_recreate_with_remote_path(): - flexmock(module.borgmatic.borg.flags).should_receive('make_repository_flags').and_return( - ('repo::archive',) - ) - flexmock(module).should_receive('write_patterns_file').and_return(None) - flexmock(module).should_receive('make_list_filter_flags').and_return(None) insert_execute_command_mock(('borg', 'recreate', '--remote-path', 'borg1', 'repo::archive')) module.recreate_archive( @@ -95,7 +86,16 @@ def test_recreate_with_remote_path(): archive='archive', config={}, local_borg_version='1.2.3', - recreate_arguments=flexmock(path=None, list=None), + recreate_arguments=flexmock( + list=None, + target=None, + comment=None, + timestamp=None, + compression=None, + chunker_params=None, + match_archives=None, + recompress=None, + ), global_arguments=flexmock(dry_run=False, log_json=False), local_path='borg', remote_path='borg1', @@ -104,11 +104,6 @@ def test_recreate_with_remote_path(): def test_recreate_with_lock_wait(): - flexmock(module.borgmatic.borg.flags).should_receive('make_repository_flags').and_return( - ('repo::archive',) - ) - flexmock(module).should_receive('write_patterns_file').and_return(None) - flexmock(module).should_receive('make_list_filter_flags').and_return(None) insert_execute_command_mock(('borg', 'recreate', '--lock-wait', '5', 'repo::archive')) module.recreate_archive( @@ -116,7 +111,16 @@ def test_recreate_with_lock_wait(): archive='archive', config={'lock_wait': '5'}, local_borg_version='1.2.3', - recreate_arguments=flexmock(path=None, list=None), + recreate_arguments=flexmock( + list=None, + target=None, + comment=None, + timestamp=None, + compression=None, + chunker_params=None, + match_archives=None, + recompress=None, + ), global_arguments=flexmock(dry_run=False, log_json=False), local_path='borg', patterns=None, @@ -124,11 +128,6 @@ def test_recreate_with_lock_wait(): def test_recreate_with_log_info(): - flexmock(module.borgmatic.borg.flags).should_receive('make_repository_flags').and_return( - ('repo::archive',) - ) - flexmock(module).should_receive('write_patterns_file').and_return(None) - flexmock(module).should_receive('make_list_filter_flags').and_return(None) insert_execute_command_mock(('borg', 'recreate', '--info', 'repo::archive')) insert_logging_mock(logging.INFO) @@ -138,7 +137,16 @@ def test_recreate_with_log_info(): archive='archive', config={}, local_borg_version='1.2.3', - recreate_arguments=flexmock(path=None, list=None), + recreate_arguments=flexmock( + list=None, + target=None, + comment=None, + timestamp=None, + compression=None, + chunker_params=None, + match_archives=None, + recompress=None, + ), global_arguments=flexmock(dry_run=False, log_json=False), local_path='borg', patterns=None, @@ -146,11 +154,6 @@ def test_recreate_with_log_info(): def test_recreate_with_log_debug(): - flexmock(module.borgmatic.borg.flags).should_receive('make_repository_flags').and_return( - ('repo::archive',) - ) - flexmock(module).should_receive('write_patterns_file').and_return(None) - flexmock(module).should_receive('make_list_filter_flags').and_return(None) insert_execute_command_mock(('borg', 'recreate', '--debug', '--show-rc', 'repo::archive')) insert_logging_mock(logging.DEBUG) @@ -159,7 +162,16 @@ def test_recreate_with_log_debug(): archive='archive', config={}, local_borg_version='1.2.3', - recreate_arguments=flexmock(path=None, list=None), + recreate_arguments=flexmock( + list=None, + target=None, + comment=None, + timestamp=None, + compression=None, + chunker_params=None, + match_archives=None, + recompress=None, + ), global_arguments=flexmock(dry_run=False, log_json=False), local_path='borg', patterns=None, @@ -167,11 +179,6 @@ def test_recreate_with_log_debug(): def test_recreate_with_log_json(): - flexmock(module.borgmatic.borg.flags).should_receive('make_repository_flags').and_return( - ('repo::archive',) - ) - flexmock(module).should_receive('write_patterns_file').and_return(None) - flexmock(module).should_receive('make_list_filter_flags').and_return(None) insert_execute_command_mock(('borg', 'recreate', '--log-json', 'repo::archive')) module.recreate_archive( @@ -179,38 +186,23 @@ def test_recreate_with_log_json(): archive='archive', config={}, local_borg_version='1.2.3', - recreate_arguments=flexmock(path=None, list=None), + recreate_arguments=flexmock( + list=None, + target=None, + comment=None, + timestamp=None, + compression=None, + chunker_params=None, + match_archives=None, + recompress=None, + ), global_arguments=flexmock(dry_run=False, log_json=True), local_path='borg', patterns=None, ) -def test_recreate_with_path_flag(): - flexmock(module.borgmatic.borg.flags).should_receive('make_repository_flags').and_return( - ('repo::archive',) - ) - flexmock(module).should_receive('write_patterns_file').and_return(None) - flexmock(module).should_receive('make_list_filter_flags').and_return(None) - insert_execute_command_mock(('borg', 'recreate', '--path', '/some/path', 'repo::archive')) - - module.recreate_archive( - repository='repo', - archive='archive', - config={}, - local_borg_version='1.2.3', - recreate_arguments=flexmock(path='/some/path', list=None), - global_arguments=flexmock(dry_run=False, log_json=False), - local_path='borg', - patterns=None, - ) - - def test_recreate_with_list_filter_flags(): - flexmock(module.borgmatic.borg.flags).should_receive('make_repository_flags').and_return( - ('repo::archive',) - ) - flexmock(module).should_receive('write_patterns_file').and_return(None) flexmock(module).should_receive('make_list_filter_flags').and_return('AME+-') insert_execute_command_mock( ('borg', 'recreate', '--list', '--filter', 'AME+-', 'repo::archive') @@ -221,7 +213,16 @@ def test_recreate_with_list_filter_flags(): archive='archive', config={}, local_borg_version='1.2.3', - recreate_arguments=flexmock(path=None, list=True), + recreate_arguments=flexmock( + list=True, + target=None, + comment=None, + timestamp=None, + compression=None, + chunker_params=None, + match_archives=None, + recompress=None, + ), global_arguments=flexmock(dry_run=False, log_json=False), local_path='borg', patterns=None, @@ -229,12 +230,8 @@ def test_recreate_with_list_filter_flags(): def test_recreate_with_patterns_from_flag(): - flexmock(module.borgmatic.borg.flags).should_receive('make_repository_flags').and_return( - ('repo::archive',) - ) mock_patterns_file = flexmock(name='patterns_file') flexmock(module).should_receive('write_patterns_file').and_return(mock_patterns_file) - flexmock(module).should_receive('make_list_filter_flags').and_return(None) insert_execute_command_mock( ('borg', 'recreate', '--patterns-from', 'patterns_file', 'repo::archive') ) @@ -244,7 +241,16 @@ def test_recreate_with_patterns_from_flag(): archive='archive', config={}, local_borg_version='1.2.3', - recreate_arguments=flexmock(path=None, list=None), + recreate_arguments=flexmock( + list=None, + target=None, + comment=None, + timestamp=None, + compression=None, + chunker_params=None, + match_archives=None, + recompress=None, + ), global_arguments=flexmock(dry_run=False, log_json=False), local_path='borg', patterns=['pattern1', 'pattern2'], @@ -252,14 +258,7 @@ def test_recreate_with_patterns_from_flag(): def test_recreate_with_exclude_flags(): - flexmock(module.borgmatic.borg.flags).should_receive( - 'make_repository_archive_flags' - ).and_return(('repo::archive',)) - flexmock(module).should_receive('write_patterns_file').and_return(None) - flexmock(module).should_receive('make_list_filter_flags').and_return(None) - # Mock the make_exclude_flags to return a sample exclude flag flexmock(module).should_receive('make_exclude_flags').and_return(('--exclude', 'pattern')) - insert_execute_command_mock(('borg', 'recreate', '--exclude', 'pattern', 'repo::archive')) module.recreate_archive( @@ -267,8 +266,18 @@ def test_recreate_with_exclude_flags(): archive='archive', config={'exclude_patterns': ['pattern']}, local_borg_version='1.2.3', - recreate_arguments=flexmock(path=None, list=None), + recreate_arguments=flexmock( + list=None, + target=None, + comment=None, + timestamp=None, + compression=None, + chunker_params=None, + match_archives=None, + recompress=None, + ), global_arguments=flexmock(dry_run=False, log_json=False), local_path='borg', patterns=None, ) +