diff --git a/borgmatic/borg/compact.py b/borgmatic/borg/compact.py index 2337a2da..2a6a717e 100644 --- a/borgmatic/borg/compact.py +++ b/borgmatic/borg/compact.py @@ -1,7 +1,7 @@ import logging import borgmatic.config.paths -from borgmatic.borg import environment, flags +from borgmatic.borg import environment, feature, flags from borgmatic.execute import execute_command logger = logging.getLogger(__name__) @@ -42,7 +42,12 @@ def compact_segments( ) if dry_run: - logging.info('Skipping compact (dry run)') + if feature.available(feature.Feature.DRY_RUN_COMPACT, local_borg_version): + logging.info('Skipping compact (dry run)') + else: + logging.warning( + 'The --dry-run option is not supported for compact in the current version of Borg.' + ) return execute_command( diff --git a/borgmatic/borg/feature.py b/borgmatic/borg/feature.py index c45899c0..32d9d1b2 100644 --- a/borgmatic/borg/feature.py +++ b/borgmatic/borg/feature.py @@ -18,6 +18,7 @@ class Feature(Enum): EXCLUDED_FILES_MINUS = 12 ARCHIVE_SERIES = 13 NO_PRUNE_STATS = 14 + DRY_RUN_COMPACT = 15 FEATURE_TO_MINIMUM_BORG_VERSION = { @@ -35,6 +36,7 @@ FEATURE_TO_MINIMUM_BORG_VERSION = { Feature.EXCLUDED_FILES_MINUS: parse('2.0.0b5'), # --list --filter uses "-" for excludes Feature.ARCHIVE_SERIES: parse('2.0.0b11'), # identically named archives form a series Feature.NO_PRUNE_STATS: parse('2.0.0b10'), # prune --stats is not available + Feature.DRY_RUN_COMPACT: parse('1.4.1'), # borg compact --dry-run support } diff --git a/tests/unit/borg/test_compact.py b/tests/unit/borg/test_compact.py index 75d89d05..c370280e 100644 --- a/tests/unit/borg/test_compact.py +++ b/tests/unit/borg/test_compact.py @@ -69,8 +69,25 @@ def test_compact_segments_with_log_debug_calls_borg_with_debug_flag(): def test_compact_segments_with_dry_run_skips_borg_call(): + # Test borg version supports dry run. + logger_mock = flexmock(logging) + logger_mock.should_receive('info').with_args('Skipping compact (dry run)').once() + flexmock(module).should_receive('execute_command').never() + module.compact_segments( + repository_path='repo', + config={}, + local_borg_version='1.4.1', + global_arguments=flexmock(), + dry_run=True, + ) + + # Test borg version that does not support dry run. + logger_mock.should_receive('warning').with_args( + 'The --dry-run option is not supported for compact in the current version of Borg.' + ).once() + module.compact_segments( repository_path='repo', config={},