|
| 1 | +"""Tests for scripts/cbz_clean_up.py.""" |
| 2 | + |
| 3 | +import os |
| 4 | + |
| 5 | +import scripts.cbz_clean_up as cbz_clean_up |
| 6 | +from tests.test_helpers import run_cli_with_env |
| 7 | + |
| 8 | + |
| 9 | +def test_cleanup_deletes_matching_files(tmp_path): |
| 10 | + """Delete only files ending in '_original.cbz'.""" |
| 11 | + keep_file = tmp_path / "book.cbz" |
| 12 | + delete_file = tmp_path / "book_original.cbz" |
| 13 | + non_cbz_file = tmp_path / "book_original.txt" |
| 14 | + |
| 15 | + keep_file.write_text("keep") |
| 16 | + delete_file.write_text("delete") |
| 17 | + non_cbz_file.write_text("keep") |
| 18 | + |
| 19 | + result = cbz_clean_up.clean_up_original_cbz_files(str(tmp_path)) |
| 20 | + |
| 21 | + assert result["success_count"] == 1 |
| 22 | + assert result["skipped_count"] == 2 |
| 23 | + assert result["failed_count"] == 0 |
| 24 | + assert keep_file.exists() |
| 25 | + assert not delete_file.exists() |
| 26 | + assert non_cbz_file.exists() |
| 27 | + |
| 28 | + |
| 29 | +def test_cleanup_is_recursive(tmp_path): |
| 30 | + """Delete matching files from nested subdirectories.""" |
| 31 | + nested = tmp_path / "nested" / "deeper" |
| 32 | + nested.mkdir(parents=True) |
| 33 | + delete_file = nested / "comic_original.cbz" |
| 34 | + keep_file = nested / "comic.cbz" |
| 35 | + |
| 36 | + delete_file.write_text("delete") |
| 37 | + keep_file.write_text("keep") |
| 38 | + |
| 39 | + result = cbz_clean_up.clean_up_original_cbz_files(str(tmp_path)) |
| 40 | + |
| 41 | + assert result["success_count"] == 1 |
| 42 | + assert result["skipped_count"] == 1 |
| 43 | + assert result["failed_count"] == 0 |
| 44 | + assert not delete_file.exists() |
| 45 | + assert keep_file.exists() |
| 46 | + |
| 47 | + |
| 48 | +def test_cleanup_does_not_delete_non_matching_original_names(tmp_path): |
| 49 | + """Do not delete files that only contain '_original' as a substring.""" |
| 50 | + files = [ |
| 51 | + tmp_path / "book_original_backup.cbz", |
| 52 | + tmp_path / "book.cbz", |
| 53 | + tmp_path / "book_originals.cbz", |
| 54 | + tmp_path / "other.txt", |
| 55 | + ] |
| 56 | + for file_path in files: |
| 57 | + file_path.write_text("keep") |
| 58 | + |
| 59 | + result = cbz_clean_up.clean_up_original_cbz_files(str(tmp_path)) |
| 60 | + |
| 61 | + assert result["success_count"] == 0 |
| 62 | + assert result["skipped_count"] == 4 |
| 63 | + assert result["failed_count"] == 0 |
| 64 | + for file_path in files: |
| 65 | + assert file_path.exists() |
| 66 | + |
| 67 | + |
| 68 | +def test_cleanup_matches_case_insensitive_extension(tmp_path): |
| 69 | + """Delete '_original.CBZ' with uppercase extension.""" |
| 70 | + delete_file = tmp_path / "book_original.CBZ" |
| 71 | + delete_file.write_text("delete") |
| 72 | + |
| 73 | + result = cbz_clean_up.clean_up_original_cbz_files(str(tmp_path)) |
| 74 | + |
| 75 | + assert result["success_count"] == 1 |
| 76 | + assert result["skipped_count"] == 0 |
| 77 | + assert result["failed_count"] == 0 |
| 78 | + assert not delete_file.exists() |
| 79 | + |
| 80 | + |
| 81 | +def test_cleanup_continue_on_delete_error(monkeypatch, tmp_path): |
| 82 | + """Continue scanning and report failed_count when os.remove raises OSError.""" |
| 83 | + failing_file = tmp_path / "bad_original.cbz" |
| 84 | + deleted_file = tmp_path / "good_original.cbz" |
| 85 | + skipped_file = tmp_path / "keep.cbz" |
| 86 | + |
| 87 | + failing_file.write_text("delete") |
| 88 | + deleted_file.write_text("delete") |
| 89 | + skipped_file.write_text("keep") |
| 90 | + |
| 91 | + original_remove = os.remove |
| 92 | + |
| 93 | + def fake_remove(path): |
| 94 | + if path == str(failing_file): |
| 95 | + raise OSError("permission denied") |
| 96 | + return original_remove(path) |
| 97 | + |
| 98 | + monkeypatch.setattr(cbz_clean_up.os, "remove", fake_remove) |
| 99 | + |
| 100 | + result = cbz_clean_up.clean_up_original_cbz_files(str(tmp_path)) |
| 101 | + |
| 102 | + assert result["success_count"] == 1 |
| 103 | + assert result["skipped_count"] == 1 |
| 104 | + assert result["failed_count"] == 1 |
| 105 | + assert failing_file.exists() |
| 106 | + assert not deleted_file.exists() |
| 107 | + assert skipped_file.exists() |
| 108 | + |
| 109 | + |
| 110 | +def test_main_uses_cbz_processor_env_var(monkeypatch, tmp_path): |
| 111 | + """main() should use CBZ_PROCESSOR_DIR without prompting.""" |
| 112 | + delete_file = tmp_path / "series_original.cbz" |
| 113 | + delete_file.write_text("delete") |
| 114 | + |
| 115 | + monkeypatch.setenv("CBZ_PROCESSOR_DIR", str(tmp_path)) |
| 116 | + |
| 117 | + captured = {} |
| 118 | + |
| 119 | + def fake_print_result(stats, _titles): |
| 120 | + captured["stats"] = stats |
| 121 | + |
| 122 | + monkeypatch.setattr(cbz_clean_up, "print_result", fake_print_result) |
| 123 | + |
| 124 | + cbz_clean_up.main() |
| 125 | + |
| 126 | + assert captured["stats"]["success_count"] == 1 |
| 127 | + assert not delete_file.exists() |
| 128 | + |
| 129 | + |
| 130 | +def test_cli_entry(tmp_path): |
| 131 | + """CLI module execution should run with CBZ_PROCESSOR_DIR set.""" |
| 132 | + env_vars = {"CBZ_PROCESSOR_DIR": str(tmp_path)} |
| 133 | + result = run_cli_with_env("scripts.cbz_clean_up", env_vars) |
| 134 | + assert result is not None |
| 135 | + assert result.returncode == 0 |
0 commit comments