test_generate.py 7.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221
  1. import os
  2. import sys
  3. from io import StringIO
  4. import pytest
  5. from flexmock import flexmock
  6. from borgmatic.config import generate as module
  7. def test_insert_newline_before_comment_does_not_raise():
  8. field_name = 'foo'
  9. config = module.yaml.comments.CommentedMap([(field_name, 33)])
  10. config.yaml_set_comment_before_after_key(key=field_name, before='Comment')
  11. module._insert_newline_before_comment(config, field_name)
  12. def test_comment_out_line_skips_blank_line():
  13. line = ' \n'
  14. assert module._comment_out_line(line) == line
  15. def test_comment_out_line_skips_already_commented_out_line():
  16. line = ' # foo'
  17. assert module._comment_out_line(line) == line
  18. def test_comment_out_line_comments_section_name():
  19. line = 'figgy-pudding:'
  20. assert module._comment_out_line(line) == '# ' + line
  21. def test_comment_out_line_comments_indented_option():
  22. line = ' enabled: true'
  23. assert module._comment_out_line(line) == ' # enabled: true'
  24. def test_comment_out_line_comments_twice_indented_option():
  25. line = ' - item'
  26. assert module._comment_out_line(line) == ' # - item'
  27. def test_comment_out_optional_configuration_comments_optional_config_only():
  28. # The "# COMMENT_OUT" comment is a sentinel used to express that the following key is optional.
  29. # It's stripped out of the final output.
  30. flexmock(module)._comment_out_line = lambda line: '# ' + line
  31. config = '''
  32. # COMMENT_OUT
  33. foo:
  34. # COMMENT_OUT
  35. bar:
  36. - baz
  37. - quux
  38. location:
  39. repositories:
  40. - one
  41. - two
  42. # This comment should be kept.
  43. # COMMENT_OUT
  44. other: thing
  45. '''
  46. # flake8: noqa
  47. expected_config = '''
  48. # foo:
  49. # bar:
  50. # - baz
  51. # - quux
  52. location:
  53. repositories:
  54. - one
  55. - two
  56. # This comment should be kept.
  57. # other: thing
  58. '''
  59. assert module._comment_out_optional_configuration(config.strip()) == expected_config.strip()
  60. def testrender_configuration_converts_configuration_to_yaml_string():
  61. yaml_string = module.render_configuration({'foo': 'bar'})
  62. assert yaml_string == 'foo: bar\n'
  63. def test_write_configuration_does_not_raise():
  64. flexmock(os.path).should_receive('exists').and_return(False)
  65. flexmock(os).should_receive('makedirs')
  66. builtins = flexmock(sys.modules['builtins'])
  67. builtins.should_receive('open').and_return(StringIO())
  68. flexmock(os).should_receive('chmod')
  69. module.write_configuration('config.yaml', 'config: yaml')
  70. def test_write_configuration_with_already_existing_file_raises():
  71. flexmock(os.path).should_receive('exists').and_return(True)
  72. with pytest.raises(FileExistsError):
  73. module.write_configuration('config.yaml', 'config: yaml')
  74. def test_write_configuration_with_already_existing_directory_does_not_raise():
  75. flexmock(os.path).should_receive('exists').and_return(False)
  76. flexmock(os).should_receive('makedirs').and_raise(FileExistsError)
  77. builtins = flexmock(sys.modules['builtins'])
  78. builtins.should_receive('open').and_return(StringIO())
  79. flexmock(os).should_receive('chmod')
  80. module.write_configuration('config.yaml', 'config: yaml')
  81. def test_add_comments_to_configuration_sequence_of_strings_does_not_raise():
  82. config = module.yaml.comments.CommentedSeq(['foo', 'bar'])
  83. schema = {'type': 'array', 'items': {'type': 'string'}}
  84. module.add_comments_to_configuration_sequence(config, schema)
  85. def test_add_comments_to_configuration_sequence_of_maps_does_not_raise():
  86. config = module.yaml.comments.CommentedSeq([module.yaml.comments.CommentedMap([('foo', 'yo')])])
  87. schema = {
  88. 'type': 'array',
  89. 'items': {'type': 'object', 'properties': {'foo': {'description': 'yo'}}},
  90. }
  91. module.add_comments_to_configuration_sequence(config, schema)
  92. def test_add_comments_to_configuration_sequence_of_maps_without_description_does_not_raise():
  93. config = module.yaml.comments.CommentedSeq([module.yaml.comments.CommentedMap([('foo', 'yo')])])
  94. schema = {'type': 'array', 'items': {'type': 'object', 'properties': {'foo': {}}}}
  95. module.add_comments_to_configuration_sequence(config, schema)
  96. def test_add_comments_to_configuration_object_does_not_raise():
  97. # Ensure that it can deal with fields both in the schema and missing from the schema.
  98. config = module.yaml.comments.CommentedMap([('foo', 33), ('bar', 44), ('baz', 55)])
  99. schema = {
  100. 'type': 'object',
  101. 'properties': {'foo': {'description': 'Foo'}, 'bar': {'description': 'Bar'}},
  102. }
  103. module.add_comments_to_configuration_object(config, schema)
  104. def test_add_comments_to_configuration_object_with_skip_first_does_not_raise():
  105. config = module.yaml.comments.CommentedMap([('foo', 33)])
  106. schema = {'type': 'object', 'properties': {'foo': {'description': 'Foo'}}}
  107. module.add_comments_to_configuration_object(config, schema, skip_first=True)
  108. def test_remove_commented_out_sentinel_keeps_other_comments():
  109. field_name = 'foo'
  110. config = module.yaml.comments.CommentedMap([(field_name, 33)])
  111. config.yaml_set_comment_before_after_key(key=field_name, before='Actual comment.\nCOMMENT_OUT')
  112. module.remove_commented_out_sentinel(config, field_name)
  113. comments = config.ca.items[field_name][module.RUAMEL_YAML_COMMENTS_INDEX]
  114. assert len(comments) == 1
  115. assert comments[0].value == '# Actual comment.\n'
  116. def test_remove_commented_out_sentinel_without_sentinel_keeps_other_comments():
  117. field_name = 'foo'
  118. config = module.yaml.comments.CommentedMap([(field_name, 33)])
  119. config.yaml_set_comment_before_after_key(key=field_name, before='Actual comment.')
  120. module.remove_commented_out_sentinel(config, field_name)
  121. comments = config.ca.items[field_name][module.RUAMEL_YAML_COMMENTS_INDEX]
  122. assert len(comments) == 1
  123. assert comments[0].value == '# Actual comment.\n'
  124. def test_remove_commented_out_sentinel_on_unknown_field_does_not_raise():
  125. field_name = 'foo'
  126. config = module.yaml.comments.CommentedMap([(field_name, 33)])
  127. config.yaml_set_comment_before_after_key(key=field_name, before='Actual comment.')
  128. module.remove_commented_out_sentinel(config, 'unknown')
  129. def test_generate_sample_configuration_does_not_raise():
  130. builtins = flexmock(sys.modules['builtins'])
  131. builtins.should_receive('open').with_args('schema.yaml').and_return('')
  132. flexmock(module.yaml).should_receive('round_trip_load')
  133. flexmock(module).should_receive('_schema_to_sample_configuration')
  134. flexmock(module).should_receive('merge_source_configuration_into_destination')
  135. flexmock(module).should_receive('render_configuration')
  136. flexmock(module).should_receive('_comment_out_optional_configuration')
  137. flexmock(module).should_receive('write_configuration')
  138. module.generate_sample_configuration(None, 'dest.yaml', 'schema.yaml')
  139. def test_generate_sample_configuration_with_source_filename_does_not_raise():
  140. builtins = flexmock(sys.modules['builtins'])
  141. builtins.should_receive('open').with_args('schema.yaml').and_return('')
  142. flexmock(module.yaml).should_receive('round_trip_load')
  143. flexmock(module.load).should_receive('load_configuration')
  144. flexmock(module).should_receive('_schema_to_sample_configuration')
  145. flexmock(module).should_receive('merge_source_configuration_into_destination')
  146. flexmock(module).should_receive('render_configuration')
  147. flexmock(module).should_receive('_comment_out_optional_configuration')
  148. flexmock(module).should_receive('write_configuration')
  149. module.generate_sample_configuration('source.yaml', 'dest.yaml', 'schema.yaml')