From c41ffb5ceb03b4b62ca471d94fac32c7b26f37e4 Mon Sep 17 00:00:00 2001 From: Dan Helfman Date: Mon, 18 Feb 2019 13:22:14 -0800 Subject: [PATCH] If no extract repository is given, then error if there are multiple configured repositories. --- borgmatic/config/validate.py | 17 +++++++++++++++++ tests/unit/config/test_validate.py | 7 +++++++ 2 files changed, 24 insertions(+) diff --git a/borgmatic/config/validate.py b/borgmatic/config/validate.py index ba1306c..8c381c2 100644 --- a/borgmatic/config/validate.py +++ b/borgmatic/config/validate.py @@ -114,10 +114,27 @@ def guard_configuration_contains_repository(repository, configurations): Given a repository path and a dict mapping from config filename to corresponding parsed config dict, ensure that the repository is declared exactly once in all of the configurations. + If no repository is given, then error if there are multiple configured repositories. + Raise ValueError if the repository is not found in a configuration, or is declared multiple times. ''' if not repository: + count = len( + tuple( + config_repository + for config in configurations.values() + for config_repository in config['repositories'] + ) + ) + + if count > 1: + raise ValueError( + 'Can\'t determine which repository to extract. Use --repository option to disambiguate'.format( + repository + ) + ) + return count = len( diff --git a/tests/unit/config/test_validate.py b/tests/unit/config/test_validate.py index 41d5c99..42bb468 100644 --- a/tests/unit/config/test_validate.py +++ b/tests/unit/config/test_validate.py @@ -104,6 +104,13 @@ def test_guard_configuration_contains_repository_does_not_raise_when_repository_ ) +def test_guard_configuration_contains_repository_errors_when_repository_assumed_to_match_config_twice(): + with pytest.raises(ValueError): + module.guard_configuration_contains_repository( + repository=None, configurations={'config.yaml': {'repositories': ['repo', 'repo2']}} + ) + + def test_guard_configuration_contains_repository_errors_when_repository_missing_from_config(): with pytest.raises(ValueError): module.guard_configuration_contains_repository(