Skip to content

Commit

Permalink
Raise errors from threads in whitenoise.compress
Browse files Browse the repository at this point in the history
Co-authored-by: Adam Chainz <[email protected]>
  • Loading branch information
Archmonger and adamchainz committed Oct 28, 2024
1 parent 003d95d commit 08fad8c
Show file tree
Hide file tree
Showing 2 changed files with 23 additions and 9 deletions.
21 changes: 12 additions & 9 deletions src/servestatic/compress.py
Original file line number Diff line number Diff line change
@@ -1,10 +1,10 @@
from __future__ import annotations

import argparse
import concurrent.futures
import gzip
import os
import re
from concurrent.futures import ThreadPoolExecutor, as_completed
from io import BytesIO

try:
Expand Down Expand Up @@ -123,12 +123,6 @@ def write_data(path, data, suffix, stat_result):
os.utime(filename, (stat_result.st_atime, stat_result.st_mtime))
return filename

def files_to_compress(self, root):
for dirpath, _dirs, files in os.walk(root):
for filename in files:
if self.should_compress(filename):
yield os.path.join(dirpath, filename)


def main(argv=None):
parser = argparse.ArgumentParser(
Expand Down Expand Up @@ -167,8 +161,17 @@ def main(argv=None):
quiet=args.quiet,
)

with concurrent.futures.ThreadPoolExecutor() as executor:
executor.map(compressor.compress, compressor.files_to_compress(args.root))
futures = []
with ThreadPoolExecutor() as executor:
for dirpath, _dirs, files in os.walk(args.root):
futures.extend(
executor.submit(compressor.compress, os.path.join(dirpath, filename))
for filename in files
if compressor.should_compress(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 @@ -78,3 +79,13 @@ def test_compress():
def test_compressed_effectively_no_orig_size():
compressor = Compressor(quiet=True)
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 08fad8c

Please sign in to comment.