Quellcode durchsuchen

If no extract repository is given, then error if there are multiple configured repositories.

Dan Helfman vor 6 Jahren
Ursprung
Commit
c41ffb5ceb
2 geänderte Dateien mit 24 neuen und 0 gelöschten Zeilen
  1. 17 0
      borgmatic/config/validate.py
  2. 7 0
      tests/unit/config/test_validate.py

+ 17 - 0
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
     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.
     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
     Raise ValueError if the repository is not found in a configuration, or is declared multiple
     times.
     times.
     '''
     '''
     if not repository:
     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
         return
 
 
     count = len(
     count = len(

+ 7 - 0
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():
 def test_guard_configuration_contains_repository_errors_when_repository_missing_from_config():
     with pytest.raises(ValueError):
     with pytest.raises(ValueError):
         module.guard_configuration_contains_repository(
         module.guard_configuration_contains_repository(