collectstatic: do not crash on broken link (#56408)

This commit is contained in:
Emmanuel Cazenave 2021-08-26 18:09:25 +02:00 committed by Frédéric Péters
parent 4156847120
commit 0c3f1f55fe
2 changed files with 17 additions and 2 deletions

View File

@ -1,5 +1,6 @@
import collections
import os
import shutil
import sys
from unittest import mock
@ -63,7 +64,7 @@ def test_loading():
cmd()
def test_collectstatic(pub):
def test_collectstatic(pub, tmp_path):
CmdCollectStatic.collectstatic(pub)
assert os.path.exists(os.path.join(pub.app_dir, 'collectstatic', 'css', 'required.png'))
assert os.path.exists(os.path.join(pub.app_dir, 'collectstatic', 'js', 'qommon.forms.js'))
@ -72,6 +73,20 @@ def test_collectstatic(pub):
CmdCollectStatic.collectstatic(pub, clear=True, link=True)
assert os.path.islink(os.path.join(pub.app_dir, 'collectstatic', 'css', 'required.png'))
# create a broken link
required_tmp = os.path.join(tmp_path, 'required.png')
required_link = os.path.join(pub.app_dir, 'collectstatic', 'css', 'required.png')
shutil.copy2(os.path.join(pub.app_dir, 'collectstatic', 'css', 'required.png'), required_tmp)
os.unlink(required_link)
os.symlink(required_tmp, required_link)
os.unlink(required_tmp)
# check that we have a broken link
assert os.path.islink(required_link) and not os.path.exists(required_link)
# still works if broken link exists
CmdCollectStatic.collectstatic(pub, link=True)
# link not broken any more
assert os.path.islink(required_link) and os.path.exists(required_link)
def test_migrate(two_pubs):
CmdMigrate().handle()

View File

@ -78,7 +78,7 @@ class Command(BaseCommand):
continue
if not os.path.exists(dst_path):
os.makedirs(dst_path)
if os.path.exists(dst_filename):
if os.path.exists(dst_filename) or os.path.islink(dst_filename):
os.unlink(dst_filename)
if link:
os.symlink(src_filename, dst_filename)