Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Raise errors from threads in whitenoise.compress #615

Merged
merged 1 commit into from
Oct 28, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 8 additions & 0 deletions docs/changelog.rst
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,14 @@
Changelog
=========

Unreleased
----------

* Raise any errors from threads in the ``whitenoise.compress`` command.

Regression in 6.8.0.
Thanks to Tom Grainger for the spotting this with a `comment on PR #484 <https://github.com/evansd/whitenoise/pull/484#discussion_r1818989096>`__.

6.8.0 (2024-10-28)
------------------

Expand Down
11 changes: 9 additions & 2 deletions src/whitenoise/compress.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
import os
import re
from concurrent.futures import ThreadPoolExecutor
from concurrent.futures import as_completed
from io import BytesIO

try:
Expand Down Expand Up @@ -181,12 +182,18 @@ def main(argv=None):
)

with ThreadPoolExecutor() as executor:
futures = []
for dirpath, _dirs, files in os.walk(args.root):
for filename in files:
if compressor.should_compress(filename):
executor.submit(
compressor.compress, os.path.join(dirpath, filename)
futures.append(
executor.submit(
compressor.compress, os.path.join(dirpath, filename)
)
)
# Trigger any errors
for future in as_completed(futures):
future.result()

return 0

Expand Down
11 changes: 11 additions & 0 deletions tests/test_compress.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
import re
import shutil
import tempfile
from unittest import mock

import pytest

Expand Down Expand Up @@ -84,3 +85,13 @@ def test_compressed_effectively_no_orig_size():
assert not compressor.is_compressed_effectively(
"test_encoding", "test_path", 0, "test_data"
)


def test_main_error(files_dir):
with (
pytest.raises(ValueError) as excinfo,
mock.patch.object(Compressor, "compress", side_effect=ValueError("woops")),
):
compress_main([files_dir, "--quiet"])

assert excinfo.value.args == ("woops",)