diff --git a/src/ytdl_sub/utils/file_handler.py b/src/ytdl_sub/utils/file_handler.py index 5cea379d..7a0a4d37 100644 --- a/src/ytdl_sub/utils/file_handler.py +++ b/src/ytdl_sub/utils/file_handler.py @@ -115,6 +115,38 @@ class FileHandlerTransactionLog: self.files_removed.add(file_name) return self + def to_output_message(self, output_directory: str) -> str: + """ + Parameters + ---------- + output_directory + Path to the output directory. Included in the output message + + Returns + ------- + The output message to show users what was recorded in the transaction log + """ + lines: List[str] = [] + if self.files_created: + created_line = f"Files created in '{output_directory}'" + created_line_dash = "-" * 40 + lines.extend([created_line, created_line_dash]) + for file_path, file_metadata in sorted(self.files_created.items()): + lines.append(file_path) + if file_metadata: + lines.extend( + [f" {metadata_line.strip()}" for metadata_line in file_metadata.metadata] + ) + + if self.files_removed: + removed_line = f"Files removed from '{output_directory}'" + removed_line_dash = "-" * 40 + lines.extend([removed_line, removed_line_dash]) + for file_path in sorted(self.files_removed): + lines.append(file_path) + + return "\n".join(lines) + class FileHandler: """ diff --git a/tests/e2e/expected_transaction_log.py b/tests/e2e/expected_transaction_log.py index e69de29b..1ba07829 100644 --- a/tests/e2e/expected_transaction_log.py +++ b/tests/e2e/expected_transaction_log.py @@ -0,0 +1,47 @@ +from pathlib import Path +from typing import List + +from ytdl_sub.utils.file_handler import FileHandlerTransactionLog + +_TRANSACTION_LOG_SUMMARY_PATH = Path("tests/e2e/resources/transaction_log_summaries") + + +def assert_transaction_log_matches( + output_directory: str, + transaction_log: FileHandlerTransactionLog, + transaction_log_summary_file_name: str, +): + """ + Parameters + ---------- + output_directory + Output directory the files are saved to + transaction_log + Transaction log to check + expected_transaction_log_summary + Name if the transaction log summary to compare. + Lives in tests/e2e/resources/transaction_log_summaries + """ + + with open( + _TRANSACTION_LOG_SUMMARY_PATH / transaction_log_summary_file_name, "r", encoding="utf-8" + ) as summary_file: + expected_summary = summary_file.read() + expected_summary = expected_summary.format(output_directory=output_directory) + summary = transaction_log.to_output_message(output_directory=output_directory) + + # Ensure there are the same number of new lines + summary_lines: List[str] = summary.split("\n") + expected_summary_lines: List[str] = expected_summary.split("\n") + + assert len(summary_lines) == len( + expected_summary_lines + ), f"Summary number of lines differ: {len(summary_lines) != len(expected_summary_lines)}" + + for idx in range(len(summary_lines)): + line = summary_lines[idx] + expected_line = expected_summary_lines[idx] + assert ( + summary_lines[idx] == expected_summary_lines[idx], + f"Summary line {idx} differs: '{line}' != {expected_line}", + ) diff --git a/tests/e2e/youtube/test_channel_as_kodi_tv_show.py b/tests/e2e/youtube/test_channel_as_kodi_tv_show.py index f5efa6ca..0d8677ca 100644 --- a/tests/e2e/youtube/test_channel_as_kodi_tv_show.py +++ b/tests/e2e/youtube/test_channel_as_kodi_tv_show.py @@ -5,6 +5,7 @@ import pytest from conftest import assert_debug_log from e2e.expected_download import ExpectedDownloadFile from e2e.expected_download import ExpectedDownloads +from e2e.expected_transaction_log import assert_transaction_log_matches import ytdl_sub.downloaders.downloader from ytdl_sub.config.config_file import ConfigFile @@ -302,7 +303,11 @@ class TestChannelAsKodiTvShow: self, full_channel_subscription, expected_full_channel_download, output_directory ): transaction_log = full_channel_subscription.download(dry_run=True) - expected_full_channel_download.assert_dry_run_files_logged(transaction_log=transaction_log) + assert_transaction_log_matches( + output_directory=output_directory, + transaction_log=transaction_log, + transaction_log_summary_file_name="test_channel_as_kodi_tv_show__full_channel.txt", + ) def test_recent_channel_download( self, recent_channel_subscription, expected_recent_channel_download, output_directory