Ticket #24452: staticfiles-adjustable-paths-tests.patch
File staticfiles-adjustable-paths-tests.patch, 12.3 KB (added by , 9 years ago) |
---|
-
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): 39 39 self.assertStaticRaises(ValueError, "does/not/exist.png", "/static/does/not/exist.png") 40 40 self.assertStaticRenders("test/file.txt", "/static/test/file.dad0999e4f8f.txt") 41 41 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") 43 43 self.assertStaticRenders("path/", "/static/path/") 44 44 self.assertStaticRenders("path/?query", "/static/path/?query") 45 45 46 46 def test_template_tag_simple_content(self): 47 47 relpath = self.hashed_file_path("cached/styles.css") 48 self.assertEqual(relpath, "cached/styles. bb84a0240107.css")48 self.assertEqual(relpath, "cached/styles.5e0040571e1a.css") 49 49 with storage.staticfiles_storage.open(relpath) as relfile: 50 50 content = relfile.read() 51 51 self.assertNotIn(b"cached/other.css", content) … … class TestHashedFiles(object): 64 64 65 65 def test_path_with_querystring(self): 66 66 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") 68 68 with storage.staticfiles_storage.open( 69 "cached/styles. bb84a0240107.css") as relfile:69 "cached/styles.5e0040571e1a.css") as relfile: 70 70 content = relfile.read() 71 71 self.assertNotIn(b"cached/other.css", content) 72 72 self.assertIn(b"other.d41d8cd98f00.css", content) 73 73 74 74 def test_path_with_fragment(self): 75 75 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") 77 77 with storage.staticfiles_storage.open( 78 "cached/styles. bb84a0240107.css") as relfile:78 "cached/styles.5e0040571e1a.css") as relfile: 79 79 content = relfile.read() 80 80 self.assertNotIn(b"cached/other.css", content) 81 81 self.assertIn(b"other.d41d8cd98f00.css", content) 82 82 83 83 def test_path_with_querystring_and_fragment(self): 84 84 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") 86 86 with storage.staticfiles_storage.open(relpath) as relfile: 87 87 content = relfile.read() 88 88 self.assertIn(b'fonts/font.a4b0478549d0.eot?#iefix', content) … … class TestHashedFiles(object): 93 93 94 94 def test_template_tag_absolute(self): 95 95 relpath = self.hashed_file_path("cached/absolute.css") 96 self.assertEqual(relpath, "cached/absolute. df312c6326e1.css")96 self.assertEqual(relpath, "cached/absolute.eb04def9f9a4.css") 97 97 with storage.staticfiles_storage.open(relpath) as relfile: 98 98 content = relfile.read() 99 99 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) 101 101 self.assertNotIn(b"/static/styles_root.css", content) 102 102 self.assertIn(b"/static/styles_root.401f2509a628.css", content) 103 103 self.assertIn(b'/static/cached/img/relative.acae32e4532b.png', content) … … class TestHashedFiles(object): 107 107 Like test_template_tag_absolute, but for a file in STATIC_ROOT (#26249). 108 108 """ 109 109 relpath = self.hashed_file_path("absolute_root.css") 110 self.assertEqual(relpath, "absolute_root.f8 64a4d7f083.css")110 self.assertEqual(relpath, "absolute_root.f821df1b64f7.css") 111 111 with storage.staticfiles_storage.open(relpath) as relfile: 112 112 content = relfile.read() 113 113 self.assertNotIn(b"/static/styles_root.css", content) … … class TestHashedFiles(object): 115 115 116 116 def test_template_tag_relative(self): 117 117 relpath = self.hashed_file_path("cached/relative.css") 118 self.assertEqual(relpath, "cached/relative. b0375bd89156.css")118 self.assertEqual(relpath, "cached/relative.c3e9e1ea6f2e.css") 119 119 with storage.staticfiles_storage.open(relpath) as relfile: 120 120 content = relfile.read() 121 121 self.assertNotIn(b"../cached/styles.css", content) 122 122 self.assertNotIn(b'@import "styles.css"', content) 123 123 self.assertNotIn(b'url(img/relative.png)', content) 124 124 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) 126 126 127 127 def test_import_replacement(self): 128 128 "See #18050" 129 129 relpath = self.hashed_file_path("cached/import.css") 130 self.assertEqual(relpath, "cached/import. 2b1d40b0bbd4.css")130 self.assertEqual(relpath, "cached/import.f53576679e5a.css") 131 131 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()) 133 133 134 134 def test_template_tag_deep_relative(self): 135 135 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") 137 137 with storage.staticfiles_storage.open(relpath) as relfile: 138 138 content = relfile.read() 139 139 self.assertNotIn(b'url(img/window.png)', content) … … class TestHashedFiles(object): 145 145 with storage.staticfiles_storage.open(relpath) as relfile: 146 146 self.assertIn(b"https://", relfile.read()) 147 147 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 148 159 def test_post_processing(self): 149 160 """ 150 161 Test that post_processing behaves correctly. … … class TestHashedFiles(object): 175 186 176 187 def test_css_import_case_insensitive(self): 177 188 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") 179 190 with storage.staticfiles_storage.open(relpath) as relfile: 180 191 content = relfile.read() 181 192 self.assertNotIn(b"cached/other.css", content) … … class TestCollectionCachedStorage(TestHashedFiles, CollectionTestCase): 206 217 """ 207 218 def test_cache_invalidation(self): 208 219 name = "cached/styles.css" 209 hashed_name = "cached/styles. bb84a0240107.css"220 hashed_name = "cached/styles.5e0040571e1a.css" 210 221 # check if the cache is filled correctly as expected 211 222 cache_key = storage.staticfiles_storage.hash_key(name) 212 223 cached_name = storage.staticfiles_storage.hashed_files.get(cache_key) … … class TestCollectionCachedStorage(TestHashedFiles, CollectionTestCase): 219 230 cached_name = storage.staticfiles_storage.hashed_files.get(cache_key) 220 231 self.assertEqual(cached_name, hashed_name) 221 232 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 222 244 def test_cache_key_memcache_validation(self): 223 245 """ 224 246 Handle cache key creation correctly, see #17861. … … class TestExtraPatternsCachedStorage(CollectionTestCase): 258 280 """ 259 281 # CSS files shouldn't be touched by JS patterns. 260 282 relpath = self.cached_file_path("cached/import.css") 261 self.assertEqual(relpath, "cached/import. 2b1d40b0bbd4.css")283 self.assertEqual(relpath, "cached/import.f53576679e5a.css") 262 284 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()) 264 286 265 287 # Confirm JS patterns have been applied to JS files. 266 288 relpath = self.cached_file_path("cached/test.js") 267 self.assertEqual(relpath, "cached/test. 62789ffcd280.js")289 self.assertEqual(relpath, "cached/test.388d7a790d46.js") 268 290 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()) 270 292 271 293 272 294 @override_settings( … … class TestStaticFilePermissions(CollectionTestCase): 446 468 dir_mode = os.stat(test_dir)[0] & 0o777 447 469 self.assertEqual(file_mode, 0o640) 448 470 self.assertEqual(dir_mode, 0o740) 471 472 473 @override_settings( 474 STATICFILES_STORAGE='django.contrib.staticfiles.storage.CachedStaticFilesStorage', 475 ) 476 class 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