add tests

This commit is contained in:
Divyansh Singh 2023-03-18 17:24:21 +05:30
parent d17b2c74db
commit 997f60b3e6
2 changed files with 44 additions and 9 deletions

View file

@ -317,9 +317,7 @@ def check_all_source_directories_exist(source_directories):
if not os.path.exists(source_directory) if not os.path.exists(source_directory)
] ]
if missing_directories: if missing_directories:
raise ValueError( raise ValueError(f"Source directories do not exist: {', '.join(missing_directories)}")
'Source directories do not exist: {}'.format(', '.join(missing_directories))
)
def create_archive( def create_archive(
@ -509,7 +507,9 @@ def create_archive(
) )
elif output_log_level is None: elif output_log_level is None:
return execute_command_and_capture_output( return execute_command_and_capture_output(
create_command, working_directory=working_directory, extra_environment=borg_environment, create_command,
working_directory=working_directory,
extra_environment=borg_environment,
) )
else: else:
execute_command( execute_command(

View file

@ -207,7 +207,6 @@ def test_make_exclude_flags_includes_exclude_patterns_filename_when_given():
def test_make_exclude_flags_includes_exclude_from_filenames_when_in_config(): def test_make_exclude_flags_includes_exclude_from_filenames_when_in_config():
exclude_flags = module.make_exclude_flags( exclude_flags = module.make_exclude_flags(
location_config={'exclude_from': ['excludes', 'other']} location_config={'exclude_from': ['excludes', 'other']}
) )
@ -1054,7 +1053,8 @@ def test_create_archive_with_compression_calls_borg_with_compression_parameters(
@pytest.mark.parametrize( @pytest.mark.parametrize(
'feature_available,option_flag', ((True, '--upload-ratelimit'), (False, '--remote-ratelimit')), 'feature_available,option_flag',
((True, '--upload-ratelimit'), (False, '--remote-ratelimit')),
) )
def test_create_archive_with_upload_rate_limit_calls_borg_with_upload_ratelimit_parameters( def test_create_archive_with_upload_rate_limit_calls_borg_with_upload_ratelimit_parameters(
feature_available, option_flag feature_available, option_flag
@ -1189,7 +1189,8 @@ def test_create_archive_with_one_file_system_calls_borg_with_one_file_system_par
@pytest.mark.parametrize( @pytest.mark.parametrize(
'feature_available,option_flag', ((True, '--numeric-ids'), (False, '--numeric-owner')), 'feature_available,option_flag',
((True, '--numeric-ids'), (False, '--numeric-owner')),
) )
def test_create_archive_with_numeric_ids_calls_borg_with_numeric_ids_parameter( def test_create_archive_with_numeric_ids_calls_borg_with_numeric_ids_parameter(
feature_available, option_flag feature_available, option_flag
@ -1291,7 +1292,12 @@ def test_create_archive_with_read_special_calls_borg_with_read_special_parameter
@pytest.mark.parametrize( @pytest.mark.parametrize(
'option_name,option_value', 'option_name,option_value',
(('ctime', True), ('ctime', False), ('birthtime', True), ('birthtime', False),), (
('ctime', True),
('ctime', False),
('birthtime', True),
('birthtime', False),
),
) )
def test_create_archive_with_basic_option_calls_borg_with_corresponding_parameter( def test_create_archive_with_basic_option_calls_borg_with_corresponding_parameter(
option_name, option_value option_name, option_value
@ -1767,7 +1773,12 @@ def test_create_archive_with_progress_and_log_info_calls_borg_with_progress_para
) )
flexmock(module.environment).should_receive('make_environment') flexmock(module.environment).should_receive('make_environment')
flexmock(module).should_receive('execute_command').with_args( flexmock(module).should_receive('execute_command').with_args(
('borg', 'create') + REPO_ARCHIVE_WITH_PATHS + ('--info', '--progress',), ('borg', 'create')
+ REPO_ARCHIVE_WITH_PATHS
+ (
'--info',
'--progress',
),
output_log_level=logging.INFO, output_log_level=logging.INFO,
output_file=module.DO_NOT_CAPTURE, output_file=module.DO_NOT_CAPTURE,
borg_local_path='borg', borg_local_path='borg',
@ -2530,3 +2541,27 @@ def test_create_archive_with_stream_processes_calls_borg_with_processes_and_read
local_borg_version='1.2.3', local_borg_version='1.2.3',
stream_processes=processes, stream_processes=processes,
) )
def test_create_archive_with_non_existent_directory_and_source_directories_must_exist_raises_error():
"""
If a source directory doesn't exist and source_directories_must_exist is True, raise an error.
"""
flexmock(module.borgmatic.logger).should_receive('add_custom_log_levels')
flexmock(module.logging).ANSWER = module.borgmatic.logger.ANSWER
flexmock(module).should_receive('collect_borgmatic_source_directories').and_return([])
flexmock(module.os.path).should_receive('exists').and_return(False)
with pytest.raises(ValueError):
module.create_archive(
dry_run=False,
repository='repo',
location_config={
'source_directories': ['foo', 'bar'],
'repositories': ['repo'],
'exclude_patterns': None,
'source_directories_must_exist': True,
},
storage_config={},
local_borg_version='1.2.3',
)