From f90be002d9d3c10b87c74741986e2cbf9f2b858e Mon Sep 17 00:00:00 2001 From: Jannis Leidel Date: Sun, 9 Feb 2014 14:48:11 +0000 Subject: [PATCH] Fixed #20780 -- Get rid of stale symlinks when using collectstatic. Thanks to John Giannelos for the initial patch. --- .../staticfiles/management/commands/collectstatic.py | 2 ++ tests/staticfiles_tests/tests.py | 9 +++++++++ 2 files changed, 11 insertions(+) diff --git a/django/contrib/staticfiles/management/commands/collectstatic.py b/django/contrib/staticfiles/management/commands/collectstatic.py index 02e95d56b9..a820fed638 100644 --- a/django/contrib/staticfiles/management/commands/collectstatic.py +++ b/django/contrib/staticfiles/management/commands/collectstatic.py @@ -275,6 +275,8 @@ class Command(NoArgsCommand): except OSError: pass try: + if os.path.lexists(full_path): + os.unlink(full_path) os.symlink(source_path, full_path) except AttributeError: import platform diff --git a/tests/staticfiles_tests/tests.py b/tests/staticfiles_tests/tests.py index 9261503726..95852acd09 100644 --- a/tests/staticfiles_tests/tests.py +++ b/tests/staticfiles_tests/tests.py @@ -694,6 +694,15 @@ class TestCollectionLinks(CollectionTestCase, TestDefaults): """ self.assertTrue(os.path.islink(os.path.join(settings.STATIC_ROOT, 'test.txt'))) + def test_broken_symlink(self): + """ + Test broken symlink gets deleted. + """ + path = os.path.join(settings.STATIC_ROOT, 'test.txt') + os.unlink(path) + self.run_collectstatic() + self.assertTrue(os.path.islink(path)) + class TestServeStatic(StaticFilesTestCase): """