Przeglądaj źródła

add test for complex constant

Divyansh Singh 2 lat temu
rodzic
commit
af95134cd2
2 zmienionych plików z 18 dodań i 1 usunięć
  1. 3 1
      borgmatic/config/load.py
  2. 15 0
      tests/integration/config/test_load.py

+ 3 - 1
borgmatic/config/load.py

@@ -1,4 +1,5 @@
 import functools
+import json
 import logging
 import os
 
@@ -104,7 +105,8 @@ def load_configuration(filename):
         config = yaml.load(file_contents)
         if config and 'constants' in config:
             for key, value in config['constants'].items():
-                file_contents = file_contents.replace(f'{{{key}}}', str(value))
+                value = json.dumps(value)
+                file_contents = file_contents.replace(f'{{{key}}}', value)
             config = yaml.load(file_contents)
             del config['constants']
         return config

+ 15 - 0
tests/integration/config/test_load.py

@@ -30,6 +30,21 @@ def test_load_configuration_replaces_constants():
     assert module.load_configuration('config.yaml') == {'key': 'value'}
 
 
+def test_load_configuration_replaces_complex_constants():
+    builtins = flexmock(sys.modules['builtins'])
+    config_file = io.StringIO(
+        '''
+        constants:
+            key:
+                subkey: value
+        key: {key}
+        '''
+    )
+    config_file.name = 'config.yaml'
+    builtins.should_receive('open').with_args('config.yaml').and_return(config_file)
+    assert module.load_configuration('config.yaml') == {'key': {'subkey': 'value'}}
+
+
 def test_load_configuration_inlines_include_relative_to_current_directory():
     builtins = flexmock(sys.modules['builtins'])
     flexmock(module.os).should_receive('getcwd').and_return('/tmp')