test_generate.py 3.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126
  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_optional_configuration_comments_optional_config_only():
  25. flexmock(module)._comment_out_line = lambda line: '# ' + line
  26. config = '''
  27. foo:
  28. bar:
  29. - baz
  30. - quux
  31. location:
  32. repositories:
  33. - one
  34. - two
  35. other: thing
  36. '''
  37. # flake8: noqa
  38. expected_config = '''
  39. # foo:
  40. # bar:
  41. # - baz
  42. # - quux
  43. #
  44. location:
  45. repositories:
  46. - one
  47. - two
  48. #
  49. # other: thing
  50. '''
  51. assert module._comment_out_optional_configuration(config.strip()) == expected_config.strip()
  52. def test_render_configuration_does_not_raise():
  53. flexmock(module.yaml).should_receive('round_trip_dump')
  54. module._render_configuration({})
  55. def test_write_configuration_does_not_raise():
  56. flexmock(os.path).should_receive('exists').and_return(False)
  57. flexmock(os).should_receive('makedirs')
  58. builtins = flexmock(sys.modules['builtins'])
  59. builtins.should_receive('open').and_return(StringIO())
  60. flexmock(os).should_receive('chmod')
  61. module.write_configuration('config.yaml', 'config: yaml')
  62. def test_write_configuration_with_already_existing_file_raises():
  63. flexmock(os.path).should_receive('exists').and_return(True)
  64. with pytest.raises(FileExistsError):
  65. module.write_configuration('config.yaml', 'config: yaml')
  66. def test_write_configuration_with_already_existing_directory_does_not_raise():
  67. flexmock(os.path).should_receive('exists').and_return(False)
  68. flexmock(os).should_receive('makedirs').and_raise(FileExistsError)
  69. builtins = flexmock(sys.modules['builtins'])
  70. builtins.should_receive('open').and_return(StringIO())
  71. flexmock(os).should_receive('chmod')
  72. module.write_configuration('config.yaml', 'config: yaml')
  73. def test_add_comments_to_configuration_does_not_raise():
  74. # Ensure that it can deal with fields both in the schema and missing from the schema.
  75. config = module.yaml.comments.CommentedMap([('foo', 33), ('bar', 44), ('baz', 55)])
  76. schema = {'map': {'foo': {'desc': 'Foo'}, 'bar': {'desc': 'Bar'}}}
  77. module.add_comments_to_configuration(config, schema)
  78. def test_generate_sample_configuration_does_not_raise():
  79. builtins = flexmock(sys.modules['builtins'])
  80. builtins.should_receive('open').with_args('schema.yaml').and_return('')
  81. flexmock(module).should_receive('_schema_to_sample_configuration')
  82. flexmock(module).should_receive('_render_configuration')
  83. flexmock(module).should_receive('_comment_out_optional_configuration')
  84. flexmock(module).should_receive('write_configuration')
  85. module.generate_sample_configuration('config.yaml', 'schema.yaml')