diff --git a/tests/custom_compressor.py b/tests/custom_compressor.py new file mode 100644 index 00000000..b25ad87d --- /dev/null +++ b/tests/custom_compressor.py @@ -0,0 +1,10 @@ +from whitenoise.compress import Compressor + +called = False + +class CustomCompressor(Compressor): + def compress(self, path): + global called + print("CALLLED") + called = True + return super().compress(path) diff --git a/tests/test_compress.py b/tests/test_compress.py index 11ea9471..1f7c2b33 100644 --- a/tests/test_compress.py +++ b/tests/test_compress.py @@ -9,9 +9,12 @@ import pytest +from django.test import override_settings from whitenoise.compress import Compressor from whitenoise.compress import main as compress_main +from . import custom_compressor + COMPRESSABLE_FILE = "application.css" TOO_SMALL_FILE = "too-small.css" WRONG_EXTENSION = "image.jpg" @@ -84,3 +87,55 @@ def test_compressed_effectively_no_orig_size(): assert not compressor.is_compressed_effectively( "test_encoding", "test_path", 0, "test_data" ) + + +def test_default_compressor(files_dir): + # Run the compression command with default compressor + custom_compressor.called = False + + compress_main([files_dir]) + + for path in TEST_FILES.keys(): + full_path = os.path.join(files_dir, path) + if path.endswith(".jpg"): + assert not os.path.exists(full_path + ".gz") + else: + if TOO_SMALL_FILE not in full_path: + assert os.path.exists(full_path + ".gz") + + assert custom_compressor.called is False + + +def test_custom_compressor(files_dir): + custom_compressor.called = False + + # Run the compression command with the custom compressor + compress_main([files_dir, "--compressor-class=tests.custom_compressor.CustomCompressor"]) + + assert custom_compressor.called is True + + for path in TEST_FILES.keys(): + full_path = os.path.join(files_dir, path) + if path.endswith(".jpg"): + assert not os.path.exists(full_path + ".gz") + else: + if TOO_SMALL_FILE not in full_path: + assert os.path.exists(full_path + ".gz") + + +@override_settings(WHITENOISE_COMPRESSOR_CLASS='tests.custom_compressor.CustomCompressor') +def test_custom_compressor_settings(files_dir): + """ Test if the custom compressor can be set via WHITENOISE_COMPRESSOR_CLASS settings """ + custom_compressor.called = False + + compress_main([files_dir]) + + assert custom_compressor.called is True + + for path in TEST_FILES.keys(): + full_path = os.path.join(files_dir, path) + if path.endswith(".jpg"): + assert not os.path.exists(full_path + ".gz") + else: + if TOO_SMALL_FILE not in full_path: + assert os.path.exists(full_path + ".gz")