test_generate.py 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358
  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.ruamel.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_schema_to_sample_configuration_comments_out_non_default_options():
  13. schema = {
  14. 'type': 'object',
  15. 'properties': dict(
  16. [
  17. ('field1', {'type': 'string', 'example': 'Example 1'}),
  18. ('field2', {'type': 'string', 'example': 'Example 2'}),
  19. ('source_directories', {'type': 'string', 'example': 'Example 3'}),
  20. ]
  21. ),
  22. }
  23. config = module.schema_to_sample_configuration(schema)
  24. assert config == dict(
  25. [
  26. ('field1', 'Example 1'),
  27. ('field2', 'Example 2'),
  28. ('source_directories', 'Example 3'),
  29. ]
  30. )
  31. assert 'COMMENT_OUT' in config.ca.items['field1'][1][-1]._value
  32. assert 'COMMENT_OUT' in config.ca.items['field2'][1][-1]._value
  33. assert 'source_directories' not in config.ca.items
  34. def test_schema_to_sample_configuration_comments_out_non_source_config_options():
  35. schema = {
  36. 'type': 'object',
  37. 'properties': dict(
  38. [
  39. ('field1', {'type': 'string', 'example': 'Example 1'}),
  40. ('field2', {'type': 'string', 'example': 'Example 2'}),
  41. ('field3', {'type': 'string', 'example': 'Example 3'}),
  42. ]
  43. ),
  44. }
  45. source_config = {'field3': 'value'}
  46. config = module.schema_to_sample_configuration(schema, source_config)
  47. assert config == dict(
  48. [
  49. ('field1', 'Example 1'),
  50. ('field2', 'Example 2'),
  51. ('field3', 'Example 3'),
  52. ]
  53. )
  54. assert 'COMMENT_OUT' in config.ca.items['field1'][1][-1]._value
  55. assert 'COMMENT_OUT' in config.ca.items['field2'][1][-1]._value
  56. assert 'field3' not in config.ca.items
  57. def test_schema_to_sample_configuration_comments_out_non_default_options_in_sequence_of_maps():
  58. schema = {
  59. 'type': 'array',
  60. 'items': {
  61. 'type': 'object',
  62. 'properties': dict(
  63. [
  64. ('field1', {'type': 'string', 'example': 'Example 1'}),
  65. ('field2', {'type': 'string', 'example': 'Example 2'}),
  66. ('source_directories', {'type': 'string', 'example': 'Example 3'}),
  67. ]
  68. ),
  69. },
  70. }
  71. config = module.schema_to_sample_configuration(schema)
  72. assert config == [
  73. dict(
  74. [('field1', 'Example 1'), ('field2', 'Example 2'), ('source_directories', 'Example 3')]
  75. )
  76. ]
  77. # The first field in a sequence does not get commented.
  78. assert 'field1' not in config[0].ca.items
  79. assert 'COMMENT_OUT' in config[0].ca.items['field2'][1][-1]._value
  80. assert 'source_directories' not in config[0].ca.items
  81. def test_schema_to_sample_configuration_comments_out_non_source_config_options_in_sequence_of_maps():
  82. schema = {
  83. 'type': 'array',
  84. 'items': {
  85. 'type': 'object',
  86. 'properties': dict(
  87. [
  88. ('field1', {'type': 'string', 'example': 'Example 1'}),
  89. ('field2', {'type': 'string', 'example': 'Example 2'}),
  90. ('field3', {'type': 'string', 'example': 'Example 3'}),
  91. ]
  92. ),
  93. },
  94. }
  95. source_config = [{'field3': 'value'}]
  96. config = module.schema_to_sample_configuration(schema, source_config)
  97. assert config == [
  98. dict([('field1', 'Example 1'), ('field2', 'Example 2'), ('field3', 'Example 3')])
  99. ]
  100. # The first field in a sequence does not get commented.
  101. assert 'field1' not in config[0].ca.items
  102. assert 'COMMENT_OUT' in config[0].ca.items['field2'][1][-1]._value
  103. assert 'field3' not in config[0].ca.items
  104. def test_comment_out_line_skips_blank_line():
  105. line = ' \n'
  106. assert module.comment_out_line(line) == line
  107. def test_comment_out_line_skips_already_commented_out_line():
  108. line = ' # foo'
  109. assert module.comment_out_line(line) == line
  110. def test_comment_out_line_comments_section_name():
  111. line = 'figgy-pudding:'
  112. assert module.comment_out_line(line) == '# ' + line
  113. def test_comment_out_line_comments_indented_option():
  114. line = ' enabled: true'
  115. assert module.comment_out_line(line) == ' # enabled: true'
  116. def test_comment_out_line_comments_twice_indented_option():
  117. line = ' - item'
  118. assert module.comment_out_line(line) == ' # - item'
  119. def test_comment_out_optional_configuration_comments_optional_config_only():
  120. # The "# COMMENT_OUT" comment is a sentinel used to express that the following key is optional.
  121. # It's stripped out of the final output.
  122. flexmock(module).comment_out_line = lambda line: '# ' + line
  123. config = '''
  124. # COMMENT_OUT
  125. foo:
  126. # COMMENT_OUT
  127. bar:
  128. - baz
  129. - quux
  130. repositories:
  131. - one
  132. - two
  133. # This comment should be kept.
  134. # COMMENT_OUT
  135. other: thing
  136. '''
  137. # flake8: noqa
  138. expected_config = '''
  139. # foo:
  140. # bar:
  141. # - baz
  142. # - quux
  143. repositories:
  144. - one
  145. - two
  146. # This comment should be kept.
  147. # other: thing
  148. '''
  149. assert module.comment_out_optional_configuration(config.strip()) == expected_config.strip()
  150. def test_render_configuration_converts_configuration_to_yaml_string():
  151. yaml_string = module.render_configuration({'foo': 'bar'})
  152. assert yaml_string == 'foo: bar\n'
  153. def test_write_configuration_does_not_raise():
  154. flexmock(os.path).should_receive('exists').and_return(False)
  155. flexmock(os).should_receive('makedirs')
  156. builtins = flexmock(sys.modules['builtins'])
  157. builtins.should_receive('open').and_return(StringIO())
  158. flexmock(os).should_receive('chmod')
  159. module.write_configuration('config.yaml', 'config: yaml')
  160. def test_write_configuration_with_already_existing_file_raises():
  161. flexmock(os.path).should_receive('exists').and_return(True)
  162. with pytest.raises(FileExistsError):
  163. module.write_configuration('config.yaml', 'config: yaml')
  164. def test_write_configuration_with_already_existing_file_and_overwrite_does_not_raise():
  165. flexmock(os.path).should_receive('exists').and_return(True)
  166. module.write_configuration('/tmp/config.yaml', 'config: yaml', overwrite=True)
  167. def test_write_configuration_with_already_existing_directory_does_not_raise():
  168. flexmock(os.path).should_receive('exists').and_return(False)
  169. flexmock(os).should_receive('makedirs').and_raise(FileExistsError)
  170. builtins = flexmock(sys.modules['builtins'])
  171. builtins.should_receive('open').and_return(StringIO())
  172. flexmock(os).should_receive('chmod')
  173. module.write_configuration('config.yaml', 'config: yaml')
  174. def test_add_comments_to_configuration_sequence_of_strings_does_not_raise():
  175. config = module.ruamel.yaml.comments.CommentedSeq(['foo', 'bar'])
  176. schema = {'type': 'array', 'items': {'type': 'string'}}
  177. module.add_comments_to_configuration_sequence(config, schema)
  178. def test_add_comments_to_configuration_sequence_of_maps_does_not_raise():
  179. config = module.ruamel.yaml.comments.CommentedSeq(
  180. [module.ruamel.yaml.comments.CommentedMap([('foo', 'yo')])]
  181. )
  182. schema = {
  183. 'type': 'array',
  184. 'items': {'type': 'object', 'properties': {'foo': {'description': 'yo'}}},
  185. }
  186. module.add_comments_to_configuration_sequence(config, schema)
  187. def test_add_comments_to_configuration_sequence_of_maps_without_description_does_not_raise():
  188. config = module.ruamel.yaml.comments.CommentedSeq(
  189. [module.ruamel.yaml.comments.CommentedMap([('foo', 'yo')])]
  190. )
  191. schema = {'type': 'array', 'items': {'type': 'object', 'properties': {'foo': {}}}}
  192. module.add_comments_to_configuration_sequence(config, schema)
  193. def test_add_comments_to_configuration_comments_out_non_default_options():
  194. # Ensure that it can deal with fields both in the schema and missing from the schema.
  195. config = module.ruamel.yaml.comments.CommentedMap([('foo', 33), ('bar', 44), ('baz', 55)])
  196. schema = {
  197. 'type': 'object',
  198. 'properties': {'foo': {'description': 'Foo'}, 'bar': {'description': 'Bar'}},
  199. }
  200. module.add_comments_to_configuration_object(config, schema)
  201. assert 'COMMENT_OUT' in config.ca.items['foo'][1][-1]._value
  202. assert 'COMMENT_OUT' in config.ca.items['bar'][1][-1]._value
  203. assert 'baz' not in config.ca.items
  204. def test_add_comments_to_configuration_comments_out_non_source_config_options():
  205. # Ensure that it can deal with fields both in the schema and missing from the schema.
  206. config = module.ruamel.yaml.comments.CommentedMap(
  207. [('repositories', 33), ('bar', 44), ('baz', 55)]
  208. )
  209. schema = {
  210. 'type': 'object',
  211. 'properties': {
  212. 'repositories': {'description': 'repositories'},
  213. 'bar': {'description': 'Bar'},
  214. },
  215. }
  216. module.add_comments_to_configuration_object(config, schema)
  217. assert 'repositories' in config.ca.items
  218. assert 'COMMENT_OUT' in config.ca.items['bar'][1][-1]._value
  219. assert 'baz' not in config.ca.items
  220. def test_add_comments_to_configuration_object_with_skip_first_does_not_comment_out_first_option():
  221. config = module.ruamel.yaml.comments.CommentedMap([('foo', 33), ('bar', 44), ('baz', 55)])
  222. schema = {
  223. 'type': 'object',
  224. 'properties': {'foo': {'description': 'Foo'}, 'bar': {'description': 'Bar'}},
  225. }
  226. module.add_comments_to_configuration_object(config, schema, skip_first=True)
  227. assert 'foo' not in config.ca.items
  228. assert 'COMMENT_OUT' in config.ca.items['bar'][1][-1]._value
  229. assert 'baz' not in config.ca.items
  230. def test_generate_sample_configuration_does_not_raise():
  231. builtins = flexmock(sys.modules['builtins'])
  232. builtins.should_receive('open').with_args('schema.yaml').and_return('')
  233. flexmock(module.ruamel.yaml).should_receive('YAML').and_return(
  234. flexmock(load=lambda filename: {})
  235. )
  236. flexmock(module).should_receive('schema_to_sample_configuration')
  237. flexmock(module).should_receive('merge_source_configuration_into_destination')
  238. flexmock(module).should_receive('render_configuration')
  239. flexmock(module).should_receive('comment_out_optional_configuration')
  240. flexmock(module).should_receive('write_configuration')
  241. module.generate_sample_configuration(False, None, 'dest.yaml', 'schema.yaml')
  242. def test_generate_sample_configuration_with_source_filename_does_not_raise():
  243. builtins = flexmock(sys.modules['builtins'])
  244. builtins.should_receive('open').with_args('schema.yaml').and_return('')
  245. flexmock(module.ruamel.yaml).should_receive('YAML').and_return(
  246. flexmock(load=lambda filename: {})
  247. )
  248. flexmock(module.load).should_receive('load_configuration')
  249. flexmock(module.normalize).should_receive('normalize')
  250. flexmock(module).should_receive('schema_to_sample_configuration')
  251. flexmock(module).should_receive('merge_source_configuration_into_destination')
  252. flexmock(module).should_receive('render_configuration')
  253. flexmock(module).should_receive('comment_out_optional_configuration')
  254. flexmock(module).should_receive('write_configuration')
  255. module.generate_sample_configuration(False, 'source.yaml', 'dest.yaml', 'schema.yaml')
  256. def test_generate_sample_configuration_with_dry_run_does_not_write_file():
  257. builtins = flexmock(sys.modules['builtins'])
  258. builtins.should_receive('open').with_args('schema.yaml').and_return('')
  259. flexmock(module.ruamel.yaml).should_receive('YAML').and_return(
  260. flexmock(load=lambda filename: {})
  261. )
  262. flexmock(module).should_receive('schema_to_sample_configuration')
  263. flexmock(module).should_receive('merge_source_configuration_into_destination')
  264. flexmock(module).should_receive('render_configuration')
  265. flexmock(module).should_receive('comment_out_optional_configuration')
  266. flexmock(module).should_receive('write_configuration').never()
  267. module.generate_sample_configuration(True, None, 'dest.yaml', 'schema.yaml')