2
0

test_generate.py 7.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228
  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 test_render_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_file_and_overwrite_does_not_raise():
  75. flexmock(os.path).should_receive('exists').and_return(True)
  76. module.write_configuration('/tmp/config.yaml', 'config: yaml', overwrite=True)
  77. def test_write_configuration_with_already_existing_directory_does_not_raise():
  78. flexmock(os.path).should_receive('exists').and_return(False)
  79. flexmock(os).should_receive('makedirs').and_raise(FileExistsError)
  80. builtins = flexmock(sys.modules['builtins'])
  81. builtins.should_receive('open').and_return(StringIO())
  82. flexmock(os).should_receive('chmod')
  83. module.write_configuration('config.yaml', 'config: yaml')
  84. def test_add_comments_to_configuration_sequence_of_strings_does_not_raise():
  85. config = module.yaml.comments.CommentedSeq(['foo', 'bar'])
  86. schema = {'type': 'array', 'items': {'type': 'string'}}
  87. module.add_comments_to_configuration_sequence(config, schema)
  88. def test_add_comments_to_configuration_sequence_of_maps_does_not_raise():
  89. config = module.yaml.comments.CommentedSeq([module.yaml.comments.CommentedMap([('foo', 'yo')])])
  90. schema = {
  91. 'type': 'array',
  92. 'items': {'type': 'object', 'properties': {'foo': {'description': 'yo'}}},
  93. }
  94. module.add_comments_to_configuration_sequence(config, schema)
  95. def test_add_comments_to_configuration_sequence_of_maps_without_description_does_not_raise():
  96. config = module.yaml.comments.CommentedSeq([module.yaml.comments.CommentedMap([('foo', 'yo')])])
  97. schema = {'type': 'array', 'items': {'type': 'object', 'properties': {'foo': {}}}}
  98. module.add_comments_to_configuration_sequence(config, schema)
  99. def test_add_comments_to_configuration_object_does_not_raise():
  100. # Ensure that it can deal with fields both in the schema and missing from the schema.
  101. config = module.yaml.comments.CommentedMap([('foo', 33), ('bar', 44), ('baz', 55)])
  102. schema = {
  103. 'type': 'object',
  104. 'properties': {'foo': {'description': 'Foo'}, 'bar': {'description': 'Bar'}},
  105. }
  106. module.add_comments_to_configuration_object(config, schema)
  107. def test_add_comments_to_configuration_object_with_skip_first_does_not_raise():
  108. config = module.yaml.comments.CommentedMap([('foo', 33)])
  109. schema = {'type': 'object', 'properties': {'foo': {'description': 'Foo'}}}
  110. module.add_comments_to_configuration_object(config, schema, skip_first=True)
  111. def test_remove_commented_out_sentinel_keeps_other_comments():
  112. field_name = 'foo'
  113. config = module.yaml.comments.CommentedMap([(field_name, 33)])
  114. config.yaml_set_comment_before_after_key(key=field_name, before='Actual comment.\nCOMMENT_OUT')
  115. module.remove_commented_out_sentinel(config, field_name)
  116. comments = config.ca.items[field_name][module.RUAMEL_YAML_COMMENTS_INDEX]
  117. assert len(comments) == 1
  118. assert comments[0].value == '# Actual comment.\n'
  119. def test_remove_commented_out_sentinel_without_sentinel_keeps_other_comments():
  120. field_name = 'foo'
  121. config = module.yaml.comments.CommentedMap([(field_name, 33)])
  122. config.yaml_set_comment_before_after_key(key=field_name, before='Actual comment.')
  123. module.remove_commented_out_sentinel(config, field_name)
  124. comments = config.ca.items[field_name][module.RUAMEL_YAML_COMMENTS_INDEX]
  125. assert len(comments) == 1
  126. assert comments[0].value == '# Actual comment.\n'
  127. def test_remove_commented_out_sentinel_on_unknown_field_does_not_raise():
  128. field_name = 'foo'
  129. config = module.yaml.comments.CommentedMap([(field_name, 33)])
  130. config.yaml_set_comment_before_after_key(key=field_name, before='Actual comment.')
  131. module.remove_commented_out_sentinel(config, 'unknown')
  132. def test_generate_sample_configuration_does_not_raise():
  133. builtins = flexmock(sys.modules['builtins'])
  134. builtins.should_receive('open').with_args('schema.yaml').and_return('')
  135. flexmock(module.yaml).should_receive('round_trip_load')
  136. flexmock(module).should_receive('_schema_to_sample_configuration')
  137. flexmock(module).should_receive('merge_source_configuration_into_destination')
  138. flexmock(module).should_receive('render_configuration')
  139. flexmock(module).should_receive('_comment_out_optional_configuration')
  140. flexmock(module).should_receive('write_configuration')
  141. module.generate_sample_configuration(None, 'dest.yaml', 'schema.yaml')
  142. def test_generate_sample_configuration_with_source_filename_does_not_raise():
  143. builtins = flexmock(sys.modules['builtins'])
  144. builtins.should_receive('open').with_args('schema.yaml').and_return('')
  145. flexmock(module.yaml).should_receive('round_trip_load')
  146. flexmock(module.load).should_receive('load_configuration')
  147. flexmock(module.normalize).should_receive('normalize')
  148. flexmock(module).should_receive('_schema_to_sample_configuration')
  149. flexmock(module).should_receive('merge_source_configuration_into_destination')
  150. flexmock(module).should_receive('render_configuration')
  151. flexmock(module).should_receive('_comment_out_optional_configuration')
  152. flexmock(module).should_receive('write_configuration')
  153. module.generate_sample_configuration('source.yaml', 'dest.yaml', 'schema.yaml')