Ticket #24452: staticfiles-adjustable-paths-tests.patch

File staticfiles-adjustable-paths-tests.patch, 12.3 KB (added by David Sanders, 8 years ago)

Tests for accurate hash filenames

  • new file tests/staticfiles_tests/project/loop/bar.css

    diff --git a/tests/staticfiles_tests/project/loop/bar.css b/tests/staticfiles_tests/project/loop/bar.css
    new file mode 100644
    index 0000000..1f11a22
    - +  
     1@import url("foo.css")
  • new file tests/staticfiles_tests/project/loop/foo.css

    diff --git a/tests/staticfiles_tests/project/loop/foo.css b/tests/staticfiles_tests/project/loop/foo.css
    new file mode 100644
    index 0000000..0903a70
    - +  
     1@import url("bar.css")
  • tests/staticfiles_tests/test_storage.py

    diff --git a/tests/staticfiles_tests/test_storage.py b/tests/staticfiles_tests/test_storage.py
    index 318b26c..3b7ad0e 100644
    a b class TestHashedFiles(object):  
    3939        self.assertStaticRaises(ValueError, "does/not/exist.png", "/static/does/not/exist.png")
    4040        self.assertStaticRenders("test/file.txt", "/static/test/file.dad0999e4f8f.txt")
    4141        self.assertStaticRenders("test/file.txt", "/static/test/file.dad0999e4f8f.txt", asvar=True)
    42         self.assertStaticRenders("cached/styles.css", "/static/cached/styles.bb84a0240107.css")
     42        self.assertStaticRenders("cached/styles.css", "/static/cached/styles.5e0040571e1a.css")
    4343        self.assertStaticRenders("path/", "/static/path/")
    4444        self.assertStaticRenders("path/?query", "/static/path/?query")
    4545
    4646    def test_template_tag_simple_content(self):
    4747        relpath = self.hashed_file_path("cached/styles.css")
    48         self.assertEqual(relpath, "cached/styles.bb84a0240107.css")
     48        self.assertEqual(relpath, "cached/styles.5e0040571e1a.css")
    4949        with storage.staticfiles_storage.open(relpath) as relfile:
    5050            content = relfile.read()
    5151            self.assertNotIn(b"cached/other.css", content)
    class TestHashedFiles(object):  
    6464
    6565    def test_path_with_querystring(self):
    6666        relpath = self.hashed_file_path("cached/styles.css?spam=eggs")
    67         self.assertEqual(relpath, "cached/styles.bb84a0240107.css?spam=eggs")
     67        self.assertEqual(relpath, "cached/styles.5e0040571e1a.css?spam=eggs")
    6868        with storage.staticfiles_storage.open(
    69                 "cached/styles.bb84a0240107.css") as relfile:
     69                "cached/styles.5e0040571e1a.css") as relfile:
    7070            content = relfile.read()
    7171            self.assertNotIn(b"cached/other.css", content)
    7272            self.assertIn(b"other.d41d8cd98f00.css", content)
    7373
    7474    def test_path_with_fragment(self):
    7575        relpath = self.hashed_file_path("cached/styles.css#eggs")
    76         self.assertEqual(relpath, "cached/styles.bb84a0240107.css#eggs")
     76        self.assertEqual(relpath, "cached/styles.5e0040571e1a.css#eggs")
    7777        with storage.staticfiles_storage.open(
    78                 "cached/styles.bb84a0240107.css") as relfile:
     78                "cached/styles.5e0040571e1a.css") as relfile:
    7979            content = relfile.read()
    8080            self.assertNotIn(b"cached/other.css", content)
    8181            self.assertIn(b"other.d41d8cd98f00.css", content)
    8282
    8383    def test_path_with_querystring_and_fragment(self):
    8484        relpath = self.hashed_file_path("cached/css/fragments.css")
    85         self.assertEqual(relpath, "cached/css/fragments.59dc2b188043.css")
     85        self.assertEqual(relpath, "cached/css/fragments.c4e6753b52d3.css")
    8686        with storage.staticfiles_storage.open(relpath) as relfile:
    8787            content = relfile.read()
    8888            self.assertIn(b'fonts/font.a4b0478549d0.eot?#iefix', content)
    class TestHashedFiles(object):  
    9393
    9494    def test_template_tag_absolute(self):
    9595        relpath = self.hashed_file_path("cached/absolute.css")
    96         self.assertEqual(relpath, "cached/absolute.df312c6326e1.css")
     96        self.assertEqual(relpath, "cached/absolute.eb04def9f9a4.css")
    9797        with storage.staticfiles_storage.open(relpath) as relfile:
    9898            content = relfile.read()
    9999            self.assertNotIn(b"/static/cached/styles.css", content)
    100             self.assertIn(b"/static/cached/styles.bb84a0240107.css", content)
     100            self.assertIn(b"/static/cached/styles.5e0040571e1a.css", content)
    101101            self.assertNotIn(b"/static/styles_root.css", content)
    102102            self.assertIn(b"/static/styles_root.401f2509a628.css", content)
    103103            self.assertIn(b'/static/cached/img/relative.acae32e4532b.png', content)
    class TestHashedFiles(object):  
    107107        Like test_template_tag_absolute, but for a file in STATIC_ROOT (#26249).
    108108        """
    109109        relpath = self.hashed_file_path("absolute_root.css")
    110         self.assertEqual(relpath, "absolute_root.f864a4d7f083.css")
     110        self.assertEqual(relpath, "absolute_root.f821df1b64f7.css")
    111111        with storage.staticfiles_storage.open(relpath) as relfile:
    112112            content = relfile.read()
    113113            self.assertNotIn(b"/static/styles_root.css", content)
    class TestHashedFiles(object):  
    115115
    116116    def test_template_tag_relative(self):
    117117        relpath = self.hashed_file_path("cached/relative.css")
    118         self.assertEqual(relpath, "cached/relative.b0375bd89156.css")
     118        self.assertEqual(relpath, "cached/relative.c3e9e1ea6f2e.css")
    119119        with storage.staticfiles_storage.open(relpath) as relfile:
    120120            content = relfile.read()
    121121            self.assertNotIn(b"../cached/styles.css", content)
    122122            self.assertNotIn(b'@import "styles.css"', content)
    123123            self.assertNotIn(b'url(img/relative.png)', content)
    124124            self.assertIn(b'url("img/relative.acae32e4532b.png")', content)
    125             self.assertIn(b"../cached/styles.bb84a0240107.css", content)
     125            self.assertIn(b"../cached/styles.5e0040571e1a.css", content)
    126126
    127127    def test_import_replacement(self):
    128128        "See #18050"
    129129        relpath = self.hashed_file_path("cached/import.css")
    130         self.assertEqual(relpath, "cached/import.2b1d40b0bbd4.css")
     130        self.assertEqual(relpath, "cached/import.f53576679e5a.css")
    131131        with storage.staticfiles_storage.open(relpath) as relfile:
    132             self.assertIn(b"""import url("styles.bb84a0240107.css")""", relfile.read())
     132            self.assertIn(b"""import url("styles.5e0040571e1a.css")""", relfile.read())
    133133
    134134    def test_template_tag_deep_relative(self):
    135135        relpath = self.hashed_file_path("cached/css/window.css")
    136         self.assertEqual(relpath, "cached/css/window.3906afbb5a17.css")
     136        self.assertEqual(relpath, "cached/css/window.5d5c10836967.css")
    137137        with storage.staticfiles_storage.open(relpath) as relfile:
    138138            content = relfile.read()
    139139            self.assertNotIn(b'url(img/window.png)', content)
    class TestHashedFiles(object):  
    145145        with storage.staticfiles_storage.open(relpath) as relfile:
    146146            self.assertIn(b"https://", relfile.read())
    147147
     148    @override_settings(
     149        STATICFILES_DIRS=[os.path.join(TEST_ROOT, 'project', 'loop')],
     150        STATICFILES_FINDERS=['django.contrib.staticfiles.finders.FileSystemFinder'],
     151    )
     152    def test_import_loop(self):
     153        finders.get_finder.cache_clear()
     154        err = six.StringIO()
     155        with self.assertRaises(RuntimeError):
     156            call_command('collectstatic', interactive=False, verbosity=0, stderr=err)
     157        self.assertEqual("Post-processing 'All' failed!\n\n", err.getvalue())
     158
    148159    def test_post_processing(self):
    149160        """
    150161        Test that post_processing behaves correctly.
    class TestHashedFiles(object):  
    175186
    176187    def test_css_import_case_insensitive(self):
    177188        relpath = self.hashed_file_path("cached/styles_insensitive.css")
    178         self.assertEqual(relpath, "cached/styles_insensitive.c609562b6d3c.css")
     189        self.assertEqual(relpath, "cached/styles_insensitive.3fa427592a53.css")
    179190        with storage.staticfiles_storage.open(relpath) as relfile:
    180191            content = relfile.read()
    181192            self.assertNotIn(b"cached/other.css", content)
    class TestCollectionCachedStorage(TestHashedFiles, CollectionTestCase):  
    206217    """
    207218    def test_cache_invalidation(self):
    208219        name = "cached/styles.css"
    209         hashed_name = "cached/styles.bb84a0240107.css"
     220        hashed_name = "cached/styles.5e0040571e1a.css"
    210221        # check if the cache is filled correctly as expected
    211222        cache_key = storage.staticfiles_storage.hash_key(name)
    212223        cached_name = storage.staticfiles_storage.hashed_files.get(cache_key)
    class TestCollectionCachedStorage(TestHashedFiles, CollectionTestCase):  
    219230        cached_name = storage.staticfiles_storage.hashed_files.get(cache_key)
    220231        self.assertEqual(cached_name, hashed_name)
    221232
     233        # Ensure everything looks as expected for files which we know had to be hashed
     234        # multiple times since their content includes other files that were hashed
     235        name = "cached/relative.css"
     236        hashed_name = "cached/relative.c3e9e1ea6f2e.css"
     237        cache_key = storage.staticfiles_storage.hash_key(name)
     238        cached_name = storage.staticfiles_storage.hashed_files.get(cache_key)
     239        self.assertEqual(cached_name, None)
     240        self.assertEqual(self.hashed_file_path(name), hashed_name)
     241        cached_name = storage.staticfiles_storage.hashed_files.get(cache_key)
     242        self.assertEqual(cached_name, hashed_name)
     243
    222244    def test_cache_key_memcache_validation(self):
    223245        """
    224246        Handle cache key creation correctly, see #17861.
    class TestExtraPatternsCachedStorage(CollectionTestCase):  
    258280        """
    259281        # CSS files shouldn't be touched by JS patterns.
    260282        relpath = self.cached_file_path("cached/import.css")
    261         self.assertEqual(relpath, "cached/import.2b1d40b0bbd4.css")
     283        self.assertEqual(relpath, "cached/import.f53576679e5a.css")
    262284        with storage.staticfiles_storage.open(relpath) as relfile:
    263             self.assertIn(b'import url("styles.bb84a0240107.css")', relfile.read())
     285            self.assertIn(b'import url("styles.5e0040571e1a.css")', relfile.read())
    264286
    265287        # Confirm JS patterns have been applied to JS files.
    266288        relpath = self.cached_file_path("cached/test.js")
    267         self.assertEqual(relpath, "cached/test.62789ffcd280.js")
     289        self.assertEqual(relpath, "cached/test.388d7a790d46.js")
    268290        with storage.staticfiles_storage.open(relpath) as relfile:
    269             self.assertIn(b'JS_URL("import.2b1d40b0bbd4.css")', relfile.read())
     291            self.assertIn(b'JS_URL("import.f53576679e5a.css")', relfile.read())
    270292
    271293
    272294@override_settings(
    class TestStaticFilePermissions(CollectionTestCase):  
    446468        dir_mode = os.stat(test_dir)[0] & 0o777
    447469        self.assertEqual(file_mode, 0o640)
    448470        self.assertEqual(dir_mode, 0o740)
     471
     472
     473@override_settings(
     474    STATICFILES_STORAGE='django.contrib.staticfiles.storage.CachedStaticFilesStorage',
     475)
     476class TestCollectionHashedFilesCache(CollectionTestCase):
     477    """
     478    Test changing an image file and make sure that css files referring to that file
     479    always use the newest hash even if they get processed before the image file.
     480    """
     481
     482    hashed_file_path = hashed_file_path
     483
     484    def setUp(self):
     485        self.testimage_path = os.path.join(TEST_ROOT, 'project', 'documents', 'cached', 'css', 'img', 'window.png')
     486        with open(self.testimage_path, 'r+b') as f:
     487            self.orig_image_content = f.read()
     488        super(TestCollectionHashedFilesCache, self).setUp()
     489
     490    def tearDown(self):
     491        with open(self.testimage_path, 'w+b') as f:
     492            f.write(self.orig_image_content)
     493        super(TestCollectionHashedFilesCache, self).tearDown()
     494
     495    def test_file_change_after_collectstatic(self):
     496        finders.get_finder.cache_clear()
     497        err = six.StringIO()
     498        call_command('collectstatic', interactive=False, verbosity=0, stderr=err)
     499        with open(self.testimage_path, 'w+b') as f:
     500            f.write("new content of png file to change it's hash")
     501
     502        # and now change modification time of self.testimage_path
     503        # to make shure it gets collected again
     504        mtime = os.path.getmtime(self.testimage_path)
     505        atime = os.path.getatime(self.testimage_path)
     506        os.utime(self.testimage_path, (mtime + 1, atime + 1))
     507
     508        call_command('collectstatic', interactive=False, verbosity=0, stderr=err)
     509        relpath = self.hashed_file_path("cached/css/window.css")
     510        with storage.staticfiles_storage.open(relpath) as relfile:
     511            self.assertIn(b"window.a836fe39729e.png", relfile.read())
     512
Back to Top