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
1 changed files with 37 additions and 23 deletions

View File

@ -551,32 +551,46 @@ class TestCollectionHashedFilesCache(CollectionTestCase):
hashed_file_path = hashed_file_path hashed_file_path = hashed_file_path
def setUp(self): 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() 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): def _get_filename_path(self, filename):
with open(self.testimage_path, 'w+b') as f: return os.path.join(self._temp_dir, 'test', filename)
f.write(self._orig_image_content)
super().tearDown()
def test_file_change_after_collectstatic(self): def test_file_change_after_collectstatic(self):
finders.get_finder.cache_clear() # Create initial static files.
err = StringIO() file_contents = (
call_command('collectstatic', interactive=False, verbosity=0, stderr=err) ('foo.png', 'foo'),
with open(self.testimage_path, 'w+b') as f: ('bar.css', 'url("foo.png")\nurl("xyz.png")'),
f.write(b"new content of png file to change it's hash") ('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 with self.modify_settings(STATICFILES_DIRS={'append': self._temp_dir}):
# collected again. finders.get_finder.cache_clear()
mtime = os.path.getmtime(self.testimage_path) err = StringIO()
atime = os.path.getatime(self.testimage_path) # First collectstatic run.
os.utime(self.testimage_path, (mtime + 1, atime + 1)) 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) # Change the contents of the png files.
relpath = self.hashed_file_path('cached/css/window.css') for filename in ('foo.png', 'xyz.png'):
with storage.staticfiles_storage.open(relpath) as relfile: with open(self._get_filename_path(filename), 'w+b') as f:
self.assertIn(b'window.a836fe39729e.png', relfile.read()) 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)