test_generate.py 3.5 KB

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