0
0
mirror of https://github.com/django/django.git synced 2024-12-01 15:42:04 +01:00

Fixed #27741 -- Isolated TestCollectionHashedFilesCache in a tmpdir.

This commit is contained in:
David Sanders 2017-02-03 10:45:27 -08:00 committed by Tim Graham
parent 4d6584000a
commit 8e3f9d3ee2

View File

@ -551,32 +551,46 @@ class TestCollectionHashedFilesCache(CollectionTestCase):
hashed_file_path = hashed_file_path
def setUp(self):
self.testimage_path = os.path.join(
TEST_ROOT, 'project', 'documents', 'cached', 'css', 'img', 'window.png'
)
with open(self.testimage_path, 'r+b') as f:
self._orig_image_content = f.read()
super().setUp()
self._temp_dir = temp_dir = tempfile.mkdtemp()
os.makedirs(os.path.join(temp_dir, 'test'))
self.addCleanup(shutil.rmtree, temp_dir)
def tearDown(self):
with open(self.testimage_path, 'w+b') as f:
f.write(self._orig_image_content)
super().tearDown()
def _get_filename_path(self, filename):
return os.path.join(self._temp_dir, 'test', filename)
def test_file_change_after_collectstatic(self):
finders.get_finder.cache_clear()
err = StringIO()
call_command('collectstatic', interactive=False, verbosity=0, stderr=err)
with open(self.testimage_path, 'w+b') as f:
f.write(b"new content of png file to change it's hash")
# Create initial static files.
file_contents = (
('foo.png', 'foo'),
('bar.css', 'url("foo.png")\nurl("xyz.png")'),
('xyz.png', 'xyz'),
)
for filename, content in file_contents:
with open(self._get_filename_path(filename), 'w') as f:
f.write(content)
# Change modification time of self.testimage_path to make sure it gets
# collected again.
mtime = os.path.getmtime(self.testimage_path)
atime = os.path.getatime(self.testimage_path)
os.utime(self.testimage_path, (mtime + 1, atime + 1))
with self.modify_settings(STATICFILES_DIRS={'append': self._temp_dir}):
finders.get_finder.cache_clear()
err = StringIO()
# First collectstatic run.
call_command('collectstatic', interactive=False, verbosity=0, stderr=err)
relpath = self.hashed_file_path('test/bar.css')
with storage.staticfiles_storage.open(relpath) as relfile:
content = relfile.read()
self.assertIn(b'foo.acbd18db4cc2.png', content)
self.assertIn(b'xyz.d16fb36f0911.png', content)
call_command('collectstatic', interactive=False, verbosity=0, stderr=err)
relpath = self.hashed_file_path('cached/css/window.css')
with storage.staticfiles_storage.open(relpath) as relfile:
self.assertIn(b'window.a836fe39729e.png', relfile.read())
# Change the contents of the png files.
for filename in ('foo.png', 'xyz.png'):
with open(self._get_filename_path(filename), 'w+b') as f:
f.write(b"new content of file to change its hash")
# The hashes of the png files in the CSS file are updated after
# a second collectstatic.
call_command('collectstatic', interactive=False, verbosity=0, stderr=err)
relpath = self.hashed_file_path('test/bar.css')
with storage.staticfiles_storage.open(relpath) as relfile:
content = relfile.read()
self.assertIn(b'foo.57a5cb9ba68d.png', content)
self.assertIn(b'xyz.57a5cb9ba68d.png', content)