Skip to content

Commit

Permalink
Raise errors from threads in whitenoise.compress (#615)
Browse files Browse the repository at this point in the history
  • Loading branch information
adamchainz authored Oct 28, 2024
1 parent 0b054e5 commit 6bbec0f
Show file tree
Hide file tree
Showing 3 changed files with 28 additions and 2 deletions.
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",)

0 comments on commit 6bbec0f

Please sign in to comment.