test_generate.py 4.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153
  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. flexmock(module)._comment_out_line = lambda line: '# ' + line
  29. config = '''
  30. foo:
  31. bar:
  32. - baz
  33. - quux
  34. location:
  35. repositories:
  36. - one
  37. - two
  38. other: thing
  39. '''
  40. # flake8: noqa
  41. expected_config = '''
  42. # foo:
  43. # bar:
  44. # - baz
  45. # - quux
  46. #
  47. location:
  48. repositories:
  49. - one
  50. - two
  51. #
  52. # other: thing
  53. '''
  54. assert module._comment_out_optional_configuration(config.strip()) == expected_config.strip()
  55. def test_render_configuration_converts_configuration_to_yaml_string():
  56. yaml_string = module._render_configuration({'foo': 'bar'})
  57. assert yaml_string == 'foo: bar\n'
  58. def test_write_configuration_does_not_raise():
  59. flexmock(os.path).should_receive('exists').and_return(False)
  60. flexmock(os).should_receive('makedirs')
  61. builtins = flexmock(sys.modules['builtins'])
  62. builtins.should_receive('open').and_return(StringIO())
  63. flexmock(os).should_receive('chmod')
  64. module.write_configuration('config.yaml', 'config: yaml')
  65. def test_write_configuration_with_already_existing_file_raises():
  66. flexmock(os.path).should_receive('exists').and_return(True)
  67. with pytest.raises(FileExistsError):
  68. module.write_configuration('config.yaml', 'config: yaml')
  69. def test_write_configuration_with_already_existing_directory_does_not_raise():
  70. flexmock(os.path).should_receive('exists').and_return(False)
  71. flexmock(os).should_receive('makedirs').and_raise(FileExistsError)
  72. builtins = flexmock(sys.modules['builtins'])
  73. builtins.should_receive('open').and_return(StringIO())
  74. flexmock(os).should_receive('chmod')
  75. module.write_configuration('config.yaml', 'config: yaml')
  76. def test_add_comments_to_configuration_sequence_of_strings_does_not_raise():
  77. config = module.yaml.comments.CommentedSeq(['foo', 'bar'])
  78. schema = {'seq': [{'type': 'str'}]}
  79. module.add_comments_to_configuration_sequence(config, schema)
  80. def test_add_comments_to_configuration_sequence_of_maps_does_not_raise():
  81. config = module.yaml.comments.CommentedSeq([module.yaml.comments.CommentedMap([('foo', 'yo')])])
  82. schema = {'seq': [{'map': {'foo': {'desc': 'yo'}}}]}
  83. module.add_comments_to_configuration_sequence(config, schema)
  84. def test_add_comments_to_configuration_sequence_of_maps_without_description_does_not_raise():
  85. config = module.yaml.comments.CommentedSeq([module.yaml.comments.CommentedMap([('foo', 'yo')])])
  86. schema = {'seq': [{'map': {'foo': {}}}]}
  87. module.add_comments_to_configuration_sequence(config, schema)
  88. def test_add_comments_to_configuration_map_does_not_raise():
  89. # Ensure that it can deal with fields both in the schema and missing from the schema.
  90. config = module.yaml.comments.CommentedMap([('foo', 33), ('bar', 44), ('baz', 55)])
  91. schema = {'map': {'foo': {'desc': 'Foo'}, 'bar': {'desc': 'Bar'}}}
  92. module.add_comments_to_configuration_map(config, schema)
  93. def test_generate_sample_configuration_does_not_raise():
  94. builtins = flexmock(sys.modules['builtins'])
  95. builtins.should_receive('open').with_args('schema.yaml').and_return('')
  96. flexmock(module).should_receive('_schema_to_sample_configuration')
  97. flexmock(module).should_receive('_render_configuration')
  98. flexmock(module).should_receive('_comment_out_optional_configuration')
  99. flexmock(module).should_receive('write_configuration')
  100. module.generate_sample_configuration('config.yaml', 'schema.yaml')