django/django — pull request #2627 by tedtieken (2014-05-02 17:36:54+00:00)
Title: Failing test and patch for #22557: staticfiles.json keeps deleted entries when collectstatic is run

https://code.djangoproject.com/ticket/22557

All staticfiles and django tests pass with these changes.

Would appreciate feedback. I'm especially not sure if we should always zero the manifest on collectstatic, or if we should only zero the manifest when --clear is called.

#current version
def on_collectstatic(self, command):
self.hashed_files = OrderedDict()

Vs.

#reasonable alternate version
def on_collectstatic(self, command):
if command.clear:
self.hashed_files = OrderedDict()

I could go either way on the question.

Copied from ticket:
There is some surprising behavior when the new HashedFilesMixin is used with the new ManifestFilesMixin.
When you run manage.py collectstatic --clear:
1) a copy of the old manifest.json is loaded from disk into memory at ManifestFilesMixin.hashed_files. This happens at the very beginning of collectstatic (when ManifestFilesMixin.__init__ is called, which happens when the storage is initialized, which happens during Command.__init__)
2) the old manifest.json is deleted (which would lead most people to believe the old manifest information is deleted as well)
3) new files are added to the ManifestFilesMixin.hashed_files dict, and updated files get their records updated. But, this is building on top of the last version of manifest.json. Keys for deleted files are never removed and the deleted file mappings persist in the new manifest.json which gets written back to disk at the end of collectstatic's post_process phase.

[Rest of ticket omitted]

To make testing cleaner, I broke post_process into two steps: post_process and save_manifest. Long term I think this is also a better API that will allow people to write cleaner sub-classes. I found this bug writing sub-classes of ManifestFilesMixin and the code to save the manifest is pretty ugly with the current API of post_process doing both steps.