2017-07-09 18:41:55 +00:00
|
|
|
from io import StringIO
|
2017-07-10 17:37:11 +00:00
|
|
|
import os
|
2017-07-09 18:41:55 +00:00
|
|
|
import sys
|
|
|
|
|
|
|
|
from flexmock import flexmock
|
2017-07-10 17:37:11 +00:00
|
|
|
import pytest
|
2017-07-09 18:41:55 +00:00
|
|
|
|
|
|
|
from borgmatic.config import generate as module
|
|
|
|
|
|
|
|
|
|
|
|
def test_insert_newline_before_comment_does_not_raise():
|
|
|
|
field_name = 'foo'
|
|
|
|
config = module.yaml.comments.CommentedMap([(field_name, 33)])
|
|
|
|
config.yaml_set_comment_before_after_key(key=field_name, before='Comment',)
|
|
|
|
|
|
|
|
module._insert_newline_before_comment(config, field_name)
|
|
|
|
|
|
|
|
|
|
|
|
def test_write_configuration_does_not_raise():
|
2017-07-10 17:37:11 +00:00
|
|
|
flexmock(os.path).should_receive('exists').and_return(False)
|
2017-07-10 22:20:50 +00:00
|
|
|
flexmock(os).should_receive('makedirs')
|
2017-07-09 18:41:55 +00:00
|
|
|
builtins = flexmock(sys.modules['builtins'])
|
|
|
|
builtins.should_receive('open').and_return(StringIO())
|
2017-07-10 22:20:50 +00:00
|
|
|
flexmock(os).should_receive('chmod')
|
2017-07-09 18:41:55 +00:00
|
|
|
|
|
|
|
module.write_configuration('config.yaml', {})
|
|
|
|
|
|
|
|
|
2017-07-10 17:37:11 +00:00
|
|
|
def test_write_configuration_with_already_existing_file_raises():
|
|
|
|
flexmock(os.path).should_receive('exists').and_return(True)
|
|
|
|
|
|
|
|
with pytest.raises(FileExistsError):
|
|
|
|
module.write_configuration('config.yaml', {})
|
|
|
|
|
|
|
|
|
2017-07-10 22:20:50 +00:00
|
|
|
def test_write_configuration_with_already_existing_directory_does_not_raise():
|
|
|
|
flexmock(os.path).should_receive('exists').and_return(False)
|
|
|
|
flexmock(os).should_receive('makedirs').and_raise(FileExistsError)
|
|
|
|
builtins = flexmock(sys.modules['builtins'])
|
|
|
|
builtins.should_receive('open').and_return(StringIO())
|
|
|
|
flexmock(os).should_receive('chmod')
|
|
|
|
|
|
|
|
module.write_configuration('config.yaml', {})
|
|
|
|
|
|
|
|
|
2017-07-09 18:41:55 +00:00
|
|
|
def test_add_comments_to_configuration_does_not_raise():
|
|
|
|
# Ensure that it can deal with fields both in the schema and missing from the schema.
|
|
|
|
config = module.yaml.comments.CommentedMap([('foo', 33), ('bar', 44), ('baz', 55)])
|
|
|
|
schema = {
|
|
|
|
'map': {
|
|
|
|
'foo': {'desc': 'Foo'},
|
|
|
|
'bar': {'desc': 'Bar'},
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
module.add_comments_to_configuration(config, schema)
|
|
|
|
|
|
|
|
|
|
|
|
def test_generate_sample_configuration_does_not_raise():
|
|
|
|
builtins = flexmock(sys.modules['builtins'])
|
|
|
|
builtins.should_receive('open').with_args('schema.yaml').and_return('')
|
|
|
|
flexmock(module).should_receive('write_configuration')
|
|
|
|
flexmock(module).should_receive('_schema_to_sample_configuration')
|
|
|
|
|
|
|
|
module.generate_sample_configuration('config.yaml', 'schema.yaml')
|