diff --git a/app/file_copier.py b/app/file_copier.py index 65d450b..ab1f20c 100644 --- a/app/file_copier.py +++ b/app/file_copier.py @@ -29,7 +29,8 @@ def __copy_file(self, file): file.source_filepath, file.destination_filepath) else: file.copied = False - + Logger(file.destination_directory()).log_unsuccessful_copy( + file.source_filepath, file.destination_filepath) self.file_gateway.update_copied(file) def __file_copied(self, file): diff --git a/app/logger.py b/app/logger.py index fd9decf..9444ff2 100644 --- a/app/logger.py +++ b/app/logger.py @@ -17,7 +17,12 @@ def __init__(self, destination_directory): self.log_file_path = self.__init_log_file(destination_directory) def log_successful_copy(self, source_file_path, destination_filepath): - self.__write_to_logfile(source_file_path, destination_filepath) + log_entry = f'Copy succeeded: {source_file_path} copied to {destination_filepath}' + self.__write_to_logfile(log_entry) + + def log_unsuccessful_copy(self, source_file_path, destination_filepath): + log_entry = f'Copy failed: {source_file_path} not copied to {destination_filepath}' + self.__write_to_logfile(log_entry) def __init_log_file(self, destination_directory): timestamp = datetime.now().strftime('%Y-%m-%d-%H%M.%S') @@ -28,6 +33,6 @@ def __init_log_file(self, destination_directory): return log_file_path - def __write_to_logfile(self, source_file_path, destination_filepath): + def __write_to_logfile(self, log_entry): with open(self.log_file_path, 'a') as file: - file.write(f'{source_file_path} copied to {destination_filepath}') + file.write(log_entry) diff --git a/test/test_logger.py b/test/test_logger.py index 79d7b72..f79a371 100644 --- a/test/test_logger.py +++ b/test/test_logger.py @@ -19,7 +19,7 @@ def test_init_log_file_creates_file(): assert os.path.basename(logger.log_file_path) == expected_filename -def test_write_to_logfile(): +def test_successful_write_to_logfile(): source_file_path = 'test_source.txt' destination_filepath = '/destination' + source_file_path logger.log_successful_copy(source_file_path, destination_filepath) @@ -27,5 +27,17 @@ def test_write_to_logfile(): with open(logger.log_file_path, 'r') as file: content = file.read() - expected_entry = f'{source_file_path} copied to {destination_filepath}' + expected_entry = f'Copy succeeded: {source_file_path} copied to {destination_filepath}' + assert expected_entry in content + + +def test_unsuccessful_write_to_logfile(): + source_file_path = 'test_source.txt' + destination_filepath = '/destination' + source_file_path + logger.log_unsuccessful_copy(source_file_path, destination_filepath) + + with open(logger.log_file_path, 'r') as file: + content = file.read() + + expected_entry = f'Copy failed: {source_file_path} not copied to {destination_filepath}' assert expected_entry in content