From 4e2d4e5a778a76f1d1be2877c4d4c93d4366e5af Mon Sep 17 00:00:00 2001 From: Raphael Boidol Date: Sat, 14 Oct 2023 10:32:25 +0200 Subject: [PATCH] refactor: concentrate test methods on logic --- tests/optimize_avif_test.py | 40 ++++++++++++++++++------------------- tests/optimize_jpg_test.py | 23 ++++++++++++--------- tests/optimize_png_test.py | 23 ++++++++++++--------- tests/optimize_svg_test.py | 23 ++++++++++++--------- tests/optimize_webp_test.py | 23 ++++++++++++--------- 5 files changed, 76 insertions(+), 56 deletions(-) diff --git a/tests/optimize_avif_test.py b/tests/optimize_avif_test.py index 9ef12e5..33c9e5d 100644 --- a/tests/optimize_avif_test.py +++ b/tests/optimize_avif_test.py @@ -1,5 +1,5 @@ +import pathlib import shutil -from pathlib import Path import pytest @@ -7,35 +7,35 @@ @pytest.fixture -def original_image(tmpdir): - return Path(__file__).parent / "test.avif" +def images(tmpdir): + test_file = pathlib.Path(__file__).parent / "test.avif" + path = pathlib.Path(tmpdir) / "test.avif" + shutil.copy(test_file, path) + return path, test_file -@pytest.fixture -def image(tmpdir, original_image): - path = Path(tmpdir) / "test.avif" - shutil.copy(original_image, path) - return path - - -def test_qmin_qmax_deprecated(image): +def test_qmin_qmax_deprecated(images): + path, _ = images with pytest.deprecated_call(): - assert main(("-min", "0", str(image))) == 0 + assert main(("-min", "0", str(path))) == 0 -def test_qmin_qmax_and_quality(image): +def test_qmin_qmax_and_quality(images): + path, _ = images with pytest.raises(SystemExit) as wrapped_exit: - assert main(("-min", "10", "--quality", "20", str(image))) == 1 + assert main(("-min", "10", "--quality", "20", str(path))) == 1 assert wrapped_exit.type == SystemExit assert wrapped_exit.value.code == 1 -def test_compress_avif(original_image, image): - assert main(("--quality", "75", "-e", "0", str(image))) == 0 - assert original_image.stat().st_size > image.stat().st_size +def test_compress_avif(images): + path, test_file = images + assert main(("--quality", "75", "-e", "0", str(path))) == 0 + assert test_file.stat().st_size > path.stat().st_size -def test_compress_avif_below_threshold(original_image, image): +def test_compress_avif_below_threshold(images): + path, test_file = images assert ( main( ( @@ -45,9 +45,9 @@ def test_compress_avif_below_threshold(original_image, image): "10", "-t", "6144", - str(image), + str(path), ) ) == 0 ) - assert original_image.stat().st_size == image.stat().st_size + assert test_file.stat().st_size == path.stat().st_size diff --git a/tests/optimize_jpg_test.py b/tests/optimize_jpg_test.py index 2b2385f..5704884 100644 --- a/tests/optimize_jpg_test.py +++ b/tests/optimize_jpg_test.py @@ -1,24 +1,29 @@ +import pathlib import shutil -from pathlib import Path + +import pytest from pre_commit_images.optimize_jpg import main -def test_compress_jpg(tmpdir): +@pytest.fixture +def images(tmpdir): image = "test.jpg" - path = Path(tmpdir) / image - test_file = Path(__file__).parent / image + path = pathlib.Path(tmpdir) / image + test_file = pathlib.Path(__file__).parent / image shutil.copy(test_file, path) + return path, test_file + + +def test_compress_jpg(images): + path, test_file = images assert main(("-q", "50", str(path))) == 0 assert test_file.stat().st_size > path.stat().st_size -def test_compress_jpg_below_threshold(tmpdir): - image = "test.jpg" - path = Path(tmpdir) / image - test_file = Path(__file__).parent / image - shutil.copy(test_file, path) +def test_compress_jpg_below_threshold(images): + path, test_file = images assert main(("-q", "97", "-t", "15000", str(path))) == 0 assert test_file.stat().st_size == path.stat().st_size diff --git a/tests/optimize_png_test.py b/tests/optimize_png_test.py index 4c87e83..02cbb54 100644 --- a/tests/optimize_png_test.py +++ b/tests/optimize_png_test.py @@ -1,24 +1,29 @@ +import pathlib import shutil -from pathlib import Path + +import pytest from pre_commit_images.optimize_png import main -def test_compress_png(tmpdir): +@pytest.fixture +def images(tmpdir): image = "test.png" - path = Path(tmpdir) / image - test_file = Path(__file__).parent / image + path = pathlib.Path(tmpdir) / image + test_file = pathlib.Path(__file__).parent / image shutil.copy(test_file, path) + return path, test_file + + +def test_compress_png(images): + path, test_file = images assert main((str(path),)) == 0 assert test_file.stat().st_size > path.stat().st_size -def test_compress_png_below_threshold(tmpdir): - image = "test.png" - path = Path(tmpdir) / image - test_file = Path(__file__).parent / image - shutil.copy(test_file, path) +def test_compress_png_below_threshold(images): + path, test_file = images assert ( main( diff --git a/tests/optimize_svg_test.py b/tests/optimize_svg_test.py index bc37979..2453c67 100644 --- a/tests/optimize_svg_test.py +++ b/tests/optimize_svg_test.py @@ -1,24 +1,29 @@ +import pathlib import shutil -from pathlib import Path + +import pytest from pre_commit_images.optimize_svg import main -def test_compress_svg(tmpdir): +@pytest.fixture +def images(tmpdir): image = "test.svg" - path = Path(tmpdir) / image - test_file = Path(__file__).parent / image + path = pathlib.Path(tmpdir) / image + test_file = pathlib.Path(__file__).parent / image shutil.copy(test_file, path) + return path, test_file + + +def test_compress_svg(images): + path, test_file = images assert main((str(path),)) == 0 assert test_file.stat().st_size > path.stat().st_size -def test_compress_svg_below_threshold(tmpdir): - image = "test.svg" - path = Path(tmpdir) / image - test_file = Path(__file__).parent / image - shutil.copy(test_file, path) +def test_compress_svg_below_threshold(images): + path, test_file = images assert ( main( diff --git a/tests/optimize_webp_test.py b/tests/optimize_webp_test.py index a90d262..77bca1c 100644 --- a/tests/optimize_webp_test.py +++ b/tests/optimize_webp_test.py @@ -1,14 +1,22 @@ +import pathlib import shutil -from pathlib import Path + +import pytest from pre_commit_images.optimize_webp import main -def test_compress_webp(tmpdir): +@pytest.fixture +def images(tmpdir): image = "test.webp" - path = Path(tmpdir) / image - test_file = Path(__file__).parent / image + path = pathlib.Path(tmpdir) / image + test_file = pathlib.Path(__file__).parent / image shutil.copy(test_file, path) + return path, test_file + + +def test_compress_webp(images): + path, test_file = images assert ( main( @@ -23,11 +31,8 @@ def test_compress_webp(tmpdir): assert test_file.stat().st_size > path.stat().st_size -def test_compress_webp_below_threshold(tmpdir): - image = "test.webp" - path = Path(tmpdir) / image - test_file = Path(__file__).parent / image - shutil.copy(test_file, path) +def test_compress_webp_below_threshold(images): + path, test_file = images assert ( main(