Skip to content

Commit

Permalink
[1.9.x] Fixed #25346 -- Allowed collectstatic to delete broken symlinks.
Browse files Browse the repository at this point in the history
Backport of 0922bbf from master
  • Loading branch information
ymyzk authored and timgraham committed Oct 17, 2015
1 parent faafd55 commit 9039ff6
Show file tree
Hide file tree
Showing 2 changed files with 18 additions and 3 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -218,7 +218,12 @@ def clear_dir(self, path):
smart_text(fpath), level=1)
else:
self.log("Deleting '%s'" % smart_text(fpath), level=1)
self.storage.delete(fpath)
full_path = self.storage.path(fpath)
if not os.path.exists(full_path) and os.path.lexists(full_path):
# Delete broken symlinks
os.unlink(full_path)
else:
self.storage.delete(fpath)
for d in dirs:
self.clear_dir(os.path.join(path, d))

Expand Down
14 changes: 12 additions & 2 deletions tests/staticfiles_tests/test_management.py
Original file line number Diff line number Diff line change
Expand Up @@ -323,8 +323,8 @@ class TestCollectionLinks(CollectionTestCase, TestDefaults):
the standard file resolving tests here, to make sure using
``--link`` does not change the file-selection semantics.
"""
def run_collectstatic(self):
super(TestCollectionLinks, self).run_collectstatic(link=True)
def run_collectstatic(self, clear=False):
super(TestCollectionLinks, self).run_collectstatic(link=True, clear=clear)

def test_links_created(self):
"""
Expand All @@ -340,3 +340,13 @@ def test_broken_symlink(self):
os.unlink(path)
self.run_collectstatic()
self.assertTrue(os.path.islink(path))

def test_clear_broken_symlink(self):
"""
With ``--clear``, broken symbolic links are deleted.
"""
nonexistent_file_path = os.path.join(settings.STATIC_ROOT, 'nonexistent.txt')
broken_symlink_path = os.path.join(settings.STATIC_ROOT, 'symlink.txt')
os.symlink(nonexistent_file_path, broken_symlink_path)
self.run_collectstatic(clear=True)
self.assertFalse(os.path.lexists(broken_symlink_path))

0 comments on commit 9039ff6

Please sign in to comment.