From 997f60b3e660a4b219c611d83d628c9bcf55626a Mon Sep 17 00:00:00 2001 From: Divyansh Singh Date: Sat, 18 Mar 2023 17:24:21 +0530 Subject: [PATCH] add tests --- borgmatic/borg/create.py | 8 +++--- tests/unit/borg/test_create.py | 45 ++++++++++++++++++++++++++++++---- 2 files changed, 44 insertions(+), 9 deletions(-) diff --git a/borgmatic/borg/create.py b/borgmatic/borg/create.py index f3c3c69..f459e51 100644 --- a/borgmatic/borg/create.py +++ b/borgmatic/borg/create.py @@ -317,9 +317,7 @@ def check_all_source_directories_exist(source_directories): if not os.path.exists(source_directory) ] if missing_directories: - raise ValueError( - 'Source directories do not exist: {}'.format(', '.join(missing_directories)) - ) + raise ValueError(f"Source directories do not exist: {', '.join(missing_directories)}") def create_archive( @@ -509,7 +507,9 @@ def create_archive( ) elif output_log_level is None: 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: execute_command( diff --git a/tests/unit/borg/test_create.py b/tests/unit/borg/test_create.py index 3c4e433..835ebed 100644 --- a/tests/unit/borg/test_create.py +++ b/tests/unit/borg/test_create.py @@ -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(): - exclude_flags = module.make_exclude_flags( location_config={'exclude_from': ['excludes', 'other']} ) @@ -1054,7 +1053,8 @@ def test_create_archive_with_compression_calls_borg_with_compression_parameters( @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( 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( - '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( feature_available, option_flag @@ -1291,7 +1292,12 @@ def test_create_archive_with_read_special_calls_borg_with_read_special_parameter @pytest.mark.parametrize( '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( 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).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_file=module.DO_NOT_CAPTURE, 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', 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', + )