From fe86a785a1079f646e7ad452f6fac08ec4d3378c Mon Sep 17 00:00:00 2001
From: David Sanders <dsanders11@ucsbalum.com>
Date: Fri, 3 Feb 2017 10:45:27 -0800
Subject: [PATCH] [1.11.x] Fixed #27741 -- Isolated
 TestCollectionHashedFilesCache in a tmpdir.

Backport of 8e3f9d3ee20fdadef6f2a28c71fb7e142b415059 from master
---
 tests/staticfiles_tests/test_storage.py | 60 +++++++++++++++----------
 1 file changed, 37 insertions(+), 23 deletions(-)

diff --git a/tests/staticfiles_tests/test_storage.py b/tests/staticfiles_tests/test_storage.py
index 4e1dec84f9..e06e54487e 100644
--- a/tests/staticfiles_tests/test_storage.py
+++ b/tests/staticfiles_tests/test_storage.py
@@ -554,32 +554,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(TestCollectionHashedFilesCache, self).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(TestCollectionHashedFilesCache, self).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 = six.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 = six.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)