collectstatic: do not crash on broken link (#56408)
This commit is contained in:
parent
4156847120
commit
0c3f1f55fe
|
@ -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()
|
||||
|
|
|
@ -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)
|
||||
|
|
Loading…
Reference in New Issue