浏览代码

Fix for the example not showing up in generated config for empty YAML objects (#303).

Dan Helfman 2 月之前
父节点
当前提交
017cbae4f9
共有 2 个文件被更改,包括 44 次插入10 次删除
  1. 15 10
      borgmatic/config/generate.py
  2. 29 0
      tests/unit/config/test_generate.py

+ 15 - 10
borgmatic/config/generate.py

@@ -53,16 +53,21 @@ def schema_to_sample_configuration(schema, source_config=None, level=0, parent_i
         if source_config and isinstance(source_config, list) and isinstance(source_config[0], dict):
             source_config = dict(collections.ChainMap(*source_config))
 
-        config = ruamel.yaml.comments.CommentedMap(
-            [
-                (
-                    field_name,
-                    schema_to_sample_configuration(
-                        sub_schema, (source_config or {}).get(field_name, {}), level + 1
-                    ),
-                )
-                for field_name, sub_schema in borgmatic.config.schema.get_properties(schema).items()
-            ]
+        config = (
+            ruamel.yaml.comments.CommentedMap(
+                [
+                    (
+                        field_name,
+                        schema_to_sample_configuration(
+                            sub_schema, (source_config or {}).get(field_name, {}), level + 1
+                        ),
+                    )
+                    for field_name, sub_schema in borgmatic.config.schema.get_properties(
+                        schema
+                    ).items()
+                ]
+            )
+            or example
         )
         indent = (level * INDENT) + (SEQUENCE_INDENT if parent_is_sequence else 0)
         add_comments_to_configuration_object(

+ 29 - 0
tests/unit/config/test_generate.py

@@ -39,6 +39,35 @@ def test_schema_to_sample_configuration_generates_config_map_with_examples():
     )
 
 
+def test_schema_to_sample_configuration_with_empty_object_generates_config_map_with_example():
+    schema = {
+        'type': 'object',
+        'example': {
+            'foo': 'Example 1',
+            'baz': 'Example 2',
+        },
+    }
+    flexmock(module.borgmatic.config.schema).should_receive('compare_types').and_return(False)
+    flexmock(module.borgmatic.config.schema).should_receive('compare_types').with_args(
+        'object', {'object'}
+    ).and_return(True)
+    flexmock(module.borgmatic.config.schema).should_receive('compare_types').with_args(
+        'string', module.SCALAR_SCHEMA_TYPES, match=all
+    ).and_return(True)
+    flexmock(module.borgmatic.config.schema).should_receive('get_properties').and_return({})
+    flexmock(module.ruamel.yaml.comments).should_receive('CommentedMap').replace_with(dict)
+    flexmock(module).should_receive('add_comments_to_configuration_object')
+
+    config = module.schema_to_sample_configuration(schema)
+
+    assert config == dict(
+        [
+            ('foo', 'Example 1'),
+            ('baz', 'Example 2'),
+        ]
+    )
+
+
 def test_schema_to_sample_configuration_generates_config_sequence_of_strings_with_example():
     flexmock(module.ruamel.yaml.comments).should_receive('CommentedSeq').replace_with(list)
     flexmock(module).should_receive('add_comments_to_configuration_sequence')