Dan Helfman пре 1 година
родитељ
комит
13cf863d89

+ 3 - 1
borgmatic/config/generate.py

@@ -236,7 +236,9 @@ def merge_source_configuration_into_destination(destination_config, source_confi
     for field_name, source_value in source_config.items():
         # Since this key/value is from the source configuration, leave it uncommented and remove any
         # sentinel that would cause it to get commented out.
-        remove_commented_out_sentinel(ruamel.yaml.comments.CommentedMap(destination_config), field_name)
+        remove_commented_out_sentinel(
+            ruamel.yaml.comments.CommentedMap(destination_config), field_name
+        )
 
         # This is a mapping. Recurse for this key/value.
         if isinstance(source_value, collections.abc.Mapping):

+ 22 - 12
tests/integration/config/test_generate.py

@@ -10,7 +10,7 @@ from borgmatic.config import generate as module
 
 def test_insert_newline_before_comment_does_not_raise():
     field_name = 'foo'
-    config = module.yaml.comments.CommentedMap([(field_name, 33)])
+    config = module.ruamel.yaml.comments.CommentedMap([(field_name, 33)])
     config.yaml_set_comment_before_after_key(key=field_name, before='Comment')
 
     module.insert_newline_before_comment(config, field_name)
@@ -125,14 +125,16 @@ def test_write_configuration_with_already_existing_directory_does_not_raise():
 
 
 def test_add_comments_to_configuration_sequence_of_strings_does_not_raise():
-    config = module.yaml.comments.CommentedSeq(['foo', 'bar'])
+    config = module.ruamel.yaml.comments.CommentedSeq(['foo', 'bar'])
     schema = {'type': 'array', 'items': {'type': 'string'}}
 
     module.add_comments_to_configuration_sequence(config, schema)
 
 
 def test_add_comments_to_configuration_sequence_of_maps_does_not_raise():
-    config = module.yaml.comments.CommentedSeq([module.yaml.comments.CommentedMap([('foo', 'yo')])])
+    config = module.ruamel.yaml.comments.CommentedSeq(
+        [module.ruamel.yaml.comments.CommentedMap([('foo', 'yo')])]
+    )
     schema = {
         'type': 'array',
         'items': {'type': 'object', 'properties': {'foo': {'description': 'yo'}}},
@@ -142,7 +144,9 @@ def test_add_comments_to_configuration_sequence_of_maps_does_not_raise():
 
 
 def test_add_comments_to_configuration_sequence_of_maps_without_description_does_not_raise():
-    config = module.yaml.comments.CommentedSeq([module.yaml.comments.CommentedMap([('foo', 'yo')])])
+    config = module.ruamel.yaml.comments.CommentedSeq(
+        [module.ruamel.yaml.comments.CommentedMap([('foo', 'yo')])]
+    )
     schema = {'type': 'array', 'items': {'type': 'object', 'properties': {'foo': {}}}}
 
     module.add_comments_to_configuration_sequence(config, schema)
@@ -150,7 +154,7 @@ def test_add_comments_to_configuration_sequence_of_maps_without_description_does
 
 def test_add_comments_to_configuration_object_does_not_raise():
     # Ensure that it can deal with fields both in the schema and missing from the schema.
-    config = module.yaml.comments.CommentedMap([('foo', 33), ('bar', 44), ('baz', 55)])
+    config = module.ruamel.yaml.comments.CommentedMap([('foo', 33), ('bar', 44), ('baz', 55)])
     schema = {
         'type': 'object',
         'properties': {'foo': {'description': 'Foo'}, 'bar': {'description': 'Bar'}},
@@ -160,7 +164,7 @@ def test_add_comments_to_configuration_object_does_not_raise():
 
 
 def test_add_comments_to_configuration_object_with_skip_first_does_not_raise():
-    config = module.yaml.comments.CommentedMap([('foo', 33)])
+    config = module.ruamel.yaml.comments.CommentedMap([('foo', 33)])
     schema = {'type': 'object', 'properties': {'foo': {'description': 'Foo'}}}
 
     module.add_comments_to_configuration_object(config, schema, skip_first=True)
@@ -168,7 +172,7 @@ def test_add_comments_to_configuration_object_with_skip_first_does_not_raise():
 
 def test_remove_commented_out_sentinel_keeps_other_comments():
     field_name = 'foo'
-    config = module.yaml.comments.CommentedMap([(field_name, 33)])
+    config = module.ruamel.yaml.comments.CommentedMap([(field_name, 33)])
     config.yaml_set_comment_before_after_key(key=field_name, before='Actual comment.\nCOMMENT_OUT')
 
     module.remove_commented_out_sentinel(config, field_name)
@@ -180,7 +184,7 @@ def test_remove_commented_out_sentinel_keeps_other_comments():
 
 def test_remove_commented_out_sentinel_without_sentinel_keeps_other_comments():
     field_name = 'foo'
-    config = module.yaml.comments.CommentedMap([(field_name, 33)])
+    config = module.ruamel.yaml.comments.CommentedMap([(field_name, 33)])
     config.yaml_set_comment_before_after_key(key=field_name, before='Actual comment.')
 
     module.remove_commented_out_sentinel(config, field_name)
@@ -192,7 +196,7 @@ def test_remove_commented_out_sentinel_without_sentinel_keeps_other_comments():
 
 def test_remove_commented_out_sentinel_on_unknown_field_does_not_raise():
     field_name = 'foo'
-    config = module.yaml.comments.CommentedMap([(field_name, 33)])
+    config = module.ruamel.yaml.comments.CommentedMap([(field_name, 33)])
     config.yaml_set_comment_before_after_key(key=field_name, before='Actual comment.')
 
     module.remove_commented_out_sentinel(config, 'unknown')
@@ -201,7 +205,9 @@ def test_remove_commented_out_sentinel_on_unknown_field_does_not_raise():
 def test_generate_sample_configuration_does_not_raise():
     builtins = flexmock(sys.modules['builtins'])
     builtins.should_receive('open').with_args('schema.yaml').and_return('')
-    flexmock(module.yaml).should_receive('round_trip_load')
+    flexmock(module.ruamel.yaml).should_receive('YAML').and_return(
+        flexmock(load=lambda filename: {})
+    )
     flexmock(module).should_receive('schema_to_sample_configuration')
     flexmock(module).should_receive('merge_source_configuration_into_destination')
     flexmock(module).should_receive('render_configuration')
@@ -214,7 +220,9 @@ def test_generate_sample_configuration_does_not_raise():
 def test_generate_sample_configuration_with_source_filename_does_not_raise():
     builtins = flexmock(sys.modules['builtins'])
     builtins.should_receive('open').with_args('schema.yaml').and_return('')
-    flexmock(module.yaml).should_receive('round_trip_load')
+    flexmock(module.ruamel.yaml).should_receive('YAML').and_return(
+        flexmock(load=lambda filename: {})
+    )
     flexmock(module.load).should_receive('load_configuration')
     flexmock(module.normalize).should_receive('normalize')
     flexmock(module).should_receive('schema_to_sample_configuration')
@@ -229,7 +237,9 @@ def test_generate_sample_configuration_with_source_filename_does_not_raise():
 def test_generate_sample_configuration_with_dry_run_does_not_write_file():
     builtins = flexmock(sys.modules['builtins'])
     builtins.should_receive('open').with_args('schema.yaml').and_return('')
-    flexmock(module.yaml).should_receive('round_trip_load')
+    flexmock(module.ruamel.yaml).should_receive('YAML').and_return(
+        flexmock(load=lambda filename: {})
+    )
     flexmock(module).should_receive('schema_to_sample_configuration')
     flexmock(module).should_receive('merge_source_configuration_into_destination')
     flexmock(module).should_receive('render_configuration')

+ 7 - 7
tests/unit/config/test_generate.py

@@ -7,7 +7,7 @@ from borgmatic.config import generate as module
 
 
 def test_schema_to_sample_configuration_generates_config_map_with_examples():
-    flexmock(module.yaml.comments).should_receive('CommentedMap').replace_with(OrderedDict)
+    flexmock(module.ruamel.yaml.comments).should_receive('CommentedMap').replace_with(OrderedDict)
     flexmock(module).should_receive('add_comments_to_configuration_object')
     schema = {
         'type': 'object',
@@ -32,7 +32,7 @@ def test_schema_to_sample_configuration_generates_config_map_with_examples():
 
 
 def test_schema_to_sample_configuration_generates_config_sequence_of_strings_with_example():
-    flexmock(module.yaml.comments).should_receive('CommentedSeq').replace_with(list)
+    flexmock(module.ruamel.yaml.comments).should_receive('CommentedSeq').replace_with(list)
     flexmock(module).should_receive('add_comments_to_configuration_sequence')
     schema = {'type': 'array', 'items': {'type': 'string'}, 'example': ['hi']}
 
@@ -42,7 +42,7 @@ def test_schema_to_sample_configuration_generates_config_sequence_of_strings_wit
 
 
 def test_schema_to_sample_configuration_generates_config_sequence_of_maps_with_examples():
-    flexmock(module.yaml.comments).should_receive('CommentedSeq').replace_with(list)
+    flexmock(module.ruamel.yaml.comments).should_receive('CommentedSeq').replace_with(list)
     flexmock(module).should_receive('add_comments_to_configuration_sequence')
     flexmock(module).should_receive('add_comments_to_configuration_object')
     schema = {
@@ -71,7 +71,7 @@ def test_merge_source_configuration_into_destination_inserts_map_fields():
     destination_config = {'foo': 'dest1', 'bar': 'dest2'}
     source_config = {'foo': 'source1', 'baz': 'source2'}
     flexmock(module).should_receive('remove_commented_out_sentinel')
-    flexmock(module).should_receive('yaml.comments.CommentedSeq').replace_with(list)
+    flexmock(module).should_receive('ruamel.yaml.comments.CommentedSeq').replace_with(list)
 
     module.merge_source_configuration_into_destination(destination_config, source_config)
 
@@ -82,7 +82,7 @@ def test_merge_source_configuration_into_destination_inserts_nested_map_fields()
     destination_config = {'foo': {'first': 'dest1', 'second': 'dest2'}, 'bar': 'dest3'}
     source_config = {'foo': {'first': 'source1'}}
     flexmock(module).should_receive('remove_commented_out_sentinel')
-    flexmock(module).should_receive('yaml.comments.CommentedSeq').replace_with(list)
+    flexmock(module).should_receive('ruamel.yaml.comments.CommentedSeq').replace_with(list)
 
     module.merge_source_configuration_into_destination(destination_config, source_config)
 
@@ -93,7 +93,7 @@ def test_merge_source_configuration_into_destination_inserts_sequence_fields():
     destination_config = {'foo': ['dest1', 'dest2'], 'bar': ['dest3'], 'baz': ['dest4']}
     source_config = {'foo': ['source1'], 'bar': ['source2', 'source3']}
     flexmock(module).should_receive('remove_commented_out_sentinel')
-    flexmock(module).should_receive('yaml.comments.CommentedSeq').replace_with(list)
+    flexmock(module).should_receive('ruamel.yaml.comments.CommentedSeq').replace_with(list)
 
     module.merge_source_configuration_into_destination(destination_config, source_config)
 
@@ -108,7 +108,7 @@ def test_merge_source_configuration_into_destination_inserts_sequence_of_maps():
     destination_config = {'foo': [{'first': 'dest1', 'second': 'dest2'}], 'bar': 'dest3'}
     source_config = {'foo': [{'first': 'source1'}, {'other': 'source2'}]}
     flexmock(module).should_receive('remove_commented_out_sentinel')
-    flexmock(module).should_receive('yaml.comments.CommentedSeq').replace_with(list)
+    flexmock(module).should_receive('ruamel.yaml.comments.CommentedSeq').replace_with(list)
 
     module.merge_source_configuration_into_destination(destination_config, source_config)