From e7f14bca87724b0d74d8d74d4ab85a225cd1dc0e Mon Sep 17 00:00:00 2001 From: Vandal <86826719+VandalByte@users.noreply.github.com> Date: Tue, 25 Mar 2025 00:16:20 +0530 Subject: [PATCH] add tests and requested changes --- borgmatic/actions/recreate.py | 5 ++-- borgmatic/borg/recreate.py | 29 +++++++++++++------- tests/unit/actions/test_recreate.py | 39 +++++++++++++++++++++++++++ tests/unit/borg/test_recreate.py | 0 tests/unit/commands/test_borgmatic.py | 21 +++++++++++++++ 5 files changed, 82 insertions(+), 12 deletions(-) create mode 100644 tests/unit/actions/test_recreate.py create mode 100644 tests/unit/borg/test_recreate.py diff --git a/borgmatic/actions/recreate.py b/borgmatic/actions/recreate.py index 0f3a0d79..53ee7fce 100644 --- a/borgmatic/actions/recreate.py +++ b/borgmatic/actions/recreate.py @@ -27,10 +27,9 @@ def run_recreate( else: logger.info('Recreating repository') - # collect and process patterns - patterns = collect_patterns(config) + # Collect and process patterns. processed_patterns = process_patterns( - patterns, borgmatic.config.paths.get_working_directory(config) + collect_patterns(config), borgmatic.config.paths.get_working_directory(config) ) borgmatic.borg.recreate.recreate_archive( diff --git a/borgmatic/borg/recreate.py b/borgmatic/borg/recreate.py index 9cc54679..a3c2e7e4 100644 --- a/borgmatic/borg/recreate.py +++ b/borgmatic/borg/recreate.py @@ -3,8 +3,8 @@ import logging import borgmatic.borg.environment import borgmatic.config.paths import borgmatic.execute -from borgmatic.borg.create import make_exclude_flags -from borgmatic.borg.flags import make_flags_from_arguments, make_repository_archive_flags +from borgmatic.borg.create import make_exclude_flags, write_patterns_file, make_list_filter_flags +from borgmatic.borg.flags import make_repository_archive_flags logger = logging.getLogger(__name__) @@ -31,20 +31,31 @@ def recreate_archive( repo_archive_arg = make_repository_archive_flags(repository, archive, local_borg_version) exclude_flags = make_exclude_flags(config) - # handle path from recreate_arguments - path_flag = ('--path', recreate_arguments.path) if recreate_arguments.path else () - pattern_flags = ('--patterns-from', patterns) if patterns else () + + # Write patterns to a temporary file and use that file with --patterns-from. + patterns_file = write_patterns_file( + patterns, borgmatic.config.paths.get_working_directory(config) + ) recreate_cmd = ( (local_path, 'recreate') + (('--remote-path', remote_path) if remote_path else ()) + repo_archive_arg - + path_flag + + (('--path', recreate_arguments.path) if recreate_arguments.path else ()) + (('--log-json',) if global_arguments.log_json else ()) + (('--lock-wait', str(lock_wait)) if lock_wait else ()) + (('--info',) if logger.getEffectiveLevel() == logging.INFO else ()) - + (('--debug', '--show-rc', '--list') if logger.isEnabledFor(logging.DEBUG) else ()) - + pattern_flags + + (('--debug', '--show-rc') if logger.isEnabledFor(logging.DEBUG) else ()) + + (('--patterns-from', patterns_file.name) if patterns_file else ()) + + ( + ( + '--list', + '--filter', + make_list_filter_flags(local_borg_version, global_arguments.dry_run), + ) + if recreate_arguments.list + else () + ) + exclude_flags ) @@ -54,7 +65,7 @@ def recreate_archive( borgmatic.execute.execute_command( recreate_cmd, - output_log_level=logging.ANSWER, + output_log_level=logging.INFO, environment=borgmatic.borg.environment.make_environment(config), working_directory=borgmatic.config.paths.get_working_directory(config), remote_path=remote_path, diff --git a/tests/unit/actions/test_recreate.py b/tests/unit/actions/test_recreate.py new file mode 100644 index 00000000..4250af2c --- /dev/null +++ b/tests/unit/actions/test_recreate.py @@ -0,0 +1,39 @@ +from flexmock import flexmock + +from borgmatic.actions import recreate as module + + +def test_run_recreate_does_not_raise(): + flexmock(module.logger).answer = lambda message: None + flexmock(module.borgmatic.config.validate).should_receive('repositories_match').and_return(True) + flexmock(module.borgmatic.borg.recreate).should_receive('recreate_archive') + + recreate_arguments = flexmock(repository=flexmock(), archive=None) + + module.run_recreate( + repository={'path': 'repo'}, + config={}, + local_borg_version=None, + recreate_arguments=recreate_arguments, + global_arguments=flexmock(), + local_path=None, + remote_path=None, + ) + + +def test_run_recreate_with_archive_does_not_raise(): + flexmock(module.logger).answer = lambda message: None + flexmock(module.borgmatic.config.validate).should_receive('repositories_match').and_return(True) + flexmock(module.borgmatic.borg.recreate).should_receive('recreate_archive') + + recreate_arguments = flexmock(repository=flexmock(), archive='test-archive') + + module.run_recreate( + repository={'path': 'repo'}, + config={}, + local_borg_version=None, + recreate_arguments=recreate_arguments, + global_arguments=flexmock(), + local_path=None, + remote_path=None, + ) diff --git a/tests/unit/borg/test_recreate.py b/tests/unit/borg/test_recreate.py new file mode 100644 index 00000000..e69de29b diff --git a/tests/unit/commands/test_borgmatic.py b/tests/unit/commands/test_borgmatic.py index 160f4454..0c474c7d 100644 --- a/tests/unit/commands/test_borgmatic.py +++ b/tests/unit/commands/test_borgmatic.py @@ -1019,6 +1019,27 @@ def test_run_actions_runs_create(): assert result == (expected,) +def test_run_actions_runs_recreate(): + flexmock(module).should_receive('add_custom_log_levels') + flexmock(module).should_receive('get_skip_actions').and_return([]) + flexmock(module.command).should_receive('Before_after_hooks').and_return(flexmock()) + + flexmock(borgmatic.actions.recreate).should_receive('run_recreate').once() + + tuple( + module.run_actions( + arguments={'global': flexmock(dry_run=False, log_file='foo'), 'recreate': flexmock()}, + config_filename=flexmock(), + config={'repositories': []}, + config_paths=[], + local_path=flexmock(), + remote_path=flexmock(), + local_borg_version=flexmock(), + repository={'path': 'repo'}, + ) + ) + + def test_run_actions_with_skip_actions_skips_create(): flexmock(module).should_receive('add_custom_log_levels') flexmock(module).should_receive('get_skip_actions').and_return(['create'])