Fixed #34407 -- Reported filename when decoding fails in collectstatic's post_process.

This commit is contained in:
Claude Paroz 2023-03-12 12:28:00 +01:00 committed by Mariusz Felisiak
parent 4db33e96d1
commit 03bc92af97
3 changed files with 18 additions and 1 deletions

View File

@ -353,7 +353,10 @@ class HashedFilesMixin:
# ..to apply each replacement pattern to the content
if name in adjustable_paths:
old_hashed_name = hashed_name
content = original_file.read().decode("utf-8")
try:
content = original_file.read().decode("utf-8")
except UnicodeDecodeError as exc:
yield name, None, exc, False
for extension, patterns in self._patterns.items():
if matches_patterns(path, (extension,)):
for pattern, template in patterns:

View File

@ -0,0 +1,2 @@
/* éviter écrasement */
.test { margin: 1 rem; }

View File

@ -368,6 +368,18 @@ class TestHashedFiles:
self.assertEqual("Post-processing 'faulty.css' failed!\n\n", err.getvalue())
self.assertPostCondition()
@override_settings(
STATICFILES_DIRS=[os.path.join(TEST_ROOT, "project", "nonutf8")],
STATICFILES_FINDERS=["django.contrib.staticfiles.finders.FileSystemFinder"],
)
def test_post_processing_nonutf8(self):
finders.get_finder.cache_clear()
err = StringIO()
with self.assertRaises(UnicodeDecodeError):
call_command("collectstatic", interactive=False, verbosity=0, stderr=err)
self.assertEqual("Post-processing 'nonutf8.css' failed!\n\n", err.getvalue())
self.assertPostCondition()
@override_settings(
STORAGES={