def test_run() -> None: _teardown() settings = Settings() settings.can_do = False wos = Walk(settings) res = wos.run() assert not res _teardown()
def test_run_optimize_after() -> None: _teardown() settings = Settings() settings.optimize_after = 3000 wos = Walk(settings) res = wos.run() assert res _teardown()
def test_is_skippable_quiet() -> None: _teardown() TMP_ROOT.mkdir() path = TMP_ROOT / "test.txt" settings = Settings() settings.verbose = 0 wob = Walk(settings) res = wob._is_skippable(path) assert res _teardown()
def test_optimize_image_none_quiet() -> None: old_path = _setup() settings = Settings() settings.verbose = 1 args = (old_path, "", settings) res = optimize.optimize_image(args) assert res.final_path == old_path assert res.bytes_in == 0 assert res.bytes_out == 0 assert res.error == "File format not selected." _teardown()
def test_is_skippable_symlink() -> None: _teardown() TMP_ROOT.mkdir() path = TMP_ROOT / "test.txt" path.symlink_to(TMP_ROOT) settings = Settings() settings.follow_symlinks = False wob = Walk(settings) res = wob._is_skippable(path) assert res _teardown()
def test_walk_file_list_only() -> None: _teardown() TMP_ROOT.mkdir() path = TMP_ROOT / "test.jpg" shutil.copy(TEST_JPG_SRC, path) path.touch() settings = Settings() settings.list_only = True wos = Walk(settings) res = wos.walk_file(path, None) assert len(res) == 0 _teardown()
def test_optimize_with_all_progs_disabled() -> None: old_path = _setup() settings = Settings() settings.mozjpeg = False settings.jpegtran = False # settings.jpegrescan = False res = optimize._optimize_with_progs(settings, Jpeg, old_path, "JPEG") old_path.unlink() assert res.final_path == old_path assert res.bytes_in == 0 assert res.bytes_out == 0 _teardown()
def test_os_error() -> None: old_size = 5 new_size = 50 old_format = _PNG_FORMAT new_format = _PNG_FORMAT settings = Settings() settings.bigger = True settings.test = True (path, b_in, b_out), old_path, new_path = _cleanup_aux(old_size, new_size, old_format, new_format, settings, True) assert path.suffix == "" assert 0 == b_in assert 0 == b_out _teardown()
def test_small_big_format_change_bigger() -> None: old_size = 5 new_size = 50 old_format = _BMP_FORMAT new_format = _PNG_FORMAT settings = Settings() settings.bigger = True (path, b_in, b_out), old_path, new_path = _cleanup_aux(old_size, new_size, old_format, new_format, settings) assert new_path.is_file() assert not old_path.exists() assert new_path == old_path.with_suffix(_fmt_to_suffix(new_format)) assert old_size == b_in assert new_size == b_out _teardown()
def test_gifiscle() -> None: _setup() shutil.copy(TEST_GIF_SRC, OLD_PATH) args = ExtArgs("", str(OLD_PATH)) res = gif.Gif.gifsicle(Settings(), args) assert res == "GIF" _teardown()
def test_comic_archive_skip() -> None: wob = Walk(Settings()) path = TMP_ROOT / "xxx" rep = ReportStats(path) res = wob._comic_archive_skip((rep,)) assert res.final_path == path _teardown()
def test_small_big_bigger() -> None: old_size = 5 new_size = 50 old_format = _PNG_FORMAT new_format = _PNG_FORMAT settings = Settings() settings.bigger = True (path, b_in, b_out), old_path, new_path = _cleanup_aux(old_size, new_size, old_format, new_format, settings) print(new_path) assert old_path.is_file() assert new_path == old_path assert old_size == b_in assert new_size == b_out assert new_size == old_path.stat().st_size _teardown()
def _setup(path: Path = TMP_ROOT) -> Tuple[Path, float, Timestamp]: _teardown() path.mkdir(parents=True) record_path = path / timestamp.RECORD_FILENAME record_path.touch() mtime = record_path.stat().st_mtime tso = Timestamp(Settings()) return record_path, mtime, tso
def test_optimize_image_error() -> None: old_path = Path("") args = (old_path, "JPEG", Settings()) res = optimize.optimize_image(args) assert res.final_path == old_path assert res.bytes_in == 0 assert res.bytes_out == 0 assert res.error == "Optimizing Image" _teardown()
def test_optimize_image() -> None: old_path = _setup() old_size = old_path.stat().st_size args = (old_path, "JPEG", Settings()) res = optimize.optimize_image(args) assert res.final_path == old_path assert res.bytes_in == old_size assert res.bytes_out == NEW_SIZE _teardown()
def test_optimize_with_progs() -> None: fmt = "JPEG" old_path = _setup(fmt) old_size = old_path.stat().st_size res = optimize._optimize_with_progs(Settings(), Jpeg, old_path, fmt) assert res.final_path == old_path assert res.bytes_in == old_size assert res.bytes_out == NEW_SIZE _teardown()
def test_walk_dir_recurse() -> None: _teardown() dir_path = TMP_ROOT / "deep" dir_path.mkdir(parents=True) settings = Settings() wos = Walk(settings) res = wos.walk_dir(dir_path, None, True) assert len(res) == 0 _teardown()
def test_walk_file_older_than() -> None: _teardown() TMP_ROOT.mkdir() path = TMP_ROOT / "text.txt" path.touch() walk_after = path.stat().st_mtime wos = Walk(Settings()) res = wos.walk_file(path, walk_after) assert len(res) == 0 _teardown()
def test_is_skippable_timestamp() -> None: _teardown() TMP_ROOT.mkdir() path = TMP_ROOT / RECORD_FILENAME path.touch() settings = Settings() wob = Walk(settings) res = wob._is_skippable(path) assert res _teardown()
def test_optimize_with_progs_no_best() -> None: fmt = "PNG" old_path = _setup(fmt) old_size = old_path.stat().st_size res = optimize._optimize_with_progs(Settings(), Png, old_path, fmt) old_path.unlink() assert res.final_path == old_path assert res.bytes_in == old_size assert res.bytes_out == 4379 # could change. maybe just > 0 _teardown()
def test_walk_all_files_empty() -> None: _teardown() wos = Walk(Settings()) record_dirs, bytes_in, bytes_out, nag, errors = wos._walk_all_files() assert len(record_dirs) == 0 assert bytes_in == 0 assert bytes_out == 0 assert not nag assert len(errors) == 0 _teardown()
def test_is_skippable_unset() -> None: _teardown() TMP_ROOT.mkdir() path = TMP_ROOT / "test.txt" path.touch() settings = Settings() wob = Walk(settings) res = wob._is_skippable(path) assert not res _teardown()
def test_run_record_timestamp() -> None: _teardown() TMP_ROOT.mkdir() settings = Settings( None, Namespace(record_timestamp=True, paths=[TMP_ROOT], recurse=True) ) wos = Walk(settings) res = wos.run() assert res _teardown()
def _setup_uncompress(fmt: str) -> Tuple[Path, str, Path, Settings]: _setup() if fmt == "CBR": src = TEST_CBR_SRC elif fmt == "CBZ": src = TEST_CBZ_SRC comic_fn = "test" + src.suffix comic_path = TMP_ROOT / comic_fn shutil.copy(src, comic_path) uncomp_dir = TMP_ROOT / Path("picopt_tmp_" + comic_fn) return comic_path, fmt, uncomp_dir, Settings()
def test_optimize_image_external() -> None: old_path = _setup() old_size = old_path.stat().st_size func = Jpeg.PROGRAMS[0] res = optimize._optimize_image_external( Settings(), old_path, func, "JPEG", old_path.suffix ) assert res.final_path == old_path assert res.bytes_in == old_size assert res.bytes_out == NEW_SIZE _teardown()
def test_walk_file_dir() -> None: _teardown() dir_path = TMP_ROOT / "deep" dir_path.mkdir(parents=True) path = dir_path / "test.txt" path.touch() settings = Settings() wos = Walk(settings) res = wos.walk_file(dir_path, None) assert len(res) == 0 _teardown()
def test_walk_comic_archive_bigger() -> None: _teardown() TMP_ROOT.mkdir() cbr = TMP_ROOT / "test.cbr" cbz = TMP_ROOT / "test.cbz" shutil.copy(TEST_CBR_SRC, cbr) old_size = cbr.stat().st_size settings = Settings() settings.comics = True settings.bigger = True wob = Walk(settings) result_set = wob.walk_comic_archive(cbr, "CBR", None) res = result_set.pop() rep = res.get() assert not rep.error assert rep.final_path == cbz assert rep.bytes_in == old_size assert rep.bytes_out > 0 wob._pool.close() wob._pool.join() _teardown()
def test_small_big() -> None: old_size = 32 new_size = 40 old_format = _PNG_FORMAT new_format = _PNG_FORMAT (path, b_in, b_out), old_path, new_path = _cleanup_aux(old_size, new_size, old_format, new_format, Settings()) assert old_path.is_file() assert old_size == b_in assert old_size == b_out assert old_path.stat().st_size == old_size _teardown()
def test_walk_all_files_one() -> None: _teardown() TMP_ROOT.mkdir() path = TMP_ROOT / "test.jpg" shutil.copy(TEST_JPG_SRC, path) wos = Walk(Settings(None, Namespace(paths=[TMP_ROOT], recurse=True))) record_dirs, bytes_in, bytes_out, nag, errors = wos._walk_all_files() assert len(record_dirs) == 1 assert bytes_in == 97373 assert bytes_out == 87922 assert not nag assert len(errors) == 0 _teardown()
def test_walk_file_comic() -> None: _teardown() TMP_ROOT.mkdir() path = TMP_ROOT / "test.cbr" shutil.copy(TEST_CBR_SRC, path) settings = Settings(None, Namespace(comics=True, bigger=True)) wos = Walk(settings) res = wos.walk_file(path, None) assert len(res) == 1 rep = res.pop().get() wos._pool.close() wos._pool.join() assert rep.final_path == path.with_suffix(".cbz") _teardown()
def test_update(self): self.assertFalse(Settings.bigger) Settings.update(self.DummySettings) self.assertTrue(Settings.bigger)