From 7f735cbe59de783467c6504c121d912ca28f5e6c Mon Sep 17 00:00:00 2001 From: Dan Helfman Date: Wed, 24 Apr 2024 16:12:58 -0700 Subject: [PATCH] Fix a traceback with "check --only spot" when the "spot" check is unconfigured (#857). --- NEWS | 1 + borgmatic/actions/check.py | 8 +++++++- tests/unit/actions/test_check.py | 30 ++++++++++++++++++++++++++++++ 3 files changed, 38 insertions(+), 1 deletion(-) diff --git a/NEWS b/NEWS index 1352e53a..c508e9e1 100644 --- a/NEWS +++ b/NEWS @@ -3,6 +3,7 @@ * #851: Fix lack of file extraction when using "extract --strip-components all" on a path with a leading slash. * #854: Fix a traceback when the "data" consistency check is used. + # #857: Fix a traceback with "check --only spot" when the "spot" check is unconfigured. 1.8.10 * #656 (beta): Add a "spot" consistency check that compares file counts and contents between your diff --git a/borgmatic/actions/check.py b/borgmatic/actions/check.py index 60d8f05d..6a96f2d9 100644 --- a/borgmatic/actions/check.py +++ b/borgmatic/actions/check.py @@ -480,7 +480,13 @@ def spot_check( ''' log_label = f'{repository.get("label", repository["path"])}' logger.debug(f'{log_label}: Running spot check') - spot_check_config = next(check for check in config['checks'] if check['name'] == 'spot') + + try: + spot_check_config = next( + check for check in config.get('checks', ()) if check.get('name') == 'spot' + ) + except StopIteration: + raise ValueError('Cannot run spot check because it is unconfigured') if spot_check_config['data_tolerance_percentage'] > spot_check_config['data_sample_percentage']: raise ValueError( diff --git a/tests/unit/actions/test_check.py b/tests/unit/actions/test_check.py index 48627205..859d2b25 100644 --- a/tests/unit/actions/test_check.py +++ b/tests/unit/actions/test_check.py @@ -769,6 +769,36 @@ def test_compare_spot_check_hashes_considers_non_existent_path_as_not_matching() ) == ('/bar',) +def test_spot_check_without_spot_configuration_errors(): + with pytest.raises(ValueError): + module.spot_check( + repository={'path': 'repo'}, + config={ + 'checks': [ + { + 'name': 'archives', + }, + ] + }, + local_borg_version=flexmock(), + global_arguments=flexmock(), + local_path=flexmock(), + remote_path=flexmock(), + ) + + +def test_spot_check_without_any_configuration_errors(): + with pytest.raises(ValueError): + module.spot_check( + repository={'path': 'repo'}, + config={}, + local_borg_version=flexmock(), + global_arguments=flexmock(), + local_path=flexmock(), + remote_path=flexmock(), + ) + + def test_spot_check_data_tolerance_percenatge_greater_than_data_sample_percentage_errors(): with pytest.raises(ValueError): module.spot_check(