2
0

test_generate.py 7.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215
  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_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 = {'seq': [{'type': 'str'}]}
  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 = {'seq': [{'map': {'foo': {'desc': 'yo'}}}]}
  88. module.add_comments_to_configuration_sequence(config, schema)
  89. def test_add_comments_to_configuration_sequence_of_maps_without_description_does_not_raise():
  90. config = module.yaml.comments.CommentedSeq([module.yaml.comments.CommentedMap([('foo', 'yo')])])
  91. schema = {'seq': [{'map': {'foo': {}}}]}
  92. module.add_comments_to_configuration_sequence(config, schema)
  93. def test_add_comments_to_configuration_map_does_not_raise():
  94. # Ensure that it can deal with fields both in the schema and missing from the schema.
  95. config = module.yaml.comments.CommentedMap([('foo', 33), ('bar', 44), ('baz', 55)])
  96. schema = {'map': {'foo': {'desc': 'Foo'}, 'bar': {'desc': 'Bar'}}}
  97. module.add_comments_to_configuration_map(config, schema)
  98. def test_add_comments_to_configuration_map_with_skip_first_does_not_raise():
  99. config = module.yaml.comments.CommentedMap([('foo', 33)])
  100. schema = {'map': {'foo': {'desc': 'Foo'}}}
  101. module.add_comments_to_configuration_map(config, schema, skip_first=True)
  102. def test_remove_commented_out_sentinel_keeps_other_comments():
  103. field_name = 'foo'
  104. config = module.yaml.comments.CommentedMap([(field_name, 33)])
  105. config.yaml_set_comment_before_after_key(key=field_name, before='Actual comment.\nCOMMENT_OUT')
  106. module.remove_commented_out_sentinel(config, field_name)
  107. comments = config.ca.items[field_name][module.RUAMEL_YAML_COMMENTS_INDEX]
  108. assert len(comments) == 1
  109. assert comments[0].value == '# Actual comment.\n'
  110. def test_remove_commented_out_sentinel_without_sentinel_keeps_other_comments():
  111. field_name = 'foo'
  112. config = module.yaml.comments.CommentedMap([(field_name, 33)])
  113. config.yaml_set_comment_before_after_key(key=field_name, before='Actual comment.')
  114. module.remove_commented_out_sentinel(config, field_name)
  115. comments = config.ca.items[field_name][module.RUAMEL_YAML_COMMENTS_INDEX]
  116. assert len(comments) == 1
  117. assert comments[0].value == '# Actual comment.\n'
  118. def test_remove_commented_out_sentinel_on_unknown_field_does_not_raise():
  119. field_name = 'foo'
  120. config = module.yaml.comments.CommentedMap([(field_name, 33)])
  121. config.yaml_set_comment_before_after_key(key=field_name, before='Actual comment.')
  122. module.remove_commented_out_sentinel(config, 'unknown')
  123. def test_generate_sample_configuration_does_not_raise():
  124. builtins = flexmock(sys.modules['builtins'])
  125. builtins.should_receive('open').with_args('schema.yaml').and_return('')
  126. flexmock(module.yaml).should_receive('round_trip_load')
  127. flexmock(module).should_receive('_schema_to_sample_configuration')
  128. flexmock(module).should_receive('merge_source_configuration_into_destination')
  129. flexmock(module).should_receive('_render_configuration')
  130. flexmock(module).should_receive('_comment_out_optional_configuration')
  131. flexmock(module).should_receive('write_configuration')
  132. module.generate_sample_configuration(None, 'dest.yaml', 'schema.yaml')
  133. def test_generate_sample_configuration_with_source_filename_does_not_raise():
  134. builtins = flexmock(sys.modules['builtins'])
  135. builtins.should_receive('open').with_args('schema.yaml').and_return('')
  136. flexmock(module.yaml).should_receive('round_trip_load')
  137. flexmock(module.load).should_receive('load_configuration')
  138. flexmock(module).should_receive('_schema_to_sample_configuration')
  139. flexmock(module).should_receive('merge_source_configuration_into_destination')
  140. flexmock(module).should_receive('_render_configuration')
  141. flexmock(module).should_receive('_comment_out_optional_configuration')
  142. flexmock(module).should_receive('write_configuration')
  143. module.generate_sample_configuration('source.yaml', 'dest.yaml', 'schema.yaml')